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