1 /*
2 * Copyright (c) 2021 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 "quickjs_native_number.h"
17
QuickJSNativeNumber(QuickJSNativeEngine * engine,JSValue value)18 QuickJSNativeNumber::QuickJSNativeNumber(QuickJSNativeEngine* engine, JSValue value) : QuickJSNativeValue(engine, value)
19 {
20 }
21
QuickJSNativeNumber(QuickJSNativeEngine * engine,int32_t value)22 QuickJSNativeNumber::QuickJSNativeNumber(QuickJSNativeEngine* engine, int32_t value)
23 : QuickJSNativeNumber(engine, JS_NewInt32(engine->GetContext(), value))
24 {
25 }
26
QuickJSNativeNumber(QuickJSNativeEngine * engine,uint32_t value)27 QuickJSNativeNumber::QuickJSNativeNumber(QuickJSNativeEngine* engine, uint32_t value)
28 : QuickJSNativeNumber(engine, JS_NewInt32(engine->GetContext(), value))
29 {
30 }
31
QuickJSNativeNumber(QuickJSNativeEngine * engine,int64_t value)32 QuickJSNativeNumber::QuickJSNativeNumber(QuickJSNativeEngine* engine, int64_t value)
33 : QuickJSNativeNumber(engine, JS_NewInt64(engine->GetContext(), value))
34 {
35 }
36
QuickJSNativeNumber(QuickJSNativeEngine * engine,double value)37 QuickJSNativeNumber::QuickJSNativeNumber(QuickJSNativeEngine* engine, double value)
38 : QuickJSNativeNumber(engine, JS_NewFloat64(engine->GetContext(), value))
39 {
40 }
41
~QuickJSNativeNumber()42 QuickJSNativeNumber::~QuickJSNativeNumber() {}
43
GetInterface(int interfaceId)44 void* QuickJSNativeNumber::GetInterface(int interfaceId)
45 {
46 return (NativeNumber::INTERFACE_ID == interfaceId) ? (NativeNumber*)this : nullptr;
47 }
48
operator int32_t()49 QuickJSNativeNumber::operator int32_t()
50 {
51 int32_t cValue = 0;
52 JS_ToInt32(engine_->GetContext(), &cValue, this->value_);
53 return cValue;
54 }
55
operator uint32_t()56 QuickJSNativeNumber::operator uint32_t()
57 {
58 uint32_t cValue = 0;
59 JS_ToUint32(engine_->GetContext(), &cValue, this->value_);
60 return cValue;
61 }
62
operator int64_t()63 QuickJSNativeNumber::operator int64_t()
64 {
65 int64_t cValue = 0;
66 JS_ToInt64(engine_->GetContext(), &cValue, this->value_);
67 return cValue;
68 }
69
operator double()70 QuickJSNativeNumber::operator double()
71 {
72 double cValue = 0;
73 JS_ToFloat64(engine_->GetContext(), &cValue, this->value_);
74 return cValue;
75 }
76