Skip to content

Added a setting to wrap calls to CallJSMethod in try catch #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test-app/app/src/main/assets/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"freeMemoryRatio": 0.50,
"markingMode": "full",
"maxLogcatObjectSize": 1024,
"forceLog": false
"forceLog": false,
"suppressCallJSMethodExceptions": false
},
"discardUncaughtJsExceptions": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.nativescript.staticbindinggenerator;

import org.apache.bcel.generic.Type;

import java.util.HashMap;
import java.util.Map;

class DefaultValues {
static final Map<Type, String> defaultValues = new HashMap<>();

// load
static {
defaultValues.put(Type.BOOLEAN, "false");
defaultValues.put(Type.BYTE, "0");
defaultValues.put(Type.SHORT, "0");
defaultValues.put(Type.INT, "0");
defaultValues.put(Type.LONG, "0L");
defaultValues.put(Type.CHAR, "'\u0000'");
defaultValues.put(Type.FLOAT, "0.0F");
defaultValues.put(Type.DOUBLE, "0.0");
}

public static final String defaultStringValueFor(Type type) {
if(defaultValues.containsKey(type)) {
return defaultValues.get(type);
} else {
return "null";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.BasicType;
import org.apache.bcel.generic.Type;

public class Generator {
Expand All @@ -47,15 +48,17 @@ public class Generator {
private final File outputDir;
private final List<DataRow> libs;
private final Map<String, JavaClass> classes;
private final boolean suppressCallJSMethodExceptions;

public Generator(File outputDir, List<DataRow> libs) throws IOException {
this(outputDir, libs, false);
this(outputDir, libs, false, false);
}

public Generator(File outputDir, List<DataRow> libs, boolean throwOnError) throws IOException {
public Generator(File outputDir, List<DataRow> libs, boolean suppressCallJSMethodExceptions, boolean throwOnError) throws IOException {
this.outputDir = outputDir;
this.libs = libs;
this.classes = readClasses(libs, throwOnError);
this.suppressCallJSMethodExceptions = suppressCallJSMethodExceptions;
}

public void writeBindings(String filename) throws IOException, ClassNotFoundException {
Expand Down Expand Up @@ -638,6 +641,12 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
}
w.write("\t\t");
Type ret = m.getReturnType();

if(this.suppressCallJSMethodExceptions) {
w.writeln("try {");
w.write("\t\t\t");
}

if (!ret.equals(Type.VOID)) {
w.write("return (");
writeType(ret, w);
Expand All @@ -648,6 +657,19 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
w.write("\", ");
writeType(ret, w);
w.writeln(".class, args);");

if(this.suppressCallJSMethodExceptions) {
w.writeln("\t\t} catch (Throwable t) {");
w.writeln("\t\t\tandroid.util.Log.w(\"Error\", t);");
if (!ret.equals(Type.VOID)) {
w.write("\t\t\t");
w.write("return ");
w.write(DefaultValues.defaultStringValueFor(ret));
w.writeln(";");
}
w.writeln("\t\t}");
}

if (m.getName().equals("onCreate") && isApplicationClass) {
w.writeln("\t\tif (runtime != null) {");
w.writeln("\t\t\truntime.run();");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class Main {
}

public static void main(String[] args) throws IOException, ClassNotFoundException {

validateInput();

getWorkerExcludeFile();
Expand All @@ -45,7 +44,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio

// generate java bindings
String inputBindingFilename = Paths.get(System.getProperty("user.dir"), SBG_BINDINGS_NAME).toString();
new Generator(outputDir, rows).writeBindings(inputBindingFilename);
new Generator(outputDir, rows, isSuppressCallJSMethodExceptionsEnabled(), false).writeBindings(inputBindingFilename);
}

/*
Expand All @@ -67,6 +66,27 @@ private static void generateJsInputFile() throws IOException {
pw.close();
}

private static boolean isSuppressCallJSMethodExceptionsEnabled() throws IOException{
File jsonFile = new File(inputDir, "package.json");
if (!jsonFile.exists()) {
return false;
}
String jsonContent = FileUtils.readFileToString(jsonFile, "UTF-8");
JSONObject pjson = null;
try {
pjson = new JSONObject(jsonContent);
if (pjson.has("android")) {
JSONObject androidSettings = (JSONObject) pjson.get("android");
if(androidSettings.has("suppressCallJSMethodExceptions") && androidSettings.get("suppressCallJSMethodExceptions").toString().equals("true")) {
return true;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}

private static void validateInput() throws IOException {
dependenciesFile = "sbg-java-dependencies.txt";
if (!(new File(dependenciesFile).exists())) {
Expand Down