• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/require/js_require_manager.h"
17 
18 #include "ecmascript/interpreter/slow_runtime_stub.h"
19 namespace panda::ecmascript {
InitializeCommonJS(JSThread * thread,CJSInfo cjsInfo)20 void RequireManager::InitializeCommonJS(JSThread *thread, CJSInfo cjsInfo)
21 {
22     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
23     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
24 
25     JSHandle<CjsModule> module = cjsInfo.moduleHdl;
26     JSHandle<JSTaggedValue> require = cjsInfo.requireHdl;
27     JSHandle<CjsExports> exports = cjsInfo.exportsHdl;
28     JSHandle<JSTaggedValue> filename = cjsInfo.filenameHdl;
29     JSHandle<JSTaggedValue> dirname = cjsInfo.dirnameHdl;
30 
31     // Set module.exports ---> exports
32     JSHandle<JSTaggedValue> exportsKey = globalConst->GetHandledCjsExportsString();
33     SlowRuntimeStub::StObjByName(thread, module.GetTaggedValue(), exportsKey.GetTaggedValue(),
34                                  exports.GetTaggedValue());
35     // initialize module
36     CjsModule::InitializeModule(thread, module, filename, dirname);
37 
38     // Set this.module ---> this.require.parent
39     JSHandle<JSTaggedValue> parentKey(factory->NewFromASCII("parent"));
40     SlowRuntimeStub::StObjByName(thread, require.GetTaggedValue(), parentKey.GetTaggedValue(),
41                                  module.GetTaggedValue());
42     // cache ----> Set Module._cache
43     JSHandle<JSTaggedValue> cacheKey = globalConst->GetHandledCjsCacheString();
44     JSHandle<JSTaggedValue> moduleObj(thread->GetEcmaVM()->GetGlobalEnv()->GetCjsModuleFunction());
45     JSTaggedValue modCache =
46         SlowRuntimeStub::LdObjByName(thread, moduleObj.GetTaggedValue(),
47                                      cacheKey.GetTaggedValue(), false, JSTaggedValue::Undefined());
48     JSHandle<CjsModuleCache> moduleCache = JSHandle<CjsModuleCache>(thread, modCache);
49     JSHandle<CjsModuleCache> newCache = CjsModuleCache::PutIfAbsentAndReset(thread, moduleCache,
50         JSHandle<JSTaggedValue>::Cast(filename), JSHandle<JSTaggedValue>(module));
51     SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(),
52                                  newCache.GetTaggedValue());
53 }
54 
CollectExecutedExp(JSThread * thread,CJSInfo cjsInfo)55 void RequireManager::CollectExecutedExp(JSThread *thread, CJSInfo cjsInfo)
56 {
57     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
58 
59     JSHandle<CjsModule> module = cjsInfo.moduleHdl;
60     JSHandle<JSTaggedValue> filename = cjsInfo.filenameHdl;
61 
62     // get Module from global env
63     JSHandle<JSTaggedValue> moduleObj(thread->GetEcmaVM()->GetGlobalEnv()->GetCjsModuleFunction());
64     JSHandle<JSTaggedValue> cacheKey = globalConst->GetHandledCjsCacheString();
65 
66     JSTaggedValue executedCacheVal = SlowRuntimeStub::LdObjByName(thread, moduleObj.GetTaggedValue(),
67                                                                   cacheKey.GetTaggedValue(),
68                                                                   false, JSTaggedValue::Undefined());
69     JSHandle<CjsModuleCache> executedCache = JSHandle<CjsModuleCache>(thread, executedCacheVal);
70     JSHandle<CjsModuleCache> resetCache = CjsModuleCache::ResetModule(thread, executedCache,
71                                                                       filename,
72                                                                       JSHandle<JSTaggedValue>::Cast(module));
73     SlowRuntimeStub::StObjByName(thread, moduleObj.GetTaggedValue(), cacheKey.GetTaggedValue(),
74                                  resetCache.GetTaggedValue());
75 }
76 } // namespace panda::ecmascript