add require module
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user