mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-09 22:54:03 +09:00
re-impl of js eval function because wtf js
This commit is contained in:
@@ -268,10 +268,15 @@ if ('function' !== typeof Array.prototype.reduceRight) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// NOTE TO PROGRAMMERS: this JS_INIT script does not, and must not be invoked with strict mode //
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
load = undefined
|
load = undefined;
|
||||||
loadWithNewGlobal = undefined
|
loadWithNewGlobal = undefined;
|
||||||
|
var eval = function(s) { // installing new eval function
|
||||||
|
return Function('"use strict";return(function(){'+s+'}())')();
|
||||||
|
}
|
||||||
//
|
//
|
||||||
function javaArrayToJs(jarr) {
|
function javaArrayToJs(jarr) {
|
||||||
if (!jarr.toString.startsWith("[")) return jarr;
|
if (!jarr.toString.startsWith("[")) return jarr;
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package net.torvald.tsvm
|
package net.torvald.tsvm
|
||||||
|
|
||||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
|
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
|
||||||
import jdk.nashorn.api.scripting.ScriptUtils
|
|
||||||
import jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java
|
|
||||||
import net.torvald.tsvm.peripheral.GraphicsAdapter
|
import net.torvald.tsvm.peripheral.GraphicsAdapter
|
||||||
import net.torvald.tsvm.vdc.Videotron2K
|
import net.torvald.tsvm.vdc.Videotron2K
|
||||||
import java.io.FileReader
|
import java.io.FileReader
|
||||||
@@ -68,7 +66,7 @@ object VMRunnerFactory {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
val engineFactory = NashornScriptEngineFactory()
|
val engineFactory = NashornScriptEngineFactory()
|
||||||
engine = engineFactory.getScriptEngine("-strict", "--no-java", "--no-syntax-extensions", "--language=es6")
|
engine = engineFactory.getScriptEngine("--no-java", "--no-syntax-extensions", "--language=es6")
|
||||||
assertNotNull(engine, "Script engine for extension $extension not found")
|
assertNotNull(engine, "Script engine for extension $extension not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,17 +81,15 @@ object VMRunnerFactory {
|
|||||||
bind.put("base64", Base64Delegate())
|
bind.put("base64", Base64Delegate())
|
||||||
bind.put("com", SerialHelperDelegate(vm))
|
bind.put("com", SerialHelperDelegate(vm))
|
||||||
|
|
||||||
if (extension == "js") {
|
val fr = FileReader("./assets/JS_INIT.js")
|
||||||
val fr = FileReader("./assets/JS_INIT.js")
|
val prg = fr.readText()
|
||||||
val prg = fr.readText()
|
fr.close()
|
||||||
fr.close()
|
engine.eval(sanitiseJS(prg), context)
|
||||||
engine.eval(sanitiseJS(prg), context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun executeCommand(command: String) {
|
override suspend fun executeCommand(command: String) {
|
||||||
try {
|
try {
|
||||||
engine.eval("\"use strict\";" + encapsulateJS(sanitiseJS(command)), context)
|
engine.eval(encapsulateJS(sanitiseJS(command)), context)
|
||||||
}
|
}
|
||||||
catch (e: javax.script.ScriptException) {
|
catch (e: javax.script.ScriptException) {
|
||||||
System.err.println("ScriptException from the script:")
|
System.err.println("ScriptException from the script:")
|
||||||
@@ -113,6 +109,6 @@ object VMRunnerFactory {
|
|||||||
|
|
||||||
|
|
||||||
private fun sanitiseJS(code: String) = code//.replace("\\", "\\\\")
|
private fun sanitiseJS(code: String) = code//.replace("\\", "\\\\")
|
||||||
private fun encapsulateJS(code: String) = "(function(){$code})()"
|
private fun encapsulateJS(code: String) = "\"use strict\";(function(){$code})()"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user