add require module
This commit is contained in:
parent
13c5719af1
commit
723c5455ab
@ -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("");
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -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()
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ export function Registor<T extends { new(...args: any[]): {} }>(constructor: T)
|
||||
const ret = class extends constructor {
|
||||
context = context
|
||||
}
|
||||
context.registor(new ret)
|
||||
context.register(new ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user