1/*
2 * Copyright (c) 2022 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/builtins/builtins_cjs_module.h"
17
18#include "ecmascript/interpreter/interpreter-inl.h"
19#include "ecmascript/module/module_path_helper.h"
20
21namespace panda::ecmascript::builtins {
22
23JSTaggedValue BuiltinsCjsModule::CjsModuleConstructor(EcmaRuntimeCallInfo *argv)
24{
25    JSThread *thread = argv->GetThread();
26    BUILTINS_API_TRACE(thread, CjsModule, CjsModuleConstructor);
27    [[maybe_unused]] EcmaHandleScope scope(thread);
28
29    LOG_ECMA(ERROR) << "BuiltinsCjsModule::CjsModuleConstructor : can not be call";
30    return JSTaggedValue::Undefined();
31}
32
33JSTaggedValue BuiltinsCjsModule::Compiler(EcmaRuntimeCallInfo *msg)
34{
35    ASSERT(msg);
36    JSThread *thread = msg->GetThread();
37    BUILTINS_API_TRACE(thread, CjsModule, Compiler);
38    [[maybe_unused]] EcmaHandleScope handleScope(thread);
39    return JSTaggedValue::Hole();
40}
41
42JSTaggedValue BuiltinsCjsModule::Load(EcmaRuntimeCallInfo *msg)
43{
44    ASSERT(msg);
45    JSThread *thread = msg->GetThread();
46    BUILTINS_API_TRACE(thread, CjsModule, Load);
47    [[maybe_unused]] EcmaHandleScope handleScope(thread);
48    return JSTaggedValue::Hole();
49}
50
51JSTaggedValue BuiltinsCjsModule::ResolveFilename(EcmaRuntimeCallInfo *argv)
52{
53    ASSERT(argv);
54    JSThread *thread = argv->GetThread();
55    BUILTINS_API_TRACE(thread, CjsModule, ResolveFilename);
56    [[maybe_unused]] EcmaHandleScope handleScope(thread);
57
58    uint32_t length = argv->GetArgsNumber();
59    CString parent;
60    CString dirname;
61    const JSPandaFile *jsPandaFile = EcmaInterpreter::GetNativeCallPandafile(thread);
62    ModulePathHelper::ResolveCurrentPath(parent, dirname, jsPandaFile);
63
64    if (length != 1) {  // strange arg's number
65        LOG_ECMA(FATAL) << "BuiltinsCjsModule::Load : can only accept one argument";
66        UNREACHABLE();
67    }
68    JSHandle<EcmaString> requestName = JSHandle<EcmaString>::Cast(GetCallArg(argv, 0));
69    CString requestNameStr = ModulePathHelper::Utf8ConvertToString(requestName.GetTaggedValue());
70    CString filename = ResolveFilenameFromNative(thread, dirname, requestNameStr);
71    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
72    JSHandle<JSTaggedValue> filenameHdl = JSHandle<JSTaggedValue>::Cast(factory->NewFromUtf8(filename));
73    return filenameHdl.GetTaggedValue();
74}
75
76JSTaggedValue BuiltinsCjsModule::Require(EcmaRuntimeCallInfo *msg)
77{
78    ASSERT(msg);
79    JSThread *thread = msg->GetThread();
80    BUILTINS_API_TRACE(thread, CjsModule, Require);
81    [[maybe_unused]] EcmaHandleScope handleScope(thread);
82    return JSTaggedValue::Hole();
83}
84
85JSTaggedValue BuiltinsCjsModule::GetExportsForCircularRequire(EcmaRuntimeCallInfo *msg)
86{
87    ASSERT(msg);
88    JSThread *thread = msg->GetThread();
89    BUILTINS_API_TRACE(thread, CjsModule, GetExportsForCircularRequire);
90    [[maybe_unused]] EcmaHandleScope handleScope(thread);
91    return JSTaggedValue::Hole();
92}
93
94JSTaggedValue BuiltinsCjsModule::UpdateChildren(EcmaRuntimeCallInfo *msg)
95{
96    ASSERT(msg);
97    JSThread *thread = msg->GetThread();
98    BUILTINS_API_TRACE(thread, CjsModule, UpdateChildren);
99    [[maybe_unused]] EcmaHandleScope handleScope(thread);
100    return JSTaggedValue::Hole();
101}
102}  // namespace panda::ecmascript::builtins
103
104