2020-04-28 00:15:24 +00:00
|
|
|
package com.thebrokenrail.scriptcraft.core.quickjs;
|
|
|
|
|
|
|
|
import com.thebrokenrail.scriptcraft.core.OSUtil;
|
|
|
|
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public class QuickJSNative {
|
|
|
|
public QuickJSNative() throws JSException {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
private long ctx;
|
|
|
|
private long rt;
|
|
|
|
|
|
|
|
public native void init() throws JSException;
|
|
|
|
|
|
|
|
public native void free();
|
|
|
|
|
|
|
|
public native Object bridge(String method, Object... args) throws JSException;
|
|
|
|
|
2020-05-26 14:12:45 +00:00
|
|
|
public native void run(String module);
|
2020-04-28 00:15:24 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
File file = File.createTempFile("lib" + ScriptCraftCore.NAMESPACE, OSUtil.getLibExtension());
|
|
|
|
file.deleteOnExit();
|
|
|
|
System.out.println("Extracting ScriptCraft Native To: " + file.getAbsoluteFile().toPath());
|
|
|
|
InputStream so = (QuickJSNative.class.getResourceAsStream(File.separator + "natives" + File.separator + OSUtil.getOS() + File.separator + "lib" + ScriptCraftCore.NAMESPACE + OSUtil.getLibExtension()));
|
|
|
|
if (so == null) {
|
|
|
|
throw new RuntimeException("ScriptCraft does not support your OS: " + OSUtil.getOS());
|
|
|
|
} else {
|
|
|
|
Files.copy(so, file.getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
System.load(file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void print(String data, boolean err) {
|
|
|
|
String[] lines = data.split("\n");
|
|
|
|
for (String line : lines) {
|
|
|
|
if (err) {
|
|
|
|
System.err.println(line);
|
|
|
|
} else {
|
|
|
|
System.out.println(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|