新增napi接口ExecuteNativeModule

Issue: https://gitee.com/openharmony/arkui_napi/issues/I8DMF8
Signed-off-by: chenlincl3 <chenlin201@huawei.com>
Change-Id: I07be422861d54f9fb97b4b6b8ea8a487b6b9f915
This commit is contained in:
chenlincl3 2023-11-03 16:42:10 +08:00
parent 5ec8bde67f
commit b4be2c0b49
4 changed files with 51 additions and 1 deletions

View File

@ -707,4 +707,43 @@ JSHandle<JSTaggedValue> ModuleManager::HostResolveImportedModule(const JSPandaFi
}
return ResolveModule(thread, jsPandaFile);
}
JSHandle<JSTaggedValue> ModuleManager::LoadNativeModule(JSThread *thread, const std::string &key)
{
ObjectFactory *factory = vm_->GetFactory();
JSHandle<EcmaString> keyHandle = factory->NewFromASCII(key.c_str());
JSMutableHandle<JSTaggedValue> requiredModule(thread, thread->GlobalConstants()->GetUndefined());
if (IsImportedModuleLoaded(keyHandle.GetTaggedValue())) {
JSHandle<SourceTextModule> moduleRecord = HostGetImportedModule(keyHandle.GetTaggedValue());
requiredModule.Update(moduleRecord);
} else {
CString requestPath = ConvertToString(keyHandle.GetTaggedValue());
CString entryPoint = PathHelper::GetStrippedModuleName(requestPath);
auto [isNative, moduleType] = SourceTextModule::CheckNativeModule(requestPath);
JSHandle<JSTaggedValue> nativeModuleHld = ResolveNativeModule(requestPath, moduleType);
JSHandle<SourceTextModule> nativeModule =
JSHandle<SourceTextModule>::Cast(nativeModuleHld);
JSHandle<JSTaggedValue> keyValue(thread, keyHandle.GetTaggedValue());
if (!SourceTextModule::LoadNativeModule(thread, nativeModule, keyValue,
moduleType)) {
LOG_FULL(ERROR) << "loading native module" << requestPath << " failed";
}
nativeModule->SetStatus(ModuleStatus::EVALUATED);
nativeModule->SetLoadingTypes(LoadingTypes::STABLE_MODULE);
requiredModule.Update(nativeModule);
}
JSHandle<ecmascript::SourceTextModule> ecmaModule = JSHandle<SourceTextModule>(requiredModule);
if (ecmaModule->GetIsNewBcVersion()) {
int index = GetExportObjectIndex(vm_, ecmaModule, key);
JSTaggedValue result = ecmaModule->GetModuleValue(thread, index, false);
JSHandle<JSTaggedValue> exportObj(thread, result);
return exportObj;
}
JSTaggedValue result = ecmaModule->GetModuleValue(thread, keyHandle.GetTaggedValue(), false);
JSHandle<JSTaggedValue> exportObj(thread, result);
return exportObj;
}
} // namespace panda::ecmascript

View File

@ -65,6 +65,8 @@ public:
const CString &recordName, bool excuteFromJob = false);
JSHandle<JSTaggedValue> HostResolveImportedModule(const JSPandaFile *jsPandaFile, const CString &filename);
JSHandle<JSTaggedValue> LoadNativeModule(JSThread *thread, const std::string &key);
JSTaggedValue GetCurrentModule();
JSTaggedValue GetNativeModuleValue(JSThread *thread, JSTaggedValue currentModule,
JSTaggedValue resolvedModule, ResolvedIndexBinding *binding);

View File

@ -1350,7 +1350,7 @@ public:
static bool ExecuteModuleFromBuffer(EcmaVM *vm, const void *data, int32_t size, const std::string &file);
static Local<ObjectRef> GetExportObject(EcmaVM *vm, const std::string &file, const std::string &key);
static Local<ObjectRef> GetExportObjectFromBuffer(EcmaVM *vm, const std::string &file, const std::string &key);
static Local<ObjectRef> ExecuteNativeModule(EcmaVM *vm, const std::string &key);
// secure memory check
static bool CheckSecureMem(uintptr_t mem);

View File

@ -972,6 +972,15 @@ Local<ObjectRef> JSNApi::GetExportObjectFromBuffer(EcmaVM *vm, const std::string
return JSNApiHelper::ToLocal<ObjectRef>(exportObj);
}
Local<ObjectRef> JSNApi::ExecuteNativeModule(EcmaVM *vm, const std::string &key)
{
CHECK_HAS_PENDING_EXCEPTION_RETURN_UNDEFINED(vm);
JSThread *thread = vm->GetJSThread();
ecmascript::ModuleManager *moduleManager = thread->GetCurrentEcmaContext()->GetModuleManager();
JSHandle<JSTaggedValue> exportObj = moduleManager->LoadNativeModule(thread, key);
return JSNApiHelper::ToLocal<ObjectRef>(exportObj);
}
// Initialize IcuData Path
void JSNApi::InitializeIcuData(const JSRuntimeOptions &options)
{