Run() 导致段错误-6ren"> Run() 导致段错误-假设我有这段代码: Local script = Script::Compile(String::New("x1 = 1;"), String::New("main.js")); printf("be-6ren">
gpt4 book ai didi

c++ - v8::Script::Compile(v8::String::New (".make.some.syntax.errors"), v8::String::New ("main"))->Run() 导致段错误

转载 作者:太空狗 更新时间:2023-10-29 23:05:25 24 4
gpt4 key购买 nike

假设我有这段代码:

Local<Script> script = Script::Compile(String::New("x1 = 1;"), String::New("main.js"));
printf("before run\n");
script->Run();
printf("after run\n");

上下文是之前创建和输入的。
此代码的输出是:

before run
after run

这是预期的。但是,如果将一些 javascript 代码放入包含语法错误(例如,".x11 = 1")的 source 中,则输出为:

main.js:0: Uncaught SyntaxError: Unexpected token .
before execution.
Segmentation fault (core dumped)

如果编译有错误,也许我不应该调用 Run 但如何检查呢?

此外:(来自 Getting Starget - Chrome V8 的代码 + 有语法错误的代码 = 同样的东西)

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

// Get the default Isolate created at startup.

Isolate* isolate = Isolate::GetCurrent();

// Create a stack-allocated handle scope.

HandleScope handle_scope(isolate);

// Create a new context.

Handle<Context> context = Context::New(isolate);

// Here's how you could create a Persistent handle to the context, if needed.

Persistent<Context> persistent_context(isolate, context);


// Enter the created context for compiling and

// running the hello world script.

Context::Scope context_scope(context);

// Create a string containing the JavaScript source code.

Handle<String> source = String::New(".>make.some.syntax.errors<");

// Compile the source code.

Handle<Script> script = Script::Compile(source);

// Run the script to get the result.

Handle<Value> result = script->Run();

// The persistent handle needs to be eventually disposed.

persistent_context.Dispose();

// Convert the result to an ASCII string and print it.

String::AsciiValue ascii(result);

printf("%s\n", *ascii);

return 0;

}

最佳答案

经过一段时间的头痛之后,我找到了解决方法。

当脚本编译失败时,它不会向 v8::Handle 返回任何内容。因此,在这种情况下,script.IsEmpty() 返回 true。

为了明确这一点,我对 Google 的 Hello World 代码进行了一些修改:

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

// Get the default Isolate created at startup.

Isolate* isolate = Isolate::GetCurrent();

// Create a stack-allocated handle scope.

HandleScope handle_scope(isolate);

// Create a new context.

Handle<Context> context = Context::New(isolate);

// Here's how you could create a Persistent handle to the context, if needed.

Persistent<Context> persistent_context(isolate, context);


// Enter the created context for compiling and

// running the hello world script.

Context::Scope context_scope(context);

// Create a string containing the JavaScript source code.

Handle<String> source = String::New(".>no.matter.what.code.is<");

// Compile the source code.

Handle<Script> script = Script::Compile(source);
if(!script.IsEmpty()) // is script compiled ?
{
// Run the script to get the result.

Handle<Value> result = script->Run();

// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);

printf("%s\n", *ascii);
}

// The persistent handle needs to be eventually disposed.

persistent_context.Dispose();

return 0;

}

关于c++ - v8::Script::Compile(v8::String::New (".make.some.syntax.errors"), v8::String::New ("main"))->Run() 导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com