add require module

This commit is contained in:
pengfei.zhou 2019-07-18 18:29:47 +08:00
parent 13c5719af1
commit 723c5455ab
9 changed files with 88 additions and 24 deletions

View File

@ -12,7 +12,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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(""); hegoContext.callJS("");
} }
} }

View File

@ -2,6 +2,9 @@ package com.github.pengfeizhou.hego;
import android.app.Application; import android.app.Application;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* @Description: Android * @Description: Android
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -17,4 +20,14 @@ public class Hego {
public static Application application() { public static Application application() {
return sApplication; return sApplication;
} }
private static Map<String, String> 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);
}
} }

View File

@ -13,12 +13,22 @@ public class HegoConstant {
public static final String TEMPLATE_CONTEXT_CREATE = "Reflect.apply(" + public static final String TEMPLATE_CONTEXT_CREATE = "Reflect.apply(" +
"function(hego,context,require,exports){" + "\n" + "function(hego,context,require,exports){" + "\n" +
"%s" + "\n" + "%s" + "\n" +
"},hego.jsObtainContext(%s),[" + "},hego.jsObtainContext(\"%s\"),[" +
"undefined," + "undefined," +
"hego.jsObtainContext(%s)," + "hego.jsObtainContext(\"%s\")," +
"hego.__require__" + "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 TEMPLATE_CONTEXT_DESTORY = "hego.jsRelease(%s)";
public static final String GLOBAL_HEGO = "hego"; public static final String GLOBAL_HEGO = "hego";
public static final String HEGO_CONTEXT_RELEASE = "jsReleaseContext"; public static final String HEGO_CONTEXT_RELEASE = "jsReleaseContext";

View File

@ -4,9 +4,11 @@ import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.text.TextUtils;
import com.github.pengfeizhou.hego.jse.HegoJSExecutor; import com.github.pengfeizhou.hego.jse.HegoJSExecutor;
import com.github.pengfeizhou.hego.jse.IHegoJSE; import com.github.pengfeizhou.hego.jse.IHegoJSE;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
@ -50,13 +52,13 @@ public class HegoJSEngine implements Handler.Callback {
String message = args[1].string(); String message = args[1].string();
switch (type) { switch (type) {
case "w": case "w":
HegoLog.w("js", message); HegoLog.w(message, "_js");
break; break;
case "e": case "e":
HegoLog.e("js", message); HegoLog.e(message, "_js");
break; break;
default: default:
HegoLog.d("js", message); HegoLog.d(message, "_js");
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
@ -65,10 +67,31 @@ public class HegoJSEngine implements Handler.Callback {
return new JavaValue(); 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() { 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 @Override
@ -89,7 +112,7 @@ public class HegoJSEngine implements Handler.Callback {
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
mHegoJSE.loadJS(packageContextScript(script, contextId), "Context://" + source); mHegoJSE.loadJS(packageContextScript(contextId, script), "Context://" + source);
} }
}; };
doOnJSThread(runnable); doOnJSThread(runnable);
@ -113,10 +136,14 @@ public class HegoJSEngine implements Handler.Callback {
doOnJSThread(runnable); 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); 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() { public boolean isJSThread() {
return Looper.myLooper() == mJSHandler.getLooper(); return Looper.myLooper() == mJSHandler.getLooper();
} }

View File

@ -10,15 +10,27 @@ import android.util.Log;
public class HegoLog { public class HegoLog {
private static String TAG = Hego.class.getSimpleName(); private static String TAG = Hego.class.getSimpleName();
public static void d(String suffix, String message) { public static void d(String message, String... suffix) {
Log.d(TAG + suffix, message); 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) { public static void w(String message, String... suffix) {
Log.w(TAG + suffix, message); 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) { public static void e(String message, String... suffix) {
Log.e(TAG + suffix, message); StringBuilder stringBuilder = new StringBuilder(TAG);
for (String s : suffix) {
stringBuilder.append(s);
}
Log.e(stringBuilder.toString(), message);
} }
} }

View File

@ -6,7 +6,8 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "tsc -p .&& rollup -c && node ./bundle/bundle.js", "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": { "repository": {
"type": "https", "type": "https",

View File

@ -9,7 +9,7 @@ export default [
output: { output: {
name: "hego", name: "hego",
format: "iife", format: "iife",
file: "bundle/sandbox.js", file: "bundle/hego-sandbox.js",
}, },
sourceMap: true, sourceMap: true,
plugins: [ plugins: [
@ -21,12 +21,11 @@ export default [
input: "build/index.js", input: "build/index.js",
output: { output: {
format: "cjs", format: "cjs",
file: "bundle/bundle.js", file: "bundle/hego-lib.js",
}, },
sourceMap: true, sourceMap: true,
plugins: [ plugins: [
resolve({ jsnext: true, main: true }), resolve({ jsnext: true, main: true }),
commonjs()
] ]
}, },
{ {

View File

@ -14,11 +14,13 @@ import { loge } from "../util/log";
* hego.__require__, * hego.__require__,
* ]) * ])
* // load module in global scope * // load module in global scope
* Reflect.apply(hego.registerModule,this,[ * Reflect.apply(hego.jsRegisterModule,this,[
* moduleName, * moduleName,
* Reflect.apply(function(__module){ * Reflect.apply(function(__module){
* return function(module,export,require){ * (function(module,exports,require){
* })(__module,__module.exports,hego.__require__) * //module content
* })(__module,__module.exports,hego.__require__);
* return __module.exports
* },this,[{exports:{}}]) * },this,[{exports:{}}])
* ]) * ])
* *
@ -83,7 +85,7 @@ export class Context {
}) })
}) })
} }
registor(instance: Object) { register(instance: Object) {
this.entity = instance this.entity = instance
} }
} }

View File

@ -5,7 +5,7 @@ export function Registor<T extends { new(...args: any[]): {} }>(constructor: T)
const ret = class extends constructor { const ret = class extends constructor {
context = context context = context
} }
context.registor(new ret) context.register(new ret)
return ret return ret
} }