javascript lfg2
This commit is contained in:
parent
108afd360f
commit
4039d11513
3 changed files with 95 additions and 75 deletions
|
@ -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<ComputerTerminal> screenCodec = ComputerTerminal.CODEC
|
||||
.fieldOf("screen")
|
||||
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
|
||||
JsVm jsVm = new JsVm();
|
||||
private List<String> 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!");
|
||||
|
|
|
@ -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, "<cmd>", 1, null);
|
||||
|
||||
return Context.toString(result);
|
||||
} finally {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
|
Loading…
Reference in a new issue