From 03eff82c016acd2d3035945901f068b361cc8626 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 25 Oct 2021 16:03:41 +0800 Subject: [PATCH 1/5] fix module's variable storing Signed-off-by: hufeng Change-Id: I1f0114bf2c1f5393936cfd99b8f45d12dbee5f3a --- ts2panda/src/addVariable2Scope.ts | 7 ------- ts2panda/src/compiler.ts | 26 ++++++++++++++------------ ts2panda/src/compilerDriver.ts | 4 ++-- ts2panda/src/lexenv.ts | 5 ++--- ts2panda/src/modules.ts | 31 ++++++++++++++----------------- ts2panda/src/recorder.ts | 5 ++--- ts2panda/src/scope.ts | 10 ---------- ts2panda/src/variable.ts | 31 ------------------------------- 8 files changed, 34 insertions(+), 85 deletions(-) diff --git a/ts2panda/src/addVariable2Scope.ts b/ts2panda/src/addVariable2Scope.ts index 8ecc7a1d3f..c9f59db164 100644 --- a/ts2panda/src/addVariable2Scope.ts +++ b/ts2panda/src/addVariable2Scope.ts @@ -25,8 +25,6 @@ import { FuncDecl, InitStatus, LetDecl, - ModDecl, - ModuleScope, Scope, VarDecl, VariableScope @@ -109,11 +107,6 @@ export function addVariableToScope(recorder: Recorder) { scope.add(decl.name, VarDeclarationKind.FUNCTION); } else if (decl instanceof CatchParameter) { scope.add(decl.name, VarDeclarationKind.LET); - } else if (decl instanceof ModDecl) { - if (!(scope instanceof ModuleScope)) { - throw new Error("ModuleVariable can't exist without ModuleScope"); - } - scope.add(decl.name, VarDeclarationKind.MODULE); } else if (decl instanceof ClassDecl) { let classNode = decl.node; if (ts.isClassDeclaration(classNode)) { diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index b87ad4cc6a..fd8b5f59ce 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -109,7 +109,6 @@ import { isAssignmentOperator } from "./syntaxCheckHelper"; import { GlobalVariable, LocalVariable, - ModuleVariable, VarDeclarationKind, Variable } from "./variable"; @@ -1356,16 +1355,18 @@ export class Compiler { isDeclaration: boolean) { if (variable.v instanceof LocalVariable) { if (isDeclaration) { - if (variable.v.isLet()) { + if (variable.v.isLetOrConst()) { 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()); + if (variable.v.isLet()) { + this.pandaGen.stLetToGlobalRecord(node, variable.v.getName()); + } else { + this.pandaGen.stConstToGlobalRecord(node, variable.v.getName()); + } + + if (variable.v.isExportVar()) { + this.pandaGen.storeModuleVar(node, variable.v.getExportedName()); + } return; } } @@ -1374,6 +1375,9 @@ export class Compiler { if (variable.v.isLetOrConst()) { if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { this.pandaGen.tryStoreGlobalByName(node, variable.v.getName()); + if (variable.v.isExportVar()) { + this.pandaGen.storeModuleVar(node, variable.v.getExportedName()); + } return; } } @@ -1407,9 +1411,7 @@ export class Compiler { } loadTarget(node: ts.Node, variable: { scope: Scope | undefined, level: number, v: Variable | undefined }) { - if (variable.v instanceof ModuleVariable) { - this.pandaGen.loadModuleVariable(node, variable.v.getModule(), variable.v.getExoticName()); - } else if (variable.v instanceof LocalVariable) { + 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()); diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index e8fa59c596..46a1533d01 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -206,7 +206,7 @@ export class CompilerDriver { let compiler = new Compiler(node, pandaGen, this, recorder); if (CmdOptions.isModules() && ts.isSourceFile(node) && scope instanceof ModuleScope) { - setImport(recorder.getImportStmts(), scope, pandaGen, compiler); + setImport(recorder.getImportStmts(), scope, pandaGen); setExportBinding(recorder.getExportStmts(), scope, pandaGen); } @@ -249,7 +249,7 @@ export class CompilerDriver { let compiler = new Compiler(node, pandaGen, this, recorder); if (CmdOptions.isModules() && ts.isSourceFile(node) && scope instanceof ModuleScope) { - setImport(recorder.getImportStmts(), scope, pandaGen, compiler); + setImport(recorder.getImportStmts(), scope, pandaGen); setExportBinding(recorder.getExportStmts(), scope, pandaGen); } diff --git a/ts2panda/src/lexenv.ts b/ts2panda/src/lexenv.ts index 3193042838..eec0a29046 100644 --- a/ts2panda/src/lexenv.ts +++ b/ts2panda/src/lexenv.ts @@ -35,7 +35,6 @@ import { PandaGen } from "./pandagen"; import { Scope } from "./scope"; import { LocalVariable, - ModuleVariable, Variable } from "./variable"; import jshelpers from "./jshelpers"; @@ -161,7 +160,7 @@ export class VariableAcessStore extends VariableAccessBase { insns.push(storeAccumulator(bindVreg)); - if (v.isExportVar() && !(v instanceof ModuleVariable)) { + if (v.isExportVar()) { insns.push(storeModuleVariable(v.getExportedName())); } @@ -197,7 +196,7 @@ export class VariableAcessStore extends VariableAccessBase { insns.push(storeLexicalVar(this.level, slot, valueReg)); insns.push(loadAccumulator(valueReg)); - if (v.isExportVar() && !(v instanceof ModuleVariable)) { + if (v.isExportVar()) { insns.push(storeModuleVariable(v.getExportedName())); } pandaGen.freeTemps(valueReg); diff --git a/ts2panda/src/modules.ts b/ts2panda/src/modules.ts index 705cba7b64..ef8c49b641 100644 --- a/ts2panda/src/modules.ts +++ b/ts2panda/src/modules.ts @@ -16,10 +16,9 @@ import * as ts from "typescript"; import { PandaGen } from "./pandagen"; import jshelpers from "./jshelpers"; -import { LocalVariable, ModuleVariable } from "./variable"; +import { LocalVariable } from "./variable"; import { DiagnosticCode, DiagnosticError } from "./diagnostic"; import { ModuleScope } from "./scope"; -import { Compiler } from "./compiler"; export class ModuleStmt { private node: ts.Node @@ -69,23 +68,26 @@ export class ModuleStmt { } } -export function setImport(importStmts: Array, moduleScope: ModuleScope, pandagen: PandaGen, compiler: Compiler) { +export function setImport(importStmts: Array, moduleScope: ModuleScope, pandagen: PandaGen) { importStmts.forEach((importStmt) => { pandagen.importModule(importStmt.getNode(), importStmt.getModuleRequest()); - let moduleReg = pandagen.allocLocalVreg(); - pandagen.storeAccumulator(importStmt.getNode(), moduleReg); - + // import * as xxx from "a.js" if (importStmt.getNameSpace()) { let v = moduleScope.findLocal(importStmt.getNameSpace())!; - pandagen.storeAccToLexEnv(importStmt.getNode(), moduleScope, 0, v, true); + pandagen.stConstToGlobalRecord(importStmt.getNode(), v.getName()); (v).initialize(); } + // import { ... } from "a.js" + // import defaultExport, * as a from "a.js" + let moduleReg = pandagen.allocLocalVreg(); + pandagen.storeAccumulator(importStmt.getNode(), moduleReg); + let bindingNameMap = importStmt.getBindingNameMap(); bindingNameMap.forEach((value: string, key: string) => { - let v = moduleScope.findLocal(key)!; - v.bindModuleVreg(moduleReg); - v.setExoticName(value); + let v = moduleScope.findLocal(key)!; + pandagen.loadObjProperty(importStmt.getNode(), moduleReg, value); + pandagen.stConstToGlobalRecord(importStmt.getNode(), v.getName()); }); }) } @@ -118,13 +120,8 @@ export function setExportBinding(exportStmts: Array, moduleScope: Mo throw new DiagnosticError(exportStmt.getNode(), DiagnosticCode.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, jshelpers.getSourceFileOfNode(exportStmt.getNode()), [value]); } - if (v instanceof ModuleVariable) { - pandagen.loadModuleVariable(exportStmt.getNode(), v.getModule(), v.getName()); - pandagen.storeModuleVar(exportStmt.getNode(), key); - } else { - (v).setExport(); - (v).setExportedName(key); - } + (v).setExport(); + (v).setExportedName(key); }); } }) diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index d74afb9d1c..387ddf83d1 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -35,7 +35,6 @@ import { LetDecl, LocalScope, LoopScope, - ModDecl, ModuleScope, Scope, VarDecl, @@ -292,7 +291,7 @@ export class Recorder { // import defaultExport from "a.js" if (importClause.name) { let name = jshelpers.getTextOfIdentifierOrLiteral(importClause.name); - scope.setDecls(new ModDecl(name, importClause.name)); + scope.setDecls(new ConstDecl(name, importClause.name)); importStmt.addLocalName(name, "default"); } @@ -313,7 +312,7 @@ export class Recorder { namedBindings.elements.forEach((element) => { let name: string = jshelpers.getTextOfIdentifierOrLiteral(element.name); let exoticName: string = element.propertyName ? jshelpers.getTextOfIdentifierOrLiteral(element.propertyName) : name; - scope.setDecls(new ModDecl(name, element)); + scope.setDecls(new ConstDecl(name, element)); importStmt.addLocalName(name, exoticName); }); } diff --git a/ts2panda/src/scope.ts b/ts2panda/src/scope.ts index e2aa29a062..3885a0da56 100644 --- a/ts2panda/src/scope.ts +++ b/ts2panda/src/scope.ts @@ -19,7 +19,6 @@ import { LOGD, LOGE } from "./log"; import { GlobalVariable, LocalVariable, - ModuleVariable, VarDeclarationKind, Variable } from "./variable"; @@ -55,12 +54,6 @@ export class ConstDecl extends Decl { } } -export class ModDecl extends Decl { - constructor(localName: string, node: ts.Node) { - super(localName, node); - } -} - export class FuncDecl extends Decl { readonly index: number; constructor(funcName: string, node: ts.Node, index: number) { @@ -393,9 +386,6 @@ export class ModuleScope extends VariableScope { } else if (declKind == VarDeclarationKind.VAR || declKind == VarDeclarationKind.FUNCTION) { v = new LocalVariable(declKind, name); this.locals.push(v); - } else if (declKind == VarDeclarationKind.MODULE) { - v = new ModuleVariable(VarDeclarationKind.CONST, name, InitStatus.INITIALIZED); - this.locals.push(v); } else { v = new LocalVariable(declKind, name, status); this.locals.push(v); diff --git a/ts2panda/src/variable.ts b/ts2panda/src/variable.ts index 966cb9191b..eba622a9ea 100644 --- a/ts2panda/src/variable.ts +++ b/ts2panda/src/variable.ts @@ -140,37 +140,6 @@ export class LocalVariable extends Variable { } } -export class ModuleVariable extends LocalVariable { - private module: VReg | undefined; - private exoticName: string = ""; - - constructor(declKind: VarDeclarationKind, name: string, status: InitStatus) { - super(declKind, name, status); - } - - bindModuleVreg(vreg: VReg) { - this.module = vreg; - } - - setExoticName(exoticName: string) { - this.exoticName = exoticName; - } - - getExoticName() { - if (this.exoticName == "") { - throw new Error("Variable doesn't have exotic name"); - } - return this.exoticName; - } - - getModule() { - if (!this.module) { - throw new Error("Variable's module has not been binded"); - } - return this.module; - } -} - export class GlobalVariable extends Variable { constructor(declKind: VarDeclarationKind, name: string) { super(declKind, name); -- Gitee From 93f26e11e25aae8fb9bdc52f2e72172a5133a591 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 1 Nov 2021 17:00:21 +0800 Subject: [PATCH 2/5] fix module lexical scope Signed-off-by: hufeng Change-Id: I029ef69bb3b3dbb2b9d7a543bc78c1e46a97a2f8 --- ts2panda/src/compiler.ts | 35 ++++++++++++----------------------- ts2panda/src/modules.ts | 3 ++- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index fd8b5f59ce..ccedc2e43c 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -1354,32 +1354,21 @@ export class Compiler { variable: { scope: Scope | undefined, level: number, v: Variable | undefined }, isDeclaration: boolean) { if (variable.v instanceof LocalVariable) { - if (isDeclaration) { - if (variable.v.isLetOrConst()) { - variable.v.initialize(); - if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { - if (variable.v.isLet()) { - this.pandaGen.stLetToGlobalRecord(node, variable.v.getName()); - } else { - this.pandaGen.stConstToGlobalRecord(node, variable.v.getName()); - } - - if (variable.v.isExportVar()) { - this.pandaGen.storeModuleVar(node, variable.v.getExportedName()); - } - return; + if (isDeclaration && variable.v.isLetOrConst()) { + variable.v.initialize(); + if (variable.scope instanceof GlobalScope) { + if (variable.v.isLet()) { + this.pandaGen.stLetToGlobalRecord(node, variable.v.getName()); + } else { + 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()); - if (variable.v.isExportVar()) { - this.pandaGen.storeModuleVar(node, variable.v.getExportedName()); - } - return; - } + if (variable.v.isLetOrConst() && variable.scope instanceof GlobalScope) { + 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 @@ -1413,7 +1402,7 @@ export class Compiler { loadTarget(node: ts.Node, variable: { scope: Scope | undefined, level: number, v: Variable | undefined }) { if (variable.v instanceof LocalVariable) { if (variable.v.isLetOrConst() || variable.v.isClass()) { - if (variable.scope instanceof GlobalScope || variable.scope instanceof ModuleScope) { + if (variable.scope instanceof GlobalScope) { this.pandaGen.tryLoadGlobalByName(node, variable.v.getName()); return; } diff --git a/ts2panda/src/modules.ts b/ts2panda/src/modules.ts index ef8c49b641..7e428d523c 100644 --- a/ts2panda/src/modules.ts +++ b/ts2panda/src/modules.ts @@ -87,7 +87,8 @@ export function setImport(importStmts: Array, moduleScope: ModuleSco bindingNameMap.forEach((value: string, key: string) => { let v = moduleScope.findLocal(key)!; pandagen.loadObjProperty(importStmt.getNode(), moduleReg, value); - pandagen.stConstToGlobalRecord(importStmt.getNode(), v.getName()); + pandagen.storeAccToLexEnv(importStmt.getNode(), moduleScope, 0, v, true); + (v).initialize(); }); }) } -- Gitee From 2054ed148220c70c0fa870fdb93608b79d9bfdf0 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 1 Nov 2021 17:05:19 +0800 Subject: [PATCH 3/5] patch_3 Signed-off-by: hufeng Change-Id: I99b2a89d5354dba23181cd1ec44eb65a098078ca --- ts2panda/src/modules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts2panda/src/modules.ts b/ts2panda/src/modules.ts index 7e428d523c..533725e41c 100644 --- a/ts2panda/src/modules.ts +++ b/ts2panda/src/modules.ts @@ -74,7 +74,7 @@ export function setImport(importStmts: Array, moduleScope: ModuleSco // import * as xxx from "a.js" if (importStmt.getNameSpace()) { let v = moduleScope.findLocal(importStmt.getNameSpace())!; - pandagen.stConstToGlobalRecord(importStmt.getNode(), v.getName()); + pandagen.storeAccToLexEnv(importStmt.getNode(), moduleScope, 0, v, true); (v).initialize(); } -- Gitee From 628346612d69a1ed967a413f86efdccb10eb4f99 Mon Sep 17 00:00:00 2001 From: hufeng Date: Tue, 30 Nov 2021 19:45:02 +0800 Subject: [PATCH 4/5] patch_4 Signed-off-by: hufeng Change-Id: If8f23754dcaa6865afc02015ffedb3b3b1cf06b3 --- ts2panda/src/statement/classStatement.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index 1163c9e0b8..021eb2631c 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -40,7 +40,6 @@ import { FunctionScope, GlobalScope, LocalScope, - ModuleScope, Scope, VariableScope } from "../scope"; @@ -117,7 +116,7 @@ export function compileClassDeclaration(compiler: Compiler, stmt: ts.ClassLikeDe if (stmt.name) { let className = jshelpers.getTextOfIdentifierOrLiteral(stmt.name); let classScope = compiler.getRecorder().getScopeOfNode(stmt); - if (!ts.isClassExpression(stmt) && (classScope.getParent() instanceof GlobalScope || classScope.getParent() instanceof ModuleScope)) { + if (!ts.isClassExpression(stmt) && classScope.getParent() instanceof GlobalScope) { pandaGen.stClassToGlobalRecord(stmt, className); } else { let classInfo = classScope.find(className); -- Gitee From 69441ba54462a87674f77d02467770de7c3a2f6b Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 1 Dec 2021 15:23:45 +0800 Subject: [PATCH 5/5] patch_5 Signed-off-by: hufeng Change-Id: I99989b96b864632d63e5760dd945c470e1c1028c --- ts2panda/src/modules.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ts2panda/src/modules.ts b/ts2panda/src/modules.ts index 533725e41c..84f1ab1ec3 100644 --- a/ts2panda/src/modules.ts +++ b/ts2panda/src/modules.ts @@ -86,7 +86,7 @@ export function setImport(importStmts: Array, moduleScope: ModuleSco let bindingNameMap = importStmt.getBindingNameMap(); bindingNameMap.forEach((value: string, key: string) => { let v = moduleScope.findLocal(key)!; - pandagen.loadObjProperty(importStmt.getNode(), moduleReg, value); + pandagen.loadModuleVariable(importStmt.getNode(), moduleReg, value); pandagen.storeAccToLexEnv(importStmt.getNode(), moduleScope, 0, v, true); (v).initialize(); }); @@ -126,4 +126,4 @@ export function setExportBinding(exportStmts: Array, moduleScope: Mo }); } }) -} \ No newline at end of file +} -- Gitee