Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure constructors of exported classes #1154

Merged
merged 2 commits into from
Mar 10, 2020
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
30 changes: 18 additions & 12 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,10 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case ElementKind.PROPERTY_PROTOTYPE: {
let getter = (<PropertyPrototype>element).getterPrototype;
let prototype = <PropertyPrototype>element;
let getter = prototype.getterPrototype;
if (getter) this.ensureModuleExport(GETTER_PREFIX + name, getter, prefix);
let setter = (<PropertyPrototype>element).setterPrototype;
let setter = prototype.setterPrototype;
if (setter) this.ensureModuleExport(SETTER_PREFIX + name, setter, prefix);
break;
}
Expand Down Expand Up @@ -699,28 +700,31 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case ElementKind.PROPERTY: {
let getter = (<Property>element).getterInstance;
let instance = <Property>element;
let getter = instance.getterInstance;
if (getter) this.ensureModuleExport(GETTER_PREFIX + name, getter, prefix);
let setter = (<Property>element).setterInstance;
let setter = instance.setterInstance;
if (setter) this.ensureModuleExport(SETTER_PREFIX + name, setter, prefix);
break;
}
case ElementKind.FIELD: {
let instance = <Field>element;
if (element.is(CommonFlags.COMPILED)) {
let module = this.module;
module.addFunctionExport((<Field>element).internalGetterName, prefix + GETTER_PREFIX + name);
module.addFunctionExport(instance.internalGetterName, prefix + GETTER_PREFIX + name);
if (!element.is(CommonFlags.READONLY)) {
module.addFunctionExport((<Field>element).internalSetterName, prefix + SETTER_PREFIX + name);
module.addFunctionExport(instance.internalSetterName, prefix + SETTER_PREFIX + name);
}
}
break;
}
case ElementKind.CLASS: {
let instance = <Class>element;
// make the class name itself represent its runtime id
if (!(<Class>element).type.isUnmanaged) {
if (!instance.type.isUnmanaged) {
let module = this.module;
let internalName = (<Class>element).internalName;
module.addGlobal(internalName, NativeType.I32, false, module.i32((<Class>element).id));
let internalName = instance.internalName;
module.addGlobal(internalName, NativeType.I32, false, module.i32(instance.id));
module.addGlobalExport(internalName, prefix + name);
}
break;
Expand Down Expand Up @@ -1501,8 +1505,7 @@ export class Compiler extends DiagnosticEmitter {
}
}
}
var ctorInstance = instance.constructorInstance;
if (ctorInstance) this.compileFunction(ctorInstance);
this.ensureConstructor(instance, instance.identifierNode);
var instanceMembers = instance.members;
if (instanceMembers) {
// TODO: for (let element of instanceMembers.values()) {
Expand Down Expand Up @@ -8704,14 +8707,17 @@ export class Compiler extends DiagnosticEmitter {
CommonNames.constructor,
new FunctionPrototype(
CommonNames.constructor,
classInstance,
classInstance, // bound
this.program.makeNativeFunctionDeclaration(CommonNames.constructor,
CommonFlags.INSTANCE | CommonFlags.CONSTRUCTOR
)
),
new Signature(this.program, null, classInstance.type, classInstance.type),
contextualTypeArguments
);
let members = classInstance.members;
if (!members) classInstance.members = members = new Map();
members.set("constructor", instance.prototype);
}

