From aee05f776cff68be7820d57be2e73e96a3c97e4c Mon Sep 17 00:00:00 2001 From: zhuoli Date: Mon, 13 Sep 2021 15:11:55 +0800 Subject: [PATCH] 1. Add globalRecord instructions 2. enable "use strict" as default Signed-off-by: zhuoli --- ts2panda/src/base/bcGenUtil.ts | 15 +++++++++++++ ts2panda/src/compiler.ts | 27 +++++++++++++++++++++++- ts2panda/src/index.ts | 1 + ts2panda/src/pandagen.ts | 21 ++++++++++++++++++ ts2panda/src/statement/classStatement.ts | 12 ++++++++--- ts2panda/src/strictMode.ts | 4 ++-- 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/ts2panda/src/base/bcGenUtil.ts b/ts2panda/src/base/bcGenUtil.ts index 4af3dc4c5e..cf7c3534be 100755 --- a/ts2panda/src/base/bcGenUtil.ts +++ b/ts2panda/src/base/bcGenUtil.ts @@ -96,6 +96,9 @@ import { MovDyn, ResultType, StaDyn, + EcmaStclasstoglobalrecord, + EcmaStconsttoglobalrecord, + EcmaStlettoglobalrecord, VReg } from "../irnodes"; @@ -412,4 +415,16 @@ export function isFalse() { export function createRegExpWithLiteral(pattern: string, flags: number) { return new EcmaCreateregexpwithliteral(pattern, new Imm(ResultType.Int, flags)); +} + +export function stLetToGlobalRecord (name: string) { + return new EcmaStlettoglobalrecord(name); +} + +export function stConstToGlobalRecord (name: string) { + return new EcmaStconsttoglobalrecord(name); +} + +export function stClassToGlobalRecord (name: string) { + return new EcmaStclasstoglobalrecord(name); } \ No newline at end of file diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index 7e6b56152f..aa859c3e8b 100755 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -1358,10 +1358,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; @@ -1394,6 +1412,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/index.ts b/ts2panda/src/index.ts index 97771062d9..f778f0a75e 100755 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -81,6 +81,7 @@ namespace Compiler { module: ts.ModuleKind.CommonJS, strictNullChecks: true, skipLibCheck: true, + alwaysStrict: true }; } } diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 807abbe611..7f638f4a17 100755 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -75,6 +75,9 @@ import { popLexicalEnv, returnUndefined, setObjectWithProto, + stClassToGlobalRecord, + stConstToGlobalRecord, + stLetToGlobalRecord, storeAccumulator, storeArraySpread, storeGlobalVar, @@ -1188,6 +1191,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 a0971a241a..1eaffd17b6 100755 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -38,7 +38,9 @@ import { PandaGen } from "../pandagen"; import { Recorder } from "../recorder"; import { FunctionScope, + GlobalScope, LocalScope, + ModuleScope, Scope, VariableScope } from "../scope"; @@ -115,9 +117,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 (!ts.isClassExpression(stmt) && (classScope.getParent() instanceof GlobalScope || classScope.getParent() 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); diff --git a/ts2panda/src/strictMode.ts b/ts2panda/src/strictMode.ts index af844ada7a..80e27a1dc7 100755 --- a/ts2panda/src/strictMode.ts +++ b/ts2panda/src/strictMode.ts @@ -16,7 +16,7 @@ import * as ts from "typescript"; import jshelpers from "./jshelpers"; -let globalStrict = false; +let globalStrict = true; export function checkStrictModeStatementList(node: ts.Node): boolean { let statements; @@ -89,4 +89,4 @@ export function isStrictMode(node: ts.Node): boolean { } return checkStrictMode(node); -} \ No newline at end of file +} -- Gitee