What is the correct way to use v8 :: Locker and why should I use it?

I am trying to embed v8 in an android application using NDK.

I have a JNI module that looks something like this (JNI mapping code not shown):

#include <jni.h>
#include <android/log.h>

#include <v8.h>
using namespace v8;

static jlong getMagicNumber() {
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  Handle<String> source = String::New("40 + 2");

  Handle<Script> script = Script::Compile(source);
  Handle<Value> result = script->Run();

  context.Dispose();

  return result->NumberValue();
}

The first time getMagicNumberit starts, it starts correctly and returns 42. The second time, when I try to start it, it fails.

In particular, this ASSERTobserved in v8 isolate.hfails:

// Returns the isolate inside which the current thread is running.
INLINE(static Isolate* Current()) {
  Isolate* isolate = reinterpret_cast<Isolate*>(
      Thread::GetExistingThreadLocal(isolate_key_));
  ASSERT(isolate != NULL);
  return isolate;
}

This is similar to this problem , which is proposed to be used v8::Lockerto obtain "exclusive access to the isolate."

By adding simple Locker l;to the beginning getMagicNumber, the crash no longer occurs. Problems that fix themselves, that easily tend to break themselves when I do not pay attention.

, , , v8::Locker . v8::Isolate v8::Locker, , "" .

: v8 ?

+5
2

, V8 V8, , V8. v8::Locker.

, V8, :

v8::Isolate* isolate = v8::Isolate::New();

, :

v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);

, ..

, , V8 , , V8 . , , .

+6

V8, , :

v8:: Locker locker ();

Locker, , Isolate . , , Isolate.

:

v8:: :: Scope ();

. . Locker , Isolate . , , , . Locker, ( ), Scope , Isolate . , , API V8 Isolate, . , (, ).

Isolate:: Scope - call isolate:: Enter() :: Exit() . , , Enter()/Exit().

0

All Articles