如何多次运行V8评估

How to run V8 evaluation multiple times?

本文关键字:评估 V8 运行 何多次      更新时间:2023-10-16

也许这是一个愚蠢的问题(我是C++新手,只是想把它用作android的库),但我无法多次运行某些JS的评估。

我从"你好世界"教程开始。但后来我想要一件简单的事情,重新运行main(只需将教程代码的内容包装到函数中,并在新的空main中运行两次

这就是我得到的:

#
# Fatal error in ../src/isolate.cc, line 1868
# Check failed: thread_data_table_.
#
==== C stack trace ===============================
1: 0xa890b9
2: 0x6a22fc
3: 0x42694f
4: 0x405f66
5: 0x405ec7
6: __libc_start_main
7: 0x405dc9
Illegal instruction (core dumped)

这是在创建新的隔离之后出现的

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

好吧,我该怎么办?我是不是用错了结构?我应该关闭/删除/清除更多内容吗

从更大的角度来看,我只想做一个evaluate函数,它可以被多次触发,除此之外,还可以在同一上下文中运行多个js-sippets(如何拆分这个函数?)。

知道吗?


更新:

好的,假设主可以分为三个逻辑部分:

init

int main(int argc, char* argv[]) {
// Initialize V8.
V8::InitializeICU();
V8::InitializeExternalStartupData(argv[0]);
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
// Create a new Isolate and make it the current one.
ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;

评估

Isolate* isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source =
String::NewFromUtf8(isolate, "'Hello' + ', World!'",
NewStringType::kNormal).ToLocalChecked();
// Compile the source code.
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
// Run the script to get the result.
Local<Value> result = script->Run(context).ToLocalChecked();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(result);
printf("%sn", *utf8);
}
isolate->Dispose();

清洁

// Dispose and tear down V8.
V8::Dispose();
V8::ShutdownPlatform();
delete platform;
return 0;

现在,正如我之前所说,如果我运行两次由init->evaluation->clean组成的main,这意味着init->evaluation->init->evaluationclean,那么错误就会发生。我已经发现,如果我将evaluation部分提取到单独的函数中,我可以多次运行它,例如作为init->(evaluation){2}->清洁

它应该这样工作吗?下一步是将这个main划分为树独立的函数,这意味着我必须有一个带有平台的静态成员?它会以某种方式导致泄漏吗?

注意:我想从android运行它,这意味着例如点击UI,通过JNI将js源代码传播到C,然后调用C++V8,它是否已经初始化。百米

首选的方式是有"blackbox",但如果我必须持有平台,那就顺其自然吧。如果不重新初始化V8,它可能也会更快,对吧?


更新2:

好吧,拆分评估部分以在同一隔离/上下文中实现多次运行仍然存在问题。

我在创建了带有存储隔离和上下文的上下文后对其进行了拆分,但没有成功。当在第二部分中尝试创建源字符串时,它失败了,可能是因为使用了存储的隔离(我想是具有隔离范围的东西)。

:(

我在UPDATE1中介绍的假设是正确的。那部分效果很好。

根据UPDATE2,我已将评估部分一分为二。

首先初始化隔离和上下文:

mIsolate = Isolate::New(mCreate_params);
Isolate::Scope isolate_scope(mIsolate);
{
// Create a stack-allocated handle scope.
HandleScope handle_scope(mIsolate);
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(mIsolate);
// Bind the global 'print' function to the C++ Print callback.
global->Set(v8::String::NewFromUtf8(mIsolate, "print"), v8::FunctionTemplate::New(mIsolate, Print));
// Create a new context.
mContext = Context::New(mIsolate, NULL, global);
Persistent<Context, CopyablePersistentTraits<Context>> persistent(mIsolate, mContext);
mContext_persistent = persistent;
}

第二个将在同一上下文中运行js:

Isolate::Scope isolate_scope(mIsolate);
{
HandleScope handle_scope(mIsolate);
mContext = Local<Context>::New(mIsolate, mContext_persistent);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(mContext);
{
// Create a string containing the JavaScript source code.
Local<String> source =
String::NewFromUtf8(mIsolate, js_source, NewStringType::kNormal).ToLocalChecked();
// Compile the source code.
Local<Script> script = Script::Compile(mContext, source).ToLocalChecked();
TryCatch trycatch(mIsolate);
// Run the script to get the result.
v8::Local<v8::Value> result;
if(!script->Run(mContext).ToLocal(&result)){
v8::String::Utf8Value exception_str(trycatch.Exception());
dprint(*exception_str);
}else{
if(!result->IsUndefined()){
String::Utf8Value utf8(result);
dprint(*utf8);
}
}
}
} 

好吧,代码在linux上运行得很好,但当我尝试在android上第二次运行第一部分(创建新上下文)时,我仍然有一些问题:

A/art: art/runtime/thread.cc:986] pthread_getschedparam failed for DumpState: No such process
A/art: art/runtime/base/mutex.cc:485] Unexpected state_ 0 in unlock for logging lock

但我想这是另一个问题。和平

您是否多次初始化v8?

v8::V8::Initialize()每个进程应该调用一次此方法。

深入项目源文件"v8/src/v8.cc",你会发现证明

bool V8::Initialize() {
InitializeOncePerProcess();
return true;
}