-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathJSIV8ValueConverter.cpp
More file actions
176 lines (157 loc) · 6.16 KB
/
JSIV8ValueConverter.cpp
File metadata and controls
176 lines (157 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) Kudo Chien.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "JSIV8ValueConverter.h"
#include "V8PointerValue.h"
namespace jsi = facebook::jsi;
namespace rnv8 {
// static
jsi::Value JSIV8ValueConverter::ToJSIValue(v8::Isolate* isolate,
const v8::Local<v8::Value>& value) {
v8::HandleScope scopedHandle(isolate);
if (value->IsUndefined()) {
return jsi::Value::undefined();
}
if (value->IsNull()) {
return jsi::Value::null();
}
if (value->IsBoolean()) {
return jsi::Value(value->BooleanValue(isolate));
}
if (value->IsNumber()) {
return jsi::Value(
value->NumberValue(isolate->GetCurrentContext()).ToChecked());
}
if (value->IsString()) {
return V8Runtime::make<jsi::String>(new V8PointerValue(isolate, value));
}
if (value->IsSymbol()) {
return V8Runtime::make<jsi::Symbol>(new V8PointerValue(isolate, value));
}
if (value->IsObject()) {
return V8Runtime::make<jsi::Object>(new V8PointerValue(isolate, value));
}
return jsi::Value::undefined();
}
// static
v8::Local<v8::Value> JSIV8ValueConverter::ToV8Value(const V8Runtime& runtime,
const jsi::Value& value) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
if (value.isUndefined()) {
return scopedHandle.Escape(v8::Undefined(runtime.isolate_));
} else if (value.isNull()) {
return scopedHandle.Escape(v8::Null(runtime.isolate_));
} else if (value.isBool()) {
return scopedHandle.Escape(
v8::Boolean::New(runtime.isolate_, std::move(value.getBool())));
} else if (value.isNumber()) {
return scopedHandle.Escape(
v8::Number::New(runtime.isolate_, std::move(value.getNumber())));
} else if (value.isString()) {
return scopedHandle.Escape(ToV8String(
runtime, std::move(value.getString(const_cast<V8Runtime&>(runtime)))));
} else if (value.isObject()) {
return scopedHandle.Escape(ToV8Object(
runtime, std::move(value.getObject(const_cast<V8Runtime&>(runtime)))));
} else {
// What are you?
std::abort();
}
}
// static
v8::Local<v8::String> JSIV8ValueConverter::ToV8String(
const V8Runtime& runtime, const jsi::String& string) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(string));
assert(v8PointerValue->Get(runtime.isolate_)->IsString());
return scopedHandle.Escape(
v8::Local<v8::String>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
v8::Local<v8::String> JSIV8ValueConverter::ToV8String(
const V8Runtime& runtime, const jsi::PropNameID& propName) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(propName));
assert(v8PointerValue->Get(runtime.isolate_)->IsString());
return scopedHandle.Escape(
v8::Local<v8::String>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
v8::MaybeLocal<v8::String> JSIV8ValueConverter::ToV8String(
const V8Runtime& runtime,
const std::shared_ptr<const jsi::Buffer>& buffer) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
v8::MaybeLocal<v8::String> ret = v8::String::NewFromUtf8(
runtime.isolate_, reinterpret_cast<const char*>(buffer->data()),
v8::NewStringType::kNormal, (int)buffer->size());
return scopedHandle.EscapeMaybe(ret);
}
// static
v8::Local<v8::Symbol> JSIV8ValueConverter::ToV8Symbol(
const V8Runtime& runtime, const jsi::Symbol& symbol) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(symbol));
assert(v8PointerValue->Get(runtime.isolate_)->IsSymbol());
return scopedHandle.Escape(
v8::Local<v8::Symbol>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
v8::Local<v8::Object> JSIV8ValueConverter::ToV8Object(
const V8Runtime& runtime, const jsi::Object& object) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(object));
assert(v8PointerValue->Get(runtime.isolate_)->IsObject());
return scopedHandle.Escape(
v8::Local<v8::Object>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
v8::Local<v8::Array> JSIV8ValueConverter::ToV8Array(const V8Runtime& runtime,
const jsi::Array& array) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(array));
assert(v8PointerValue->Get(runtime.isolate_)->IsArray());
return scopedHandle.Escape(
v8::Local<v8::Array>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
v8::Local<v8::Function> JSIV8ValueConverter::ToV8Function(
const V8Runtime& runtime, const jsi::Function& function) {
v8::EscapableHandleScope scopedHandle(runtime.isolate_);
const V8PointerValue* v8PointerValue =
static_cast<const V8PointerValue*>(runtime.getPointerValue(function));
assert(v8PointerValue->Get(runtime.isolate_)->IsFunction());
return scopedHandle.Escape(
v8::Local<v8::Function>::Cast(v8PointerValue->Get(runtime.isolate_)));
}
// static
jsi::PropNameID JSIV8ValueConverter::ToJSIPropNameID(
const V8Runtime& runtime, const v8::Local<v8::Name>& property) {
v8::HandleScope scopedHandle(runtime.isolate_);
return runtime.make<jsi::PropNameID>(
new V8PointerValue(runtime.isolate_, property));
}
// static
std::string JSIV8ValueConverter::ToSTLString(
const v8::String::Utf8Value& string) {
if (*string) {
return std::string(*string, string.length());
}
return {};
}
// static
std::string JSIV8ValueConverter::ToSTLString(
v8::Isolate* isolate, const v8::Local<v8::Value>& string) {
v8::HandleScope scopedHandle(isolate);
assert(string->IsString());
v8::String::Utf8Value utf8(isolate, string);
return ToSTLString(utf8);
}
} // namespace rnv8