Skip to content

Commit cc0f4ec

Browse files
committed
Add closures feature flag and enablement checks
Introduces a new 'closures' feature flag to the compiler, updates feature enumeration, and adds checks to ensure closures are only used when the feature is enabled. Test configurations are updated to enable the closures feature for relevant tests.
1 parent b085c6a commit cc0f4ec

File tree

8 files changed

+38
-2
lines changed

8 files changed

+38
-2
lines changed

src/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export namespace CommonNames {
191191
export const ASC_FEATURE_RELAXED_SIMD = "ASC_FEATURE_RELAXED_SIMD";
192192
export const ASC_FEATURE_EXTENDED_CONST = "ASC_FEATURE_EXTENDED_CONST";
193193
export const ASC_FEATURE_STRINGREF = "ASC_FEATURE_STRINGREF";
194+
export const ASC_FEATURE_CLOSURES = "ASC_FEATURE_CLOSURES";
194195
export const ASC_VERSION_MAJOR = "ASC_VERSION_MAJOR";
195196
export const ASC_VERSION_MINOR = "ASC_VERSION_MINOR";
196197
export const ASC_VERSION_PATCH = "ASC_VERSION_PATCH";

src/compiler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7497,6 +7497,14 @@ export class Compiler extends DiagnosticEmitter {
74977497
// Analyze captured variables before compiling
74987498
let captures = this.analyzeCapturedVariables(declaration, flow);
74997499
if (captures.size > 0) {
7500+
// Check if closures feature is enabled
7501+
if (!this.options.hasFeature(Feature.Closures)) {
7502+
this.error(
7503+
DiagnosticCode.Feature_0_is_not_enabled,
7504+
expression.range, "closures"
7505+
);
7506+
return module.unreachable();
7507+
}
75007508
instance.capturedLocals = captures;
75017509
instance.outerFunction = sourceFunction;
75027510
this.ensureClosureEnvironment(sourceFunction, captures, flow);
@@ -7515,6 +7523,14 @@ export class Compiler extends DiagnosticEmitter {
75157523
// Analyze captured variables before compiling
75167524
let captures = this.analyzeCapturedVariables(declaration, flow);
75177525
if (captures.size > 0) {
7526+
// Check if closures feature is enabled
7527+
if (!this.options.hasFeature(Feature.Closures)) {
7528+
this.error(
7529+
DiagnosticCode.Feature_0_is_not_enabled,
7530+
expression.range, "closures"
7531+
);
7532+
return module.unreachable();
7533+
}
75187534
instance.capturedLocals = captures;
75197535
instance.outerFunction = sourceFunction;
75207536
this.ensureClosureEnvironment(sourceFunction, captures, flow);
@@ -7990,6 +8006,14 @@ export class Compiler extends DiagnosticEmitter {
79908006
let declaration = funcExpr.declaration;
79918007
let capturedNames = this.analyzeCapturedVariablesWithDeclared(declaration, flow, instance, declaredVars);
79928008
if (capturedNames.size > 0) {
8009+
// Check if closures feature is enabled
8010+
if (!this.options.hasFeature(Feature.Closures)) {
8011+
this.error(
8012+
DiagnosticCode.Feature_0_is_not_enabled,
8013+
funcExpr.range, "closures"
8014+
);
8015+
break;
8016+
}
79938017
// Store captured names for later use when compiling variable declarations
79948018
let existingNames = instance.preCapturedNames;
79958019
if (!existingNames) {

src/index-wasm.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export const FEATURE_RELAXED_SIMD = Feature.RelaxedSimd;
202202
export const FEATURE_EXTENDED_CONST = Feature.ExtendedConst;
203203
/** String references. */
204204
export const FEATURE_STRINGREF = Feature.Stringref;
205+
/** Closures. */
206+
export const FEATURE_CLOSURES = Feature.Closures;
205207
/** All features. */
206208
export const FEATURES_ALL = Feature.All;
207209
/** Default features. */

src/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,8 @@ export class Program extends DiagnosticEmitter {
10861086
i64_new(options.hasFeature(Feature.ExtendedConst) ? 1 : 0, 0));
10871087
this.registerConstantInteger(CommonNames.ASC_FEATURE_STRINGREF, Type.bool,
10881088
i64_new(options.hasFeature(Feature.Stringref) ? 1 : 0, 0));
1089+
this.registerConstantInteger(CommonNames.ASC_FEATURE_CLOSURES, Type.bool,
1090+
i64_new(options.hasFeature(Feature.Closures) ? 1 : 0, 0));
10891091

10901092
// remember deferred elements
10911093
let queuedImports = new Array<QueuedImport>();

std/assembly/shared/feature.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ export const enum Feature {
3434
ExtendedConst = 1 << 13, // see: https://github.com/WebAssembly/extended-const
3535
/** Reference typed strings. */
3636
Stringref = 1 << 14, // see: https://github.com/WebAssembly/stringref
37+
/** Closures. */
38+
Closures = 1 << 15,
3739
/** All features. */
38-
All = (1 << 15) - 1
40+
All = (1 << 16) - 1
3941
}
4042

4143
/** Gets the name of the specified feature one would specify on the command line. */
@@ -56,6 +58,7 @@ export function featureToString(feature: Feature): string {
5658
case Feature.RelaxedSimd: return "relaxed-simd";
5759
case Feature.ExtendedConst: return "extended-const";
5860
case Feature.Stringref: return "stringref";
61+
case Feature.Closures: return "closures";
5962
}
6063
assert(false);
6164
return "";

tests/compiler/closure-class.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"asc_flags": []
2+
"asc_flags": [
3+
"--enable", "closures"
4+
]
35
}

tests/compiler/closure-stress.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"asc_flags": [
3+
"--enable", "closures"
34
]
45
}

tests/compiler/closure.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"asc_flags": [
3+
"--enable", "closures"
34
]
45
}

0 commit comments

Comments
 (0)