From b358744844a935fbfff99018db311da07a5f1509 Mon Sep 17 00:00:00 2001 From: zhuoli Date: Tue, 7 Sep 2021 22:01:21 +0800 Subject: [PATCH] Add globalRecord instructions Signed-off-by: zhuoli --- ts2panda/src/base/bcGenUtil.ts | 19 ++++++++++++++-- ts2panda/src/compiler.ts | 27 ++++++++++++++++++++++- ts2panda/src/pandagen.ts | 28 ++++++++++++++++++++---- ts2panda/src/statement/classStatement.ts | 12 +++++++--- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/ts2panda/src/base/bcGenUtil.ts b/ts2panda/src/base/bcGenUtil.ts index 70061b0907..64c6667604 100644 --- a/ts2panda/src/base/bcGenUtil.ts +++ b/ts2panda/src/base/bcGenUtil.ts @@ -29,7 +29,6 @@ import { CreateObjectHavingMethod, CreateObjectWithBuffer, CreateObjectWithExcludedKeys, - SetObjectWithProto, Debugger, DefineClassWithBuffer, DefineGetterSetterByValue, @@ -63,9 +62,13 @@ import { PopLexEnv, ResultType, ReturnUndefined, + SetObjectWithProto, StaDyn, StArraySpread, + StClassToGlobalRecord, + StConstToGlobalRecord, StGlobalVar, + StLetToGlobalRecord, StLexVar, StModuleVar, StObjByIndex, @@ -79,8 +82,8 @@ import { SuperCall, SuperCallSpread, ThrowConstAssignment, - ThrowDyn, ThrowDeleteSuperProperty, + ThrowDyn, ThrowIfNotObject, ThrowIfSuperNotCorrectCall, ThrowPatternNonCoercible, @@ -383,3 +386,15 @@ export function copyModuleIntoCurrentModule(mod: VReg) { export function loadHomeObject() { return new LdHomeObject(); } + +export function stLetToGlobalRecord (name: string) { + return new StLetToGlobalRecord(name); +} + +export function stConstToGlobalRecord (name: string) { + return new StConstToGlobalRecord(name); +} + +export function stClassToGlobalRecord (name: string) { + return new StClassToGlobalRecord(name); +} \ No newline at end of file diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index e3f64edce2..e8053ff4d6 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -1380,10 +1380,28 @@ export class Compiler { isDeclaration: boolean) { if (variable.v instanceof LocalVariable) { if (isDeclaration) { - if (variable.v.isLetOrConst()) { + if (variable.v.isLet()) { variable.v.initialize(); + if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { + this.pandaGen.stLetToGlobalRecord(node, variable.v.getName()); + return; + } + } else if (variable.v.isConst()) { + variable.v.initialize(); + if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { + this.pandaGen.stConstToGlobalRecord(node, variable.v.getName()); + return; + } + } + } + + if (variable.v.isLetOrConst()) { + if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { + this.pandaGen.tryStoreGlobalByName(node, variable.v.getName()); + return; } } + if (variable.scope && variable.level >= 0) { // inner most function will load outer env instead of new a lex env let scope = this.scope; let needSetLexVar: boolean = false; @@ -1416,6 +1434,13 @@ export class Compiler { if (variable.v instanceof ModuleVariable) { this.pandaGen.loadModuleVariable(node, variable.v.getModule(), variable.v.getExoticName()); } else if (variable.v instanceof LocalVariable) { + if (variable.v.isLetOrConst() || variable.v.isClass()) { + if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { + this.pandaGen.tryLoadGlobalByName(node, variable.v.getName()); + return; + } + } + if (variable.scope && variable.level >= 0) { // inner most function will load outer env instead of new a lex env let scope = this.scope; let needSetLexVar: boolean = false; diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 0f545cc00a..360df87b77 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -38,7 +38,6 @@ import { createObjectHavingMethod, createObjectWithBuffer, createObjectWithExcludedKeys, - setObjectWithProto, defineClassWithBuffer, defineGetterSetterByValue, deleteObjProperty, @@ -67,6 +66,10 @@ import { newObject, popLexicalEnv, returnUndefined, + setObjectWithProto, + stClassToGlobalRecord, + stConstToGlobalRecord, + stLetToGlobalRecord, storeAccumulator, storeArraySpread, storeGlobalVar, @@ -82,8 +85,8 @@ import { stSuperByValue, superCall, superCallSpread, - throwException, throwDeleteSuperProperty, + throwException, throwIfNotObject, throwIfSuperNotCorrectCall, throwObjectNonCoercible, @@ -94,20 +97,19 @@ import { tryStoreGlobalByName, tryStoreGlobalByValue } from "./base/bcGenUtil"; +import { LiteralBuffer } from "./base/literal"; import { CacheList, getVregisterCache, VregisterCache } from "./base/vregisterCache"; import { CmdOptions } from "./cmdOptions"; -import { Compiler } from "./compiler"; import { DebugInfo, NodeKind, VariableDebugInfo } from "./debuginfo"; import { isInteger } from "./expression/numericLiteral"; -import { LiteralBuffer } from "./base/literal"; import { Add2Dyn, And2Dyn, @@ -1171,6 +1173,24 @@ export class PandaGen { ) } + stLetToGlobalRecord(node: ts.Node, string_id: string) { + this.add( + node, + stLetToGlobalRecord(string_id)); + } + + stConstToGlobalRecord(node: ts.Node, string_id: string) { + this.add( + node, + stConstToGlobalRecord(string_id)); + } + + stClassToGlobalRecord(node: ts.Node, string_id: string) { + this.add( + node, + stClassToGlobalRecord(string_id)); + } + private binaryRelation(node: ts.Node, op: BinaryOperator, lhs: VReg) { let falseLabel = new Label(); let endLabel = new Label(); diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index 4850cd2641..446e98772b 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -31,7 +31,9 @@ import { PandaGen } from "../pandagen"; import { Recorder } from "../recorder"; import { FunctionScope, + GlobalScope, LocalScope, + ModuleScope, Scope, VariableScope } from "../scope"; @@ -111,9 +113,13 @@ export function compileClassDeclaration(compiler: Compiler, stmt: ts.ClassLikeDe if (stmt.name) { let className = jshelpers.getTextOfIdentifierOrLiteral(stmt.name); let classScope = compiler.getRecorder().getScopeOfNode(stmt); - let classInfo = classScope.find(className); - (classInfo.v).initialize(); - pandaGen.storeAccToLexEnv(stmt, classInfo.scope!, classInfo.level, classInfo.v!, true); + if (classScope instanceof GlobalScope || classScope instanceof ModuleScope) { + pandaGen.stClassToGlobalRecord(stmt, className); + } else { + let classInfo = classScope.find(className); + (classInfo.v).initialize(); + pandaGen.storeAccToLexEnv(stmt, classInfo.scope!, classInfo.level, classInfo.v!, true); + } } pandaGen.freeTemps(classReg, baseVreg); -- Gitee