add:uptime and currentTimeInMills for JS sys class

This commit is contained in:
minjaesong
2020-09-22 11:22:34 +09:00
parent 50b567d9ce
commit dd3bb54e55
5 changed files with 34 additions and 4 deletions

View File

@@ -54,7 +54,8 @@ import kotlin.random.Random
*/ */
class VM( class VM(
_memsize: Long _memsize: Long,
val worldInterface: WorldInterface
) { ) {
val id = java.util.Random().nextInt() val id = java.util.Random().nextInt()

View File

@@ -11,7 +11,7 @@ import java.io.FileReader
class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter() { class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter() {
val vm = VM(64.kB()) val vm = VM(64.kB(), TheRealWorld())
lateinit var gpu: GraphicsAdapter lateinit var gpu: GraphicsAdapter
lateinit var batch: SpriteBatch lateinit var batch: SpriteBatch

View File

@@ -1,6 +1,6 @@
package net.torvald.tsvm package net.torvald.tsvm
import net.torvald.tsvm.peripheral.GraphicsAdapter import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUlong
/** /**
* Pass the instance of the class to the ScriptEngine's binding, preferably under the namespace of "vm" * Pass the instance of the class to the ScriptEngine's binding, preferably under the namespace of "vm"
@@ -13,6 +13,23 @@ class VMJSR223Delegate(val vm: VM) {
fun malloc(size: Int) = vm.malloc(size) fun malloc(size: Int) = vm.malloc(size)
fun free(ptr: Int) = vm.free(ptr) fun free(ptr: Int) = vm.free(ptr)
fun uptime(): Long {
vm.poke(-69, -1)
var r = 0L
for (i in 0L..7L) {
r = r or vm.peek(-73 - i)!!.toUlong().shl(8 * i.toInt())
}
return r
}
fun currentTimeInMills(): Long {
vm.poke(-69, -1)
var r = 0L
for (i in 0L..7L) {
r = r or vm.peek(-81 - i)!!.toUlong().shl(8 * i.toInt())
}
return r
}
fun print(s: String) { fun print(s: String) {
//System.out.print("[Nashorn] $s") //System.out.print("[Nashorn] $s")
vm.getPrintStream().write(s.toByteArray()) vm.getPrintStream().write(s.toByteArray())

View File

@@ -0,0 +1,12 @@
package net.torvald.tsvm
interface WorldInterface {
fun currentTimeInMills(): Long
}
/**
* Real world interface for non-ingame testing. For the Ingame, implement your own.
*/
class TheRealWorld : WorldInterface {
override fun currentTimeInMills(): Long = System.currentTimeMillis()
}

View File

@@ -209,7 +209,7 @@ class IOSpace(val vm: VM) : PeriBase, InputProcessor {
if (RTClatched) { if (RTClatched) {
RTClatched = false RTClatched = false
rtc = System.currentTimeMillis() rtc = vm.worldInterface.currentTimeInMills()
} }
} }