Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,16 @@ exports.main = function main(argv, options, callback) {
// Validate the module if requested
if (!args.noValidate) {
stats.validateCount++;
let callbackErrorResponse;
stats.validateTime += measure(() => {
if (!module.validate()) {
module.dispose();
return callback(Error("Validate error"));
callbackErrorResponse = callback(Error("Validate error"));
}
});
if (callbackErrorResponse) {
return callbackErrorResponse;
}
}

// Set Binaryen-specific options
Expand Down
15 changes: 11 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8234,10 +8234,11 @@ export class Compiler extends DiagnosticEmitter {
assert(!declaration.typeParameters); // function expression cannot be generic
var flow = this.currentFlow;
var actualFunction = flow.actualFunction;
var anonymousFunctionName = declaration.name.text.length
? declaration.name.text
: "~anonymous|" + (actualFunction.nextAnonymousId++).toString();
var prototype = new FunctionPrototype(
declaration.name.text.length
? declaration.name.text
: "anonymous|" + (actualFunction.nextAnonymousId++).toString(),
anonymousFunctionName,
actualFunction,
declaration,
DecoratorFlags.NONE
Expand Down Expand Up @@ -8599,8 +8600,14 @@ export class Compiler extends DiagnosticEmitter {
if (!originalFunctionInstance || !this.compileFunction(originalFunctionInstance)) return module.unreachable();

// Create a new closure function for our original function
// TODO: Figure out why recursive closures don't work.
let anonymousFunctionName = functionPrototype.name + "~anonymous|" + (actualFunction.nextAnonymousId++).toString();
// Check if we are recursively calling the original actual function from our closure
if (actualFunction.name.includes( functionPrototype.name + "~anonymous|")) {
// anonymousFunctionName += "~recursive|" + (Function.nextAnonymousRecursiveId++).toString();
}
let closureFunctionInstance = new Function(
functionPrototype.name + "~anonymous|" + (actualFunction.nextAnonymousId++).toString(),
anonymousFunctionName,
functionPrototype,
null,
originalFunctionInstance.signature.toClosureSignature(),
Expand Down
1 change: 1 addition & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3477,6 +3477,7 @@ export class Function extends TypedElement {
nextInlineId: i32 = 0;
/** Counting id of anonymous inner functions. */
nextAnonymousId: i32 = 0;
static nextAnonymousRecursiveId: i32 = 0;
/** Counting id of autorelease variables. */
nextAutoreleaseId: i32 = 0;

Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/builtins.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(data (i32.const 704) "\08\00\00\00\01\00\00\00\01\00\00\00\08\00\00\00v\00o\00i\00d\00")
(data (i32.const 736) "\14\00\00\00\01\00\00\00\01\00\00\00\14\00\00\00s\00o\00m\00e\00 \00v\00a\00l\00u\00e\00")
(table $0 3 funcref)
(elem (i32.const 1) $start:builtins~anonymous|0 $start:builtins~anonymous|1)
(elem (i32.const 1) $start:builtins~~anonymous|0 $start:builtins~~anonymous|1)
(global $builtins/b (mut i32) (i32.const 0))
(global $builtins/i (mut i32) (i32.const 0))
(global $builtins/I (mut i64) (i64.const 0))
Expand Down Expand Up @@ -313,7 +313,7 @@
call $~lib/rt/stub/__release
local.get $2
)
(func $start:builtins~anonymous|0 (param $0 i32)
(func $start:builtins~~anonymous|0 (param $0 i32)
nop
)
(func $~lib/rt/stub/maybeGrowMemory (param $0 i32)
Expand Down Expand Up @@ -425,7 +425,7 @@
i32.store offset=12
local.get $2
)
(func $start:builtins~anonymous|1 (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32)
(func $start:builtins~~anonymous|1 (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32)
nop
)
(func $start:builtins
Expand Down
12 changes: 6 additions & 6 deletions tests/compiler/closure-common-js-patterns.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(data (i32.const 1184) ":\00\00\00\01\00\00\00\01\00\00\00:\00\00\00c\00l\00o\00s\00u\00r\00e\00-\00c\00o\00m\00m\00o\00n\00-\00j\00s\00-\00p\00a\00t\00t\00e\00r\00n\00s\00.\00t\00s")
(data (i32.const 1264) "\04\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 ")
(table $0 6 funcref)
(elem (i32.const 1) $start:closure-common-js-patterns~anonymous|0 $start:closure-common-js-patterns~anonymous|1~anonymous|0 $start:closure-common-js-patterns~anonymous|1 $start:closure-common-js-patterns~anonymous|2~anonymous|0 $start:closure-common-js-patterns~anonymous|2)
(elem (i32.const 1) $start:closure-common-js-patterns~~anonymous|0 $start:closure-common-js-patterns~~anonymous|1~~anonymous|0 $start:closure-common-js-patterns~~anonymous|1 $start:closure-common-js-patterns~~anonymous|2~~anonymous|0 $start:closure-common-js-patterns~~anonymous|2)
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/collectLock (mut i32) (i32.const 0))
(global $closure-common-js-patterns/myData (mut i32) (i32.const 0))
Expand Down Expand Up @@ -1104,15 +1104,15 @@
call $~lib/rt/pure/decrement
end
)
(func $start:closure-common-js-patterns~anonymous|0 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|0 (param $0 i32) (result i32)
i32.const 1
)
(func $start:closure-common-js-patterns~anonymous|1~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|1~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $1
i32.const 1
i32.add
)
(func $start:closure-common-js-patterns~anonymous|1 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|1 (param $0 i32) (result i32)
(local $1 i32)
i32.const 8
i32.const 3
Expand Down Expand Up @@ -1142,13 +1142,13 @@
call $~lib/rt/pure/__release
local.get $0
)
(func $start:closure-common-js-patterns~anonymous|2~anonymous|0 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|2~~anonymous|0 (param $0 i32) (result i32)
local.get $0
i32.load offset=4
i32.const 24
i32.add
)
(func $start:closure-common-js-patterns~anonymous|2 (param $0 i32) (param $1 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|2 (param $0 i32) (param $1 i32) (result i32)
i32.const 8
i32.const 0
call $~lib/rt/tlsf/__alloc
Expand Down
12 changes: 6 additions & 6 deletions tests/compiler/closure-common-js-patterns.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(data (i32.const 176) ":\00\00\00\01\00\00\00\01\00\00\00:\00\00\00c\00l\00o\00s\00u\00r\00e\00-\00c\00o\00m\00m\00o\00n\00-\00j\00s\00-\00p\00a\00t\00t\00e\00r\00n\00s\00.\00t\00s\00")
(data (i32.const 256) "\04\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00")
(table $0 6 funcref)
(elem (i32.const 1) $start:closure-common-js-patterns~anonymous|0 $start:closure-common-js-patterns~anonymous|1~anonymous|0 $start:closure-common-js-patterns~anonymous|1 $start:closure-common-js-patterns~anonymous|2~anonymous|0 $start:closure-common-js-patterns~anonymous|2)
(elem (i32.const 1) $start:closure-common-js-patterns~~anonymous|0 $start:closure-common-js-patterns~~anonymous|1~~anonymous|0 $start:closure-common-js-patterns~~anonymous|1 $start:closure-common-js-patterns~~anonymous|2~~anonymous|0 $start:closure-common-js-patterns~~anonymous|2)
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
(global $~lib/rt/tlsf/collectLock (mut i32) (i32.const 0))
Expand Down Expand Up @@ -1536,10 +1536,10 @@
call $~lib/rt/pure/decrement
end
)
(func $start:closure-common-js-patterns~anonymous|0 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|0 (param $0 i32) (result i32)
i32.const 1
)
(func $start:closure-common-js-patterns~anonymous|1~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|1~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $1
i32.const 1
i32.add
Expand All @@ -1562,7 +1562,7 @@
i32.store offset=4
local.get $0
)
(func $start:closure-common-js-patterns~anonymous|1 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|1 (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
Expand Down Expand Up @@ -1593,13 +1593,13 @@
call $~lib/rt/pure/__release
local.get $3
)
(func $start:closure-common-js-patterns~anonymous|2~anonymous|0 (param $0 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|2~~anonymous|0 (param $0 i32) (result i32)
i32.const 24
local.get $0
i32.load offset=4
i32.add
)
(func $start:closure-common-js-patterns~anonymous|2 (param $0 i32) (param $1 i32) (result i32)
(func $start:closure-common-js-patterns~~anonymous|2 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/closure.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(data (i32.const 1136) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00p\00u\00r\00e\00.\00t\00s")
(data (i32.const 1184) "\03\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 ")
(table $0 7 funcref)
(elem (i32.const 1) $closure/testParam~inner $closure/testParam~inner $closure/testParam~inner $closure/createClosure~anonymous|0 $closure/runInline~anonymous|0 $closure/returnOverBoundary~anonymous|0)
(elem (i32.const 1) $closure/testParam~inner $closure/testParam~inner $closure/testParam~inner $closure/createClosure~~anonymous|0 $closure/runInline~~anonymous|0 $closure/returnOverBoundary~~anonymous|0)
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/collectLock (mut i32) (i32.const 0))
(global $~lib/rt/__rtti_base i32 (i32.const 1184))
Expand Down Expand Up @@ -1111,7 +1111,7 @@
local.get $0
i32.load offset=4
)
(func $closure/createClosure~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $closure/createClosure~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $1
local.get $0
i32.load offset=4
Expand All @@ -1135,7 +1135,7 @@
local.get $0
call $~lib/rt/pure/__release
)
(func $closure/runInline~anonymous|0 (param $0 i32) (result i32)
(func $closure/runInline~~anonymous|0 (param $0 i32) (result i32)
local.get $0
i32.load offset=12
local.get $0
Expand All @@ -1145,7 +1145,7 @@
i32.add
i32.add
)
(func $closure/returnOverBoundary~anonymous|0 (param $0 i32) (result i32)
(func $closure/returnOverBoundary~~anonymous|0 (param $0 i32) (result i32)
i32.const 6
)
(func $closure/returnOverBoundary (result i32)
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/closure.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(data (i32.const 128) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00p\00u\00r\00e\00.\00t\00s\00")
(data (i32.const 176) "\03\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00")
(table $0 7 funcref)
(elem (i32.const 1) $closure/testParam~inner $closure/testVar~inner $closure/testLet~inner $closure/createClosure~anonymous|0 $closure/runInline~anonymous|0 $closure/returnOverBoundary~anonymous|0)
(elem (i32.const 1) $closure/testParam~inner $closure/testVar~inner $closure/testLet~inner $closure/createClosure~~anonymous|0 $closure/runInline~~anonymous|0 $closure/returnOverBoundary~~anonymous|0)
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
(global $~lib/rt/tlsf/collectLock (mut i32) (i32.const 0))
Expand Down Expand Up @@ -1629,7 +1629,7 @@
call $~lib/rt/pure/__release
local.get $3
)
(func $closure/createClosure~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $closure/createClosure~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $0
i32.load offset=4
local.get $1
Expand Down Expand Up @@ -1694,7 +1694,7 @@
call $~lib/rt/pure/__release
local.get $2
)
(func $closure/runInline~anonymous|0 (param $0 i32) (result i32)
(func $closure/runInline~~anonymous|0 (param $0 i32) (result i32)
local.get $0
i32.load offset=4
local.get $0
Expand Down Expand Up @@ -1753,7 +1753,7 @@
call $~lib/rt/pure/__release
local.get $2
)
(func $closure/returnOverBoundary~anonymous|0 (param $0 i32) (result i32)
(func $closure/returnOverBoundary~~anonymous|0 (param $0 i32) (result i32)
i32.const 6
)
(func $closure/returnOverBoundary (result i32)
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/exportimport-table.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
(type $none_=>_none (func))
(type $i32_=>_none (func (param i32)))
(import "env" "table" (table $0 2 funcref))
(elem (i32.const 1) $start:exportimport-table~anonymous|0)
(elem (i32.const 1) $start:exportimport-table~~anonymous|0)
(memory $0 0)
(export "memory" (memory $0))
(export "table" (table $0))
(start $~start)
(func $start:exportimport-table~anonymous|0 (param $0 i32)
(func $start:exportimport-table~~anonymous|0 (param $0 i32)
nop
)
(func $~start
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/exportimport-table.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(import "env" "table" (table $0 2 funcref))
(elem (i32.const 1) $start:exportimport-table~anonymous|0)
(elem (i32.const 1) $start:exportimport-table~~anonymous|0)
(memory $0 0)
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
Expand All @@ -13,7 +13,7 @@
(export "memory" (memory $0))
(export "table" (table $0))
(start $~start)
(func $start:exportimport-table~anonymous|0 (param $0 i32)
(func $start:exportimport-table~~anonymous|0 (param $0 i32)
nop
)
(func $~lib/rt/stub/maybeGrowMemory (param $0 i32)
Expand Down
11 changes: 9 additions & 2 deletions tests/compiler/features/reference-types.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(module
(type $none_=>_none (func))
(type $i32_=>_none (func (param i32)))
(type $anyref_=>_anyref (func (param anyref) (result anyref)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $anyref_=>_none (func (param anyref)))
Expand All @@ -25,6 +26,12 @@
(func $features/reference-types/someFunc
nop
)
(func $features/reference-types/someFunc~anonymous|0 (param $0 i32)
nop
)
(func $features/reference-types/someFunc~anonymous|1 (param $0 i32)
nop
)
(func $start:features/reference-types
(local $0 anyref)
(local $1 anyref)
Expand Down Expand Up @@ -136,9 +143,9 @@
call $~lib/builtins/abort
unreachable
end
ref.func $features/reference-types/someFunc
ref.func $features/reference-types/someFunc~anonymous|0
global.set $features/reference-types/funcGlobal
ref.func $features/reference-types/someFunc
ref.func $features/reference-types/someFunc~anonymous|1
local.set $1
)
(func $features/reference-types/internal (param $0 anyref) (result anyref)
Expand Down
20 changes: 10 additions & 10 deletions tests/compiler/function-expression.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(memory $0 1)
(data (i32.const 1024) ",\00\00\00\01\00\00\00\01\00\00\00,\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s")
(table $0 18 funcref)
(elem (i32.const 1) $start:function-expression~anonymous|0 $start:function-expression~anonymous|0 $start:function-expression~someName $start:function-expression~anonymous|2 $start:function-expression~anonymous|3 $start:function-expression~anonymous|4 $start:function-expression~anonymous|5 $start:function-expression~anonymous|3 $start:function-expression~anonymous|4 $start:function-expression~anonymous|5 $start:function-expression~anonymous|2 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testGlobal~anonymous|0 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testLocal~anonymous|0 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testField~anonymous|0)
(elem (i32.const 1) $start:function-expression~~anonymous|0 $start:function-expression~~anonymous|0 $start:function-expression~someName $start:function-expression~~anonymous|2 $start:function-expression~~anonymous|3 $start:function-expression~~anonymous|4 $start:function-expression~~anonymous|5 $start:function-expression~~anonymous|3 $start:function-expression~~anonymous|4 $start:function-expression~~anonymous|5 $start:function-expression~~anonymous|2 $function-expression/testGlobal~~anonymous|0~~anonymous|0 $function-expression/testGlobal~~anonymous|0 $function-expression/testGlobal~~anonymous|0~~anonymous|0 $function-expression/testLocal~~anonymous|0 $function-expression/testGlobal~~anonymous|0~~anonymous|0 $function-expression/testField~~anonymous|0)
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(global $function-expression/f1 (mut i32) (i32.const 0))
(global $function-expression/f2 (mut i32) (i32.const 0))
Expand All @@ -18,7 +18,7 @@
(global $function-expression/globalFunc (mut i32) (i32.const 0))
(export "memory" (memory $0))
(start $~start)
(func $start:function-expression~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $start:function-expression~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $1
)
(func $~lib/rt/stub/__alloc (param $0 i32) (result i32)
Expand Down Expand Up @@ -90,10 +90,10 @@
(func $start:function-expression~someName (param $0 i32)
nop
)
(func $start:function-expression~anonymous|2 (param $0 i32) (result i32)
(func $start:function-expression~~anonymous|2 (param $0 i32) (result i32)
i32.const 1
)
(func $start:function-expression~anonymous|3 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(func $start:function-expression~~anonymous|3 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
local.get $1
local.get $2
i32.add
Expand All @@ -106,34 +106,34 @@
i32.load
call_indirect (type $i32_i32_i32_=>_i32)
)
(func $start:function-expression~anonymous|4 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(func $start:function-expression~~anonymous|4 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
local.get $1
)
(func $start:function-expression~anonymous|5 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(func $start:function-expression~~anonymous|5 (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
i32.const 42
)
(func $function-expression/testGlobal~anonymous|0~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
(func $function-expression/testGlobal~~anonymous|0~~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
local.get $1
i32.const 24
i32.add
)
(func $function-expression/testGlobal~anonymous|0 (param $0 i32) (result i32)
(func $function-expression/testGlobal~~anonymous|0 (param $0 i32) (result i32)
i32.const 0
call $~lib/rt/stub/__alloc
local.tee $0
i32.const 12
i32.store
local.get $0
)
(func $function-expression/testLocal~anonymous|0 (param $0 i32) (result i32)
(func $function-expression/testLocal~~anonymous|0 (param $0 i32) (result i32)
i32.const 0
call $~lib/rt/stub/__alloc
local.tee $0
i32.const 14
i32.store
local.get $0
)
(func $function-expression/testField~anonymous|0 (param $0 i32) (result i32)
(func $function-expression/testField~~anonymous|0 (param $0 i32) (result i32)
i32.const 0
call $~lib/rt/stub/__alloc
local.tee $0
Expand Down
Loading