Return Integer from C++ function in d8.cc

60 views
Skip to first unread message

Abhinav Jangda

unread,
Sep 25, 2015, 8:37:24 AM9/25/15
to v8-users
Hello Everyone,

I am doing some changes in d8.cc as needed by my project. For this I need to add and register a C++ function which will be called in Javascript source. This C++ function must return an integer. Here is my function (in v8 namespace)

Local<Value> test (const FunctionCallbackInfo<Value>& args)
{                
 
return args[0];
}


And Here is how I am registering it in CreateGlobalTemplate function of Shell.
Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
 
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
  global_template
->Set(String::NewFromUtf8 (isolate, "test", NewStringType::kNormal).ToLocalChecked(),
 
FunctionTemplate::New (isolate, test));


When I am making d8 I get the following errors

CXX(target) /home/abhi/ssl/v/v8/out/ia32.release/obj.target/d8/src/d8.o
../src/d8.cc: In static member function static v8::Local<v8::ObjectTemplate> v8::Shell::CreateGlobalTemplate(v8::Isolate*)’:
../src/d8.cc:1059:39: error: no matching function for call to v8::FunctionTemplate::New(v8::Isolate*&, v8::Local<v8::Value> (&)(const v8::FunctionCallbackInfo<v8::Value>&))’
   
FunctionTemplate::New (isolate, test));
                                       
^
../src/d8.cc:1059:39: note: candidate is:
In file included from .././src/v8.h:29:0,
                 
from .././src/gdb-jit.h:8,
                 
from ../src/d8.cc:30:
.././include/v8.h:4350:34: note: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, v8::FunctionCallback, v8::Local<v8::Value>, v8::Local<v8::Signature>, int) <near match>
   
static Local<FunctionTemplate> New(
                                 
^
.././include/v8.h:4350:34: note:   no known conversion for argument 2 from v8::Local<v8::Value>(const v8::FunctionCallbackInfo<v8::Value>&)’ to v8::FunctionCallback {aka void (*)(const v8::FunctionCallbackInfo<v8::Value>&)}’


Can anyone please tell me what should be the function declaration if I need to return a value. Any help will be highly appreciated.

Thank You,
Abhinav


Louis Santillan

unread,
Sep 25, 2015, 12:57:08 PM9/25/15
to v8-u...@googlegroups.com
Take an example that already exists in d8.cc [0], the v8 js global
function, `read( fn )` [1], which reads file named in `fn`, converts
the buffer to a v8/js string [2] and returns that to the caller
[3][4]. So the "magic" is in the `args.GetReturnValue().Set(source);`
[4] line. The `const v8::FunctionCallbackInfo<v8::Value>& args` stack
variable (and the return from the C++ function) is the interface
between your code and the v8 runtime.

If `source` in [4] was some other JS type, say, `v8::Integer`,
`v8::Number`, or `v8::Value`, then setting the return value is how you
"dynamically set the type" (JS is dynamic typed language) from C++ (a
statically typed language). It is not necessary to statically declare
what your intention is for the return value, nor are you required to
return only one type for any given input.

[0] https://github.com/v8/v8/blob/master/src/d8.cc
[1] https://github.com/v8/v8/blob/master/src/d8.cc#L545
[2] https://github.com/v8/v8/blob/master/src/d8.cc#L1189
[3] https://github.com/v8/v8/blob/master/src/d8.cc#L551
[4] https://github.com/v8/v8/blob/master/src/d8.cc#L556
> --
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages