From 086beeb2c2613510414fe057ea0ad9381f9db027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 24 Oct 2019 10:39:06 +0800 Subject: [PATCH] js call native over ws completed --- .../doric/engine/remote/RemoteJSExecutor.java | 29 +++++++++++++++++-- js-framework/index.debug.ts | 3 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java index 2eb822d9..65e5f7d5 100644 --- a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java +++ b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java @@ -4,8 +4,10 @@ import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaValue; import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import org.jetbrains.annotations.NotNull; @@ -66,10 +68,31 @@ public class RemoteJSExecutor { switch (cmd) { case "injectGlobalJSFunction": String name = jo.get("name").getAsString(); - JsonObject arguments = jo.getAsJsonObject("arguments"); - for (String key : arguments.keySet()) { - System.out.println(key + " " + arguments.get(key)); + JsonArray arguments = jo.getAsJsonArray("arguments"); + JSDecoder[] decoders = new JSDecoder[arguments.size()]; + for (int i = 0; i < arguments.size(); i++) { + if (arguments.get(i).isJsonPrimitive()) { + JsonPrimitive jp = ((JsonPrimitive) arguments.get(i)); + if (jp.isNumber()) { + ValueBuilder vb = new ValueBuilder(jp.getAsNumber()); + JSDecoder decoder = new JSDecoder(vb.build()); + decoders[i] = decoder; + } else if (jp.isBoolean()) { + ValueBuilder vb = new ValueBuilder(jp.getAsBoolean()); + JSDecoder decoder = new JSDecoder(vb.build()); + decoders[i] = decoder; + } else if (jp.isString()) { + ValueBuilder vb = new ValueBuilder(jp.getAsString()); + JSDecoder decoder = new JSDecoder(vb.build()); + decoders[i] = decoder; + } + } else { + ValueBuilder vb = new ValueBuilder(arguments.get(i)); + JSDecoder decoder = new JSDecoder(vb.build()); + decoders[i] = decoder; + } } + globalFunctions.get(name).exec(decoders); break; } } diff --git a/js-framework/index.debug.ts b/js-framework/index.debug.ts index 976e5a92..d602cd8b 100644 --- a/js-framework/index.debug.ts +++ b/js-framework/index.debug.ts @@ -30,10 +30,11 @@ wss.on('connection', function connection(ws) { case "injectGlobalJSFunction": console.log(messageObject.name) Reflect.set(global, messageObject.name, function() { + let args = [].slice.call(arguments) ws.send(JSON.stringify({ cmd: 'injectGlobalJSFunction', name: messageObject.name, - arguments: arguments + arguments: args })) }) break