diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index e00c3723ca0f6cd2b84e2d998f57420e9e455c4f..0609e828c5b069c5a0e2794261d1c4c800655330 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -690,6 +690,21 @@ JSHandle EcmaVM::GetModuleByName(JSHandle moduleNa auto pos = scriptName.find_last_of('.'); CString abcPath = dirPath.append(scriptName.substr(0, pos == std::string::npos ? 0 : pos)).append(".abc"); + // handle relative path + if (abcPath.find("./") == 0) { // starts with "./" + std::string fullPath = std::get<1>(pandaFileWithProgram_.back())->GetFilename(); + auto lastSlash = fullPath.find_last_of('/'); + if (lastSlash != std::string::npos) { + abcPath = fullPath.substr(0, lastSlash).append(abcPath.substr(1)); // 1: ignore "." + } + } else if (abcPath.find("../") == 0) { // starts with "../" + std::string fullPath = std::get<1>(pandaFileWithProgram_.back())->GetFilename(); + auto lastSlash = fullPath.find_last_of('/'); + if (lastSlash != std::string::npos) { + abcPath = fullPath.substr(0, lastSlash + 1).append(abcPath); // 1: with "/" + } + } + // Uniform module name JSHandle abcModuleName = factory_->NewFromString(abcPath); diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index a2fe10f7623e8fccfc405eff1ae4ff4994276ff8..0648fb4b0d194fb778d9aded4acbf778279aa334 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -25,7 +25,7 @@ group("ark_js_moduletest") { "helloworld:helloworldAction", "lexicalenv:lexicalenvAction", - # "module:moduleAction", + "module:moduleAction", "multiargs:multiargsAction", "newobjdynrange:newobjdynrangeAction", "promise:promiseAction", diff --git a/test/moduletest/module/BUILD.gn b/test/moduletest/module/BUILD.gn index fbf4dea604bd2bf0419d0fdd4efad43e1f033682..5e9c5ddd6310c7fc9a3f7980f8572f3384c0881d 100644 --- a/test/moduletest/module/BUILD.gn +++ b/test/moduletest/module/BUILD.gn @@ -15,10 +15,12 @@ import("//ark/js_runtime/test/test_helper.gni") host_moduletest_action("B") { deps = [] + is_module = true } host_moduletest_action("C") { deps = [] + is_module = true } host_moduletest_action("module") { @@ -26,4 +28,5 @@ host_moduletest_action("module") { ":gen_B_abc", ":gen_C_abc", ] + is_module = true } diff --git a/test/test_helper.gni b/test/test_helper.gni index 6fda2157a78b41637a88aa23effa233cd63fa3db..74c307151b2955e0bfac0e48b822eb78cb0cb909 100644 --- a/test/test_helper.gni +++ b/test/test_helper.gni @@ -65,6 +65,10 @@ template("host_unittest_action") { template("host_moduletest_action") { _target_name_ = "${target_name}" _deps_ = invoker.deps + _is_module_ = false + if (defined(invoker.is_module) && invoker.is_module) { + _is_module_ = true + } _test_js_path_ = "./${_target_name_}.js" _test_abc_path_ = "$target_out_dir/${_target_name_}.abc" @@ -76,6 +80,9 @@ template("host_moduletest_action") { src_js = rebase_path(_test_js_path_) dst_file = rebase_path(_test_abc_path_) extra_args = [ "--debug" ] + if (_is_module_) { + extra_args += [ "--module" ] + } in_puts = [ _test_js_path_,