1 /*
2 * Copyright (c) 2025 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 #ifndef ECMASCRIPT_JS_BIGINT_INL_H
17 #define ECMASCRIPT_JS_BIGINT_INL_H
18
19 #include "ecmascript/js_bigint.h"
20 #include "ecmascript/object_factory-inl.h"
21
22 namespace panda::ecmascript {
23 template <MemSpaceType type>
CreateBigint(JSThread * thread,uint32_t length)24 JSHandle<BigInt> BigInt::CreateBigint(JSThread *thread, uint32_t length)
25 {
26 if (length > MAXSIZE) {
27 JSHandle<BigInt> bigint(thread, JSTaggedValue::Exception());
28 THROW_RANGE_ERROR_AND_RETURN(thread, "Maximum BigInt size exceeded", bigint);
29 }
30 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
31 JSHandle<BigInt> bigint = factory->NewBigInt<type>(length);
32 return bigint;
33 }
34
35 // Create a bigint without data initialization, additional SetSign and SetData are required
36 template <MemSpaceType type>
CreateRawBigInt(JSThread * thread,uint32_t length)37 JSHandle<BigInt> BigInt::CreateRawBigInt(JSThread *thread, uint32_t length)
38 {
39 if (length > MAXSIZE) {
40 JSHandle<BigInt> bigint(thread, JSTaggedValue::Exception());
41 THROW_RANGE_ERROR_AND_RETURN(thread, "Maximum BigInt size exceeded", bigint);
42 }
43 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44 JSHandle<BigInt> bigint = factory->NewBigIntWithoutInitData<type>(length);
45 return bigint;
46 }
47
48 template <MemSpaceType type>
CreateSubBigInt(JSThread * thread,const JSHandle<BigInt> & x,uint32_t length)49 JSHandle<BigInt> BigInt::CreateSubBigInt(JSThread *thread, const JSHandle<BigInt>& x, uint32_t length)
50 {
51 if (length > MAXSIZE) {
52 JSHandle<BigInt> bigint(thread, JSTaggedValue::Exception());
53 THROW_RANGE_ERROR_AND_RETURN(thread, "Maximum BigInt size exceeded", bigint);
54 }
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 JSHandle<BigInt> bigint = factory->NewSubBigInt<type>(x, length);
57 return bigint;
58 }
59 } // namespace panda::ecmascript
60 #endif // ECMASCRIPT_JS_BIGINT_INL_H
61