instance.internalName = classInstance.internalName + INSTANCE_DELIMITER + "constructor";
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/extends-recursive.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(module
(type $none_=>_none (func))
(type $i32_i32_=>_none (func (param i32 i32)))
(type $i32_=>_i32 (func (param i32) (result i32)))
(memory $0 0)
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(global $extends-recursive/Child i32 (i32.const 3))
(export "memory" (memory $0))
(export "Child" (global $extends-recursive/Child))
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
(start $~start)
(func $extends-recursive/Parent#get:child (; 0 ;) (param $0 i32) (result i32)
local.get $0
i32.load
Expand All @@ -19,4 +23,10 @@
local.get $1
i32.store
)
(func $~start (; 2 ;)
i32.const 16
global.set $~lib/rt/stub/startOffset
i32.const 16
global.set $~lib/rt/stub/offset
)
)
164 changes: 160 additions & 4 deletions tests/compiler/extends-recursive.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,26 +1,170 @@
(module
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_=>_none (func (param i32)))
(type $none_=>_none (func))
(type $i32_i32_=>_none (func (param i32 i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(memory $0 0)
(table $0 1 funcref)
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(global $~lib/heap/__heap_base i32 (i32.const 8))
(global $extends-recursive/Child i32 (i32.const 3))
(export "memory" (memory $0))
(export "Child" (global $extends-recursive/Child))
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
(func $~lib/rt/stub/__retain (; 0 ;) (param $0 i32) (result i32)
(start $~start)
(func $~lib/rt/stub/maybeGrowMemory (; 0 ;) (param $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
memory.size
local.set $1
local.get $1
i32.const 16
i32.shl
local.set $2
local.get $0
local.get $2
i32.gt_u
if
local.get $0
local.get $2
i32.sub
i32.const 65535
i32.add
i32.const 65535
i32.const -1
i32.xor
i32.and
i32.const 16
i32.shr_u
local.set $3
local.get $1
local.tee $4
local.get $3
local.tee $5
local.get $4
local.get $5
i32.gt_s
select
local.set $4
local.get $4
memory.grow
i32.const 0
i32.lt_s
if
local.get $3
memory.grow
i32.const 0
i32.lt_s
if
unreachable
end
end
end
local.get $0
global.set $~lib/rt/stub/offset
)
(func $~lib/rt/stub/__alloc (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
local.get $0
i32.const 1073741808
i32.gt_u
if
unreachable
end
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.set $2
local.get $0
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
local.tee $3
i32.const 16
local.tee $4
local.get $3
local.get $4
i32.gt_u
select
local.set $5
local.get $2
local.get $5
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
i32.const 16
i32.sub
local.set $6
local.get $6
local.get $5
i32.store
local.get $6
i32.const 1
i32.store offset=4
local.get $6
local.get $1
i32.store offset=8
local.get $6
local.get $0
i32.store offset=12
local.get $2
)
(func $~lib/rt/stub/__retain (; 2 ;) (param $0 i32) (result i32)
local.get $0
)
(func $extends-recursive/Parent#get:child (; 1 ;) (param $0 i32) (result i32)
(func $extends-recursive/Parent#constructor (; 3 ;) (param $0 i32) (result i32)
local.get $0
i32.eqz
if
i32.const 4
i32.const 4
call $~lib/rt/stub/__alloc
call $~lib/rt/stub/__retain
local.set $0
end
local.get $0
i32.const 0
call $~lib/rt/stub/__retain
i32.store
local.get $0
)
(func $extends-recursive/Child#constructor (; 4 ;) (param $0 i32) (result i32)
local.get $0
i32.eqz
if
i32.const 4
i32.const 3
call $~lib/rt/stub/__alloc
call $~lib/rt/stub/__retain
local.set $0
end
local.get $0
call $extends-recursive/Parent#constructor
local.set $0
local.get $0
)
(func $extends-recursive/Parent#get:child (; 5 ;) (param $0 i32) (result i32)
local.get $0
i32.load
call $~lib/rt/stub/__retain
)
(func $~lib/rt/stub/__release (; 2 ;) (param $0 i32)
(func $~lib/rt/stub/__release (; 6 ;) (param $0 i32)
nop
)
(func $extends-recursive/Parent#set:child (; 3 ;) (param $0 i32) (param $1 i32)
(func $extends-recursive/Parent#set:child (; 7 ;) (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
local.get $1
Expand All @@ -38,4 +182,16 @@
local.get $1
i32.store
)
(func $~start (; 8 ;)
global.get $~lib/heap/__heap_base
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
global.set $~lib/rt/stub/startOffset
global.get $~lib/rt/stub/startOffset
global.set $~lib/rt/stub/offset
)
)
Loading