From ecb8c0a77eaec0d762421905fe0ac21ce60cd49d Mon Sep 17 00:00:00 2001 From: wuxiesaber Date: Tue, 22 Apr 2025 21:07:01 +0800 Subject: [PATCH 1/2] refactor of ldexternalmodulevar IR path Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICQD57 Signed-off-by: wuxiesaber Change-Id: I49e5bb95b548d9dbe80ce9605d4b72b9ee313d8b --- ecmascript/compiler/interpreter_stub.cpp | 4 +- ecmascript/compiler/stub_builder-inl.h | 35 +- ecmascript/compiler/stub_builder.cpp | 429 ++++++++++++++------ ecmascript/compiler/stub_builder.h | 32 +- ecmascript/message_string.h | 2 - ecmascript/module/module_value_accessor.cpp | 4 +- ecmascript/stubs/runtime_stub_list.h | 9 +- ecmascript/stubs/runtime_stubs-inl.h | 2 +- ecmascript/stubs/runtime_stubs.cpp | 123 +++--- 9 files changed, 423 insertions(+), 217 deletions(-) diff --git a/ecmascript/compiler/interpreter_stub.cpp b/ecmascript/compiler/interpreter_stub.cpp index b2d6077c34..fdaea9c153 100644 --- a/ecmascript/compiler/interpreter_stub.cpp +++ b/ecmascript/compiler/interpreter_stub.cpp @@ -4202,7 +4202,7 @@ DECLARE_ASM_HANDLER(HandleLdexternalmodulevarImm8) SetCurrentGlobalEnv(globalEnv); #if ENABLE_NEXT_OPTIMIZATION GateRef indexInt32 = ZExtInt8ToInt32(index); - varAcc = LoadExternalmodulevar(glue, indexInt32, module); + varAcc = LoadExternalModuleVar(glue, indexInt32, module); #else varAcc = CallRuntimeWithGlobalEnv(glue, globalEnv, RTSTUB_ID(LdExternalModuleVarByIndexWithModule), {Int8ToTaggedInt(index), module}); @@ -4222,7 +4222,7 @@ DECLARE_ASM_HANDLER(HandleWideLdexternalmodulevarPrefImm16) SetCurrentGlobalEnv(globalEnv); #if ENABLE_NEXT_OPTIMIZATION GateRef indexInt32 = ZExtInt16ToInt32(index); - varAcc = LoadExternalmodulevar(glue, indexInt32, module); + varAcc = LoadExternalModuleVar(glue, indexInt32, module); #else varAcc = CallRuntimeWithGlobalEnv(glue, globalEnv, RTSTUB_ID(LdExternalModuleVarByIndexWithModule), {Int16ToTaggedInt(index), module}); diff --git a/ecmascript/compiler/stub_builder-inl.h b/ecmascript/compiler/stub_builder-inl.h index c04d8e028b..547bb79468 100644 --- a/ecmascript/compiler/stub_builder-inl.h +++ b/ecmascript/compiler/stub_builder-inl.h @@ -4432,12 +4432,6 @@ inline GateRef StubBuilder::GetLengthOfJSArray(GateRef array) return env_->GetBuilder()->GetLengthOfJSArray(array); } -inline void StubBuilder::ResolvedModuleMustBeSourceTextModule([[maybe_unused]] GateRef glue, - [[maybe_unused]] GateRef resolvedModule) -{ - ASM_ASSERT(GET_MESSAGE_STRING_ID(CurrentModuleNotSourceTextModule), IsSourceTextModule(glue, resolvedModule)); -} - inline void StubBuilder::ModuleEnvMustBeDefined([[maybe_unused]] GateRef curEnv) { ASM_ASSERT(GET_MESSAGE_STRING_ID(ModuleEnvMustBeDefined), BoolNot(TaggedIsUndefined(curEnv))); @@ -4448,11 +4442,6 @@ inline void StubBuilder::CheckIsResolvedIndexBinding([[maybe_unused]] GateRef gl ASM_ASSERT(GET_MESSAGE_STRING_ID(CheckIsResolvedIndexBinding), IsResolvedIndexBinding(glue, resolution)); } -inline void StubBuilder::RecordNameMustBeString([[maybe_unused]] GateRef glue, [[maybe_unused]] GateRef recordName) -{ - ASM_ASSERT(GET_MESSAGE_STRING_ID(RecordNameMustBeString), TaggedIsString(glue, recordName)); -} - inline GateRef StubBuilder::GetNameDictionary(GateRef glue, GateRef module) { GateRef nameDictionaryOffset = IntPtr(SourceTextModule::NAME_DICTIONARY_OFFSET); @@ -4496,7 +4485,7 @@ inline GateRef StubBuilder::GetResolveModuleFromResolvedBinding(GateRef glue, Ga return Load(VariableType::JS_ANY(), glue, resolvedBinding, moduleOffset); } -inline GateRef StubBuilder::GetIdxOfResolvedIndexBinding(GateRef resolvedBinding) +inline GateRef StubBuilder::GetIdxOfIndexBinding(GateRef resolvedBinding) { GateRef indexOffset = IntPtr(ResolvedIndexBinding::INDEX_OFFSET); return LoadPrimitive(VariableType::INT32(), resolvedBinding, indexOffset); @@ -4515,19 +4504,19 @@ inline void StubBuilder::SetIsUpdatedFromResolvedBindingOfResolvedIndexBinding(G Store(VariableType::INT32(), glue, resolvedBinding, bitFieldOffset, newVal); } -inline GateRef StubBuilder::GetIdxOfResolvedRecordIndexBinding(GateRef resolvedBinding) +inline GateRef StubBuilder::GetIdxOfRecordIndexBinding(GateRef resolvedBinding) { GateRef indexOffset = IntPtr(ResolvedRecordIndexBinding::INDEX_OFFSET); return LoadPrimitive(VariableType::INT32(), resolvedBinding, indexOffset); } -inline GateRef StubBuilder::GetModuleRecord(GateRef glue, GateRef resolvedBinding) +inline GateRef StubBuilder::GetBindingNameFromResolvedBinding(GateRef glue, GateRef resolvedBinding) { - GateRef moduleRecordIdxOffset = IntPtr(ResolvedRecordIndexBinding::MODULE_RECORD_INDEX_OFFSET); - return Load(VariableType::JS_ANY(), glue, resolvedBinding, moduleRecordIdxOffset); + GateRef bindingNameOffset = IntPtr(ResolvedBinding::BINDING_NAME_OFFSET); + return Load(VariableType::JS_ANY(), glue, resolvedBinding, bindingNameOffset); } -inline GateRef StubBuilder::GetBindingName(GateRef glue, GateRef resolvedBinding) +inline GateRef StubBuilder::GetBindingNameFromRecordBinding(GateRef glue, GateRef resolvedBinding) { GateRef bindingNameOffset = IntPtr(ResolvedRecordBinding::BINDING_NAME_OFFSET); return Load(VariableType::JS_ANY(), glue, resolvedBinding, bindingNameOffset); @@ -4570,9 +4559,14 @@ inline GateRef StubBuilder::GetModuleType(GateRef module) Int32((1LU << SourceTextModule::MODULE_TYPE_BITS) - 1)); } -inline GateRef StubBuilder::IsNativeModule(GateRef module) +inline GateRef StubBuilder::GetModuleStatus(GateRef module) +{ + GateRef bitfield = GetBitFieldFromSourceTextModule(module); + return Int32And(bitfield, Int32((1LU << SourceTextModule::STATUS_BITS) - 1)); +} + +inline GateRef StubBuilder::IsNativeModule(GateRef moduleType) { - GateRef moduleType = GetModuleType(module); return LogicOrBuilder(env_) .Or(Int32Equal(moduleType, Int32(static_cast(ModuleTypes::OHOS_MODULE)))) .Or(Int32Equal(moduleType, Int32(static_cast(ModuleTypes::APP_MODULE)))) @@ -4581,9 +4575,8 @@ inline GateRef StubBuilder::IsNativeModule(GateRef module) .Done(); } -inline GateRef StubBuilder::IsCjsModule(GateRef module) +inline GateRef StubBuilder::IsCjsModule(GateRef moduleType) { - GateRef moduleType = GetModuleType(module); return Int32Equal(moduleType, Int32(static_cast(ModuleTypes::CJS_MODULE))); } diff --git a/ecmascript/compiler/stub_builder.cpp b/ecmascript/compiler/stub_builder.cpp index 994052c760..ad0d5dc7fe 100644 --- a/ecmascript/compiler/stub_builder.cpp +++ b/ecmascript/compiler/stub_builder.cpp @@ -11864,7 +11864,6 @@ GateRef StubBuilder::SearchFromModuleCache(GateRef glue, GateRef moduleName) result = FastGetPropertyByName(glue, cachedModule, exportsName, ProfileOperation()); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -11881,7 +11880,8 @@ GateRef StubBuilder::GetNativeOrCjsExports(GateRef glue, GateRef resolvedModule) Label isNativeModule(env); Label judgeCjsModule(env); Label exit(env); - BRANCH(IsNativeModule(resolvedModule), &isNativeModule, &judgeCjsModule); + GateRef moduleType = GetModuleType(resolvedModule); + BRANCH(IsNativeModule(moduleType), &isNativeModule, &judgeCjsModule); Bind(&isNativeModule); { @@ -11895,11 +11895,10 @@ GateRef StubBuilder::GetNativeOrCjsExports(GateRef glue, GateRef resolvedModule) Jump(&exit); } } - Bind(&judgeCjsModule); { Label isCjsModule(env); - BRANCH(IsCjsModule(resolvedModule), &isCjsModule, &exit); + BRANCH(IsCjsModule(moduleType), &isCjsModule, &exit); Bind(&isCjsModule); Label exportsIsHole(env); @@ -11913,7 +11912,6 @@ GateRef StubBuilder::GetNativeOrCjsExports(GateRef glue, GateRef resolvedModule) Jump(&exit); } } - Bind(&exit); auto ret = *exports; env->SubCfgExit(); @@ -11938,7 +11936,6 @@ GateRef StubBuilder::GetValueFromExportObject(GateRef glue, GateRef exports, Gat result = exports; Jump(&exit); } - Bind(&validIdx); { Label isDictionaryMode(env); @@ -11951,7 +11948,6 @@ GateRef StubBuilder::GetValueFromExportObject(GateRef glue, GateRef exports, Gat result = GetValueFromDictionary(glue, properties, index); Jump(&checkResultIsAccessor); } - Bind(¬DictionaryMode); { GateRef hClass = LoadHClass(glue, exports); @@ -11961,7 +11957,6 @@ GateRef StubBuilder::GetValueFromExportObject(GateRef glue, GateRef exports, Gat Jump(&checkResultIsAccessor); } } - Bind(&checkResultIsAccessor); { Label isAccessor(env); @@ -11973,7 +11968,6 @@ GateRef StubBuilder::GetValueFromExportObject(GateRef glue, GateRef exports, Gat Jump(&exit); } } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -11998,13 +11992,11 @@ GateRef StubBuilder::GetNativeOrCjsModuleValue(GateRef glue, GateRef module, Gat result = Exception(); Jump(&exit); } - Bind(&noException); { result = GetValueFromExportObject(glue, exports, index); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12029,13 +12021,11 @@ GateRef StubBuilder::GetModuleValueByIndex(GateRef glue, GateRef module, GateRef result = GetValueFromTaggedArray(glue, dictionary, index); Jump(&exit); } - Bind(&isUndefined); { result = CallRuntime(glue, RTSTUB_ID(CheckAndThrowModuleError), {module, isThrow}); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12052,7 +12042,8 @@ GateRef StubBuilder::GetModuleValue(GateRef glue, GateRef module, GateRef index) Label isNativeOrCjsModule(env); Label notNativeOrCjsModule(env); Label exit(env); - GateRef checkNativeOrCjsModule = BitOr(IsNativeModule(module), IsCjsModule(module)); + GateRef moduleType = GetModuleType(module); + GateRef checkNativeOrCjsModule = BitOr(IsNativeModule(moduleType), IsCjsModule(moduleType)); BRANCH(checkNativeOrCjsModule, &isNativeOrCjsModule, ¬NativeOrCjsModule); Bind(&isNativeOrCjsModule); @@ -12060,13 +12051,11 @@ GateRef StubBuilder::GetModuleValue(GateRef glue, GateRef module, GateRef index) result = GetNativeOrCjsModuleValue(glue, module, index); Jump(&exit); } - Bind(¬NativeOrCjsModule); { result = GetModuleValueByIndex(glue, module, index, TaggedFalse()); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12091,7 +12080,6 @@ GateRef StubBuilder::GetNativeOrCjsModuleValueByName(GateRef glue, GateRef modul result = Exception(); Jump(&exit); } - Bind(&noPendingException); { GateRef defaultString = GetGlobalConstantValue(VariableType::JS_POINTER(), glue, ConstantIndex::DEFAULT_INDEX); @@ -12104,14 +12092,12 @@ GateRef StubBuilder::GetNativeOrCjsModuleValueByName(GateRef glue, GateRef modul result = exports; Jump(&exit); } - Bind(¬SameVal); { result = FastGetPropertyByName(glue, exports, bindingName, ProfileOperation()); Jump(&exit); } } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12138,13 +12124,11 @@ GateRef StubBuilder::ResolveElementOfObject(GateRef glue, GateRef hClass, GateRe result = CallRuntime(glue, RTSTUB_ID(NewResolvedIndexBindingRecord), {module, IntToTaggedInt(idx)}); Jump(&exit); } - Bind(&invalidIdx); { result = GetGlobalConstantValue(VariableType::JS_ANY(), glue, ConstantIndex::UNDEFINED_INDEX); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12195,7 +12179,6 @@ GateRef StubBuilder::ResolveExportObject(GateRef glue, GateRef module, GateRef e result = CallRuntime(glue, RTSTUB_ID(NewResolvedIndexBindingRecord), {module, IntToTaggedInt(entry)}); Jump(&checkResultIsUndefined); } - Bind(¬DictionaryMode); { Label notEqualZero(env); @@ -12208,23 +12191,19 @@ GateRef StubBuilder::ResolveExportObject(GateRef glue, GateRef module, GateRef e result = ResolveElementOfObject(glue, hClass, exportName, module, layoutInfo); Jump(&checkResultIsUndefined); } - Bind(&checkResultIsUndefined); BRANCH(TaggedIsUndefined(*result), &exportsNotJSObject, &exit); } - Bind(&defaultCallNewBindingRecord); { result = CallRuntime(glue, RTSTUB_ID(NewResolvedIndexBindingRecord), {module, IntToTaggedInt(Int32(-1))}); Jump(&exit); } - Bind(&exportsNotJSObject); { result = GetGlobalConstantValue(VariableType::JS_ANY(), glue, ConstantIndex::NULL_INDEX); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12260,73 +12239,305 @@ GateRef StubBuilder::UpdateBindingAndGetModuleValue(GateRef glue, GateRef module {module, requiredModule, bindingName, resolution}); Jump(&exit); } - Bind(¬NullOrString); { CheckIsResolvedIndexBinding(glue, resolution); SetIsUpdatedFromResolvedBindingOfResolvedIndexBinding(glue, resolution, True()); SetValueToTaggedArray(VariableType::JS_ANY(), glue, curModuleEnv, index, resolution); - result = GetValueFromExportObject(glue, exports, GetIdxOfResolvedIndexBinding(resolution)); + result = GetValueFromExportObject(glue, exports, GetIdxOfIndexBinding(resolution)); Jump(&exit); } } - Bind(&hasException); { result = Exception(); Jump(&exit); } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} + +GateRef StubBuilder::EvaluateNativeModule(GateRef glue, GateRef module, GateRef moduleType) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + DEFVARIABLE(result, VariableType::JS_ANY(), TaggedTrue()); + Label notEvaluated(env); + Label exit(env); + GateRef moduleStatus = GetModuleStatus(module); + GateRef evaluatedStatus = Int32(static_cast(ModuleStatus::EVALUATED)); + BRANCH(Int32Equal(moduleStatus, evaluatedStatus), &exit, ¬Evaluated); + + Bind(¬Evaluated); + { + result = CallRuntime(glue, RTSTUB_ID(LoadNativeModule), {module, IntToTaggedInt(moduleType)}); + Jump(&exit); + } Bind(&exit); auto ret = *result; env->SubCfgExit(); return ret; } -GateRef StubBuilder::GetResolvedRecordIndexBindingModule(GateRef glue, GateRef module, GateRef resolvedBinding) +void StubBuilder::EvaluateModuleIfNeeded(GateRef glue, GateRef module, bool isLazy) { - GateRef recordName = GetModuleRecord(glue, resolvedBinding); - RecordNameMustBeString(glue, recordName); - GateRef moduleManager = GetModuleManager(glue); - GateRef result = CallRuntime(glue, RTSTUB_ID(GetResolvedRecordIndexBindingModule), - {module, resolvedBinding, moduleManager, recordName}); - return result; + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + Label exit(env); + if (isLazy) { + Label notEvaluatedOrErrored(env); + GateRef moduleStatus = GetModuleStatus(module); + GateRef evaluatedStatus = Int32(static_cast(ModuleStatus::EVALUATED)); + BRANCH(Int32GreaterThanOrEqual(moduleStatus, evaluatedStatus), &exit, ¬EvaluatedOrErrored); + + Bind(¬EvaluatedOrErrored); + Label isNativeModule(env); + Label notNativeModule(env); + GateRef moduleType = GetModuleType(module); + BRANCH(IsNativeModule(moduleType), &isNativeModule, ¬NativeModule); + + Bind(&isNativeModule); + EvaluateNativeModule(glue, module, moduleType); + Jump(&exit); + + Bind(¬NativeModule); + CallRuntime(glue, RTSTUB_ID(EvaluateModule), {module}); + Jump(&exit); + } else { + Jump(&exit); + } + Bind(&exit); + env->SubCfgExit(); } -GateRef StubBuilder::GetResolvedRecordBindingModule(GateRef glue, GateRef module, GateRef resolvedBinding) +void StubBuilder::LogModuleLoadInfo(GateRef glue, GateRef module, GateRef requiredModule, GateRef index, + bool isSendable) { - GateRef recordName = GetModuleRecord(glue, resolvedBinding); - RecordNameMustBeString(glue, recordName); - GateRef moduleManager = GetModuleManager(glue); - GateRef result = CallRuntime(glue, RTSTUB_ID(GetResolvedRecordBindingModule), - {module, moduleManager, recordName}); - return result; + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + Label exit(env); + if (!isSendable) { + Label notNullPtr(env); + BRANCH_LIKELY(IntPtrEuqal(GetModuleLogger(glue), IntPtr(0)), &exit, ¬NullPtr); + + Bind(¬NullPtr); + CallRuntime(glue, RTSTUB_ID(InsertModuleLoadInfo), {module, requiredModule, IntToTaggedInt(index)}); + Jump(&exit); + } else { + Jump(&exit); + } + Bind(&exit); + env->SubCfgExit(); } -GateRef StubBuilder::LoadExternalmodulevar(GateRef glue, GateRef index, GateRef curModule) +GateRef StubBuilder::GetModuleValueFromIndexBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, + GateRef index, bool isSendable, bool isLazy) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); + DEFVARIABLE(resolvedModule, VariableType::JS_ANY(), Undefined()); + Label exit(env); + Label next(env); + Label getModuleVal(env); + resolvedModule = GetResolveModuleFromResolvedIndexBinding(glue, resolvedBinding); + + if (!isSendable) { + Label isLdEndExecPatchMain(env); + BRANCH(IsLdEndExecPatchMain(glue), &isLdEndExecPatchMain, &next); + + Bind(&isLdEndExecPatchMain); + Label notHole(env); + GateRef resolvedModuleOfHotReload = CallNGCRuntime(glue, RTSTUB_ID(FindPatchModule), + {glue, *resolvedModule}); + BRANCH(TaggedIsHole(resolvedModuleOfHotReload), &next, ¬Hole); + + Bind(¬Hole); + resolvedModule = resolvedModuleOfHotReload; + Jump(&next); + } else { + Jump(&next); + } + Bind(&next); + { + if (isSendable && isLazy) { + CallRuntime(glue, RTSTUB_ID(EvaluateModule), {*resolvedModule}); + Jump(&getModuleVal); + } else { + EvaluateModuleIfNeeded(glue, *resolvedModule, isLazy); + Jump(&getModuleVal); + } + } + Bind(&getModuleVal); + { + Label hasException(env); + Label noException(env); + BRANCH(HasPendingException(glue), &hasException, &noException); + + Bind(&hasException); + result = Exception(); + Jump(&exit); + + Bind(&noException); + Label isSharedModule(env); + Label notSharedModule(env); + BRANCH(IsSharedModule(*resolvedModule), &isSharedModule, ¬SharedModule); + + Bind(&isSharedModule); + resolvedModule = CallNGCRuntime(glue, RTSTUB_ID(UpdateSharedModule), {glue, *resolvedModule}); + Jump(¬SharedModule); + + Bind(¬SharedModule); + LogModuleLoadInfo(glue, curModule, *resolvedModule, index, isSendable); + GateRef idxOfResolvedBinding = GetIdxOfIndexBinding(resolvedBinding); + result = GetModuleValue(glue, *resolvedModule, idxOfResolvedBinding); + Jump(&exit); + } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} + +GateRef StubBuilder::GetModuleValueFromBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, GateRef index, + bool isSendable, bool isLazy) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); + Label isNativeOrCjsModule(env); + Label notNativeOrCjsModule(env); + Label exit(env); + GateRef resolvedModule = GetResolveModuleFromResolvedBinding(glue, resolvedBinding); + GateRef moduletype = GetModuleType(resolvedModule); + BRANCH_LIKELY(BitOr(IsNativeModule(moduletype), IsCjsModule(moduletype)), &isNativeOrCjsModule, + ¬NativeOrCjsModule); + + Bind(&isNativeOrCjsModule); + { + Label hasException(env); + Label noException(env); + EvaluateModuleIfNeeded(glue, resolvedModule, isLazy); + BRANCH(HasPendingException(glue), &hasException, &noException); + + Bind(&hasException); + result = Exception(); + Jump(&exit); + + Bind(&noException); + LogModuleLoadInfo(glue, curModule, resolvedModule, index, isSendable); + result = UpdateBindingAndGetModuleValue(glue, curModule, resolvedModule, index, + GetBindingNameFromResolvedBinding(glue, resolvedBinding)); + Jump(&exit); + } + Bind(¬NativeOrCjsModule); + { + CallNGCRuntime(glue, RTSTUB_ID(FatalPrintMisstakenResolvedBinding), {index, curModule}); + Jump(&exit); + } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} + +GateRef StubBuilder::GetModuleValueFromRecordIndexBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, + GateRef index, bool isSendable, bool isLazy) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); + Label hasException(env); + Label noException(env); + Label exit(env); + GateRef isLazyTagged = isLazy ? TaggedTrue() : TaggedFalse(); + GateRef resolvedModule = CallRuntime(glue, RTSTUB_ID(GetResolvedModuleFromRecordIndexBinding), + {curModule, resolvedBinding, isLazyTagged}); + BRANCH(HasPendingException(glue), &hasException, &noException); + + Bind(&hasException); + { + result = Exception(); + Jump(&exit); + } + Bind(&noException); + { + LogModuleLoadInfo(glue, curModule, resolvedModule, index, isSendable); + GateRef idxOfRecordIndexBinding = GetIdxOfRecordIndexBinding(resolvedBinding); + result = GetModuleValue(glue, resolvedModule, idxOfRecordIndexBinding); + Jump(&exit); + } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} + +GateRef StubBuilder::GetModuleValueFromRecordBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, + GateRef index, bool isSendable, bool isLazy) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); + Label hasException(env); + Label noException(env); + Label exit(env); + GateRef isLazyTagged = isLazy ? TaggedTrue() : TaggedFalse(); + GateRef resolvedModule = CallRuntime(glue, RTSTUB_ID(GetResolvedModuleFromRecordBinding), + {curModule, resolvedBinding, isLazyTagged}); + BRANCH(HasPendingException(glue), &hasException, &noException); + + Bind(&hasException); + { + result = Exception(); + Jump(&exit); + } + Bind(&noException); + { + LogModuleLoadInfo(glue, curModule, resolvedModule, index, isSendable); + result = GetNativeOrCjsModuleValueByName(glue, resolvedModule, + GetBindingNameFromRecordBinding(glue, resolvedBinding)); + Jump(&exit); + } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} + +GateRef StubBuilder::GetModuleValueOuterInternal(GateRef glue, GateRef index, GateRef curModule, bool isLazy) { auto env = GetEnvironment(); Label subentry(env); env->SubCfgEntry(&subentry); DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); - Label notSendableFunctionModule(env); Label moduleUndefined(env); Label moduleIsdefined(env); Label moduleEnvUndefined(env); Label moduleEnvIsdefined(env); - Label isSendableFunctionModule(env); - Label isNullPtr(env); - Label notNullPtr(env); Label resolvedBindingIsHeapObj(env); Label misstakenResolvedBinding(env); Label judgeResolvedBinding(env); Label judgeResolvedRecordIndexBinding(env); Label judgeResolvedRecordBinding(env); Label exit(env); - BRANCH(IsSendableFunctionModule(glue, curModule), &isSendableFunctionModule, ¬SendableFunctionModule); - - Bind(¬SendableFunctionModule); BRANCH_UNLIKELY(TaggedIsUndefined(curModule), &moduleUndefined, &moduleIsdefined); Bind(&moduleIsdefined); @@ -12336,9 +12547,6 @@ GateRef StubBuilder::LoadExternalmodulevar(GateRef glue, GateRef index, GateRef Bind(&moduleEnvIsdefined); ModuleEnvMustBeValid(glue, curModuleEnv); GateRef resolvedBinding = GetValueFromTaggedArray(glue, curModuleEnv, index); - BRANCH_LIKELY(IntPtrEuqal(GetModuleLogger(glue), IntPtr(0)), &isNullPtr, ¬NullPtr); - - Bind(&isNullPtr); BRANCH(TaggedIsHeapObject(resolvedBinding), &resolvedBindingIsHeapObj, &misstakenResolvedBinding); Bind(&resolvedBindingIsHeapObj); @@ -12347,60 +12555,18 @@ GateRef StubBuilder::LoadExternalmodulevar(GateRef glue, GateRef index, GateRef BRANCH(IsResolvedIndexBinding(glue, resolvedBinding), &isResolvedIndexBinding, &judgeResolvedBinding); Bind(&isResolvedIndexBinding); - { - Label isLdEndExecPatchMain(env); - Label notLdEndExecPatchMain(env); - Label notHole(env); - DEFVARIABLE(resolvedModule, VariableType::JS_ANY(), Hole()); - resolvedModule = GetResolveModuleFromResolvedIndexBinding(glue, resolvedBinding); - ResolvedModuleMustBeSourceTextModule(glue, *resolvedModule); - GateRef idxOfResolvedBinding = GetIdxOfResolvedIndexBinding(resolvedBinding); - BRANCH(IsLdEndExecPatchMain(glue), &isLdEndExecPatchMain, ¬LdEndExecPatchMain); - - Bind(&isLdEndExecPatchMain); - GateRef resolvedModuleOfHotReload = CallNGCRuntime(glue, RTSTUB_ID(FindPatchModule), - {glue, *resolvedModule}); - BRANCH(TaggedIsHole(resolvedModuleOfHotReload), ¬LdEndExecPatchMain, ¬Hole); - - Bind(¬LdEndExecPatchMain); - Label isSharedModule(env); - Label notSharedModule(env); - BRANCH(IsSharedModule(*resolvedModule), &isSharedModule, ¬SharedModule); - Bind(&isSharedModule); - resolvedModule = CallNGCRuntime(glue, RTSTUB_ID(UpdateSharedModule), {glue, *resolvedModule}); - Jump(¬SharedModule); - Bind(¬SharedModule); - result = GetModuleValue(glue, *resolvedModule, idxOfResolvedBinding); - Jump(&exit); - - Bind(¬Hole); - result = GetModuleValue(glue, resolvedModuleOfHotReload, idxOfResolvedBinding); - Jump(&exit); - } + result = GetModuleValueFromIndexBinding(glue, curModule, resolvedBinding, index, false, isLazy); + Jump(&exit); } - Bind(&judgeResolvedBinding); { Label isResolvedBinding(env); BRANCH(IsResolvedBinding(glue, resolvedBinding), &isResolvedBinding, &judgeResolvedRecordIndexBinding); Bind(&isResolvedBinding); - { - GateRef resolvedModule = GetResolveModuleFromResolvedBinding(glue, resolvedBinding); - ResolvedModuleMustBeSourceTextModule(glue, resolvedModule); - Label isNativeOrCjsModule(env); - GateRef checkNativeOrCjsModule = BitOr(IsNativeModule(resolvedModule), IsCjsModule(resolvedModule)); - BRANCH(checkNativeOrCjsModule, &isNativeOrCjsModule, &misstakenResolvedBinding); - - Bind(&isNativeOrCjsModule); - { - result = UpdateBindingAndGetModuleValue(glue, curModule, resolvedModule, index, - GetBindingName(glue, resolvedBinding)); - Jump(&exit); - } - } + result = GetModuleValueFromBinding(glue, curModule, resolvedBinding, index, false, isLazy); + Jump(&exit); } - Bind(&judgeResolvedRecordIndexBinding); { Label isResolvedRecordIndexBinding(env); @@ -12408,58 +12574,62 @@ GateRef StubBuilder::LoadExternalmodulevar(GateRef glue, GateRef index, GateRef &judgeResolvedRecordBinding); Bind(&isResolvedRecordIndexBinding); - { - GateRef resolvedModule = GetResolvedRecordIndexBindingModule(glue, curModule, resolvedBinding); - GateRef idxOfResolvedRecordIndexBinding = GetIdxOfResolvedRecordIndexBinding(resolvedBinding); - result = GetModuleValue(glue, resolvedModule, idxOfResolvedRecordIndexBinding); - Jump(&exit); - } + result = GetModuleValueFromRecordIndexBinding(glue, curModule, resolvedBinding, index, false, isLazy); + Jump(&exit); } - Bind(&judgeResolvedRecordBinding); { Label isResolvedRecordBinding(env); BRANCH(IsResolvedRecordBinding(glue, resolvedBinding), &isResolvedRecordBinding, &misstakenResolvedBinding); - Bind(&isResolvedRecordBinding); - { - GateRef resolvedModule = GetResolvedRecordBindingModule(glue, curModule, resolvedBinding); - result = GetNativeOrCjsModuleValueByName(glue, resolvedModule, GetBindingName(glue, resolvedBinding)); - Jump(&exit); - } - } - Bind(&isSendableFunctionModule); - { - result = CallRuntime(glue, RTSTUB_ID(LdExternalModuleVarByIndexWithModule), - {IntToTaggedInt(index), curModule}); + Bind(&isResolvedRecordBinding); + result = GetModuleValueFromRecordBinding(glue, curModule, resolvedBinding, index, false, isLazy); Jump(&exit); } - Bind(&moduleUndefined); { FatalPrint(glue, {Int32(GET_MESSAGE_STRING_ID(CurrentModuleUndefined))}); Jump(&exit); } - Bind(&moduleEnvUndefined); { result = Undefined(); Jump(&exit); } - - Bind(¬NullPtr); + Bind(&misstakenResolvedBinding); { - result = CallRuntime(glue, RTSTUB_ID(GetModuleValueOuterInternal), - {curModule, IntToTaggedInt(index)}); + CallNGCRuntime(glue, RTSTUB_ID(FatalPrintMisstakenResolvedBinding), {index, curModule}); Jump(&exit); } + Bind(&exit); + auto ret = *result; + env->SubCfgExit(); + return ret; +} - Bind(&misstakenResolvedBinding); +GateRef StubBuilder::LoadExternalModuleVar(GateRef glue, GateRef index, GateRef module) +{ + auto env = GetEnvironment(); + Label subentry(env); + env->SubCfgEntry(&subentry); + + DEFVARIABLE(result, VariableType::JS_ANY(), Hole()); + DEFVARIABLE(curModule, VariableType::JS_ANY(), module); + Label isSendableFunctionModule(env); + Label getModuleVal(env); + Label exit(env); + BRANCH(IsSendableFunctionModule(glue, module), &isSendableFunctionModule, &getModuleVal); + + Bind(&isSendableFunctionModule); { - CallNGCRuntime(glue, RTSTUB_ID(FatalPrintMisstakenResolvedBinding), {index, curModule}); + curModule = CallRuntime(glue, RTSTUB_ID(HostGetImportedModule), {module}); + Jump(&getModuleVal); + } + Bind(&getModuleVal); + { + result = GetModuleValueOuterInternal(glue, index, *curModule, false); Jump(&exit); } - Bind(&exit); auto ret = *result; env->SubCfgExit(); @@ -12507,7 +12677,8 @@ GateRef StubBuilder::LoadModuleNamespaceByIndex(GateRef glue, GateRef index, Gat Bind(&requiredModuleIsSourceTextModule); BRANCH_LIKELY(IntPtrEuqal(GetModuleLogger(glue), IntPtr(0)), &isNullPtr, &slowPath); Bind(&isNullPtr); - BRANCH(IsNativeModule(requiredModule), &isNativeModule, ¬NativeModule); + GateRef moduleType = GetModuleType(requiredModule); + BRANCH(IsNativeModule(moduleType), &isNativeModule, ¬NativeModule); Bind(&isNativeModule); { GateRef dictionary = GetNameDictionary(glue, requiredModule); @@ -12517,7 +12688,7 @@ GateRef StubBuilder::LoadModuleNamespaceByIndex(GateRef glue, GateRef index, Gat Jump(&exit); } Bind(¬NativeModule); - BRANCH(IsCjsModule(requiredModule), &isCjsModule, ¬CjsModule); + BRANCH(IsCjsModule(moduleType), &isCjsModule, ¬CjsModule); Bind(&isCjsModule); { // CommonJS module involves CString, jump to slowPath here diff --git a/ecmascript/compiler/stub_builder.h b/ecmascript/compiler/stub_builder.h index b142418a15..dedc7d67d7 100644 --- a/ecmascript/compiler/stub_builder.h +++ b/ecmascript/compiler/stub_builder.h @@ -1202,10 +1202,8 @@ public: // start: Fast path of Loading Module variable. GateRef Loadlocalmodulevar(GateRef glue, GateRef index, GateRef module); - inline void ResolvedModuleMustBeSourceTextModule(GateRef glue, GateRef resolvedModule); inline void ModuleEnvMustBeDefined(GateRef curEnv); inline void CheckIsResolvedIndexBinding(GateRef glue, GateRef resolution); - inline void RecordNameMustBeString(GateRef glue, GateRef recordName); inline GateRef GetNameDictionary(GateRef glue, GateRef module); inline GateRef GetCurrentModuleEnv(GateRef glue, GateRef curModule); inline GateRef GetBitFieldFromSourceTextModule(GateRef curModule); @@ -1213,20 +1211,21 @@ public: inline GateRef GetNamespaceFromSourceTextModule(GateRef glue, GateRef module); inline GateRef GetResolveModuleFromResolvedIndexBinding(GateRef glue, GateRef resolvedBinding); inline GateRef GetResolveModuleFromResolvedBinding(GateRef glue, GateRef resolvedBinding); - inline GateRef GetIdxOfResolvedIndexBinding(GateRef resolvedBinding); + inline GateRef GetIdxOfIndexBinding(GateRef resolvedBinding); inline void SetIsUpdatedFromResolvedBindingOfResolvedIndexBinding(GateRef glue, GateRef resolvedBinding, GateRef value); - inline GateRef GetIdxOfResolvedRecordIndexBinding(GateRef resolvedBinding); - inline GateRef GetModuleRecord(GateRef glue, GateRef resolvedBinding); - inline GateRef GetBindingName(GateRef glue, GateRef resolvedBinding); + inline GateRef GetIdxOfRecordIndexBinding(GateRef resolvedBinding); + inline GateRef GetBindingNameFromResolvedBinding(GateRef glue, GateRef resolvedBinding); + inline GateRef GetBindingNameFromRecordBinding(GateRef glue, GateRef resolvedBinding); inline GateRef IsResolvedIndexBinding(GateRef glue, GateRef resolvedBinding); inline GateRef IsResolvedBinding(GateRef glue, GateRef resolvedBinding); inline GateRef IsResolvedRecordIndexBinding(GateRef glue, GateRef resolvedBinding); inline GateRef IsResolvedRecordBinding(GateRef glue, GateRef resolvedBinding); inline GateRef IsLdEndExecPatchMain(GateRef glue); inline GateRef GetModuleType(GateRef module); - inline GateRef IsNativeModule(GateRef module); - inline GateRef IsCjsModule(GateRef module); + inline GateRef GetModuleStatus(GateRef module); + inline GateRef IsNativeModule(GateRef moduleType); + inline GateRef IsCjsModule(GateRef moduleType); inline GateRef GetSharedType(GateRef module); inline GateRef IsSharedModule(GateRef module); inline GateRef GetCjsModuleFunction(GateRef glue); @@ -1243,9 +1242,20 @@ public: GateRef ResolveExportObject(GateRef glue, GateRef module, GateRef exports, GateRef exportName); GateRef UpdateBindingAndGetModuleValue(GateRef glue, GateRef module, GateRef requiredModule, GateRef index, GateRef bindingName); - GateRef GetResolvedRecordIndexBindingModule(GateRef glue, GateRef module, GateRef resolvedBinding); - GateRef GetResolvedRecordBindingModule(GateRef glue, GateRef module, GateRef resolvedBinding); - GateRef LoadExternalmodulevar(GateRef glue, GateRef index, GateRef curModule); + GateRef EvaluateNativeModule(GateRef glue, GateRef module, GateRef moduleType); + void EvaluateModuleIfNeeded(GateRef glue, GateRef module, bool isLazy); + void LogModuleLoadInfo(GateRef glue, GateRef module, GateRef requiredModule, GateRef index, bool isSendable); + GateRef GetModuleValueFromIndexBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, GateRef index, + bool isSendable, bool isLazy); + GateRef GetModuleValueFromBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, GateRef index, + bool isSendable, bool isLazy); + GateRef GetModuleValueFromRecordIndexBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, + GateRef index, bool isSendable, bool isLazy); + GateRef GetModuleValueFromRecordBinding(GateRef glue, GateRef curModule, GateRef resolvedBinding, GateRef index, + bool isSendable, bool isLazy); + GateRef GetModuleValueOuterInternal(GateRef glue, GateRef index, GateRef curModule, bool isLazy); + GateRef LoadExternalModuleVar(GateRef glue, GateRef index, GateRef module); + GateRef LoadLazyModuleVar(GateRef glue, GateRef index, GateRef module); // end: Fast path of Loading Module variable. GateRef LoadModuleNamespaceByIndex(GateRef glue, GateRef index, GateRef module); diff --git a/ecmascript/message_string.h b/ecmascript/message_string.h index 23abd8d258..ba65e43057 100644 --- a/ecmascript/message_string.h +++ b/ecmascript/message_string.h @@ -84,10 +84,8 @@ namespace panda::ecmascript { V(InOperatorOnNonObject, "Cannot use 'in' operator in Non-Object") \ V(CurrentModuleUndefined, "GetModuleValueOutter currentModule failed") \ V(MisstakenResolvedBinding, "Get module value failed, mistaken ResolvedBinding") \ - V(CurrentModuleNotSourceTextModule, "Current module is not SourceTextModule") \ V(ModuleEnvMustBeDefined, "Environment of current module must not be undefined") \ V(CheckIsResolvedIndexBinding, "Current resolution must be ResolvedIndexBinding") \ - V(RecordNameMustBeString, "RecordName must be string") \ V(LexicalEnvIsInvalid, "LexicalEnv of native JSFunction is invalid, it should be GlobalEnv or Undefined") \ V(CurrentEnvIsInvalid, "CurrentEnv is invalid, it should be GlobalEnv, LexicalEnv or SFunctionEnv") \ V(AccessCompositeClassField, "this field of CompositeClass is unavailable") diff --git a/ecmascript/module/module_value_accessor.cpp b/ecmascript/module/module_value_accessor.cpp index 2f14161db6..1a92e90d13 100644 --- a/ecmascript/module/module_value_accessor.cpp +++ b/ecmascript/module/module_value_accessor.cpp @@ -339,7 +339,7 @@ JSTaggedValue ModuleValueAccessor::GetModuleValueFromBinding(const GetModuleValu } EvaluateModuleIfNeeded(info.thread, resolvedModule); RETURN_VALUE_IF_ABRUPT_COMPLETION(info.thread, JSTaggedValue::Exception()); - LogModuleLoadInfo(info.thread, info.module, resolvedModule, info.index, false); + LogModuleLoadInfo(info.thread, info.module, resolvedModule, info.index, info.isSendable); return UpdateBindingAndGetModuleValue(info.thread, info.module, resolvedModule, info.index, binding->GetBindingName(info.thread)); } @@ -419,7 +419,7 @@ JSTaggedValue ModuleValueAccessor::UpdateBindingAndGetModuleValue(JSThread *thre template void ModuleValueAccessor::EvaluateModuleIfNeeded(JSThread* thread, JSHandle module) { - if (!isLazy) { + if (!isLazy) { return; } if (module->GetStatus() >= ModuleStatus::EVALUATED) { diff --git a/ecmascript/stubs/runtime_stub_list.h b/ecmascript/stubs/runtime_stub_list.h index 993612e26e..43a9aebe5b 100644 --- a/ecmascript/stubs/runtime_stub_list.h +++ b/ecmascript/stubs/runtime_stub_list.h @@ -318,12 +318,15 @@ namespace panda::ecmascript { V(LdLocalModuleVarByIndexOnJSFunc) \ V(LdExternalModuleVarByIndexOnJSFunc) \ V(LdModuleVar) \ - V(GetModuleValueOuterInternal) \ + V(HostGetImportedModule) \ + V(EvaluateModule) \ + V(LoadNativeModule) \ + V(InsertModuleLoadInfo) \ + V(GetResolvedModuleFromRecordIndexBinding) \ + V(GetResolvedModuleFromRecordBinding) \ V(ThrowExportsIsHole) \ V(HandleResolutionIsNullOrString) \ V(CheckAndThrowModuleError) \ - V(GetResolvedRecordIndexBindingModule) \ - V(GetResolvedRecordBindingModule) \ V(GetPropIteratorSlowpath) \ V(PrimitiveStringCreate) \ V(AsyncFunctionEnter) \ diff --git a/ecmascript/stubs/runtime_stubs-inl.h b/ecmascript/stubs/runtime_stubs-inl.h index 30b41fa8a3..6b5e518c6d 100644 --- a/ecmascript/stubs/runtime_stubs-inl.h +++ b/ecmascript/stubs/runtime_stubs-inl.h @@ -1467,9 +1467,9 @@ inline JSTaggedValue RuntimeStubs::RuntimeLdExternalModuleVarWithModule(JSThread JSHandle moduleHdl) { JSTaggedValue module = moduleHdl.GetTaggedValue(); - ModuleManager* mmgr = thread->GetModuleManager(); if (SourceTextModule::IsSendableFunctionModule(module)) { const CString recordNameStr = SourceTextModule::GetModuleName(module); + ModuleManager* mmgr = thread->GetModuleManager(); module = mmgr->HostGetImportedModule(recordNameStr).GetTaggedValue(); moduleHdl = JSHandle(thread, module); } diff --git a/ecmascript/stubs/runtime_stubs.cpp b/ecmascript/stubs/runtime_stubs.cpp index 4f213f59f5..d30a2c4a4f 100644 --- a/ecmascript/stubs/runtime_stubs.cpp +++ b/ecmascript/stubs/runtime_stubs.cpp @@ -46,6 +46,7 @@ #include "ecmascript/stubs/runtime_stubs.h" #include "ecmascript/linked_hash_table.h" #include "ecmascript/builtins/builtins_object.h" +#include "ecmascript/module/module_logger.h" #include "ecmascript/module/module_value_accessor.h" #include "ecmascript/module/module_path_helper.h" #include "common_components/heap/allocator/region_desc.h" @@ -1964,13 +1965,83 @@ DEF_RUNTIME_STUBS(LdModuleVar) return RuntimeLdModuleVar(thread, key, innerFlag).GetRawData(); } -DEF_RUNTIME_STUBS(GetModuleValueOuterInternal) +DEF_RUNTIME_STUBS(HostGetImportedModule) { - RUNTIME_STUBS_HEADER(GetModuleValueOuterInternal); - JSTaggedValue curModule = GetArg(argv, argc, 0); // 0: means the zeroth parameter - int32_t index = - JSTaggedValue::ToInt32(thread, GetHArg(argv, argc, 1)); // 2: means the second parameter - return ModuleValueAccessor::GetModuleValueOuterInternal(thread, index, curModule).GetRawData(); + RUNTIME_STUBS_HEADER(HostGetImportedModule); + JSTaggedValue module = GetArg(argv, argc, 0); // 0: means the zeroth parameter + const CString recordNameStr = SourceTextModule::GetModuleName(module); + ModuleManager* mmgr = thread->GetModuleManager(); + return mmgr->HostGetImportedModule(recordNameStr).GetTaggedValue().GetRawData(); +} + +DEF_RUNTIME_STUBS(EvaluateModule) +{ + RUNTIME_STUBS_HEADER(EvaluateModule); + JSHandle module = GetHArg(argv, argc, 0); // 0: means the zeroth parameter + JSTaggedValue capability = SourceTextModule::Evaluate(thread, module, nullptr, 0, ExecuteTypes::LAZY); + return capability.GetRawData(); +} + +DEF_RUNTIME_STUBS(LoadNativeModule) +{ + RUNTIME_STUBS_HEADER(LoadNativeModule); + JSHandle nativeModule = GetHArg(argv, argc, 0); // 0: means the zeroth parameter + JSHandle moduleType = GetHArg(argv, argc, 1); // 1: means the first parameter + ModuleTypes moduleTypeCast = static_cast(JSTaggedValue::ToInt8(thread, moduleType)); + if (!SourceTextModule::LoadNativeModule(thread, nativeModule, moduleTypeCast)) { + LOG_FULL(INFO) << "LoadNativeModule " << nativeModule->GetEcmaModuleRecordNameString() << " failed"; + return JSTaggedValue::False().GetRawData(); + } + nativeModule->SetStatus(ModuleStatus::EVALUATED); + return JSTaggedValue::True().GetRawData(); +} + +DEF_RUNTIME_STUBS(InsertModuleLoadInfo) +{ + RUNTIME_STUBS_HEADER(InsertModuleLoadInfo); + JSHandle currentModule = + GetHArg(argv, argc, 0); // 0: means the zeroth parameter + JSHandle exportModule = GetHArg(argv, argc, 1); // 1: means the first parameter + JSHandle index = GetHArg(argv, argc, 2); // 2: means the second parameter + ModuleLogger *moduleLogger = thread->GetModuleLogger(); + moduleLogger->InsertModuleLoadInfo(currentModule, exportModule, JSTaggedValue::ToInt32(thread, index)); + return JSTaggedValue::Hole().GetRawData(); +} + +DEF_RUNTIME_STUBS(GetResolvedModuleFromRecordIndexBinding) +{ + RUNTIME_STUBS_HEADER(GetResolvedModuleFromRecordIndexBinding); + JSHandle module = GetHArg(argv, argc, 0); // 0: means the zeroth parameter + JSHandle binding = + GetHArg(argv, argc, 1); // 1: means the first parameter + bool isLazy = GetArg(argv, argc, 2).ToBoolean(); // 2: means the second parameter + JSHandle resolvedModule; + if (isLazy) { + resolvedModule = ModuleValueAccessor::GetResolvedModule( + thread, module, binding, ModulePathHelper::Utf8ConvertToString(thread, binding->GetModuleRecord(thread))); + } else { + resolvedModule = ModuleValueAccessor::GetResolvedModule( + thread, module, binding, ModulePathHelper::Utf8ConvertToString(thread, binding->GetModuleRecord(thread))); + } + return resolvedModule.GetTaggedValue().GetRawData(); +} + +DEF_RUNTIME_STUBS(GetResolvedModuleFromRecordBinding) +{ + RUNTIME_STUBS_HEADER(GetResolvedModuleFromRecordBinding); + JSHandle module = GetHArg(argv, argc, 0); // 0: means the zeroth parameter + JSHandle binding = + GetHArg(argv, argc, 1); // 1: means the first parameter + bool isLazy = GetArg(argv, argc, 2).ToBoolean(); // 2: means the second parameter + JSHandle resolvedModule; + if (isLazy) { + resolvedModule = ModuleValueAccessor::GetResolvedModule( + thread, module, binding, ModulePathHelper::Utf8ConvertToString(thread, binding->GetModuleRecord(thread))); + } else { + resolvedModule = ModuleValueAccessor::GetResolvedModule( + thread, module, binding, ModulePathHelper::Utf8ConvertToString(thread, binding->GetModuleRecord(thread))); + } + return resolvedModule.GetTaggedValue().GetRawData(); } DEF_RUNTIME_STUBS(GetModuleName) @@ -2036,46 +2107,6 @@ DEF_RUNTIME_STUBS(CheckAndThrowModuleError) return JSTaggedValue::Hole().GetRawData(); } -DEF_RUNTIME_STUBS(GetResolvedRecordIndexBindingModule) -{ - RUNTIME_STUBS_HEADER(GetResolvedRecordIndexBindingModule); - JSHandle module = GetHArg(argv, argc, 0); // 0: means the zeroth parameter - JSHandle binding = - GetHArg(argv, argc, 1); // 1: means the first parameter - JSTaggedType argModuleManager = GetTArg(argv, argc, 2); // 2: means the second parameter - ModuleManager *moduleManager = reinterpret_cast(argModuleManager); - JSTaggedValue recordName = GetArg(argv, argc, 3); // 3: means the third parameter - CString recordNameStr = ModulePathHelper::Utf8ConvertToString(thread, recordName); - if (!moduleManager->IsEvaluatedModule(recordNameStr)) { - auto isMergedAbc = !module->GetEcmaModuleRecordNameString().empty(); - CString fileName = ModulePathHelper::Utf8ConvertToString(thread, (binding->GetAbcFileName(thread))); - if (!JSPandaFileExecutor::LazyExecuteModule(thread, - recordNameStr, fileName, isMergedAbc)) { // LCOV_EXCL_BR_LINE - LOG_ECMA(FATAL) << "LazyExecuteModule failed"; - } - } - return moduleManager->HostGetImportedModule(recordNameStr).GetTaggedValue().GetRawData(); -} - -DEF_RUNTIME_STUBS(GetResolvedRecordBindingModule) -{ - RUNTIME_STUBS_HEADER(GetResolvedRecordBindingModule); - JSHandle module = GetHArg(argv, argc, 0); // 0: means the zeroth parameter - JSTaggedType argModuleManager = GetTArg(argv, argc, 1); // 1: means the first parameter - ModuleManager *moduleManager = reinterpret_cast(argModuleManager); - JSTaggedValue recordName = GetArg(argv, argc, 2); // 2: means the second parameter - CString recordNameStr = ModulePathHelper::Utf8ConvertToString(thread, recordName); - if (!moduleManager->IsEvaluatedModule(recordNameStr)) { - auto isMergedAbc = !module->GetEcmaModuleRecordNameString().empty(); - CString fileName = module->GetEcmaModuleFilenameString(); - if (!JSPandaFileExecutor::LazyExecuteModule(thread, - recordNameStr, fileName, isMergedAbc)) { // LCOV_EXCL_BR_LINE - LOG_ECMA(FATAL) << "LazyExecuteModule failed"; - } - } - return moduleManager->HostGetImportedModule(recordNameStr).GetTaggedValue().GetRawData(); -} - DEF_RUNTIME_STUBS(GetPropIteratorSlowpath) { RUNTIME_STUBS_HEADER(GetPropIteratorSlowpath); -- Gitee From 437578a3cc2bb0cc0583926240f31a42b1b9b476 Mon Sep 17 00:00:00 2001 From: wuxiesaber Date: Tue, 22 Apr 2025 21:17:13 +0800 Subject: [PATCH 2/2] add IR path for ldlazymodulevar Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICQD57 Signed-off-by: wuxiesaber Change-Id: I49e5bb95b548d9dbe80ce9605d4b72b9ee313d8b --- ecmascript/compiler/interpreter_stub.cpp | 18 +++++++++++++++--- ecmascript/compiler/stub_builder.cpp | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ecmascript/compiler/interpreter_stub.cpp b/ecmascript/compiler/interpreter_stub.cpp index fdaea9c153..9049fb9eeb 100644 --- a/ecmascript/compiler/interpreter_stub.cpp +++ b/ecmascript/compiler/interpreter_stub.cpp @@ -6313,6 +6313,7 @@ DECLARE_ASM_HANDLER(HandleCallRuntimeLdSendableVarImm16Imm16) DISPATCH_WITH_ACC(CALLRUNTIME_WIDELDSENDABLEVAR_PREF_IMM16_IMM16); } + DECLARE_ASM_HANDLER(HandleCallRuntimeLdLazyModuleVarPrefImm8) { DEFVARIABLE(varAcc, VariableType::JS_ANY(), acc); @@ -6321,9 +6322,15 @@ DECLARE_ASM_HANDLER(HandleCallRuntimeLdLazyModuleVarPrefImm8) GateRef frame = GetFrame(sp); GateRef currentFunc = GetFunctionFromFrame(glue, frame); GateRef currentEnv = GetEnvFromFrame(glue, frame); +#if ENABLE_NEXT_OPTIMIZATION + SetCurrentGlobalEnv(currentEnv); + GateRef indexInt32 = ZExtInt8ToInt32(index); + GateRef module = GetModuleFromFunction(glue, currentFunc); + moduleRef = LoadLazyModuleVar(glue, indexInt32, module); +#else moduleRef = CallRuntimeWithCurrentEnv(glue, currentEnv, RTSTUB_ID(LdLazyExternalModuleVarByIndex), { Int8ToTaggedInt(index), currentFunc }); - +#endif auto env = GetEnvironment(); Label notException(env); CHECK_EXCEPTION_WITH_JUMP(*moduleRef, ¬Exception); @@ -6338,14 +6345,19 @@ DECLARE_ASM_HANDLER(HandleCallRuntimeWideLdLazyModuleVarPrefImm16) { DEFVARIABLE(varAcc, VariableType::JS_ANY(), acc); DEFVARIABLE(moduleRef, VariableType::JS_ANY(), Undefined()); - GateRef index = ReadInst16_1(pc); GateRef frame = GetFrame(sp); GateRef currentFunc = GetFunctionFromFrame(glue, frame); GateRef currentEnv = GetEnvFromFrame(glue, frame); +#if ENABLE_NEXT_OPTIMIZATION + SetCurrentGlobalEnv(currentEnv); + GateRef indexInt32 = ZExtInt16ToInt32(index); + GateRef module = GetModuleFromFunction(glue, currentFunc); + moduleRef = LoadLazyModuleVar(glue, indexInt32, module); +#else moduleRef = CallRuntimeWithCurrentEnv(glue, currentEnv, RTSTUB_ID(LdLazyExternalModuleVarByIndex), { Int16ToTaggedInt(index), currentFunc }); - +#endif auto env = GetEnvironment(); Label notException(env); CHECK_EXCEPTION_WITH_JUMP(*moduleRef, ¬Exception); diff --git a/ecmascript/compiler/stub_builder.cpp b/ecmascript/compiler/stub_builder.cpp index ad0d5dc7fe..4eaed327e3 100644 --- a/ecmascript/compiler/stub_builder.cpp +++ b/ecmascript/compiler/stub_builder.cpp @@ -12636,6 +12636,11 @@ GateRef StubBuilder::LoadExternalModuleVar(GateRef glue, GateRef index, GateRef return ret; } +GateRef StubBuilder::LoadLazyModuleVar(GateRef glue, GateRef index, GateRef module) +{ + return GetModuleValueOuterInternal(glue, index, module, true); +} + GateRef StubBuilder::LoadModuleNamespaceByIndex(GateRef glue, GateRef index, GateRef module) { auto env = GetEnvironment(); -- Gitee