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