mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-11 05:31:51 +09:00
logo for the shell
This commit is contained in:
@@ -100,7 +100,7 @@ class Memvwr(val vm: VM) : JFrame() {
|
||||
}
|
||||
|
||||
if (x + 15 in intArrayOf(3, 7, 11))
|
||||
sb.append('|')
|
||||
sb.append(' ')
|
||||
}
|
||||
|
||||
sb.append("|\n")
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package net.torvald.tsvm
|
||||
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUlong
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* Pass the instance of the class to the ScriptEngine's binding, preferably under the namespace of "vm"
|
||||
*/
|
||||
class VMJSR223Delegate(val vm: VM) {
|
||||
|
||||
private val SYSTEM_CHARSET = Charsets.ISO_8859_1
|
||||
|
||||
fun poke(addr: Int, value: Int) = vm.poke(addr.toLong(), value.toByte())
|
||||
fun peek(addr: Int) = vm.peek(addr.toLong())!!.toInt().and(255)
|
||||
fun nanoTime() = System.nanoTime()
|
||||
@@ -32,11 +35,11 @@ class VMJSR223Delegate(val vm: VM) {
|
||||
|
||||
fun print(s: String) {
|
||||
//System.out.print("[Nashorn] $s")
|
||||
vm.getPrintStream().write(s.toByteArray())
|
||||
vm.getPrintStream().write(s.toByteArray(SYSTEM_CHARSET))
|
||||
}
|
||||
fun println(s: String) {
|
||||
System.out.println("[Nashorn] $s")
|
||||
vm.getPrintStream().write((s + '\n').toByteArray())
|
||||
vm.getPrintStream().write((s + '\n').toByteArray(SYSTEM_CHARSET))
|
||||
}
|
||||
fun println() = print('\n')
|
||||
|
||||
|
||||
@@ -80,12 +80,19 @@ object VMRunnerFactory {
|
||||
val fr = FileReader("./assets/JS_INIT.js")
|
||||
val prg = fr.readText()
|
||||
fr.close()
|
||||
engine.eval(toSingleLine(prg), context)
|
||||
engine.eval(sanitiseJS(prg), context)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun executeCommand(command: String) {
|
||||
engine.eval("\"use strict\";" + encapsulateJS(sanitiseJS(command)), context)
|
||||
try {
|
||||
engine.eval("\"use strict\";" + encapsulateJS(sanitiseJS(command)), context)
|
||||
}
|
||||
catch (e: javax.script.ScriptException) {
|
||||
System.err.println("ScriptException from the script:")
|
||||
System.err.println(command.substring(0, minOf(1024, command.length)))
|
||||
System.err.println(e)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun evalGlobal(command: String) {
|
||||
@@ -98,8 +105,7 @@ object VMRunnerFactory {
|
||||
}
|
||||
|
||||
|
||||
private fun toSingleLine(code: String) = code.replace(Regex("//[^\\n]*"), "").replace('\n', ' ')
|
||||
private fun sanitiseJS(code: String) = toSingleLine(code).replace("\\", "\\\\")
|
||||
private fun encapsulateJS(code: String) = "eval('$code')"
|
||||
private fun sanitiseJS(code: String) = code//.replace("\\", "\\\\")
|
||||
private fun encapsulateJS(code: String) = "(function(){$code})()"
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user