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 #ifndef ECMASCRIPT_MODULE_VALUE_ACCESSOR_H 17 #define ECMASCRIPT_MODULE_VALUE_ACCESSOR_H 18 19 #include "ecmascript/module/js_module_source_text.h" 20 #include "ecmascript/stubs/runtime_stubs.h" 21 22 namespace panda::ecmascript { 23 class ModuleValueAccessor { 24 public: 25 static JSTaggedValue GetModuleValueInner(JSThread *thread, int32_t index); 26 static JSTaggedValue GetModuleValueInner(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 27 static JSTaggedValue GetModuleValueInner(JSThread *thread, int32_t index, JSHandle<JSTaggedValue> curModule); 28 static JSTaggedValue GetSendableModuleValueInner(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 29 30 static JSTaggedValue GetModuleValueOuter(JSThread *thread, int32_t index); 31 static JSTaggedValue GetModuleValueOuter(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 32 static JSTaggedValue GetModuleValueOuter(JSThread *thread, int32_t index, JSHandle<JSTaggedValue> curModule); 33 static JSTaggedValue GetLazyModuleValueOuter(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 34 static JSTaggedValue GetSendableModuleValueOuter(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 35 static JSTaggedValue GetLazySendableModuleValueOuter(JSThread *thread, int32_t index, JSTaggedValue jsFunc); 36 37 static JSTaggedValue GetModuleNamespace(JSThread *thread, int32_t index); 38 static JSTaggedValue GetModuleNamespace(JSThread *thread, int32_t index, JSTaggedValue curFunc); 39 40 static JSTaggedValue GetNativeOrCjsModuleValue(JSThread *thread, JSHandle<SourceTextModule> module, int32_t index); 41 static JSTaggedValue GetNativeOrCjsModuleValue(JSThread *thread, 42 JSHandle<SourceTextModule> module, 43 JSTaggedValue bindingName); 44 45 static void StoreModuleValue(JSThread *thread, int32_t index, JSTaggedValue value); 46 static void StoreModuleValue(JSThread *thread, int32_t index, JSTaggedValue value, JSTaggedValue jsFunc); 47 static JSHandle<JSTaggedValue> GetNativeOrCjsExports(JSThread *thread, JSTaggedValue resolvedModule); 48 protected: 49 struct GetModuleValueFromBindingInfo { 50 JSThread *thread; 51 JSHandle<SourceTextModule> module; 52 JSTaggedValue resolvedBinding; 53 int32_t index; 54 bool isSendable; 55 }; 56 static JSTaggedValue GetCurrentModule(JSThread *thread); 57 static JSTaggedValue GetModuleValue(JSThread *thread, JSHandle<SourceTextModule> module, int32_t index); 58 59 template <bool isLazy> 60 static JSTaggedValue GetModuleValueOuterInternal(JSThread *thread, 61 int32_t index, 62 JSTaggedValue curModule); 63 template <bool isLazy> 64 static JSTaggedValue GetSendableModuleValueOuterInternal(JSThread *thread, 65 int32_t index, 66 JSTaggedValue curModule); 67 static JSTaggedValue GetModuleNamespaceInternal(JSThread *thread, int32_t index, JSTaggedValue curModule); 68 69 static void StoreModuleValueInternal(JSThread *thread, 70 JSHandle<SourceTextModule> &curModule, 71 int32_t index, 72 JSTaggedValue value); 73 74 template <bool isLazy> 75 static JSTaggedValue GetModuleValueFromIndexBinding(const GetModuleValueFromBindingInfo &info); 76 template <bool isLazy> 77 static JSTaggedValue GetModuleValueFromBinding(const GetModuleValueFromBindingInfo &info); 78 template <bool isLazy> 79 static JSTaggedValue GetModuleValueFromRecordIndexBinding(const GetModuleValueFromBindingInfo &info); 80 template <bool isLazy> 81 static JSTaggedValue GetModuleValueFromRecordBinding(const GetModuleValueFromBindingInfo &info); 82 static JSTaggedValue UpdateBindingAndGetModuleValue(JSThread *thread, 83 JSHandle<SourceTextModule> module, 84 JSHandle<SourceTextModule> requiredModule, 85 int32_t index, 86 JSTaggedValue bindingName); 87 88 template <bool isLazy> 89 static void EvaluateModuleIfNeeded(JSThread *thread, JSHandle<SourceTextModule> module); 90 static void LogModuleLoadInfo(JSThread *thread, 91 JSHandle<SourceTextModule> module, 92 JSHandle<SourceTextModule> requiredModule, 93 int32_t index, 94 bool isSendable); 95 96 template <bool isLazy, typename BindingType> 97 static JSHandle<SourceTextModule> GetResolvedModule(JSThread *thread, 98 JSHandle<SourceTextModule> module, 99 JSHandle<BindingType> binding, 100 const CString& requestModuleRecordName); 101 102 friend class DeprecatedModuleValueAccessor; 103 friend class RuntimeStubs; 104 }; 105 class DeprecatedModuleValueAccessor { 106 public: 107 static JSTaggedValue GetModuleValueInner(JSThread *thread, JSTaggedValue key); 108 static JSTaggedValue GetModuleValueInner(JSThread *thread, JSTaggedValue key, JSTaggedValue jsFunc); 109 110 static JSTaggedValue GetModuleValueOuter(JSThread *thread, JSTaggedValue key); 111 static JSTaggedValue GetModuleValueOuter(JSThread *thread, JSTaggedValue key, JSTaggedValue jsFunc); 112 113 static JSTaggedValue GetModuleNamespace(JSThread *thread, JSTaggedValue localName); 114 static JSTaggedValue GetModuleNamespace(JSThread *thread, JSTaggedValue localName, JSTaggedValue curFunc); 115 116 static void StoreModuleValue(JSThread *thread, JSTaggedValue key, JSTaggedValue value); 117 static void StoreModuleValue(JSThread *thread, JSTaggedValue key, JSTaggedValue value, JSTaggedValue jsFunc); 118 protected: 119 static JSTaggedValue GetModuleValueOuterInternal(JSThread *thread, JSTaggedValue key, JSTaggedValue curModule); 120 static JSTaggedValue GetModuleNamespaceInternal(JSThread *thread, JSTaggedValue localName, JSTaggedValue curModule); 121 static void StoreModuleValueInternal(JSThread *thread, 122 JSHandle<SourceTextModule> &curModule, 123 JSTaggedValue key, 124 JSTaggedValue value); 125 }; 126 } // namespace panda::ecmascript 127 #endif // ECMASCRIPT_MODULE_VALUE_ACCESSOR_H 128