Repository navigation
Expand file tree
/
Copy pathArgConverter.cpp
More file actions
221 lines (188 loc) · 7.91 KB
/
Copy pathArgConverter.cpp
File metadata and controls
221 lines (188 loc) · 7.91 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "ArgConverter.h"
#include "ObjectManager.h"
#include "Util.h"
#include "V8StringConstants.h"
#include "NativeScriptException.h"
#include "NumericCasts.h"
#include "Runtime.h"
#include "V8GlobalHelpers.h"
#include "NativeScriptAssert.h"
#include "RuntimeState.h"
#include <sstream>
using namespace v8;
using namespace std;
using namespace tns;
void ArgConverter::Init(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
auto cache = GetTypeLongCache(isolate);
auto ft = FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongFunctionCallback);
ft->SetClassName(V8StringConstants::GetLongNumber(isolate));
ft->InstanceTemplate()->Set(V8StringConstants::GetValueOf(isolate), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongValueOfFunctionCallback));
ft->InstanceTemplate()->Set(V8StringConstants::GetToString(isolate), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongToStringFunctionCallback));
cache->LongNumberCtorFunc = new Persistent<Function>(isolate, ft->GetFunction(context).ToLocalChecked());
auto nanObject = Number::New(isolate, numeric_limits<double>::quiet_NaN()).As<NumberObject>();
cache->NanNumberObject = new Persistent<NumberObject>(isolate, nanObject);
}
void ArgConverter::NativeScriptLongValueOfFunctionCallback(const v8::FunctionCallbackInfo<Value>& args) {
try {
args.GetReturnValue().Set(numeric_limits<double>::quiet_NaN());
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}
void ArgConverter::NativeScriptLongToStringFunctionCallback(const v8::FunctionCallbackInfo<Value>& args) {
try {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
Local<Value> result;
if (args.This()->Get(context, V8StringConstants::GetValue(isolate)).ToLocal(&result)) {
args.GetReturnValue().Set(result);
} else {
args.GetReturnValue().SetUndefined();
}
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}
void ArgConverter::NativeScriptLongFunctionCallback(const v8::FunctionCallbackInfo<Value>& args) {
try {
auto isolate = args.GetIsolate();
auto thiz = args.This();
auto cache = GetTypeLongCache(isolate);
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetJavaLong(isolate), Boolean::New(isolate, true));
NumericCasts::MarkAsLong(isolate, thiz, args[0]);
auto context = isolate->GetCurrentContext();
thiz->SetPrototype(context, Local<NumberObject>::New(isolate, *cache->NanNumberObject));
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}
Local<Array> ArgConverter::ConvertJavaArgsToJsArgs(Local<Context> context, jobjectArray args) {
JEnv env;
int argc = env.GetArrayLength(args) / 3;
auto isolate = v8::Isolate::GetCurrent();
Local<Array> arr(Array::New(isolate, argc));
auto runtime = Runtime::GetRuntime(isolate);
auto objectManager = runtime->GetObjectManager();
int jArrayIndex = 0;
for (int i = 0; i < argc; i++) {
JniLocalRef argTypeIDObj(env.GetObjectArrayElement(args, jArrayIndex++));
JniLocalRef arg(env.GetObjectArrayElement(args, jArrayIndex++));
JniLocalRef argJavaClassPath(env.GetObjectArrayElement(args, jArrayIndex++));
Type argTypeID = (Type) JType::IntValue(env, argTypeIDObj);
Local<Value> jsArg;
switch (argTypeID) {
case Type::Boolean:
jsArg = Boolean::New(isolate, JType::BooleanValue(env, arg));
break;
case Type::Char:
jsArg = jcharToV8String(isolate, JType::CharValue(env, arg));
break;
case Type::Byte:
jsArg = Number::New(isolate, JType::ByteValue(env, arg));
break;
case Type::Short:
jsArg = Number::New(isolate, JType::ShortValue(env, arg));
break;
case Type::Int:
jsArg = Number::New(isolate, JType::IntValue(env, arg));
break;
case Type::Long:
jsArg = Number::New(isolate, JType::LongValue(env, arg));
break;
case Type::Float:
jsArg = Number::New(isolate, JType::FloatValue(env, arg));
break;
case Type::Double:
jsArg = Number::New(isolate, JType::DoubleValue(env, arg));
break;
case Type::String:
jsArg = jstringToV8String(isolate, (jstring) arg);
break;
case Type::JsObject: {
jint javaObjectID = JType::IntValue(env, arg);
jsArg = objectManager->GetJsObjectByJavaObject(javaObjectID);
if (jsArg.IsEmpty()) {
string argClassName = jstringToString(ObjectToString(argJavaClassPath));
argClassName = Util::ConvertFromCanonicalToJniName(argClassName);
jsArg = objectManager->CreateJSWrapper(javaObjectID, argClassName);
}
break;
}
case Type::Null:
jsArg = Null(isolate);
break;
}
arr->Set(context, i, jsArg);
}
return arr;
}
Local<Value> ArgConverter::ConvertFromJavaLong(Isolate* isolate, jlong value) {
Local<Value> convertedValue;
long long longValue = value;
if ((-JS_LONG_LIMIT < longValue) && (longValue < JS_LONG_LIMIT)) {
convertedValue = Number::New(isolate, longValue);
} else {
auto cache = GetTypeLongCache(isolate);
char strNumber[24];
sprintf(strNumber, "%lld", longValue);
Local<Value> strValue = ConvertToV8String(isolate, strNumber);
auto context = isolate->GetCurrentContext();
convertedValue = Local<Function>::New(isolate, *cache->LongNumberCtorFunc)->CallAsConstructor(context, 1, &strValue).ToLocalChecked();
}
return convertedValue;
}
int64_t ArgConverter::ConvertToJavaLong(Isolate* isolate, const Local<Value>& value) {
NS_DCHECK(!value.IsEmpty());
auto obj = Local<Object>::Cast(value);
NS_DCHECK(!obj.IsEmpty());
auto context = isolate->GetCurrentContext();
Local<Value> temp;
bool success = obj->Get(context, V8StringConstants::GetValue(isolate)).ToLocal(&temp);
NS_DCHECK(success && !temp.IsEmpty());
auto valueProp = temp.As<Object>();
string num = ConvertToString(valueProp->ToString(context).ToLocalChecked());
int64_t longValue = atoll(num.c_str());
return longValue;
}
ArgConverter::TypeLongOperationsCache* ArgConverter::GetTypeLongCache(v8::Isolate* isolate) {
// Per runtime, so there is no shared table to race on; see RuntimeState.h.
auto* cache = RuntimeState::For<TypeLongOperationsCache>(isolate);
if (cache == nullptr) {
throw NativeScriptException("Long conversion cache requested after the runtime was torn down");
}
return cache;
}
u16string ArgConverter::ConvertToUtf16String(const v8::Local<String>& s) {
if (s.IsEmpty()) {
return {};
} else {
auto str = ConvertToString(s);
auto utf16str = Util::ConvertFromUtf8ToUtf16(str);
return utf16str;
}
}