Namespaced imports

This commit is contained in:
dcodeIO 2017-12-27 22:38:32 +01:00
parent eccac129ad
commit 7795d48d98
13 changed files with 157 additions and 74 deletions

View File

@ -88,48 +88,48 @@ export function initialize(program: Program): void {
i64Func: FunctionPrototype, i64Func: FunctionPrototype,
u64Func: FunctionPrototype; u64Func: FunctionPrototype;
addFunction(program, "i8").members = new Map([ addFunction(program, "i8").members = new Map([
[ "MIN_VALUE", new Global(program, "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ],
[ "MAX_VALUE", new Global(program, "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ]
]); ]);
addFunction(program, "i16").members = new Map([ addFunction(program, "i16").members = new Map([
[ "MIN_VALUE", new Global(program, "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ],
[ "MAX_VALUE", new Global(program, "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ]
]); ]);
(i32Func = addFunction(program, "i32")).members = new Map([ (i32Func = addFunction(program, "i32")).members = new Map([
[ "MIN_VALUE", new Global(program, "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ],
[ "MAX_VALUE", new Global(program, "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ]
]); ]);
(i64Func = addFunction(program, "i64")).members = new Map([ (i64Func = addFunction(program, "i64")).members = new Map([
[ "MIN_VALUE", new Global(program, "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ],
[ "MAX_VALUE", new Global(program, "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ]
]); ]);
addFunction(program, "u8").members = new Map([ addFunction(program, "u8").members = new Map([
[ "MIN_VALUE", new Global(program, "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ]
]); ]);
addFunction(program, "u16").members = new Map([ addFunction(program, "u16").members = new Map([
[ "MIN_VALUE", new Global(program, "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ]
]); ]);
(u32Func = addFunction(program, "u32")).members = new Map([ (u32Func = addFunction(program, "u32")).members = new Map([
[ "MIN_VALUE", new Global(program, "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ]
]); ]);
(u64Func = addFunction(program, "u64")).members = new Map([ (u64Func = addFunction(program, "u64")).members = new Map([
[ "MIN_VALUE", new Global(program, "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ]
]); ]);
addFunction(program, "bool").members = new Map([ addFunction(program, "bool").members = new Map([
[ "MIN_VALUE", new Global(program, "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ], [ "MIN_VALUE", new Global(program, "MIN_VALUE", "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ] [ "MAX_VALUE", new Global(program, "MAX_VALUE", "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ]
]); ]);
addFunction(program, "f32").members = new Map([ addFunction(program, "f32").members = new Map([
[ "MIN_SAFE_INTEGER", new Global(program, "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ], [ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ],
[ "MAX_SAFE_INTEGER", new Global(program, "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ] [ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ]
]); ]);
addFunction(program, "f64").members = new Map([ addFunction(program, "f64").members = new Map([
[ "MIN_SAFE_INTEGER", new Global(program, "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ], [ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ],
[ "MAX_SAFE_INTEGER", new Global(program, "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ] [ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ]
]); ]);
if (program.target == Target.WASM64) { if (program.target == Target.WASM64) {
program.elements.set("isize", i64Func); program.elements.set("isize", i64Func);
@ -144,7 +144,7 @@ export function initialize(program: Program): void {
/** Adds a built-in constant to the specified program. */ /** Adds a built-in constant to the specified program. */
function addConstant(program: Program, name: string, type: Type): Global { function addConstant(program: Program, name: string, type: Type): Global {
const global: Global = new Global(program, name, null, null); const global: Global = new Global(program, name, name, null);
global.isBuiltIn = true; global.isBuiltIn = true;
global.isConstant = true; global.isConstant = true;
global.type = type; global.type = type;

View File

@ -363,6 +363,18 @@ export class Compiler extends DiagnosticEmitter {
} }
const nativeType = global.type.toNativeType(); const nativeType = global.type.toNativeType();
if (global.isDeclared) {
if (global.isConstant) {
this.module.addGlobalImport(global.internalName, global.namespace ? global.namespace.simpleName : "env", global.simpleName, nativeType);
global.isCompiled = true;
return true;
} else if (declaration) {
this.error(DiagnosticCode.Operation_not_supported, declaration.range);
}
return false;
}
let initializeInStart = false; let initializeInStart = false;
if (global.hasConstantValue) { if (global.hasConstantValue) {
@ -569,8 +581,8 @@ export class Compiler extends DiagnosticEmitter {
typeRef = this.module.addFunctionType(signatureNameParts.join(""), nativeResultType, nativeParamTypes); typeRef = this.module.addFunctionType(signatureNameParts.join(""), nativeResultType, nativeParamTypes);
// create the function // create the function
if (instance.isDeclared) { // TODO: use parent namespace as externalModuleName, if applicable if (instance.isDeclared) {
this.module.addFunctionImport(instance.internalName, "env", declaration.name.name, typeRef); this.module.addFunctionImport(instance.internalName, instance.prototype.namespace ? instance.prototype.namespace.simpleName : "env", declaration.name.name, typeRef);
} else { } else {
this.module.addFunction(instance.internalName, typeRef, typesToNativeTypes(instance.additionalLocals), this.module.createBlock(null, <ExpressionRef[]>stmts, NativeType.None)); this.module.addFunction(instance.internalName, typeRef, typesToNativeTypes(instance.additionalLocals), this.module.createBlock(null, <ExpressionRef[]>stmts, NativeType.None));
} }

View File

@ -420,9 +420,10 @@ export class Parser extends DiagnosticEmitter {
if (!initializer) if (!initializer)
return null; return null;
} else { } else {
if (hasModifier(ModifierKind.CONST, parentModifiers)) if (hasModifier(ModifierKind.CONST, parentModifiers)) {
this.error(DiagnosticCode._const_declarations_must_be_initialized, identifier.range); if (!hasModifier(ModifierKind.DECLARE, parentModifiers))
else if (!type) // neither type nor initializer this.error(DiagnosticCode._const_declarations_must_be_initialized, identifier.range);
} else if (!type) // neither type nor initializer
this.error(DiagnosticCode.Type_expected, tn.range(tn.pos)); // recoverable this.error(DiagnosticCode.Type_expected, tn.range(tn.pos)); // recoverable
} }
return Node.createVariableDeclaration(identifier, type, initializer, parentModifiers, parentDecorators, Range.join(identifier.range, tn.range())); return Node.createVariableDeclaration(identifier, type, initializer, parentModifiers, parentDecorators, Range.join(identifier.range, tn.range()));

View File

@ -317,7 +317,7 @@ export class Program extends DiagnosticEmitter {
} }
} else } else
classPrototype.members = new Map(); classPrototype.members = new Map();
const staticField = new Global(this, internalName, declaration, null); const staticField = new Global(this, name, internalName, declaration, null);
classPrototype.members.set(name, staticField); classPrototype.members.set(name, staticField);
this.elements.set(internalName, staticField); this.elements.set(internalName, staticField);
@ -330,7 +330,7 @@ export class Program extends DiagnosticEmitter {
} }
} else } else
classPrototype.instanceMembers = new Map(); classPrototype.instanceMembers = new Map();
const instanceField = new FieldPrototype(classPrototype, internalName, declaration); const instanceField = new FieldPrototype(classPrototype, name, internalName, declaration);
classPrototype.instanceMembers.set(name, instanceField); classPrototype.instanceMembers.set(name, instanceField);
} }
} }
@ -433,7 +433,7 @@ export class Program extends DiagnosticEmitter {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return; return;
} }
const enm = new Enum(this, internalName, declaration); const enm = new Enum(this, declaration.name.name, internalName, declaration);
enm.namespace = namespace; enm.namespace = namespace;
this.elements.set(internalName, enm); this.elements.set(internalName, enm);
@ -469,7 +469,7 @@ export class Program extends DiagnosticEmitter {
} }
} else } else
enm.members = new Map(); enm.members = new Map();
const value = new EnumValue(enm, this, internalName, declaration); const value = new EnumValue(enm, this, name, internalName, declaration);
enm.members.set(name, value); enm.members.set(name, value);
} }
@ -699,7 +699,7 @@ export class Program extends DiagnosticEmitter {
let namespace = this.elements.get(internalName); let namespace = this.elements.get(internalName);
if (!namespace) { if (!namespace) {
namespace = new Namespace(this, internalName, declaration); namespace = new Namespace(this, declaration.name.name, internalName, declaration);
namespace.namespace = parentNamespace; namespace.namespace = parentNamespace;
this.elements.set(internalName, namespace); this.elements.set(internalName, namespace);
} }
@ -780,7 +780,7 @@ export class Program extends DiagnosticEmitter {
continue; continue;
} }
const global = new Global(this, internalName, declaration, null); const global = new Global(this, declaration.name.name, internalName, declaration, null);
global.namespace = namespace; global.namespace = namespace;
this.elements.set(internalName, global); this.elements.set(internalName, global);
@ -1041,6 +1041,8 @@ export abstract class Element {
kind: ElementKind; kind: ElementKind;
/** Containing {@link Program}. */ /** Containing {@link Program}. */
program: Program; program: Program;
/** Simple name. */
simpleName: string;
/** Internal name referring to this element. */ /** Internal name referring to this element. */
internalName: string; internalName: string;
/** Element flags. */ /** Element flags. */
@ -1051,8 +1053,9 @@ export abstract class Element {
namespace: Element | null = null; namespace: Element | null = null;
/** Constructs a new element, linking it to its containing {@link Program}. */ /** Constructs a new element, linking it to its containing {@link Program}. */
protected constructor(program: Program, internalName: string) { protected constructor(program: Program, simpleName: string, internalName: string) {
this.program = program; this.program = program;
this.simpleName = simpleName;
this.internalName = internalName; this.internalName = internalName;
} }
@ -1111,8 +1114,8 @@ export class Namespace extends Element {
declaration: NamespaceDeclaration | null; // more specific declaration: NamespaceDeclaration | null; // more specific
/** Constructs a new namespace. */ /** Constructs a new namespace. */
constructor(program: Program, internalName: string, declaration: NamespaceDeclaration | null = null) { constructor(program: Program, simpleName: string, internalName: string, declaration: NamespaceDeclaration | null = null) {
super(program, internalName); super(program, simpleName, internalName);
if ((this.declaration = declaration) && this.declaration.modifiers) { if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
switch (this.declaration.modifiers[i].modifierKind) { switch (this.declaration.modifiers[i].modifierKind) {
@ -1135,8 +1138,8 @@ export class Enum extends Element {
declaration: EnumDeclaration | null; declaration: EnumDeclaration | null;
/** Constructs a new enum. */ /** Constructs a new enum. */
constructor(program: Program, internalName: string, declaration: EnumDeclaration | null = null) { constructor(program: Program, simpleName: string, internalName: string, declaration: EnumDeclaration | null = null) {
super(program, internalName); super(program, simpleName, internalName);
if ((this.declaration = declaration) && this.declaration.modifiers) { if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
switch (this.declaration.modifiers[i].modifierKind) { switch (this.declaration.modifiers[i].modifierKind) {
@ -1163,8 +1166,8 @@ export class EnumValue extends Element {
/** Constant value, if applicable. */ /** Constant value, if applicable. */
constantValue: i32 = 0; constantValue: i32 = 0;
constructor(enm: Enum, program: Program, internalName: string, declaration: EnumValueDeclaration | null = null) { constructor(enm: Enum, program: Program, simpleName: string, internalName: string, declaration: EnumValueDeclaration | null = null) {
super(program, internalName); super(program, simpleName, internalName);
this.enum = enm; this.enum = enm;
this.declaration = declaration; this.declaration = declaration;
} }
@ -1184,8 +1187,8 @@ export class Global extends Element {
/** Constant float value, if applicable. */ /** Constant float value, if applicable. */
constantFloatValue: f64 = 0; constantFloatValue: f64 = 0;
constructor(program: Program, internalName: string, declaration: VariableLikeDeclarationStatement | null = null, type: Type | null = null) { constructor(program: Program, simpleName: string, internalName: string, declaration: VariableLikeDeclarationStatement | null = null, type: Type | null = null) {
super(program, internalName); super(program, simpleName, internalName);
if (this.declaration = declaration) { if (this.declaration = declaration) {
if (this.declaration.modifiers) { if (this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1250,8 +1253,8 @@ export class Local extends Element {
/** Local type. */ /** Local type. */
type: Type; type: Type;
constructor(program: Program, internalName: string, index: i32, type: Type) { constructor(program: Program, simpleName: string, index: i32, type: Type) {
super(program, internalName); super(program, simpleName, simpleName);
this.index = index; this.index = index;
this.type = type; this.type = type;
} }
@ -1268,13 +1271,10 @@ export class FunctionPrototype extends Element {
classPrototype: ClassPrototype | null; classPrototype: ClassPrototype | null;
/** Resolved instances. */ /** Resolved instances. */
instances: Map<string,Function> = new Map(); instances: Map<string,Function> = new Map();
/** Simple name. */
simpleName: string;
/** Constructs a new function prototype. */ /** Constructs a new function prototype. */
constructor(program: Program, simpleName: string, internalName: string, declaration: FunctionDeclaration | null, classPrototype: ClassPrototype | null = null) { constructor(program: Program, simpleName: string, internalName: string, declaration: FunctionDeclaration | null, classPrototype: ClassPrototype | null = null) {
super(program, internalName); super(program, simpleName, internalName);
this.simpleName = simpleName;
if (this.declaration = declaration) { if (this.declaration = declaration) {
if (this.declaration.modifiers) if (this.declaration.modifiers)
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1420,7 +1420,7 @@ export class Function extends Element {
/** Constructs a new concrete function. */ /** Constructs a new concrete function. */
constructor(prototype: FunctionPrototype, internalName: string, typeArguments: Type[] | null, parameters: Parameter[], returnType: Type, instanceMethodOf: Class | null) { constructor(prototype: FunctionPrototype, internalName: string, typeArguments: Type[] | null, parameters: Parameter[], returnType: Type, instanceMethodOf: Class | null) {
super(prototype.program, internalName); super(prototype.program, prototype.simpleName, internalName);
this.prototype = prototype; this.prototype = prototype;
this.typeArguments = typeArguments; this.typeArguments = typeArguments;
this.parameters = parameters; this.parameters = parameters;
@ -1559,8 +1559,8 @@ export class FieldPrototype extends Element {
classPrototype: ClassPrototype; classPrototype: ClassPrototype;
/** Constructs a new field prototype. */ /** Constructs a new field prototype. */
constructor(classPrototype: ClassPrototype, internalName: string, declaration: FieldDeclaration | null = null) { constructor(classPrototype: ClassPrototype, simpleName: string, internalName: string, declaration: FieldDeclaration | null = null) {
super(classPrototype.program, internalName); super(classPrototype.program, simpleName, internalName);
this.classPrototype = classPrototype; this.classPrototype = classPrototype;
if ((this.declaration = declaration) && this.declaration.modifiers) { if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1598,7 +1598,7 @@ export class Field extends Element {
/** Constructs a new field. */ /** Constructs a new field. */
constructor(prototype: FieldPrototype, internalName: string, type: Type) { constructor(prototype: FieldPrototype, internalName: string, type: Type) {
super(prototype.program, internalName); super(prototype.program, prototype.simpleName, internalName);
this.flags = prototype.flags; this.flags = prototype.flags;
this.type = type; this.type = type;
} }
@ -1609,8 +1609,6 @@ export class Property extends Element {
kind = ElementKind.PROPERTY; kind = ElementKind.PROPERTY;
/** Simple name. */
simpleName: string;
/** Parent class prototype. */ /** Parent class prototype. */
parent: ClassPrototype; parent: ClassPrototype;
/** Getter prototype. */ /** Getter prototype. */
@ -1620,8 +1618,7 @@ export class Property extends Element {
/** Constructs a new property prototype. */ /** Constructs a new property prototype. */
constructor(program: Program, simpleName: string, internalName: string, parent: ClassPrototype) { constructor(program: Program, simpleName: string, internalName: string, parent: ClassPrototype) {
super(program, internalName); super(program, simpleName, internalName);
this.simpleName = simpleName;
this.parent = parent; this.parent = parent;
} }
} }
@ -1637,12 +1634,9 @@ export class ClassPrototype extends Element {
instances: Map<string,Class> = new Map(); instances: Map<string,Class> = new Map();
/** Instance member prototypes. */ /** Instance member prototypes. */
instanceMembers: Map<string,Element> | null = null; instanceMembers: Map<string,Element> | null = null;
/** Simple name. */
simpleName: string;
constructor(program: Program, simpleName: string, internalName: string, declaration: ClassDeclaration | null = null) { constructor(program: Program, simpleName: string, internalName: string, declaration: ClassDeclaration | null = null) {
super(program, internalName); super(program, simpleName, internalName);
this.simpleName = simpleName;
if (this.declaration = declaration) { if (this.declaration = declaration) {
if (this.declaration.modifiers) { if (this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1736,7 +1730,7 @@ export class Class extends Element {
/** Constructs a new class. */ /** Constructs a new class. */
constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] = [], base: Class | null = null) { constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] = [], base: Class | null = null) {
super(prototype.program, internalName); super(prototype.program, prototype.simpleName, internalName);
this.prototype = prototype; this.prototype = prototype;
this.flags = prototype.flags; this.flags = prototype.flags;
this.typeArguments = typeArguments; this.typeArguments = typeArguments;

View File

@ -211,10 +211,11 @@ export class Type {
// export class ClassType extends Type { // export class ClassType extends Type {
// constructor(cls: Class) { // constructor(cls: Class) {
// super(TypeKind.USIZE, clz.size); // super(TypeKind.USIZE, /* clz.size */ 4); // TODO
// } // }
// } // }
// // TODO: what about 'type fun = <T>(a: T) => T;' ?
// export class FunctionType extends Type { // export class FunctionType extends Type {
// constructor(fun: Function) { // constructor(fun: Function) {
// super(TypeKind.USIZE, fun.program.target == Target.WASM64 ? 8 : 4); // super(TypeKind.USIZE, fun.program.target == Target.WASM64 ? 8 : 4);

View File

@ -61,7 +61,12 @@ glob.sync(filter, { cwd: __dirname + "/compiler" }).forEach(filename => {
var wasmModule = new WebAssembly.Module(module.toBinary()); var wasmModule = new WebAssembly.Module(module.toBinary());
var wasmInstance = new WebAssembly.Instance(wasmModule, { var wasmInstance = new WebAssembly.Instance(wasmModule, {
env: { env: {
external: function() {} externalFunc: function() {},
externalConst: 1
},
external: {
externalFunc: function() {},
externalConst: 2
} }
}); });
console.log(chalk.default.green("instantiate OK")); console.log(chalk.default.green("instantiate OK"));

View File

@ -1,7 +1,12 @@
(module (module
(type $v (func)) (type $v (func))
(import "env" "external" (func $declare/external)) (import "env" "externalFunc" (func $declare/externalFunc))
(import "external" "externalFunc" (func $declare/external.externalFunc))
(memory $0 1) (memory $0 1)
(export "external" (func $declare/external)) (export "test" (func $declare/test))
(export "memory" (memory $0)) (export "memory" (memory $0))
(func $declare/test (; 2 ;) (type $v)
(call $declare/externalFunc)
(call $declare/external.externalFunc)
)
) )

View File

@ -1,3 +1,15 @@
declare function external(): void; declare function externalFunc(): void;
declare const externalConst: i32;
export { external }; namespace external {
export declare function externalFunc(): void;
export declare const externalConst: i32;
}
export function test(): void {
// cannot be interpreted
externalFunc();
externalConst;
external.externalFunc();
external.externalConst;
}

View File

@ -1,10 +1,23 @@
(module (module
(type $v (func)) (type $v (func))
(import "env" "external" (func $declare/external)) (import "env" "externalFunc" (func $declare/externalFunc))
(import "env" "externalConst" (global $declare/externalConst i32))
(import "external" "externalFunc" (func $declare/external.externalFunc))
(import "external" "externalConst" (global $declare/external.externalConst i32))
(global $HEAP_BASE i32 (i32.const 4)) (global $HEAP_BASE i32 (i32.const 4))
(memory $0 1) (memory $0 1)
(export "external" (func $declare/external)) (export "test" (func $declare/test))
(export "memory" (memory $0)) (export "memory" (memory $0))
(func $declare/test (; 2 ;) (type $v)
(call $declare/externalFunc)
(drop
(get_global $declare/externalConst)
)
(call $declare/external.externalFunc)
(drop
(get_global $declare/external.externalConst)
)
)
) )
(; (;
[program.elements] [program.elements]
@ -52,7 +65,12 @@
FUNCTION_PROTOTYPE: isize FUNCTION_PROTOTYPE: isize
FUNCTION_PROTOTYPE: usize FUNCTION_PROTOTYPE: usize
GLOBAL: HEAP_BASE GLOBAL: HEAP_BASE
FUNCTION_PROTOTYPE: declare/external FUNCTION_PROTOTYPE: declare/externalFunc
GLOBAL: declare/externalConst
NAMESPACE: declare/external
FUNCTION_PROTOTYPE: declare/external.externalFunc
GLOBAL: declare/external.externalConst
FUNCTION_PROTOTYPE: declare/test
[program.exports] [program.exports]
FUNCTION_PROTOTYPE: declare/external FUNCTION_PROTOTYPE: declare/test
;) ;)

View File

@ -2,6 +2,7 @@
(type $i (func (result i32))) (type $i (func (result i32)))
(type $v (func)) (type $v (func))
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0)) (global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
(global $namespace/Joined.THREE i32 (i32.const 3))
(memory $0 1) (memory $0 1)
(export "memory" (memory $0)) (export "memory" (memory $0))
(start $start) (start $start)
@ -13,5 +14,12 @@
) )
) )
) )
(drop
(block (result i32)
(block $__inlined_func$namespace/Joined.anotherFunc (result i32)
(get_global $namespace/Joined.THREE)
)
)
)
) )
) )

View File

@ -2,15 +2,22 @@
(type $i (func (result i32))) (type $i (func (result i32)))
(type $v (func)) (type $v (func))
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0)) (global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
(global $namespace/Joined.THREE i32 (i32.const 3))
(memory $0 1) (memory $0 1)
(export "memory" (memory $0)) (export "memory" (memory $0))
(start $start) (start $start)
(func $namespace/Outer.Inner.aFunc (; 0 ;) (type $i) (result i32) (func $namespace/Outer.Inner.aFunc (; 0 ;) (type $i) (result i32)
(get_global $namespace/Outer.Inner.aVar) (get_global $namespace/Outer.Inner.aVar)
) )
(func $start (; 1 ;) (type $v) (func $namespace/Joined.anotherFunc (; 1 ;) (type $i) (result i32)
(get_global $namespace/Joined.THREE)
)
(func $start (; 2 ;) (type $v)
(drop (drop
(call $namespace/Outer.Inner.aFunc) (call $namespace/Outer.Inner.aFunc)
) )
(drop
(call $namespace/Joined.anotherFunc)
)
) )
) )

View File

@ -9,3 +9,12 @@ namespace Outer {
Outer.Inner.aVar; Outer.Inner.aVar;
Outer.Inner.aFunc(); Outer.Inner.aFunc();
Outer.Inner.anEnum.ONE; Outer.Inner.anEnum.ONE;
enum Joined {
THREE = 3
}
namespace Joined {
export function anotherFunc(): i32 { return Joined.THREE; }
}
Joined.anotherFunc();

View File

@ -4,6 +4,7 @@
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0)) (global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
(global $namespace/Outer.Inner.anEnum.ONE i32 (i32.const 1)) (global $namespace/Outer.Inner.anEnum.ONE i32 (i32.const 1))
(global $namespace/Outer.Inner.anEnum.TWO i32 (i32.const 2)) (global $namespace/Outer.Inner.anEnum.TWO i32 (i32.const 2))
(global $namespace/Joined.THREE i32 (i32.const 3))
(global $HEAP_BASE i32 (i32.const 4)) (global $HEAP_BASE i32 (i32.const 4))
(memory $0 1) (memory $0 1)
(export "memory" (memory $0)) (export "memory" (memory $0))
@ -13,7 +14,12 @@
(get_global $namespace/Outer.Inner.aVar) (get_global $namespace/Outer.Inner.aVar)
) )
) )
(func $start (; 1 ;) (type $v) (func $namespace/Joined.anotherFunc (; 1 ;) (type $i) (result i32)
(return
(get_global $namespace/Joined.THREE)
)
)
(func $start (; 2 ;) (type $v)
(drop (drop
(get_global $namespace/Outer.Inner.aVar) (get_global $namespace/Outer.Inner.aVar)
) )
@ -23,6 +29,9 @@
(drop (drop
(get_global $namespace/Outer.Inner.anEnum.ONE) (get_global $namespace/Outer.Inner.anEnum.ONE)
) )
(drop
(call $namespace/Joined.anotherFunc)
)
) )
) )
(; (;
@ -76,6 +85,8 @@
GLOBAL: namespace/Outer.Inner.aVar GLOBAL: namespace/Outer.Inner.aVar
FUNCTION_PROTOTYPE: namespace/Outer.Inner.aFunc FUNCTION_PROTOTYPE: namespace/Outer.Inner.aFunc
ENUM: namespace/Outer.Inner.anEnum ENUM: namespace/Outer.Inner.anEnum
ENUM: namespace/Joined
FUNCTION_PROTOTYPE: namespace/Joined.anotherFunc
[program.exports] [program.exports]
;) ;)