Fix Module Loading
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit

This commit is contained in:
TheBrokenRail 2020-05-26 10:12:45 -04:00
parent fca6d18021
commit 7fc1f01736
4 changed files with 32 additions and 13 deletions

View File

@ -469,9 +469,25 @@ static int eval_buf(JNIEnv *env, JSContext *ctx, const void *buf, int buf_len, c
JNIEXPORT void JNICALL Java_com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative_run(JNIEnv *env, jobject this_val, jstring data) { JNIEXPORT void JNICALL Java_com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative_run(JNIEnv *env, jobject this_val, jstring data) {
JSContext *ctx = (JSContext *) get_pointer(env, this_val, "ctx"); JSContext *ctx = (JSContext *) get_pointer(env, this_val, "ctx");
JSValue module_name, json_str;
char *init_js;
const char *native_string = (*env)->GetStringUTFChars(env, data, 0); const char *native_string = (*env)->GetStringUTFChars(env, data, 0);
eval_buf(env, ctx, native_string, strlen(native_string), "<init>", JS_EVAL_TYPE_MODULE); module_name = JS_NewString(ctx, native_string);
(*env)->ReleaseStringUTFChars(env, data, native_string); (*env)->ReleaseStringUTFChars(env, data, native_string);
json_str = JS_JSONStringify(ctx, module_name, JS_NULL, JS_NULL);
JS_FreeValue(ctx, module_name);
const char *json_native_str = JS_ToCString(ctx, json_str);
JS_FreeValue(ctx, json_str);
asprintf(&init_js, "import %s;", json_native_str);
JS_FreeCString(ctx, json_native_str);
eval_buf(env, ctx, init_js, strlen(init_js), "<init>", JS_EVAL_TYPE_MODULE);
free(init_js);
} }
void print_data(char *data, int err) { void print_data(char *data, int err) {

View File

@ -55,7 +55,7 @@ public class ScriptCraftCore implements ModInitializer {
mod.registerBridges(); mod.registerBridges();
} }
for (ScriptCraftEntryPoint mod : mods) { for (ScriptCraftEntryPoint mod : mods) {
quickjs.run("import `" + mod.getEntryPoint().replaceAll("`", "\\`") + "`;"); quickjs.run(mod.getEntryPoint());
} }
return null; return null;
} }

View File

@ -12,17 +12,26 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List; import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class QuickJSModules { public class QuickJSModules {
private static boolean isDirectory(String data, String path, Class<?> clazz) {
String[] lines = data.split("\n");
for (String line : lines) {
InputStream stream = clazz.getResourceAsStream(path + File.separator + line);
return stream != null;
}
return false;
}
public static String loadModule(String name) { public static String loadModule(String name) {
List<ScriptCraftEntryPoint> mods = FabricLoader.getInstance().getEntrypoints(ScriptCraftCore.NAMESPACE, ScriptCraftEntryPoint.class); List<ScriptCraftEntryPoint> mods = FabricLoader.getInstance().getEntrypoints(ScriptCraftCore.NAMESPACE, ScriptCraftEntryPoint.class);
for (ScriptCraftEntryPoint mod : mods) { for (ScriptCraftEntryPoint mod : mods) {
//noinspection CatchMayIgnoreException //noinspection CatchMayIgnoreException
try { try {
InputStream stream = mod.getClass().getResourceAsStream(File.separator + ScriptCraftCore.NAMESPACE + File.separator + name); String path = File.separator + ScriptCraftCore.NAMESPACE + File.separator + name;
InputStream stream = mod.getClass().getResourceAsStream(path);
if (stream != null) { if (stream != null) {
StringBuilder textBuilder = new StringBuilder(); StringBuilder textBuilder = new StringBuilder();
@ -34,7 +43,7 @@ public class QuickJSModules {
} }
stream.close(); stream.close();
return textBuilder.toString(); return isDirectory(textBuilder.toString(), path, mod.getClass()) ? null : textBuilder.toString();
} }
} catch (IOException e) { } catch (IOException e) {
} }
@ -52,13 +61,7 @@ public class QuickJSModules {
} }
} }
private static final String[] BUILTIN_MODULES = new String[]{"scriptcraft-core"};
public static String normalizeModule(String baseName, String name) { public static String normalizeModule(String baseName, String name) {
if (Arrays.asList(BUILTIN_MODULES).contains(name)) {
return name;
}
String normalizedPath; String normalizedPath;
baseName = baseName.replaceAll("/", File.separator); baseName = baseName.replaceAll("/", File.separator);
@ -83,6 +86,6 @@ public class QuickJSModules {
} }
} }
return null; return normalizedPath;
} }
} }

View File

@ -24,7 +24,7 @@ public class QuickJSNative {
public native Object bridge(String method, Object... args) throws JSException; public native Object bridge(String method, Object... args) throws JSException;
public native void run(String data); public native void run(String module);
static { static {
try { try {