From 723c5455ab50255ef7266fd99e7ff0ece3c53341 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 18 Jul 2019 18:29:47 +0800 Subject: [PATCH] add require module --- .../pengfeizhou/hegodemo/MainActivity.java | 2 +- .../com/github/pengfeizhou/hego/Hego.java | 13 +++++++ .../github/pengfeizhou/hego/HegoConstant.java | 14 ++++++- .../github/pengfeizhou/hego/HegoJSEngine.java | 39 ++++++++++++++++--- .../com/github/pengfeizhou/hego/HegoLog.java | 24 +++++++++--- js-framework/package.json | 3 +- js-framework/rollup.config.js | 5 +-- js-framework/src/runtime/sandbox.ts | 10 +++-- js-framework/src/ui/panel.ts | 2 +- 9 files changed, 88 insertions(+), 24 deletions(-) diff --git a/Android/app/src/main/java/com/github/pengfeizhou/hegodemo/MainActivity.java b/Android/app/src/main/java/com/github/pengfeizhou/hegodemo/MainActivity.java index 1c39e1f9..e7b258c6 100644 --- a/Android/app/src/main/java/com/github/pengfeizhou/hegodemo/MainActivity.java +++ b/Android/app/src/main/java/com/github/pengfeizhou/hegodemo/MainActivity.java @@ -12,7 +12,7 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - HegoContext hegoContext = HegoContext.createContext(HegoUtils.readAssetFile("test.js"), "demo"); + HegoContext hegoContext = HegoContext.createContext(HegoUtils.readAssetFile("demo.js"), "demo"); hegoContext.callJS(""); } } diff --git a/Android/hego/src/main/java/com/github/pengfeizhou/hego/Hego.java b/Android/hego/src/main/java/com/github/pengfeizhou/hego/Hego.java index e067d0d3..241406a9 100644 --- a/Android/hego/src/main/java/com/github/pengfeizhou/hego/Hego.java +++ b/Android/hego/src/main/java/com/github/pengfeizhou/hego/Hego.java @@ -2,6 +2,9 @@ package com.github.pengfeizhou.hego; import android.app.Application; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * @Description: Android * @Author: pengfei.zhou @@ -17,4 +20,14 @@ public class Hego { public static Application application() { return sApplication; } + + private static Map bundles = new ConcurrentHashMap<>(); + + public static void registerJSModuleContent(String name, String bundle) { + bundles.put(name, bundle); + } + + public static String getJSModuleContent(String name) { + return bundles.get(name); + } } diff --git a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoConstant.java b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoConstant.java index a3bf0f90..d899200f 100644 --- a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoConstant.java +++ b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoConstant.java @@ -13,12 +13,22 @@ public class HegoConstant { public static final String TEMPLATE_CONTEXT_CREATE = "Reflect.apply(" + "function(hego,context,require,exports){" + "\n" + "%s" + "\n" + - "},hego.jsObtainContext(%s),[" + + "},hego.jsObtainContext(\"%s\"),[" + "undefined," + - "hego.jsObtainContext(%s)," + + "hego.jsObtainContext(\"%s\")," + "hego.__require__" + ",{}" + "])"; + + public static final String TEMPLATE_MODULE = "Reflect.apply(hego.jsRegisterModule,this,[" + + "\"%s\"," + + "Reflect.apply(function(__module){" + + "(function(module,exports,require){" + "\n" + + "%s" + "\n" + + "})(__module,__module.exports,hego.__require__);" + + "\nreturn __module.exports;" + + "},this,[{exports:{}}])" + + "])"; public static final String TEMPLATE_CONTEXT_DESTORY = "hego.jsRelease(%s)"; public static final String GLOBAL_HEGO = "hego"; public static final String HEGO_CONTEXT_RELEASE = "jsReleaseContext"; diff --git a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoJSEngine.java b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoJSEngine.java index 0727c421..85186566 100644 --- a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoJSEngine.java +++ b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoJSEngine.java @@ -4,9 +4,11 @@ import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; +import android.text.TextUtils; import com.github.pengfeizhou.hego.jse.HegoJSExecutor; import com.github.pengfeizhou.hego.jse.IHegoJSE; +import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaValue; @@ -50,13 +52,13 @@ public class HegoJSEngine implements Handler.Callback { String message = args[1].string(); switch (type) { case "w": - HegoLog.w("js", message); + HegoLog.w(message, "_js"); break; case "e": - HegoLog.e("js", message); + HegoLog.e(message, "_js"); break; default: - HegoLog.d("js", message); + HegoLog.d(message, "_js"); break; } } catch (Exception e) { @@ -65,10 +67,31 @@ public class HegoJSEngine implements Handler.Callback { return new JavaValue(); } }); + mHegoJSE.injectGlobalJSFunction(HegoConstant.INJECT_REQUIRE, new JavaFunction() { + @Override + public JavaValue exec(JSDecoder[] args) { + try { + String name = args[0].string(); + String content = Hego.getJSModuleContent(name); + if (TextUtils.isEmpty(content)) { + HegoLog.e("error"); + return new JavaValue(false); + } + mHegoJSE.loadJS(packageModuleScript(name, content), "Module://" + name); + return new JavaValue(true); + } catch (Exception e) { + e.printStackTrace(); + return new JavaValue(false); + } + } + }); } private void initHugoRuntime() { - loadBuiltinJS("sandbox.js"); + loadBuiltinJS("hego-sandbox.js"); + String libName = "./index"; + String libJS = HegoUtils.readAssetFile("hego-lib.js"); + mHegoJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName); } @Override @@ -89,7 +112,7 @@ public class HegoJSEngine implements Handler.Callback { Runnable runnable = new Runnable() { @Override public void run() { - mHegoJSE.loadJS(packageContextScript(script, contextId), "Context://" + source); + mHegoJSE.loadJS(packageContextScript(contextId, script), "Context://" + source); } }; doOnJSThread(runnable); @@ -113,10 +136,14 @@ public class HegoJSEngine implements Handler.Callback { doOnJSThread(runnable); } - private String packageContextScript(String content, String contextId) { + private String packageContextScript(String contextId, String content) { return String.format(HegoConstant.TEMPLATE_CONTEXT_CREATE, content, contextId, contextId); } + private String packageModuleScript(String moduleName, String content) { + return String.format(HegoConstant.TEMPLATE_MODULE, moduleName, content); + } + public boolean isJSThread() { return Looper.myLooper() == mJSHandler.getLooper(); } diff --git a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoLog.java b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoLog.java index 9a146738..814bee5f 100644 --- a/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoLog.java +++ b/Android/hego/src/main/java/com/github/pengfeizhou/hego/HegoLog.java @@ -10,15 +10,27 @@ import android.util.Log; public class HegoLog { private static String TAG = Hego.class.getSimpleName(); - public static void d(String suffix, String message) { - Log.d(TAG + suffix, message); + public static void d(String message, String... suffix) { + StringBuilder stringBuilder = new StringBuilder(TAG); + for (String s : suffix) { + stringBuilder.append(s); + } + Log.d(stringBuilder.toString(), message); } - public static void w(String suffix, String message) { - Log.w(TAG + suffix, message); + public static void w(String message, String... suffix) { + StringBuilder stringBuilder = new StringBuilder(TAG); + for (String s : suffix) { + stringBuilder.append(s); + } + Log.w(stringBuilder.toString(), message); } - public static void e(String suffix, String message) { - Log.e(TAG + suffix, message); + public static void e(String message, String... suffix) { + StringBuilder stringBuilder = new StringBuilder(TAG); + for (String s : suffix) { + stringBuilder.append(s); + } + Log.e(stringBuilder.toString(), message); } } diff --git a/js-framework/package.json b/js-framework/package.json index b95a7c55..0ff0621d 100644 --- a/js-framework/package.json +++ b/js-framework/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "tsc -p .&& rollup -c && node ./bundle/bundle.js", - "build": "tsc -p . && rollup -c " + "build": "tsc -p . && rollup -c ", + "clean": "rm -rf build && rm -rf demo && rm -rf bundle" }, "repository": { "type": "https", diff --git a/js-framework/rollup.config.js b/js-framework/rollup.config.js index 4b42334c..8c1c3a38 100644 --- a/js-framework/rollup.config.js +++ b/js-framework/rollup.config.js @@ -9,7 +9,7 @@ export default [ output: { name: "hego", format: "iife", - file: "bundle/sandbox.js", + file: "bundle/hego-sandbox.js", }, sourceMap: true, plugins: [ @@ -21,12 +21,11 @@ export default [ input: "build/index.js", output: { format: "cjs", - file: "bundle/bundle.js", + file: "bundle/hego-lib.js", }, sourceMap: true, plugins: [ resolve({ jsnext: true, main: true }), - commonjs() ] }, { diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index 080f1872..38ef7ec0 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -14,11 +14,13 @@ import { loge } from "../util/log"; * hego.__require__, * ]) * // load module in global scope - * Reflect.apply(hego.registerModule,this,[ + * Reflect.apply(hego.jsRegisterModule,this,[ * moduleName, * Reflect.apply(function(__module){ - * return function(module,export,require){ - * })(__module,__module.exports,hego.__require__) + * (function(module,exports,require){ + * //module content + * })(__module,__module.exports,hego.__require__); + * return __module.exports * },this,[{exports:{}}]) * ]) * @@ -83,7 +85,7 @@ export class Context { }) }) } - registor(instance: Object) { + register(instance: Object) { this.entity = instance } } diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index dd2df157..db5caca8 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -5,7 +5,7 @@ export function Registor(constructor: T) const ret = class extends constructor { context = context } - context.registor(new ret) + context.register(new ret) return ret }