From 4039d115133aa6e43f196180e13c5065a1ec65e5 Mon Sep 17 00:00:00 2001 From: echo Date: Mon, 22 Jul 2024 00:02:07 +0330 Subject: [PATCH] javascript lfg2 --- .../ajarc/computer/ComputerBlockEntity.java | 5 +- src/main/java/dev/exhq/ajarc/vm/JsVm.java | 35 +++-- src/main/java/dev/exhq/ajarc/vm/WasmVm.java | 130 +++++++++--------- 3 files changed, 95 insertions(+), 75 deletions(-) diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java index 139fe42..49156f0 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java @@ -5,7 +5,7 @@ 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 dev.exhq.ajarc.vm.WasmVm; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; @@ -35,6 +35,7 @@ public class ComputerBlockEntity extends BlockEntity { private static final MapCodec screenCodec = ComputerTerminal.CODEC .fieldOf("screen") .setPartial(() -> ComputerTerminal.ofSize(20, 30)); + JsVm jsVm = new JsVm(); private List lines = new ArrayList<>(); @Override @@ -76,7 +77,7 @@ public class ComputerBlockEntity extends BlockEntity { public void executeCommand(String line) { lines.add("$ " + line); - lines.add(JsVm.EvalJs(line)); + lines.add(jsVm.evalJs(line)); // if (line.equals("small")) { // screen = ComputerTerminal.ofSize(10, 20); // lines.add("Made small!"); diff --git a/src/main/java/dev/exhq/ajarc/vm/JsVm.java b/src/main/java/dev/exhq/ajarc/vm/JsVm.java index 3424004..6e293b4 100644 --- a/src/main/java/dev/exhq/ajarc/vm/JsVm.java +++ b/src/main/java/dev/exhq/ajarc/vm/JsVm.java @@ -1,25 +1,44 @@ 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; +import org.mozilla.javascript.JavaScriptException; public class JsVm { - public static String EvalJs(String command){ - var cx = Context.enter(); + private Context cx; + private Scriptable scope; + + public JsVm() { + cx = Context.enter(); try { - Scriptable scope = cx.initStandardObjects(); + 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(); } } + public String evalJs(String command) { + Context.enter(); + try { + Object result = cx.evaluateString(scope, command, "", 1, null); + return Context.toString(result); + } catch (JavaScriptException e) { + return e.getMessage(); + } catch (Exception e) { + return e.getMessage(); + } finally { + Context.exit(); + } + } -} \ No newline at end of file + public void cleanup() { + if (cx != null) { + cx.exit(); + cx = null; + } + } +} diff --git a/src/main/java/dev/exhq/ajarc/vm/WasmVm.java b/src/main/java/dev/exhq/ajarc/vm/WasmVm.java index fb26537..73b512f 100644 --- a/src/main/java/dev/exhq/ajarc/vm/WasmVm.java +++ b/src/main/java/dev/exhq/ajarc/vm/WasmVm.java @@ -1,65 +1,65 @@ -package dev.exhq.ajarc.vm; - -import com.dylibso.chicory.runtime.HostFunction; -import com.dylibso.chicory.runtime.HostImports; -import com.dylibso.chicory.runtime.Instance; -import com.dylibso.chicory.runtime.Module; -import com.dylibso.chicory.wasm.types.Value; -import com.dylibso.chicory.wasm.types.ValueType; -import dev.exhq.ajarc.Ajar; -import dev.exhq.ajarc.computer.ComputerBlockEntity; -import dev.exhq.ajarc.computer.ComputerTerminal; -import net.minecraft.client.Minecraft; - -import java.io.IOException; -import java.util.List; - -public class WasmVm { - - private final Instance instance; - - public WasmVm(ComputerBlockEntity entity) { - try { - var setSize = new HostFunction( - (instance1, args) -> { - int rows = args[0].asInt(); - int cols = args[1].asInt(); - entity.setTerminal(ComputerTerminal.ofSize(rows, cols)); - return new Value[0]; - }, - "env", - "setSize", - List.of(ValueType.I32, ValueType.I32), - List.of()); - var inputStream = Minecraft.getInstance() - .getResourceManager() - .getResourceOrThrow(Ajar.identifier("test.wasm")) - .open(); - var module = Module.builder(inputStream) - .withHostImports(new HostImports( - new HostFunction[]{ - setSize - } - )) - .build(); - this.instance = module.instantiate(); - } catch (IOException e) { - Ajar.LOGGER.error("Could not load wasm module", e); - throw new RuntimeException(e); - } - } - - public int add(int left, int right) { - var export = instance.export("add"); - var timer = System.nanoTime(); - for (int i = 0; i < 10000; i++) { - export.apply(Value.i32(left), Value.i32(right)); - } - System.out.println("Took " + ((System.nanoTime() - timer) / 1_000_000) + "ms to add 1000*2 numbers"); - var result = export.apply(Value.i32(left), Value.i32(right)); - assert result.length == 1; - return result[0].asInt(); - } - - -} +//package dev.exhq.ajarc.vm; +// +//import com.dylibso.chicory.runtime.HostFunction; +//import com.dylibso.chicory.runtime.HostImports; +//import com.dylibso.chicory.runtime.Instance; +//import com.dylibso.chicory.runtime.Module; +//import com.dylibso.chicory.wasm.types.Value; +//import com.dylibso.chicory.wasm.types.ValueType; +//import dev.exhq.ajarc.Ajar; +//import dev.exhq.ajarc.computer.ComputerBlockEntity; +//import dev.exhq.ajarc.computer.ComputerTerminal; +//import net.minecraft.client.Minecraft; +// +//import java.io.IOException; +//import java.util.List; +// +//public class WasmVm { +// +// private final Instance instance; +// +// public WasmVm(ComputerBlockEntity entity) { +// try { +// var setSize = new HostFunction( +// (instance1, args) -> { +// int rows = args[0].asInt(); +// int cols = args[1].asInt(); +// entity.setTerminal(ComputerTerminal.ofSize(rows, cols)); +// return new Value[0]; +// }, +// "env", +// "setSize", +// List.of(ValueType.I32, ValueType.I32), +// List.of()); +// var inputStream = Minecraft.getInstance() +// .getResourceManager() +// .getResourceOrThrow(Ajar.identifier("test.wasm")) +// .open(); +// var module = Module.builder(inputStream) +// .withHostImports(new HostImports( +// new HostFunction[]{ +// setSize +// } +// )) +// .build(); +// this.instance = module.instantiate(); +// } catch (IOException e) { +// Ajar.LOGGER.error("Could not load wasm module", e); +// throw new RuntimeException(e); +// } +// } +// +// public int add(int left, int right) { +// var export = instance.export("add"); +// var timer = System.nanoTime(); +// for (int i = 0; i < 10000; i++) { +// export.apply(Value.i32(left), Value.i32(right)); +// } +// System.out.println("Took " + ((System.nanoTime() - timer) / 1_000_000) + "ms to add 1000*2 numbers"); +// var result = export.apply(Value.i32(left), Value.i32(right)); +// assert result.length == 1; +// return result[0].asInt(); +// } +// +// +//}