javascript lfg

This commit is contained in:
amy 2024-07-21 08:03:36 +03:30
parent 88fad02b96
commit 108afd360f
4 changed files with 48 additions and 15 deletions

View file

@ -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}"

View file

@ -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<ComputerTerminal> screenCodec = ComputerTerminal.CODEC
.fieldOf("screen")
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
private WasmVm vm = new WasmVm(this);
private List<String> 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) {

View file

@ -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, "<cmd>", 1, null);
return Context.toString(result);
} finally {
Context.exit();
}
}
}

View file

@ -0,0 +1,7 @@
package dev.exhq.ajarc.vm;
public class MathJ {
public int add(int a, int b) {
return a + b;
}
}