Skip to content

Commit bb5ccc1

Browse files
author
Mihail Slavchev
committed
refactor 'cast' checks (performance optimization)
1 parent e9165ce commit bb5ccc1

9 files changed

Lines changed: 336 additions & 431 deletions

src/jni/ArgConverter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "V8StringConstants.h"
77
#include "NativeScriptAssert.h"
88
#include "NativeScriptException.h"
9+
#include "NumericCasts.h"
910
#include "JType.h"
1011
#include <assert.h>
1112
#include <sstream>
@@ -25,7 +26,7 @@ void ArgConverter::Init(JavaVM *jvm)
2526
ft->SetClassName(V8StringConstants::GetLongNumber());
2627
ft->InstanceTemplate()->Set(V8StringConstants::GetValueOf(), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongValueOfFunctionCallback));
2728
ft->InstanceTemplate()->Set(V8StringConstants::GetToString(), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongToStringFunctionCallback));
28-
NATIVESCRIPT_NUMERA_CTOR_FUNC = new Persistent<Function>(isolate, ft->GetFunction());
29+
NATIVESCRIPT_NUMBER_CTOR_FUNC = new Persistent<Function>(isolate, ft->GetFunction());
2930

3031
auto nanObject = Number::New(isolate, numeric_limits<double>::quiet_NaN()).As<NumberObject>();
3132
NAN_NUMBER_OBJECT = new Persistent<NumberObject>(isolate, nanObject);
@@ -81,11 +82,10 @@ void ArgConverter::NativeScriptLongFunctionCallback(const v8::FunctionCallbackIn
8182
try
8283
{
8384
auto isolate = Isolate::GetCurrent();
84-
args.This()->SetHiddenValue(V8StringConstants::GetJavaLong(), Boolean::New(isolate, true));
85-
args.This()->SetHiddenValue(V8StringConstants::GetMarkedAsLong(), args[0]);
86-
args.This()->Set(V8StringConstants::GetValue(), args[0]);
87-
88-
args.This()->SetPrototype(Local<NumberObject>::New(Isolate::GetCurrent(), *NAN_NUMBER_OBJECT));
85+
auto thiz = args.This();
86+
thiz->SetHiddenValue(V8StringConstants::GetJavaLong(), Boolean::New(isolate, true));
87+
NumericCasts::MarkAsLong(thiz, args[0]);
88+
thiz->SetPrototype(Local<NumberObject>::New(Isolate::GetCurrent(), *NAN_NUMBER_OBJECT));
8989
}
9090
catch (NativeScriptException& e)
9191
{
@@ -265,7 +265,7 @@ Local<Value> ArgConverter::ConvertFromJavaLong(jlong value)
265265
char strNumber[24];
266266
sprintf(strNumber, "%lld", longValue);
267267
Local<Value> strValue = ConvertToV8String(strNumber);
268-
convertedValue = Local<Function>::New(isolate, *NATIVESCRIPT_NUMERA_CTOR_FUNC)->CallAsConstructor(1, &strValue);
268+
convertedValue = Local<Function>::New(isolate, *NATIVESCRIPT_NUMBER_CTOR_FUNC)->CallAsConstructor(1, &strValue);
269269
}
270270

271271
return convertedValue;
@@ -317,6 +317,6 @@ bool ArgConverter::TryConvertToJavaLong(const Local<Value>& value, jlong& javaLo
317317
}
318318

319319
JavaVM* ArgConverter::jvm = nullptr;
320-
Persistent<Function>* ArgConverter::NATIVESCRIPT_NUMERA_CTOR_FUNC = nullptr;
320+
Persistent<Function>* ArgConverter::NATIVESCRIPT_NUMBER_CTOR_FUNC = nullptr;
321321
Persistent<NumberObject>* ArgConverter::NAN_NUMBER_OBJECT = nullptr;
322322
char* ArgConverter::charBuffer = new char[ArgConverter::BUFFER_SIZE];

src/jni/ArgConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace tns
4949

5050
static const long long JS_LONG_LIMIT = ((long long) 1) << 53;
5151

52-
static v8::Persistent<v8::Function> *NATIVESCRIPT_NUMERA_CTOR_FUNC;
52+
static v8::Persistent<v8::Function> *NATIVESCRIPT_NUMBER_CTOR_FUNC;
5353
static v8::Persistent<v8::NumberObject> *NAN_NUMBER_OBJECT;
5454

5555
static char *charBuffer;

src/jni/JsArgConverter.cpp

Lines changed: 107 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "Util.h"
1010
#include "V8GlobalHelpers.h"
1111
#include "V8StringConstants.h"
12+
#include "NumericCasts.h"
13+
#include "NativeScriptException.h"
1214

1315
using namespace v8;
1416
using namespace std;
@@ -110,113 +112,120 @@ bool JsArgConverter::ConvertArg(const Local<Value>& arg, int index)
110112
jlong javaLongValue;
111113
auto jsObject = arg->ToObject();
112114

113-
auto hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsLong());
114-
if (!hidden.IsEmpty())
115-
{
116-
if (hidden->IsString())
117-
{
118-
string strValue = ConvertToString(hidden->ToString());
119-
int64_t longArg = atoll(strValue.c_str());
120-
jlong value = (jlong) longArg;
121-
success = ConvertFromCastFunctionObject(value, index);
122-
}
123-
else if (hidden->IsInt32())
124-
{
125-
jlong value = (jlong) hidden->ToInt32()->IntegerValue();
126-
success = ConvertFromCastFunctionObject(value, index);
127-
}
128-
return success;
129-
}
130-
else if (ArgConverter::TryConvertToJavaLong(jsObject, javaLongValue))
131-
{
132-
m_args[index].j = javaLongValue;
133-
success = true;
134-
return success;
135-
}
115+
auto castType = NumericCasts::GetCastType(jsObject);
136116

