2022-06-08 02:59:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-07-23 10:33:33 +00:00
|
|
|
|
|
|
|
#include "ecmascript/builtins/builtins_cjs_require.h"
|
|
|
|
|
|
|
|
#include "ecmascript/base/builtins_base.h"
|
2022-06-08 02:59:06 +00:00
|
|
|
#include "ecmascript/ecma_macros.h"
|
|
|
|
#include "ecmascript/global_env.h"
|
|
|
|
#include "ecmascript/js_object-inl.h"
|
|
|
|
#include "ecmascript/js_tagged_value-inl.h"
|
|
|
|
#include "ecmascript/js_thread.h"
|
2022-07-23 10:33:33 +00:00
|
|
|
#include "ecmascript/require/js_cjs_module.h"
|
2022-06-08 02:59:06 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript::builtins {
|
|
|
|
JSTaggedValue BuiltinsCjsRequire::CjsRequireConstructor(EcmaRuntimeCallInfo *argv)
|
|
|
|
{
|
|
|
|
JSThread *thread = argv->GetThread();
|
2023-04-12 03:23:14 +00:00
|
|
|
BUILTINS_API_TRACE(thread, CjsRequire, CjsRequireConstructor);
|
2022-06-08 02:59:06 +00:00
|
|
|
[[maybe_unused]] EcmaHandleScope scope(thread);
|
|
|
|
|
|
|
|
// 1. If NewTarget is not undefined, throw a TypeError exception.
|
|
|
|
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
|
|
|
|
if (!newTarget->IsUndefined()) {
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
|
|
|
|
}
|
2022-08-05 02:06:50 +00:00
|
|
|
uint32_t length = argv->GetArgsNumber();
|
2022-06-08 02:59:06 +00:00
|
|
|
JSHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
|
|
|
|
if (length != 1) { // strange arg's number
|
2022-12-01 07:29:41 +00:00
|
|
|
LOG_ECMA(FATAL) << "BuiltinsCjsRequire::CjsRequireConstructor : can only accept one argument";
|
2022-06-08 02:59:06 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
JSHandle<EcmaString> requestName = JSHandle<EcmaString>::Cast(GetCallArg(argv, 0));
|
2022-07-07 09:29:10 +00:00
|
|
|
result = CjsModule::Load(thread, requestName);
|
2023-07-18 11:32:21 +00:00
|
|
|
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
2022-06-10 06:22:23 +00:00
|
|
|
return result.GetTaggedValue();
|
2022-06-08 02:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSTaggedValue BuiltinsCjsRequire::Main(EcmaRuntimeCallInfo *msg)
|
|
|
|
{
|
|
|
|
ASSERT(msg);
|
|
|
|
JSThread *thread = msg->GetThread();
|
2023-04-12 03:23:14 +00:00
|
|
|
BUILTINS_API_TRACE(thread, CjsRequire, Main);
|
2022-06-08 02:59:06 +00:00
|
|
|
[[maybe_unused]] EcmaHandleScope handleScope(thread);
|
|
|
|
return JSTaggedValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSTaggedValue BuiltinsCjsRequire::Resolve(EcmaRuntimeCallInfo *msg)
|
|
|
|
{
|
|
|
|
ASSERT(msg);
|
|
|
|
JSThread *thread = msg->GetThread();
|
2023-04-12 03:23:14 +00:00
|
|
|
BUILTINS_API_TRACE(thread, CjsRequire, Resolve);
|
2022-06-08 02:59:06 +00:00
|
|
|
[[maybe_unused]] EcmaHandleScope handleScope(thread);
|
|
|
|
return JSTaggedValue::Hole();
|
|
|
|
}
|
|
|
|
} // namespace panda::ecmascript::builtins
|
|
|
|
|