From 9eb789815b435a5e6f02b9e4c112fba8f966d41c Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 20 Jul 2022 15:34:40 +0800 Subject: [PATCH] Fix class exporting & importedVariable loading Signed-off-by: hufeng Change-Id: I324355cef4bb176e35ce1d4c4a27a9a3c0c8d520 --- ts2panda/src/scope.ts | 2 +- ts2panda/src/statement/classStatement.ts | 8 ++- ts2panda/tests/esmodule.test.ts | 78 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 ts2panda/tests/esmodule.test.ts diff --git a/ts2panda/src/scope.ts b/ts2panda/src/scope.ts index 24e7ebb468..3c8b01a1c5 100644 --- a/ts2panda/src/scope.ts +++ b/ts2panda/src/scope.ts @@ -420,7 +420,7 @@ export class ModuleScope extends VariableScope { setExportDecl(exportedLocalName: string) { let decl = this.getDecl(exportedLocalName); - if (decl) { + if (decl && decl.isModule != ModuleVarKind.IMPORTED) { decl.isModule = ModuleVarKind.EXPORTED; } } diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index ed9498afbf..4f6ba70520 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -137,8 +137,12 @@ export function compileClassDeclaration(compiler: Compiler, stmt: ts.ClassLikeDe pandaGen.stClassToGlobalRecord(stmt, className); } else { let classInfo = classScope.find(className); - (classInfo.v).initialize(); - pandaGen.storeAccToLexEnv(stmt, classInfo.scope!, classInfo.level, classInfo.v!, true); + (classInfo.v).initialize(); + if (classInfo.v instanceof ModuleVariable) { + pandaGen.storeModuleVariable(stmt, className); + } else { + pandaGen.storeAccToLexEnv(stmt, classInfo.scope!, classInfo.level, classInfo.v!, true); + } } } else { // throw SyntaxError in SyntaxChecker diff --git a/ts2panda/tests/esmodule.test.ts b/ts2panda/tests/esmodule.test.ts new file mode 100644 index 0000000000..e28b9b6db4 --- /dev/null +++ b/ts2panda/tests/esmodule.test.ts @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + expect +} from 'chai'; +import 'mocha'; +import { checkInstructions, SnippetCompiler } from "./utils/base"; +import { + EcmaDefineclasswithbuffer, + EcmaLdmodulevar, + EcmaReturnundefined, + EcmaStmodulevar, + EcmaThrowundefinedifhole, + Imm, + LdaDyn, + LdaStr, + MovDyn, + StaDyn, + VReg +} from "../src/irnodes"; +import { CmdOptions } from '../src/cmdOptions'; + + +describe("ExportDeclaration", function () { + + it("exportClassTest ", function() { + CmdOptions.isModules = () => {return true}; + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compile(`class C {}; export {C}`); + CmdOptions.isModules = () => {return false}; + let funcMainInsns = snippetCompiler.getGlobalInsns(); + let classReg = new VReg(); + let expected = [ + new MovDyn(new VReg(), new VReg()), + new EcmaDefineclasswithbuffer("#1#C", new Imm(0), new Imm(0), new VReg(), new VReg()), + new StaDyn(classReg), + new LdaDyn(classReg), + new EcmaStmodulevar('C'), + new EcmaReturnundefined(), + ]; + expect(checkInstructions(funcMainInsns, expected)).to.be.true; + }); + + it("Re-exportImportVarTest ", function() { + CmdOptions.isModules = () => {return true}; + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compile(`import a from 'test.js'; let v = a; export {a};`); + CmdOptions.isModules = () => {return false}; + let funcMainInsns = snippetCompiler.getGlobalInsns(); + let a = new VReg(); + let v = new VReg(); + let name = new VReg(); + let expected = [ + new EcmaLdmodulevar("a", new Imm(0)), + new StaDyn(a), + new LdaStr("a"), + new StaDyn(name), + new EcmaThrowundefinedifhole(a, name), + new LdaDyn(a), + new StaDyn(v), + new EcmaReturnundefined(), + ]; + expect(checkInstructions(funcMainInsns, expected)).to.be.true; + }); +}); \ No newline at end of file -- Gitee