137-
hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsByte());
138-
if (!hidden.IsEmpty())
139-
{
140-
if (hidden->IsString())
141-
{
142-
string strValue = ConvertToString(hidden->ToString());
143-
int byteArg = atoi(strValue.c_str());
144-
jbyte value = (jbyte) byteArg;
145-
success = ConvertFromCastFunctionObject(value, index);
146-
}
147-
else if (hidden->IsInt32())
148-
{
149-
jbyte value = (jbyte) hidden->ToInt32()->Int32Value();
150-
success = ConvertFromCastFunctionObject(value, index);
151-
}
152-
return success;
153-
}
117+
Local<Value> castValue;
118+
JniLocalRef obj;
154119

155-
hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsShort());
156-
if (!hidden.IsEmpty())
120+
switch (castType)
157121
{
158-
if (hidden->IsString())
159-
{
160-
string strValue = ConvertToString(hidden->ToString());
161-
int shortArg = atoi(strValue.c_str());
162-
jshort value = (jshort) shortArg;
163-
success = ConvertFromCastFunctionObject(value, index);
164-
}
165-
else if (hidden->IsInt32())
166-
{
167-
jshort value = (jshort) hidden->ToInt32()->Int32Value();
168-
success = ConvertFromCastFunctionObject(value, index);
169-
}
170-
return success;
171-
}
122+
case CastType::Char:
123+
castValue = NumericCasts::GetCastValue(jsObject);
124+
if (castValue->IsString())
125+
{
126+
string value = ConvertToString(castValue->ToString());
127+
m_args[index].c = (jchar) value[0];
128+
success = true;
129+
}
130+
break;
172131

173-
hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsDouble());
174-
if (!hidden.IsEmpty())
175-
{
176-
if (hidden->IsNumber())
177-
{
178-
double doubleArg = hidden->ToNumber()->NumberValue();
179-
jdouble value = (jdouble) doubleArg;
180-
success = ConvertFromCastFunctionObject(value, index);
181-
}
182-
return success;
183-
}
132+
case CastType::Byte:
133+
castValue = NumericCasts::GetCastValue(jsObject);
134+
if (castValue->IsString())
135+
{
136+
string strValue = ConvertToString(castValue->ToString());
137+
int byteArg = atoi(strValue.c_str());
138+
jbyte value = (jbyte) byteArg;
139+
success = ConvertFromCastFunctionObject(value, index);
140+
}
141+
else if (castValue->IsInt32())
142+
{
143+
jbyte value = (jbyte) castValue->ToInt32()->Int32Value();
144+
success = ConvertFromCastFunctionObject(value, index);
145+
}
146+
break;
184147

