Skip to content

Commit 2550988

Browse files
author
Mihail Slavchev
committed
Merge branch 'master' into slavchev/bytebuffer-support
2 parents 1f1ab6e + 99ac966 commit 2550988

2 files changed

Lines changed: 85 additions & 31 deletions

File tree

build/project-template-gradle/build.gradle

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ apply plugin: "com.android.application"
3333
def isWinOs = System.properties['os.name'].toLowerCase().contains('windows')
3434
def metadataParams = new LinkedList <String> ()
3535
def allJarPaths = new LinkedList <String> ()
36-
def configurationsDir = "configurations"
36+
def configurationsDir = "$projectDir/configurations"
3737
def createPluginConfigFile = false
3838
def configStage = "\n:config phase: "
3939
def nodeModulesDir = "../../node_modules/"
40-
def libDir = "../../lib/Android/"
40+
def libDir = "$projectDir/../../lib/Android/"
4141
def pluginNames = new ArrayList<String>()
4242
def configDir = file(configurationsDir)
4343
def appResExists = false
@@ -102,7 +102,7 @@ android {
102102
}
103103

104104
sourceSets.main {
105-
jniLibs.srcDir "libs/jni"
105+
jniLibs.srcDir "$projectDir/libs/jni"
106106
}
107107

108108
signingConfigs {
@@ -158,7 +158,7 @@ dependencies {
158158
compile "com.android.support:appcompat-v7:$suppotVer"
159159

160160
// take all jars within the libs dir
161-
compile fileTree(dir: "libs", include: ["**/*.jar"])
161+
compile fileTree(dir: "$projectDir/libs", include: ["**/*.jar"])
162162

163163
// take all jars within the node_modules dir
164164
compile fileTree(dir: nodeModulesDir, include: ["**/platforms/android/**/*.jar"], exclude: '**/.bin/**')
@@ -278,7 +278,7 @@ task createPluginsConfigFile {
278278
task pluginExtend {
279279
description "applies additional configuration"
280280

281-
def pathToAppGradle = "../../app/App_Resources/Android/app.gradle"
281+
def pathToAppGradle = "$projectDir/../../app/App_Resources/Android/app.gradle"
282282
def appGradle = file(pathToAppGradle)
283283
if(appGradle.exists()) {
284284
apply from: pathToAppGradle
@@ -310,13 +310,13 @@ task pluginExtend {
310310
task copyAarDependencies (type: Copy) {
311311
println "$configStage copyAarDependencies"
312312
from fileTree(dir: nodeModulesDir, include: ["**/*.aar"], exclude: '**/.bin/**').files
313-
into "libs/aar"
313+
into "$projectDir/libs/aar"
314314
}
315315

316316
task addAarDependencies << {
317317
println "$configStage addAarDependencies"
318318
// manually traverse all the locally copied AAR files and add them to the project compilation dependencies list
319-
FileTree tree = fileTree(dir: "libs/aar", include: ["**/*.aar"])
319+
FileTree tree = fileTree(dir: "$projectDir/libs/aar", include: ["**/*.aar"])
320320
tree.each { File file ->
321321
// remove the extension of the file (.aar)
322322
def length = file.name.length() - 4
@@ -332,11 +332,11 @@ task addAarDependencies << {
332332
////////////////////////////////////////////////////////////////////////////////////
333333

334334
task cleanLocalAarFiles(type: Delete) {
335-
delete fileTree(dir: "libs/aar", include: ["*.aar"])
335+
delete fileTree(dir: "$projectDir/libs/aar", include: ["*.aar"])
336336
}
337337

338338
task ensureMetadataOutDir {
339-
def outputDir = file("$rootDir/metadata/output/assets/metadata")
339+
def outputDir = file("$projectDir/metadata/output/assets/metadata")
340340
outputDir.mkdirs()
341341
}
342342

@@ -361,12 +361,12 @@ task collectAllJars {
361361
}
362362

363363
metadataParams.add("metadata-generator.jar")
364-
metadataParams.add("../metadata/output/assets/metadata")
364+
metadataParams.add("$projectDir/metadata/output/assets/metadata")
365365
for(def i = 0; i < allJarPaths.size(); i++) {
366366
metadataParams.add(allJarPaths.get(i));
367367
}
368368

369-
def classesDir = "$rootDir/build/intermediates/classes"
369+
def classesDir = "$buildDir/intermediates/classes"
370370

371371
def classesSubDirs = new File(classesDir).listFiles()
372372

@@ -393,7 +393,7 @@ task buildMetadata (type: JavaExec) {
393393
description "builds metadata with provided jar dependencies"
394394

395395
inputs.files(allJarPaths)
396-
inputs.dir("build/intermediates/classes")
396+
inputs.dir("$buildDir/intermediates/classes")
397397

398398
outputs.files("metadata/output/assets/metadata/treeNodeStream.dat", "metadata/output/assets/metadata/treeStringsStream.dat", "metadata/output/assets/metadata/treeValueStream.dat")
399399

@@ -407,8 +407,8 @@ task buildMetadata (type: JavaExec) {
407407

408408
doLast {
409409
copy {
410-
from "metadata/output/assets/metadata"
411-
into "src/main/assets/metadata"
410+
from "$projectDir/metadata/output/assets/metadata"
411+
into "$projectDir/src/main/assets/metadata"
412412
}
413413

414414
def files = new File("${buildDir}/intermediates/res").listFiles()
@@ -432,14 +432,14 @@ task buildMetadata (type: JavaExec) {
432432

433433
exec {
434434
ignoreExitValue true
435-
workingDir "metadata/output"
435+
workingDir "$projectDir/metadata/output"
436436
commandLine removeCmdParams.toArray()
437437
}
438438

439439
def addCmdParams = new ArrayList<String>([aaptCommand, "add", tmpAPKPath, "assets/metadata/treeNodeStream.dat", "assets/metadata/treeStringsStream.dat", "assets/metadata/treeValueStream.dat"])
440440

441441
exec {
442-
workingDir "metadata/output"
442+
workingDir "$projectDir/metadata/output"
443443
commandLine addCmdParams.toArray()
444444
}
445445
}

src/jni/com_tns_Runtime.cpp

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,44 @@ extern "C" void Java_com_tns_Runtime_initNativeScript(JNIEnv *_env, jobject obj,
5353
}
5454
}
5555

56+
Runtime* TryGetRuntime(int runtimeId)
57+
{
58+
Runtime *runtime = nullptr;
59+
try
60+
{
61+
runtime = Runtime::GetRuntime(runtimeId);
62+
}
63+
catch (NativeScriptException& e)
64+
{
65+
e.ReThrowToJava();
66+
}
67+
catch (std::exception e) {
68+
stringstream ss;
69+
ss << "Error: c++ exception: " << e.what() << endl;
70+
NativeScriptException nsEx(ss.str());
71+
nsEx.ReThrowToJava();
72+
}
73+
catch (...) {
74+
NativeScriptException nsEx(std::string("Error: c++ exception!"));
75+
nsEx.ReThrowToJava();
76+
}
77+
return runtime;
78+
}
79+
5680
extern "C" void Java_com_tns_Runtime_runModule(JNIEnv *_env, jobject obj, jint runtimeId, jstring scriptFile)
5781
{
58-
auto runtime = Runtime::GetRuntime(runtimeId);
82+
auto runtime = TryGetRuntime(runtimeId);
83+
if (runtime == nullptr)
84+
{
85+
return;
86+
}
87+
5988
auto isolate = runtime->GetIsolate();
6089
v8::Isolate::Scope isolate_scope(isolate);
6190
v8::HandleScope handleScope(isolate);
6291

6392
try
6493
{
65-
auto runtime = Runtime::GetRuntime(runtimeId);
6694
runtime->RunModule(_env, obj, scriptFile);
6795
}
6896
catch (NativeScriptException& e)
@@ -83,13 +111,18 @@ extern "C" void Java_com_tns_Runtime_runModule(JNIEnv *_env, jobject obj, jint r
83111

84112
extern "C" jobject Java_com_tns_Runtime_runScript(JNIEnv *_env, jobject obj, jint runtimeId, jstring scriptFile)
85113
{
86-
auto runtime = Runtime::GetRuntime(runtimeId);
87-
auto isolate = runtime->GetIsolate();
114+
jobject o = nullptr;
88115

116+
auto runtime = TryGetRuntime(runtimeId);
117+
if (runtime == nullptr)
118+
{
119+
return o;
120+
}
121+
122+
auto isolate = runtime->GetIsolate();
89123
v8::Isolate::Scope isolate_scope(isolate);
90124
v8::HandleScope handleScope(isolate);
91125

92-
jobject o = nullptr;
93126
try
94127
{
95128
o = runtime->RunScript(_env, obj, scriptFile);
@@ -113,13 +146,18 @@ extern "C" jobject Java_com_tns_Runtime_runScript(JNIEnv *_env, jobject obj, jin
113146

114147
extern "C" jobject Java_com_tns_Runtime_callJSMethodNative(JNIEnv *_env, jobject obj, jint runtimeId, jint javaObjectID, jstring methodName, jint retType, jboolean isConstructor, jobjectArray packagedArgs)
115148
{
116-
auto runtime = Runtime::GetRuntime(runtimeId);
117-
auto isolate = runtime->GetIsolate();
149+
jobject o = nullptr;
150+
151+
auto runtime = TryGetRuntime(runtimeId);
152+
if (runtime == nullptr)
153+
{
154+
return o;
155+
}
118156

157+
auto isolate = runtime->GetIsolate();
119158
v8::Isolate::Scope isolate_scope(isolate);
120159
v8::HandleScope handleScope(isolate);
121160

122-
jobject o = nullptr;
123161
try
124162
{
125163
o = runtime->CallJSMethodNative(_env, obj, javaObjectID, methodName, retType, isConstructor, packagedArgs);
@@ -143,9 +181,13 @@ extern "C" jobject Java_com_tns_Runtime_callJSMethodNative(JNIEnv *_env, jobject
143181

144182
extern "C" void Java_com_tns_Runtime_createJSInstanceNative(JNIEnv *_env, jobject obj, jint runtimeId, jobject javaObject, jint javaObjectID, jstring className)
145183
{
146-
auto runtime = Runtime::GetRuntime(runtimeId);
147-
auto isolate = runtime->GetIsolate();
184+
auto runtime = TryGetRuntime(runtimeId);
185+
if (runtime == nullptr)
186+
{
187+
return;
188+
}
148189

190+
auto isolate = runtime->GetIsolate();
149191
v8::Isolate::Scope isolate_scope(isolate);
150192
v8::HandleScope handleScope(isolate);
151193

@@ -173,7 +215,11 @@ extern "C" jint Java_com_tns_Runtime_generateNewObjectId(JNIEnv *env, jobject ob
173215
{
174216
try
175217
{
176-
auto runtime = Runtime::GetRuntime(runtimeId);
218+
auto runtime = TryGetRuntime(runtimeId);
219+
if (runtime == nullptr)
220+
{
221+
return 0;
222+
}
177223
return runtime->GenerateNewObjectId(env, obj);
178224
}
179225
catch (NativeScriptException& e)
@@ -194,9 +240,13 @@ extern "C" jint Java_com_tns_Runtime_generateNewObjectId(JNIEnv *env, jobject ob
194240

195241
extern "C" void Java_com_tns_Runtime_adjustAmountOfExternalAllocatedMemoryNative(JNIEnv *env, jobject obj, jint runtimeId, jlong usedMemory)
196242
{
197-
auto runtime = Runtime::GetRuntime(runtimeId);
198-
auto isolate = runtime->GetIsolate();
243+
auto runtime = TryGetRuntime(runtimeId);
244+
if (runtime == nullptr)
245+
{
246+
return;
247+
}
199248

249+
auto isolate = runtime->GetIsolate();
200250
v8::Isolate::Scope isolate_scope(isolate);
201251
v8::HandleScope handleScope(isolate);
202252

@@ -222,9 +272,13 @@ extern "C" void Java_com_tns_Runtime_adjustAmountOfExternalAllocatedMemoryNative
222272

223273
extern "C" void Java_com_tns_Runtime_passUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jint runtimeId, jthrowable exception, jstring stackTrace)
224274
{
225-
auto runtime = Runtime::GetRuntime(runtimeId);
226-
auto isolate = runtime->GetIsolate();
275+
auto runtime = TryGetRuntime(runtimeId);
276+
if (runtime == nullptr)
277+
{
278+
return;
279+
}
227280

281+
auto isolate = runtime->GetIsolate();
228282
v8::Isolate::Scope isolate_scope(isolate);
229283
v8::HandleScope handleScope(isolate);
230284

0 commit comments

Comments
 (0)