From f52dd697159c6f6feb25d37bb0432b97ef3ac80d Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 3 Apr 2023 19:45:57 +0800 Subject: [PATCH] Append index to exportEntry's localName Signed-off-by: hufeng Change-Id: I34ca3320a91bf6aa3a8a315d6defe63ac6667263 --- es2panda/parser/module/sourceTextModuleRecord.cpp | 12 ++++++++++++ ts2panda/src/ecmaModule.ts | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/es2panda/parser/module/sourceTextModuleRecord.cpp b/es2panda/parser/module/sourceTextModuleRecord.cpp index a6b213ed4a..a5c1f4ad63 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.cpp +++ b/es2panda/parser/module/sourceTextModuleRecord.cpp @@ -169,6 +169,18 @@ namespace panda::es2panda::parser { for (auto it = localExportEntries_.begin(); it != localExportEntries_.end(); it = localExportEntries_.upper_bound(it->first)) { + // local module variable's index is suffixed to the exportEntry's localName as format liking + // `[identifier]#index` to resolve resolvingBindings's index correctly in runtime. + std::pair localNames = + localExportEntries_.equal_range(it->first); + util::StringView updatedLocalName = + util::UString(std::string(it->first) + '#' + std::to_string(index), allocator_).View(); + for (auto localNameIter = localNames.first; localNameIter != localNames.second; ++localNameIter) { + auto *entry = localNameIter->second; + entry->localName_ = updatedLocalName; + } + CheckAndAssignIndex(moduleScope, it->first, &index); } diff --git a/ts2panda/src/ecmaModule.ts b/ts2panda/src/ecmaModule.ts index d69254de38..1be9e67a6a 100644 --- a/ts2panda/src/ecmaModule.ts +++ b/ts2panda/src/ecmaModule.ts @@ -244,6 +244,12 @@ export function assignIndexToModuleVariable(moduleScope: Scope) { let index: number = 0; // @ts-ignore moduleScope.module().getLocalExportEntries().forEach((entries: Array, localName: string) => { + // local module variable's index is suffixed to the exportEntry's localName as format liking + // `[identifier]#index` to resolve resolvingBindings's index correctly in runtime. + let updatedLocalName: string = localName + '#' + index; + entries.forEach(entry => { + entry.localName = updatedLocalName; + }); (moduleScope.findLocal(localName)!).assignIndex(index++); }); index = 0; -- Gitee