Skip to content

Commit bb62ef0

Browse files
authored
Merge pull request #541 from NativeScript/pete/refactor-statics
Refactor static class members
2 parents a9ee29e + 9763cb5 commit bb62ef0

53 files changed

Lines changed: 829 additions & 1102 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

runtime/src/main/java/com/tns/Runtime.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.lang.reflect.Array;
88
import java.lang.reflect.Constructor;
99
import java.lang.reflect.Field;
10-
import java.lang.reflect.InvocationTargetException;
1110
import java.lang.reflect.Method;
1211
import java.lang.reflect.Modifier;
1312
import java.util.ArrayList;

runtime/src/main/jni/ArgConverter.cpp

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
#include "ArgConverter.h"
22
#include "ObjectManager.h"
3-
#include "JniLocalRef.h"
43
#include "Util.h"
5-
#include "V8GlobalHelpers.h"
64
#include "V8StringConstants.h"
7-
#include "NativeScriptAssert.h"
85
#include "NativeScriptException.h"
96
#include "NumericCasts.h"
10-
#include "JType.h"
117
#include "Runtime.h"
12-
#include <assert.h>
138
#include <sstream>
14-
#include <cstdlib>
159

1610
using namespace v8;
1711
using namespace std;
1812
using namespace tns;
1913

2014
void ArgConverter::Init(Isolate *isolate)
2115
{
22-
auto cache = GetCache(isolate);
16+
auto cache = GetTypeLongCache(isolate);
2317

2418
auto ft = FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongFunctionCallback);
25-
ft->SetClassName(V8StringConstants::GetLongNumber());
26-
ft->InstanceTemplate()->Set(V8StringConstants::GetValueOf(), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongValueOfFunctionCallback));
27-
ft->InstanceTemplate()->Set(V8StringConstants::GetToString(), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongToStringFunctionCallback));
19+
ft->SetClassName(V8StringConstants::GetLongNumber(isolate));
20+
ft->InstanceTemplate()->Set(V8StringConstants::GetValueOf(isolate), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongValueOfFunctionCallback));
21+
ft->InstanceTemplate()->Set(V8StringConstants::GetToString(isolate), FunctionTemplate::New(isolate, ArgConverter::NativeScriptLongToStringFunctionCallback));
2822
cache->LongNumberCtorFunc = new Persistent<Function>(isolate, ft->GetFunction());
2923

