1

I'm using J2V8 port for Android (https://github.com/eclipsesource/J2V8).

Is it possible to enable context methods (setInterval, setTimeout, ..)?

V8 runtime = V8.createV8Runtime("global");
runtime.executeIntegerScript("setInterval(function() { 
console.log(\"Hello\"); }, 1000)");

It fails with error: "ReferenceError: setInterval is not defined".

Or engines can execute only pure javascript?

Zalexei
  • 93
  • 2
  • 10

1 Answers1

1

V8 Engine can execute only pure javascript. But you mimic the same, by registering setTimeout method in the engine, when you get the call for this function, you can schedule. like following. But you have to use Executors.newSingleThreadScheduledExecutor()

private var setTimeOutCallback: JavaCallback = JavaCallback { _, v8Array ->
    val v8Function = v8Array.getObject(0) as V8Function
    val time = v8Array.getInteger(1).toLong()
    val taskId = Random.nextInt(1000, 9999)
    val task = v8Executor.schedule({
        v8Function.call(runtime, null)
    }, time, TimeUnit.MILLISECONDS)
    taskId
}
Gopi S
  • 523
  • 1
  • 5
  • 17
  • What is the v8Executor. Should and how can I set the v8Executor to the v8Runtime? – user3875388 May 24 '20 at 16:34
  • Here v8Executor is Executors.newSingleThreadScheduledExecutor() you have to create runtime inside executor.submit then call all js call is executor.submit or exector.schedule – Gopi S May 26 '20 at 10:17