开发者

Segmentation Fault when wrapping V8 in a class?

开发者 https://www.devze.com 2023-01-26 05:02 出处:网络
I want to use Google\'s Javascript Engine V8 in a project, and attempted to write a wrapper class for the engine. Parts of the Code are copied from samples/shell.cc, from the V8 Distribution.

I want to use Google's Javascript Engine V8 in a project, and attempted to write a wrapper class for the engine. Parts of the Code are copied from samples/shell.cc, from the V8 Distribution.

However, it just aborts with a Segmentation fault, and I can't figure out why, although the problem is happening around v8::internal::Top::global_context() (due to an invalid context, which appears to be NULL).. The code itself looks fine to me, but maybe I did something incredibly stupid :-).

The Segmentation fault in my Code happens in v8::Script::Compile.

Code in Question (Updated): https://gist.githu开发者_运维技巧b.com/4c28227185a14bb6288c

Thanks to Luis G. Costantini R.'s Answer, there is no longer a problem in Set (It doesn't abort anymore), however, exposed names are still not available and will result in a ReferenceError...


Thy to change v8::Context::Scope context_scope(context); from the constructor (line 134) to internal_executeString (before script = v8::Script::Compile(source, name);). That because the destructor of the class v8::Context::Scope exits from the context.

I changed the method addFunction:

void addFunction(const std::string& fname, v8::InvocationCallback func)
{
    v8::HandleScope handle_scope;
    std::cout << "before ::Set()" << std::endl;
    v8::Context::Scope context_scope(context);
    context->Global()->Set(v8::String::New(fname.c_str()),
                           v8::FunctionTemplate::New(func)->GetFunction());
    std::cout << "after ::Set()" << std::endl;
}

The function must be added to the global object of the context used to execute the script. There is an excellent tutorial (in two parts) of V8: http://www.homepluspower.info/2010/06/v8-javascript-engine-tutorial-part-1.html and http://www.homepluspower.info/2010/06/v8-javascript-engine-tutorial-part-2.html


If you try to create an instance of JavaScript Function (FunctionTemplate::GetFunction()) or JavaScript Object (ObjectTemplate::NewInstance()) before entering the context (via Context::Scope), you get the segmentation fault. The reason: there is no JavaScript context available and both Function and Object always exist in a JavaScript execution context only. As per V8 documentation:

Function: A JavaScript function object (ECMA-262, 15.3).

Object: A JavaScript object (ECMA-262, 4.3.3).


The stack backtrace is almost useless unless I download all the source and try to build it myself, so... :)

Change js.executeString("1+1", true, false); to js.executeString("1+1", true, true); and see what the exception handler tells you?


Looks like you just got stung by this bug, that is if you have not already taken note of it. Perhaps submit another report since the referenced one looks old. Perhaps dig a little deeper and investigate the stack frame at every function call until the Segmentation Fault is received, you could either find a work around or the fix for this bug :)


I had a similar segmentation fault and the problem turned out to be the following. I was creating a new thread and attempting to create an object template and object in that thread. Unfortunately it seems that if you create a thread, you need to make sure that you enter a v8::Context again in order to do such things.

I got it working by passing a Handle to the v8::Context::Calling to the newly created thread and entered it in the new thread by using a scope.

I wrote this here as it is the only useful thing that comes up when I do a google search for the segmentation fault.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号