3024
auto nanObject = Number::New(isolate, numeric_limits<double>::quiet_NaN()).As<NumberObject>();
@@ -58,7 +52,8 @@ void ArgConverter::NativeScriptLongToStringFunctionCallback(const v8::FunctionCa
5852
{
5953
try
6054
{
61-
args.GetReturnValue().Set(args.This()->Get(V8StringConstants::GetValue()));
55+
auto isolate = args.GetIsolate();
56+
args.GetReturnValue().Set(args.This()->Get(V8StringConstants::GetValue(isolate)));
6257
}
6358
catch (NativeScriptException& e)
6459
{
@@ -82,8 +77,8 @@ void ArgConverter::NativeScriptLongFunctionCallback(const v8::FunctionCallbackIn
8277
{
8378
auto isolate = args.GetIsolate();
8479
auto thiz = args.This();
85-
auto cache = GetCache(isolate);
86-
thiz->SetHiddenValue(V8StringConstants::GetJavaLong(), Boolean::New(isolate, true));
80+
auto cache = GetTypeLongCache(isolate);
81+
thiz->SetHiddenValue(V8StringConstants::GetJavaLong(isolate), Boolean::New(isolate, true));
8782
NumericCasts::MarkAsLong(thiz, args[0]);
8883
thiz->SetPrototype(Local<NumberObject>::New(isolate, *cache->NanNumberObject));
8984
}
@@ -125,7 +120,6 @@ Local<Array> ArgConverter::ConvertJavaArgsToJsArgs(Isolate *isolate, jobjectArra
125120
JniLocalRef arg(env.GetObjectArrayElement(args, jArrayIndex++));
126121
JniLocalRef argJavaClassPath(env.GetObjectArrayElement(args, jArrayIndex++));
127122

128-
jint length;
129123
Type argTypeID = (Type) JType::IntValue(env, argTypeIDObj);
130124

131125
Local<Value> jsArg;
@@ -135,7 +129,7 @@ Local<Array> ArgConverter::ConvertJavaArgsToJsArgs(Isolate *isolate, jobjectArra
135129
jsArg = Boolean::New(isolate, JType::BooleanValue(env, arg));
136130
break;
137131
case Type::Char:
138-
jsArg = jcharToV8String(JType::CharValue(env, arg));
132+
jsArg = jcharToV8String(isolate, JType::CharValue(env, arg));
139133
break;
140134
case Type::Byte:
141135
jsArg = Number::New(isolate, JType::ByteValue(env, arg));
@@ -156,7 +150,7 @@ Local<Array> ArgConverter::ConvertJavaArgsToJsArgs(Isolate *isolate, jobjectArra
156150
jsArg = Number::New(isolate, JType::DoubleValue(env, arg));
157151
break;
158152
case Type::String:
159-
jsArg = jstringToV8String((jstring) arg);
153+
jsArg = jstringToV8String(isolate, (jstring) arg);
160154
break;
161155
case Type::JsObject:
162156
{
@@ -207,17 +201,17 @@ std::string ArgConverter::jstringToString(jstring value)
207201
return s;
208202
}
209203

210-
Local<Value> ArgConverter::jstringToV8String(jstring value)
204+
Local<Value> ArgConverter::jstringToV8String(Isolate *isolate, jstring value)
211205
{
212206
if (value == nullptr)
213207
{
214-
return Null(Isolate::GetCurrent());
208+
return Null(isolate);
215209
}
216210

217211
JEnv env;
218212
auto chars = env.GetStringChars(value, NULL);
219213
auto length = env.GetStringLength(value);
220-
auto v8String = ConvertToV8String(chars, length);
214+
auto v8String = ConvertToV8String(isolate, chars, length);
221215
env.ReleaseStringChars(value, chars);
222216

223217
return v8String;
@@ -244,9 +238,9 @@ bool ArgConverter::ReadJStringInBuffer(jstring value, jsize& utfLength)
244238
return true;
245239
}
246240

247-
Local<String> ArgConverter::jcharToV8String(jchar value)
241+
Local<String> ArgConverter::jcharToV8String(Isolate *isolate, jchar value)
248242
{
249-
auto v8String = ConvertToV8String(&value, 1);
243+
auto v8String = ConvertToV8String(isolate, &value, 1);
250244
return v8String;
251245
}
252246

@@ -261,25 +255,25 @@ Local<Value> ArgConverter::ConvertFromJavaLong(Isolate *isolate, jlong value)
261255
}
262256
else
263257
{
264-
auto cache = GetCache(isolate);
258+
auto cache = GetTypeLongCache(isolate);
265259
char strNumber[24];
266260
sprintf(strNumber, "%lld", longValue);
267-
Local<Value> strValue = ConvertToV8String(strNumber);
261+
Local<Value> strValue = ConvertToV8String(isolate, strNumber);
268262
convertedValue = Local<Function>::New(isolate, *cache->LongNumberCtorFunc)->CallAsConstructor(1, &strValue);
269263
}
270264

271265
return convertedValue;
272266
}
273267

274-
int64_t ArgConverter::ConvertToJavaLong(const Local<Value>& value)
268+
int64_t ArgConverter::ConvertToJavaLong(Isolate *isolate, const Local<Value>& value)
275269
{
276270
assert(!value.IsEmpty());
277271

278272
auto obj = Local<Object>::Cast(value);
279273

280274
assert(!obj.IsEmpty());
281275

282-
auto valueProp = obj->Get(V8StringConstants::GetValue());
276+
auto valueProp = obj->Get(V8StringConstants::GetValue(isolate));
283277

284278
assert(!valueProp.IsEmpty());
285279

@@ -290,21 +284,61 @@ int64_t ArgConverter::ConvertToJavaLong(const Local<Value>& value)
290284
return longValue;
291285
}
292286

293-
ArgConverter::Cache* ArgConverter::GetCache(v8::Isolate *isolate)
287+
ArgConverter::TypeLongOperationsCache * ArgConverter::GetTypeLongCache(v8::Isolate *isolate)
294288
{
295-
Cache *cache;
296-
auto itFound = s_cache.find(isolate);
297-
if (itFound == s_cache.end())
289+
TypeLongOperationsCache *cache;
290+
auto itFound = s_type_long_operations_cache.find(isolate);
291+
if (itFound == s_type_long_operations_cache.end())
298292
{
299-
cache = new Cache;
300-
s_cache.insert(make_pair(isolate, cache));
293+
cache = new TypeLongOperationsCache;
294+
s_type_long_operations_cache.insert(make_pair(isolate, cache));
301295
}
302296
else
303297
{
304298
cache = itFound->second;
305299
}
300+
306301
return cache;
307302
}
308303

309-
std::map<Isolate*, ArgConverter::Cache*> ArgConverter::s_cache;
304+
305+
string ArgConverter::ConvertToString(const v8::Local<String>& s)
306+
{
307+
if (s.IsEmpty())
308+
{
309+
return string();
310+
}
311+
else
312+
{
313+
String::Utf8Value str(s);
314+
return string(*str);
315+
}
316+
}
317+
318+
jstring ArgConverter::ConvertToJavaString(const Local<Value>& value)
319+
{
320+
JEnv env;
321+
String::Value stringValue(value);
322+
return env.NewString((const jchar*) *stringValue, stringValue.length());
323+
}
324+
325+
Local<String> ArgConverter::ConvertToV8String(Isolate *isolate, const jchar* data, int length)
326+
{
327+
return String::NewFromTwoByte(isolate, (const uint16_t*) data, String::kNormalString, length);
328+
}
329+
330+
Local<String> ArgConverter::ConvertToV8String(Isolate *isolate, const string& s)
331+
{
332+
Local<String> str;
333+
String::NewFromUtf8(isolate, s.c_str(), NewStringType::kNormal, s.length()).ToLocal(&str);
334+
return str;
335+
}
336+
337+
Local<String> ArgConverter::ConvertToV8String(Isolate *isolate, const char *data, int length)
338+
{
339+
return String::NewFromUtf8(isolate, (const char *) data, String::kNormalString, length);
340+
}
341+
342+
343+
std::map<Isolate*, ArgConverter::TypeLongOperationsCache *> ArgConverter::s_type_long_operations_cache;
310344
char* ArgConverter::charBuffer = new char[ArgConverter::BUFFER_SIZE];

runtime/src/main/jni/ArgConverter.h

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,57 @@ namespace tns
2424

2525
static v8::Local<v8::Value> ConvertFromJavaLong(v8::Isolate *isolate, jlong value);
2626

27-
static int64_t ConvertToJavaLong(const v8::Local<v8::Value>& value);
27+
static int64_t ConvertToJavaLong(v8::Isolate *isolate, const v8::Local<v8::Value>& value);
2828

29-
static v8::Local<v8::Value> jstringToV8String(jstring value);
29+
static v8::Local<v8::Value> jstringToV8String(v8::Isolate *isolate, jstring value);
3030

3131
static std::string jstringToString(jstring value);
3232

33-
private:
34-
struct Cache;
35-
36-
static Cache* GetCache(v8::Isolate *isolate);
33+
static std::string ConvertToString(const v8::Local<v8::String>& s);
3734

38-
static bool ReadJStringInBuffer(jstring value, jsize& utfLength);
35+
static jstring ConvertToJavaString(const v8::Local<v8::Value>& jsValue);
3936

40-
static jstring ObjectToString(jobject object);
37+
static v8::Local<v8::String> ConvertToV8String(v8::Isolate *isolate, const jchar* data, int length);
4138

42-
static v8::Local<v8::String> jcharToV8String(jchar value);
39+
static v8::Local<v8::String> ConvertToV8String(v8::Isolate *isolate, const std::string& s);
4340

44-
static void NativeScriptLongFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
41+
static v8::Local<v8::String> ConvertToV8String(v8::Isolate *isolate, const char *data, int length);
4542

46-
static void NativeScriptLongValueOfFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
47-
48-
static void NativeScriptLongToStringFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
43+
private:
4944

45+
// TODO: plamen5kov: rewrite logic for java long number operations in javascript (java long -> javascript number operations check)
5046
static const long long JS_LONG_LIMIT = ((long long) 1) << 53;
5147

52-
struct Cache
48+
struct TypeLongOperationsCache
5349
{
5450
v8::Persistent<v8::Function> *LongNumberCtorFunc;
5551

5652
v8::Persistent<v8::NumberObject> *NanNumberObject;
5753
};
54+
//
55+
56+
static TypeLongOperationsCache *GetTypeLongCache(v8::Isolate *isolate);
57+
58+
static bool ReadJStringInBuffer(jstring value, jsize& utfLength);
59+
60+
static jstring ObjectToString(jobject object);
61+
62+
static v8::Local<v8::String> jcharToV8String(v8::Isolate *isolate, jchar value);
63+
64+
static void NativeScriptLongFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
65+
66+
static void NativeScriptLongValueOfFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
67+
68+
static void NativeScriptLongToStringFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
5869

5970
static char *charBuffer;
6071
static const int BUFFER_SIZE = 1024 * 64; // 64KB size. TODO: Do we need a larger/smaller buffer?
61-
static std::map<v8::Isolate*, Cache*> s_cache;
72+
73+
/*
74+
* "s_type_long_operations_cache" used to keep function
75+
* dealing with operations concerning java long -> javascript number.
76+
*/
77+
static std::map<v8::Isolate*, TypeLongOperationsCache *> s_type_long_operations_cache;
6278
};
6379
}
6480

runtime/src/main/jni/ArrayBufferHelper.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "ArrayBufferHelper.h"
2-
#include "JEnv.h"
3-
#include "V8GlobalHelpers.h"
2+
#include "ArgConverter.h"
43
#include "NativeScriptException.h"
54
#include <sstream>
65

@@ -16,12 +15,11 @@ ArrayBufferHelper::ArrayBufferHelper()
1615
void ArrayBufferHelper::CreateConvertFunctions(Isolate *isolate, const Local<Object>& global, ObjectManager *objectManager)
1716
{
1817
m_objectManager = objectManager;
19-
2018
auto extData = External::New(isolate, this);
2119
auto fromFunc = FunctionTemplate::New(isolate, CreateFromCallbackStatic, extData)->GetFunction();
2220
auto ctx = isolate->GetCurrentContext();
23-
auto arrBufferCtorFunc = global->Get(ConvertToV8String("ArrayBuffer")).As<Function>();
24-
arrBufferCtorFunc->Set(ctx, ConvertToV8String("from"), fromFunc);
21+
auto arrBufferCtorFunc = global->Get(ArgConverter::ConvertToV8String(isolate, "ArrayBuffer")).As<Function>();
22+
arrBufferCtorFunc->Set(ctx, ArgConverter::ConvertToV8String(isolate, "from"), fromFunc);
2523
}
2624

2725
void ArrayBufferHelper::CreateFromCallbackStatic(const FunctionCallbackInfo<Value>& info)
@@ -109,7 +107,7 @@ void ArrayBufferHelper::CreateFromCallbackImpl(const FunctionCallbackInfo<Value>
109107

110108
auto arrayBuffer = ArrayBuffer::New(isolate, data, size);
111109
auto ctx = isolate->GetCurrentContext();
112-
arrayBuffer->Set(ctx, ConvertToV8String("nativeObject"), argObj);
110+
arrayBuffer->Set(ctx, ArgConverter::ConvertToV8String(isolate, "nativeObject"), argObj);
113111

114112
info.GetReturnValue().Set(arrayBuffer);
115113
}

runtime/src/main/jni/ArrayElementAccessor.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#include "ArrayElementAccessor.h"
22
#include "JsArgToArrayConverter.h"
33
#include "ArgConverter.h"
4-
#include "JniLocalRef.h"
54
#include "Util.h"
6-
#include "V8GlobalHelpers.h"
7-
#include "NativeScriptAssert.h"
85
#include "NativeScriptException.h"
9-
#include "JType.h"
106
#include "Runtime.h"
117

128
using namespace v8;
@@ -151,7 +147,7 @@ void ArrayElementAccessor::SetArrayElement(Isolate *isolate, const Local<Object>
151147
jlong longElementValue;
152148
if (value->IsObject())
153149
{
154-
longElementValue = (jlong) ArgConverter::ConvertToJavaLong(value);
150+
longElementValue = (jlong) ArgConverter::ConvertToJavaLong(isolate, value);
155151
}
156152
else
157153
{
@@ -213,7 +209,7 @@ Local<Value> ArrayElementAccessor::ConvertToJsValue(Isolate *isolate, ObjectMana
213209
}
214210
else if (elementSignature == "C")
215211
{
216-
jsValue = ConvertToV8String((const char*) value, 1);
212+
jsValue = ArgConverter::ConvertToV8String(isolate, (const char*) value, 1);
217213
}
218214
else if (elementSignature == "S")
219215
{
@@ -243,7 +239,7 @@ Local<Value> ArrayElementAccessor::ConvertToJsValue(Isolate *isolate, ObjectMana
243239

244240
if (isString)
245241
{
246-
jsValue = ArgConverter::jstringToV8String(*(jstring*) value);
242+
jsValue = ArgConverter::jstringToV8String(isolate, *(jstring*) value);
247243
}
248244
else
249245
{

0 commit comments

Comments
 (0)