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

标签 c++ v8 embedded-v8

假设我有这段代码:

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/

相关文章:

c++ - 取消引用空指针时是否保证段错误 (C/C++)

c++ - 在 Windows 上获取实际文件名(使用正确的大小写)

ruby - therubyracer v8 init.so 段错误

javascript - V8 不会清理所有垃圾

javascript - 如何在 v8 Javascript 中跨多个函数使用相同的上下文?

c++ - 如何将 URI 列表编码为字符串?

C++密码程序,字符串在退格时不会删除最后一个字符

c++ - native v8::Promise 结果

node.js - 在现代 Node.js 中更新 Error 对象的性能损失

c++ - 如何释放 V8 占用的内存?