Handle static readonly members like constants

This commit is contained in:
dcodeIO
2018-01-17 06:56:12 +01:00
parent 1662950f3c
commit 461daab2a2
4 changed files with 332 additions and 196 deletions

View File

@ -416,27 +416,37 @@ export class Compiler extends DiagnosticEmitter {
if (!this.module.noEmit)
this.startFunctionBody.push(setExpr);
} else {
this.module.addGlobal(internalName, nativeType, global.isMutable, initExpr);
if (!global.isMutable && !this.module.noEmit) {
var exprType = _BinaryenExpressionGetType(initExpr);
switch (exprType) {
case NativeType.I32:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0);
break;
case NativeType.I64:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr));
break;
case NativeType.F32:
global.constantFloatValue = _BinaryenConstGetValueF32(initExpr);
break;
case NativeType.F64:
global.constantFloatValue = _BinaryenConstGetValueF64(initExpr);
break;
default:
throw new Error("concrete type expected");
// TODO: not necessary to create a global if constant and not a file-level export anyway
if (!global.isMutable) {
if (!this.module.noEmit) {
var exprType = _BinaryenExpressionGetType(initExpr);
switch (exprType) {
case NativeType.I32:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0);
break;
case NativeType.I64:
global.constantIntegerValue = new I64(_BinaryenConstGetValueI64Low(initExpr), _BinaryenConstGetValueI64High(initExpr));
break;
case NativeType.F32:
global.constantFloatValue = _BinaryenConstGetValueF32(initExpr);
break;
case NativeType.F64:
global.constantFloatValue = _BinaryenConstGetValueF64(initExpr);
break;
default:
throw new Error("concrete type expected");
}
global.hasConstantValue = true;
if (!declaration || isModuleExport(global, declaration))
this.module.addGlobal(internalName, nativeType, global.isMutable, initExpr);
}
global.hasConstantValue = true;
}
} else if (!this.module.noEmit)
this.module.addGlobal(internalName, nativeType, global.isMutable, initExpr);
}
global.isCompiled = true;
return true;

View File

@ -1436,6 +1436,7 @@ export class Global extends VariableLikeElement {
case ModifierKind.EXPORT: this.isExported = true; break;
case ModifierKind.CONST: this.isConstant = true; break;
case ModifierKind.DECLARE: this.isDeclared = true; break;
case ModifierKind.READONLY: this.isConstant = true; break;
case ModifierKind.STATIC: break; // static fields become globals
default: throw new Error("unexpected modifier");
}