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:
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 ?