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