diff --git a/build.gradle b/build.gradle index ed88d78..2064e23 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,7 @@ dependencies { // And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version. // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. implementation "net.neoforged:neoforge:${neo_version}" - libraries 'com.dylibso.chicory:runtime:0.0.12' + libraries 'org.mozilla:rhino-engine:1.7.15' // Example optional mod dependency with JEI // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java index d6528c2..139fe42 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java @@ -4,6 +4,7 @@ import com.mojang.serialization.MapCodec; import dev.exhq.ajarc.Ajar; import dev.exhq.ajarc.network.ComputerScreenUpdate; import dev.exhq.ajarc.register.Register; +import dev.exhq.ajarc.vm.JsVm; import dev.exhq.ajarc.vm.WasmVm; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; @@ -34,7 +35,6 @@ public class ComputerBlockEntity extends BlockEntity { private static final MapCodec screenCodec = ComputerTerminal.CODEC .fieldOf("screen") .setPartial(() -> ComputerTerminal.ofSize(20, 30)); - private WasmVm vm = new WasmVm(this); private List lines = new ArrayList<>(); @Override @@ -76,19 +76,20 @@ public class ComputerBlockEntity extends BlockEntity { public void executeCommand(String line) { lines.add("$ " + line); - if (line.equals("small")) { - screen = ComputerTerminal.ofSize(10, 20); - lines.add("Made small!"); - } else if (line.equals("big")) { - screen = ComputerTerminal.ofSize(20, 30); - lines.add("Made big!"); - } else if (line.equals("add")) { - int left = 42, right = 69; - int result = vm.add(left, right); - lines.add(left + " + " + right + " = " + result); - } else { - lines.add("Made unknown!"); - } + lines.add(JsVm.EvalJs(line)); +// if (line.equals("small")) { +// screen = ComputerTerminal.ofSize(10, 20); +// lines.add("Made small!"); +// } else if (line.equals("big")) { +// screen = ComputerTerminal.ofSize(20, 30); +// lines.add("Made big!"); +// } else if (line.startsWith("add")) { +// var newline = line.replaceFirst("add ", "").split(" "); +// lines.add(JsVm.EvalJs("mathj.add("+newline[0]+","+newline[1]+")")); +// } +// else { +// lines.add("Made unknown!"); +// } } public ComputerScreenUpdate getSyncPacket(int windowId) { diff --git a/src/main/java/dev/exhq/ajarc/vm/JsVm.java b/src/main/java/dev/exhq/ajarc/vm/JsVm.java new file mode 100644 index 0000000..3424004 --- /dev/null +++ b/src/main/java/dev/exhq/ajarc/vm/JsVm.java @@ -0,0 +1,25 @@ +package dev.exhq.ajarc.vm; + +import dev.exhq.ajarc.computer.ComputerBlockEntity; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; + +public class JsVm { + public static String EvalJs(String command){ + var cx = Context.enter(); + try { + Scriptable scope = cx.initStandardObjects(); + MathJ mathJ = new MathJ(); + Object wrappedJsVm = Context.javaToJS(mathJ, scope); + ScriptableObject.putProperty(scope, "mathj", wrappedJsVm); + Object result = cx.evaluateString(scope, command, "", 1, null); + + return Context.toString(result); + } finally { + Context.exit(); + } + } + + +} \ No newline at end of file diff --git a/src/main/java/dev/exhq/ajarc/vm/MathJ.java b/src/main/java/dev/exhq/ajarc/vm/MathJ.java new file mode 100644 index 0000000..3d0a32f --- /dev/null +++ b/src/main/java/dev/exhq/ajarc/vm/MathJ.java @@ -0,0 +1,7 @@ +package dev.exhq.ajarc.vm; + +public class MathJ { + public int add(int a, int b) { + return a + b; + } +}