javascript lfg2

This commit is contained in:
amy 2024-07-22 00:02:07 +03:30
parent 108afd360f
commit 4039d11513
3 changed files with 95 additions and 75 deletions

View file

@ -5,7 +5,7 @@ import dev.exhq.ajarc.Ajar;
import dev.exhq.ajarc.network.ComputerScreenUpdate; import dev.exhq.ajarc.network.ComputerScreenUpdate;
import dev.exhq.ajarc.register.Register; import dev.exhq.ajarc.register.Register;
import dev.exhq.ajarc.vm.JsVm; 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.BlockPos;
import net.minecraft.core.HolderLookup; import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -35,6 +35,7 @@ public class ComputerBlockEntity extends BlockEntity {
private static final MapCodec<ComputerTerminal> screenCodec = ComputerTerminal.CODEC private static final MapCodec<ComputerTerminal> screenCodec = ComputerTerminal.CODEC
.fieldOf("screen") .fieldOf("screen")
.setPartial(() -> ComputerTerminal.ofSize(20, 30)); .setPartial(() -> ComputerTerminal.ofSize(20, 30));
JsVm jsVm = new JsVm();
private List<String> lines = new ArrayList<>(); private List<String> lines = new ArrayList<>();
@Override @Override
@ -76,7 +77,7 @@ public class ComputerBlockEntity extends BlockEntity {
public void executeCommand(String line) { public void executeCommand(String line) {
lines.add("$ " + line); lines.add("$ " + line);
lines.add(JsVm.EvalJs(line)); lines.add(jsVm.evalJs(line));
// if (line.equals("small")) { // if (line.equals("small")) {
// screen = ComputerTerminal.ofSize(10, 20); // screen = ComputerTerminal.ofSize(10, 20);
// lines.add("Made small!"); // lines.add("Made small!");

View file

@ -1,25 +1,44 @@
package dev.exhq.ajarc.vm; package dev.exhq.ajarc.vm;
import dev.exhq.ajarc.computer.ComputerBlockEntity;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.JavaScriptException;
public class JsVm { public class JsVm {
public static String EvalJs(String command){ private Context cx;
var cx = Context.enter(); private Scriptable scope;
public JsVm() {
cx = Context.enter();
try { try {
Scriptable scope = cx.initStandardObjects(); scope = cx.initStandardObjects();
MathJ mathJ = new MathJ(); MathJ mathJ = new MathJ();
Object wrappedJsVm = Context.javaToJS(mathJ, scope); Object wrappedJsVm = Context.javaToJS(mathJ, scope);
ScriptableObject.putProperty(scope, "mathj", wrappedJsVm); ScriptableObject.putProperty(scope, "mathj", wrappedJsVm);
Object result = cx.evaluateString(scope, command, "<cmd>", 1, null);
return Context.toString(result);
} finally { } finally {
Context.exit(); Context.exit();
} }
} }
public String evalJs(String command) {
Context.enter();
try {
Object result = cx.evaluateString(scope, command, "<cmd>", 1, null);
return Context.toString(result);
} catch (JavaScriptException e) {
return e.getMessage();
} catch (Exception e) {
return e.getMessage();
} finally {
Context.exit();
}
}
} public void cleanup() {
if (cx != null) {
cx.exit();
cx = null;
}
}
}

View file

@ -1,65 +1,65 @@
package dev.exhq.ajarc.vm; //package dev.exhq.ajarc.vm;
//
import com.dylibso.chicory.runtime.HostFunction; //import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.HostImports; //import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.Instance; //import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Module; //import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.wasm.types.Value; //import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType; //import com.dylibso.chicory.wasm.types.ValueType;
import dev.exhq.ajarc.Ajar; //import dev.exhq.ajarc.Ajar;
import dev.exhq.ajarc.computer.ComputerBlockEntity; //import dev.exhq.ajarc.computer.ComputerBlockEntity;
import dev.exhq.ajarc.computer.ComputerTerminal; //import dev.exhq.ajarc.computer.ComputerTerminal;
import net.minecraft.client.Minecraft; //import net.minecraft.client.Minecraft;
//
import java.io.IOException; //import java.io.IOException;
import java.util.List; //import java.util.List;
//
public class WasmVm { //public class WasmVm {
//
private final Instance instance; // private final Instance instance;
//
public WasmVm(ComputerBlockEntity entity) { // public WasmVm(ComputerBlockEntity entity) {
try { // try {
var setSize = new HostFunction( // var setSize = new HostFunction(
(instance1, args) -> { // (instance1, args) -> {
int rows = args[0].asInt(); // int rows = args[0].asInt();
int cols = args[1].asInt(); // int cols = args[1].asInt();
entity.setTerminal(ComputerTerminal.ofSize(rows, cols)); // entity.setTerminal(ComputerTerminal.ofSize(rows, cols));
return new Value[0]; // return new Value[0];
}, // },
"env", // "env",
"setSize", // "setSize",
List.of(ValueType.I32, ValueType.I32), // List.of(ValueType.I32, ValueType.I32),
List.of()); // List.of());
var inputStream = Minecraft.getInstance() // var inputStream = Minecraft.getInstance()
.getResourceManager() // .getResourceManager()
.getResourceOrThrow(Ajar.identifier("test.wasm")) // .getResourceOrThrow(Ajar.identifier("test.wasm"))
.open(); // .open();
var module = Module.builder(inputStream) // var module = Module.builder(inputStream)
.withHostImports(new HostImports( // .withHostImports(new HostImports(
new HostFunction[]{ // new HostFunction[]{
setSize // setSize
} // }
)) // ))
.build(); // .build();
this.instance = module.instantiate(); // this.instance = module.instantiate();
} catch (IOException e) { // } catch (IOException e) {
Ajar.LOGGER.error("Could not load wasm module", e); // Ajar.LOGGER.error("Could not load wasm module", e);
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
} // }
//
public int add(int left, int right) { // public int add(int left, int right) {
var export = instance.export("add"); // var export = instance.export("add");
var timer = System.nanoTime(); // var timer = System.nanoTime();
for (int i = 0; i < 10000; i++) { // for (int i = 0; i < 10000; i++) {
export.apply(Value.i32(left), Value.i32(right)); // export.apply(Value.i32(left), Value.i32(right));
} // }
System.out.println("Took " + ((System.nanoTime() - timer) / 1_000_000) + "ms to add 1000*2 numbers"); // 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)); // var result = export.apply(Value.i32(left), Value.i32(right));
assert result.length == 1; // assert result.length == 1;
return result[0].asInt(); // return result[0].asInt();
} // }
//
//
} //}