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 "v8_native_number.h"
17
18 #include <math.h>
19
V8NativeNumber(V8NativeEngine * engine,v8::Local<v8::Value> value)20 V8NativeNumber::V8NativeNumber(V8NativeEngine* engine, v8::Local<v8::Value> value) : V8NativeValue(engine, value) {}
21
V8NativeNumber(V8NativeEngine * engine,int32_t value)22 V8NativeNumber::V8NativeNumber(V8NativeEngine* engine, int32_t value)
23 : V8NativeNumber(engine, v8::Number::New(engine->GetIsolate(), value))
24 {
25 }
26
V8NativeNumber(V8NativeEngine * engine,uint32_t value)27 V8NativeNumber::V8NativeNumber(V8NativeEngine* engine, uint32_t value)
28 : V8NativeNumber(engine, v8::Number::New(engine->GetIsolate(), value))
29 {
30 }
31
V8NativeNumber(V8NativeEngine * engine,int64_t value)32 V8NativeNumber::V8NativeNumber(V8NativeEngine* engine, int64_t value)
33 : V8NativeNumber(engine, v8::Number::New(engine->GetIsolate(), value))
34 {
35 }
36
V8NativeNumber(V8NativeEngine * engine,double value)37 V8NativeNumber::V8NativeNumber(V8NativeEngine* engine, double value)
38 : V8NativeNumber(engine, v8::Number::New(engine->GetIsolate(), value))
39 {
40 }
41
~V8NativeNumber()42 V8NativeNumber::~V8NativeNumber() {}
43
GetInterface(int interfaceId)44 void* V8NativeNumber::GetInterface(int interfaceId)
45 {
46 return (NativeNumber::INTERFACE_ID == interfaceId) ? (NativeNumber*)this : nullptr;
47 }
48
operator int32_t()49 V8NativeNumber::operator int32_t()
50 {
51 v8::Local<v8::Value> value = value_;
52 int32_t result = 0;
53
54 if (value->IsInt32()) {
55 result = value.As<v8::Int32>()->Value();
56 }
57
58 double doubleValue = value.As<v8::Number>()->Value();
59 if (isfinite(doubleValue)) {
60 result = value->IntegerValue(engine_->GetContext()).FromJust();
61 } else {
62 result = 0;
63 }
64
65 return result;
66 }
67
operator uint32_t()68 V8NativeNumber::operator uint32_t()
69 {
70 v8::Local<v8::Value> value = value_;
71 uint32_t result = 0;
72
73 if (value->IsUint32()) {
74 result = value.As<v8::Uint32>()->Value();
75 } else {
76 result = value->Uint32Value(engine_->GetContext()).FromJust();
77 }
78
79 return result;
80 }
81
operator int64_t()82 V8NativeNumber::operator int64_t()
83 {
84 v8::Local<v8::Value> value = value_;
85 int64_t result = 0;
86
87 if (value->IsInt32()) {
88 result = value.As<v8::Int32>()->Value();
89 }
90
91 double doubleValue = value.As<v8::Number>()->Value();
92 if (isfinite(doubleValue)) {
93 result = value->IntegerValue(engine_->GetContext()).FromJust();
94 } else {
95 result = 0;
96 }
97
98 return result;
99 }
100
operator double()101 V8NativeNumber::operator double()
102 {
103 v8::Local<v8::Value> value = value_;
104 double result = 0.0;
105
106 result = value.As<v8::Number>()->Value();
107
108 return result;
109 }
110