0

I have already successfully gotten V8 to run arbitrary Javascript files. The trouble comes when I try to expose a C++ function so that it can be run by the Javascript code.

I define the following simple toy function,

v8::Handle<v8::Value> f(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    v8::Isolate* isolate = args.GetIsolate();
    v8::HandleScope scope(isolate);
    
    return v8::Number::New(isolate,2);
}

Then, I attempt to add it to a global object template as follows.

        v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
            global->Set(isolate, "f",
                        v8::FunctionTemplate::New(isolate, f) );

        v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global);

This exact pattern worked perfectly fine when f was a void function, but now that I ask for it to give a return value, I get the following error.

cisco/test.cpp: In function ‘int main(int, char**)’:
cisco/test.cpp:52:61: error: invalid conversion from ‘v8::Handle<v8::Value> (*)(const v8::FunctionCallbackInfo<v8::Value>&) {aka v8::Local<v8::Value> (*)(const v8::FunctionCallbackInfo<v8::Value>&)}’ to ‘v8::FunctionCallback {aka void (*)(const v8::FunctionCallbackInfo<v8::Value>&)}’ [-fpermissive]
                         v8::FunctionTemplate::New(isolate, f) );
                                                             ^
In file included from cisco/test.cpp:2:0:
./include/v8.h:6495:34: note:   initializing argument 2 of ‘static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, v8::FunctionCallback, v8::Local<v8::Value>, v8::Local<v8::Signature>, int, v8::ConstructorBehavior, v8::SideEffectType, const v8::CFunction*, uint8_t, uint8_t, uint8_t)’
   static Local<FunctionTemplate> New(

The samples from the V8 docs do not have any non-void examples. In fact, I only know that the return type should be Handle<Value> because of this example from 2011. It basically uses the same pattern I did to add the global function (I did try changing Local<ObjectTemplate> to Handle<ObjectTemplate>, but this didn't fix it), so I'm not sure why mine isn't working.

I'm somewhat at a loss for how to proceed. Any help would be appreciated.

1 Answer 1

1

https://v8docs.nodesource.com/node-16.0/dd/d0d/classv8_1_1_function_callback_info.html

Return void. Your args has a ReturnValue property. You can set your return value into it.

args.GetReturnValue().Set( 2 );

I find google code tends to change APIs a lot, so looking at current docs and/or source is your best bet.

Sign up to request clarification or add additional context in comments.

6 Comments

You're a godsend, thank you. I wasn't able to find any explanations for how that worked anywhere, and the samples from the current docs didn't help either. Do you have any recommendations for any projects with up-to-date V8 code that I can refer to as a sample?
@cisco Nope, I just googled some terms in your error messages and read the docs. Have never touched v8 personally. Hard part was reading current, not decade old, docs.
That makes sense, I suppose my googling isn't so strong then. v8.dev/docs/embed isn't a decade old, though, and still contained a frustrating lack of information. Looking at decade-old samples on github probably wasn't the smartest idea, though. But anyway, thanks for the help.
Chrome code-search can help you out as well to find users of an existing API. test-api.cc is the place where V8 dumps detailed tests: source.chromium.org/chromium/chromium/src/+/main:v8/test/cctest/…
FWIW, I count 8 usages of GetReturnValue().Set(...) in the V8 samples you already linked to; v8.dev/docs/embed mentions it three times.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.