我编译了V8在 Ubuntu 上有一个非常简单的 V8 程序,叫做 isolate_test.cc。它基于 Hello World example from Google :
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
V8::initialize();
Isolate* isolate = Isolate::GetCurrent(); //Always returns NULL
return 0;
}
我用来编译这个程序的命令是:
g++ -Iinclude -g isolate_test.cc -o isolate_test -Wl,--start-group out/x64.debug/obj.target/{tools/gyp/libv8_{base,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt -lpthread
问题是 Isolate::GetCurrent()
始终返回 NULL
。为什么会发生这种情况?获取当前 Isolate
的正确方法是什么?
我可能偏离了正轨,但我的第一个想法是这与 Isolate::GetCurrent()
无法从 libpthread
获取当前线程有关。
更新:根据this question我添加了 V8::initialize()
作为程序中的第一个调用,但这并没有解决问题。
最佳答案
我也有同样的问题。我真的不知道根本原因,但是这里的 NULL 表示没有创建和输入默认隔离区。明显的解决方法是手动完成
Isolate* isolate = Isolate::GetCurrent(); // returns NULL
if (!isolate) {
isolate = Isolate::New();
isolate->Enter();
}
关于c++ - 在 V8 中,为什么 Isolate::GetCurrent() 返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825034/