185-
hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsFloat());
186-
if (!hidden.IsEmpty())
187-
{
188-
if (hidden->IsNumber())
189-
{
190-
double floatArg = hidden->ToNumber()->NumberValue();
191-
jfloat value = (jfloat) floatArg;
192-
success = ConvertFromCastFunctionObject(value, index);
193-
}
194-
return success;
195-
}
148+
case CastType::Short:
149+
castValue = NumericCasts::GetCastValue(jsObject);
150+
if (castValue->IsString())
151+
{
152+
string strValue = ConvertToString(castValue->ToString());
153+
int shortArg = atoi(strValue.c_str());
154+
jshort value = (jshort) shortArg;
155+
success = ConvertFromCastFunctionObject(value, index);
156+
}
157+
else if (castValue->IsInt32())
158+
{
159+
jshort value = (jshort) castValue->ToInt32()->Int32Value();
160+
success = ConvertFromCastFunctionObject(value, index);
161+
}
162+
break;
196163

197-
hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsChar());
198-
if (!hidden.IsEmpty())
199-
{
200-
if (hidden->IsString())
201-
{
202-
string value = ConvertToString(hidden->ToString());
203-
m_args[index].c = (jchar) value[0];
204-
success = true;
205-
}
206-
return success;
207-
}
164+
case CastType::Long:
165+
castValue = NumericCasts::GetCastValue(jsObject);
166+
if (castValue->IsString())
167+
{
168+
string strValue = ConvertToString(castValue->ToString());
169+
int64_t longArg = atoll(strValue.c_str());
170+
jlong value = (jlong) longArg;
171+
success = ConvertFromCastFunctionObject(value, index);
172+
}
173+
else if (castValue->IsInt32())
174+
{
175+
jlong value = (jlong) castValue->ToInt32()->IntegerValue();
176+
success = ConvertFromCastFunctionObject(value, index);
177+
}
178+
break;
179+
180+
case CastType::Float:
181+
castValue = NumericCasts::GetCastValue(jsObject);
182+
if (castValue->IsNumber())
183+
{
184+
double floatArg = castValue->ToNumber()->NumberValue();
185+
jfloat value = (jfloat) floatArg;
186+
success = ConvertFromCastFunctionObject(value, index);
187+
}
188+
break;
208189

209-
auto obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject);
210-
success = !obj.IsNull();
190+
case CastType::Double:
191+
castValue = NumericCasts::GetCastValue(jsObject);
192+
if (castValue->IsNumber())
193+
{
194+
double doubleArg = castValue->ToNumber()->NumberValue();
195+
jdouble value = (jdouble) doubleArg;
196+
success = ConvertFromCastFunctionObject(value, index);
197+
}
198+
break;
211199

212-
if (success)
213-
{
214-
SetConvertedObject(index, obj.Move(), obj.IsGlobal());
215-
}
216-
else
217-
{
218-
sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index);
200+
201+
case CastType::None:
202+
obj = ObjectManager::GetJavaObjectByJsObjectStatic(jsObject);
203+
success = !obj.IsNull();
204+
205+
if (success)
206+
{
207+
SetConvertedObject(index, obj.Move(), obj.IsGlobal());
208+
}
209+
else
210+
{
211+
sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index);
212+
}
213+
break;
214+
215+
default:
216+
throw NativeScriptException("Unsupported cast type");
219217
}
218+
219+
// auto hidden = jsObject->GetHiddenValue(V8StringConstants::GetMarkedAsLong());
220+
// if (!hidden.IsEmpty())
221+
// {
222+
// }
223+
// else if (ArgConverter::TryConvertToJavaLong(jsObject, javaLongValue))
224+
// {
225+
// m_args[index].j = javaLongValue;
226+
// success = true;
227+
// return success;
228+
// }
220229
}
221230
else if (arg->IsUndefined() || arg->IsNull())
222231
{

0 commit comments

Comments
 (0)