1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/module/static/static_module_loader.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/object_factory-inl.h"
19 #include "ecmascript/module/module_path_helper.h"
20 #include "ecmascript/module/js_module_deregister.h"
21 #include "ecmascript/module/js_shared_module_manager.h"
22 #include "ecmascript/shared_objects/js_shared_array.h"
23 #include "ecmascript/module/js_module_source_text.h"
24 #include "ecmascript/module/module_data_extractor.h"
25 #include "ecmascript/builtins/builtins_promise_job.h"
26 #include "ecmascript/interpreter/interpreter.h"
27 #include "ecmascript/js_promise.h"
28 #include "ecmascript/module/module_path_helper.h"
29 #include "ecmascript/module/static/static_module_proxy_handler.h"
30
31 namespace panda::ecmascript {
CanTryLoadStaticModulePath(const CString & requestPath)32 bool StaticModuleLoader::CanTryLoadStaticModulePath(const CString &requestPath)
33 {
34 // Filters the 1.0 ohmurl that can be determined
35 if (ModulePathHelper::IsOhmUrl(requestPath) || ModulePathHelper::IsImportFile(requestPath)
36 || StringHelper::StringStartWith(requestPath, ModulePathHelper::PREFIX_MODULE)
37 || StringHelper::StringStartWith(requestPath, ModulePathHelper::RAW_ARKUIX_PREFIX)
38 || StringHelper::StringStartWith(requestPath, ModulePathHelper::PREFIX_NORMALIZED)) {
39 return false;
40 }
41 return true;
42 }
43
GetStaticModuleLoadFunc(EcmaVM * vm)44 Local<JSValueRef> StaticModuleLoader::GetStaticModuleLoadFunc(EcmaVM *vm)
45 {
46 auto thread = vm->GetJSThread();
47 auto globalConstants = thread->GlobalConstants();
48 Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm);
49 Local<JSValueRef> pandaObject = globalObject->Get(vm,
50 JSNApiHelper::ToLocal<StringRef>(globalConstants->GetHandledPandaString()));
51 if (pandaObject->IsUndefined()) {
52 LOG_ECMA(DEBUG) << "the current env is not 1.2 vm";
53 return JSValueRef::Undefined(vm);
54 }
55 JSHandle<JSTaggedValue> getModule = JSTaggedValue::GetProperty(thread, JSNApiHelper::ToJSHandle(pandaObject),
56 globalConstants->GetHandledGetModuleString()).GetValue();
57 Local<JSValueRef> getModuleRef = JSNApiHelper::ToLocal<JSValueRef>(getModule);
58 if (!getModuleRef->IsFunction(vm)) {
59 return JSValueRef::Undefined(vm);
60 }
61 return getModuleRef;
62 }
63
LoadStaticModule(JSThread * thread,Local<FunctionRef> getEsModuleFunc,const CString & key)64 JSHandle<JSTaggedValue> StaticModuleLoader::LoadStaticModule(JSThread *thread,
65 Local<FunctionRef> getEsModuleFunc, const CString &key)
66 {
67 auto moduleManager = thread->GetModuleManager();
68 if (moduleManager->IsModuleLoaded(key)) {
69 LOG_ECMA(INFO) << "load static module from cache.";
70 JSHandle<SourceTextModule> moduleRecord = moduleManager->HostGetImportedModule(key);
71 return JSHandle<JSTaggedValue>(thread, moduleRecord->GetModuleValue(thread, 0, false));
72 }
73
74 auto vm = thread->GetEcmaVM();
75 auto globalConstants = thread->GlobalConstants();
76
77 JSHandle<JSTaggedValue> moduleRecord = ModuleDataExtractor::ParseNativeModule(thread, key,
78 "", ModuleTypes::STATIC_MODULE);
79 JSHandle<SourceTextModule> nativeModule = JSHandle<SourceTextModule>::Cast(moduleRecord);
80 std::vector<Local<JSValueRef>> moduleArgs;
81 moduleArgs.emplace_back(StringRef::NewFromUtf8(vm, key.c_str()));
82 Local<JSValueRef> moduleExport = getEsModuleFunc->Call(vm, JSValueRef::Undefined(vm),
83 moduleArgs.data(), moduleArgs.size());
84 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
85 JSHandle<JSProxy> proxyExport = StaticModuleProxyHandler::CreateStaticModuleProxyHandler(thread,
86 JSNApiHelper::ToJSHandle(moduleExport));
87 JSHandle<JSTaggedValue> value(proxyExport);
88 SourceTextModule::RecordEvaluatedOrError(thread, nativeModule);
89 nativeModule->SetLoadingTypes(LoadingTypes::STABLE_MODULE);
90 SourceTextModule::StoreModuleValue(thread, nativeModule, 0, value);
91 moduleManager->AddResolveImportedModule(key, moduleRecord.GetTaggedValue());
92 return value;
93 }
94
95 } // namespace panda::ecmascript
96