mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 17:01:37 +00:00
Handle static readonly members like constants
This commit is contained in:
@ -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;
|
||||
|
@ -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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user