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_require.h"
17
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_object-inl.h"
22 #include "ecmascript/js_tagged_value-inl.h"
23 #include "ecmascript/js_thread.h"
24 #include "ecmascript/require/js_cjs_module.h"
25
26 namespace panda::ecmascript::builtins {
CjsRequireConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsCjsRequire::CjsRequireConstructor(EcmaRuntimeCallInfo *argv)
28 {
29 JSThread *thread = argv->GetThread();
30 [[maybe_unused]] EcmaHandleScope scope(thread);
31
32 // 1. If NewTarget is not undefined, throw a TypeError exception.
33 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
34 if (!newTarget->IsUndefined()) {
35 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
36 }
37 uint32_t length = argv->GetArgsNumber();
38 JSHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
39 if (length != 1) { // strange arg's number
40 LOG_ECMA(ERROR) << "BuiltinsCjsRequire::CjsRequireConstructor : can only accept one argument";
41 UNREACHABLE();
42 }
43 JSHandle<EcmaString> requestName = JSHandle<EcmaString>::Cast(GetCallArg(argv, 0));
44 result = CjsModule::Load(thread, requestName);
45 return result.GetTaggedValue();
46 }
47
Main(EcmaRuntimeCallInfo * msg)48 JSTaggedValue BuiltinsCjsRequire::Main(EcmaRuntimeCallInfo *msg)
49 {
50 ASSERT(msg);
51 JSThread *thread = msg->GetThread();
52 [[maybe_unused]] EcmaHandleScope handleScope(thread);
53 return JSTaggedValue(0);
54 }
55
Resolve(EcmaRuntimeCallInfo * msg)56 JSTaggedValue BuiltinsCjsRequire::Resolve(EcmaRuntimeCallInfo *msg)
57 {
58 ASSERT(msg);
59 JSThread *thread = msg->GetThread();
60 [[maybe_unused]] EcmaHandleScope handleScope(thread);
61 return JSTaggedValue::Hole();
62 }
63 } // namespace panda::ecmascript::builtins
64
65