javascript lfg
This commit is contained in:
parent
88fad02b96
commit
108afd360f
4 changed files with 48 additions and 15 deletions
|
@ -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}"
|
||||
|
|
|
@ -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) {
|
||||
|
|
25
src/main/java/dev/exhq/ajarc/vm/JsVm.java
Normal file
25
src/main/java/dev/exhq/ajarc/vm/JsVm.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
7
src/main/java/dev/exhq/ajarc/vm/MathJ.java
Normal file
7
src/main/java/dev/exhq/ajarc/vm/MathJ.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package dev.exhq.ajarc.vm;
|
||||
|
||||
public class MathJ {
|
||||
public int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue