14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
204514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile.h"
214514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_manager.h"
224514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_source_text.h"
234514f5e3Sopenharmony_ci#include "ecmascript/module/js_shared_module.h"
244514f5e3Sopenharmony_ci#include "ecmascript/napi/jsnapi_helper.h"
254514f5e3Sopenharmony_ci#include "ecmascript/napi/jsnapi_helper.h"
264514f5e3Sopenharmony_ci#include "ecmascript/tagged_dictionary.h"
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_cinamespace panda::ecmascript {
294514f5e3Sopenharmony_cistruct StateVisit {
304514f5e3Sopenharmony_ci    Mutex mutex;
314514f5e3Sopenharmony_ci    ConditionVariable cv;
324514f5e3Sopenharmony_ci    uint32_t threadId;
334514f5e3Sopenharmony_ci};
344514f5e3Sopenharmony_ciclass SharedModuleManager {
354514f5e3Sopenharmony_cipublic:
364514f5e3Sopenharmony_ci    static PUBLIC_API SharedModuleManager *GetInstance();
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    void Initialize() {};
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    void Destroy()
414514f5e3Sopenharmony_ci    {
424514f5e3Sopenharmony_ci        resolvedSharedModules_.clear();
434514f5e3Sopenharmony_ci    }
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    JSTaggedValue GetSendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    JSTaggedValue GetSendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    JSTaggedValue GetLazySendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    JSTaggedValue GetLazySendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    void Iterate(const RootVisitor &v);
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ResolveImportedModule(JSThread *thread, const CString &referencingModule,
564514f5e3Sopenharmony_ci                                                  bool executeFromJob);
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> PUBLIC_API ResolveImportedModuleWithMerge(JSThread *thread, const CString &fileName,
594514f5e3Sopenharmony_ci                                                                      const CString &recordName, bool executeFromJob);
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    StateVisit &findModuleMutexWithLock(JSThread *thread, const JSHandle<SourceTextModule> &module);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    bool SearchInSModuleManager(JSThread *thread, const CString &recordName);
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    void InsertInSModuleManager(JSThread *thread, const CString &recordName,
664514f5e3Sopenharmony_ci        JSHandle<SourceTextModule> &moduleRecord);
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    void PUBLIC_API TransferSModule(JSThread *thread);
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    bool IsInstantiatedSModule(JSThread *thread, const JSHandle<SourceTextModule> &module);
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> GenerateFuncModule(JSThread *thread, const JSPandaFile *jsPandaFile,
734514f5e3Sopenharmony_ci        const CString &entryPoint, ClassKind classKind = ClassKind::NON_SENDABLE);
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    JSHandle<ModuleNamespace> SModuleNamespaceCreate(JSThread *thread, const JSHandle<JSTaggedValue> &module,
764514f5e3Sopenharmony_ci                                                            const JSHandle<TaggedArray> &exports);
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    inline void AddResolveImportedSModule(const CString &recordName, JSTaggedValue module)
794514f5e3Sopenharmony_ci    {
804514f5e3Sopenharmony_ci        resolvedSharedModules_.emplace(recordName, module);
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    inline void UpdateResolveImportedSModule(const CString &recordName, JSTaggedValue module)
844514f5e3Sopenharmony_ci    {
854514f5e3Sopenharmony_ci        resolvedSharedModules_[recordName] = module;
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci    void SharedNativeObjDestory();
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ciprivate:
904514f5e3Sopenharmony_ci    SharedModuleManager() = default;
914514f5e3Sopenharmony_ci    ~SharedModuleManager() = default;
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(SharedModuleManager);
944514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(SharedModuleManager);
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ResolveSharedImportedModuleWithMerge(JSThread *thread, const CString &fileName,
974514f5e3Sopenharmony_ci        const CString &recordName, const JSPandaFile *jsPandaFile, JSRecordInfo *recordInfo);
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    bool SearchInSModuleManagerUnsafe(const CString &recordName);
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    JSHandle<SourceTextModule> GetSModuleUnsafe(JSThread *thread, const CString &recordName);
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ci    JSHandle<SourceTextModule> GetSModule(JSThread *thread, const CString &recordName);
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4;
1064514f5e3Sopenharmony_ci    CUnorderedMap<CString, JSTaggedValue> resolvedSharedModules_;
1074514f5e3Sopenharmony_ci    CMap<CString, StateVisit> sharedModuleMutex_;
1084514f5e3Sopenharmony_ci    Mutex mutex_;
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    friend class SourceTextModule;
1114514f5e3Sopenharmony_ci    friend class EcmaContext;
1124514f5e3Sopenharmony_ci    friend class ModuleManager;
1134514f5e3Sopenharmony_ci};
1144514f5e3Sopenharmony_ci} // namespace panda::ecmascript
1154514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
116