Search code examples
javascriptc++v8

Access 'Arguments' in JavaScript V8 Callback


I'd like to be able to access the Arguments& args of a callback. Right now when I set the C++ function to be called from the JavaScript side it looks like this:

global->Set(String::New("login"), FunctionTemplate::New(Login));

And the prototype of the C++ function is:

Handle<Value> MyClass::Login(const Arguments& args)

How do I access this Arguments& args so that I can pass in a variable when the callback occurs? Thanks for the help.


Solution

  • v8::Arguments::Length() will return the number of arguments passed from JavaScript. The v8::Arguments& variable is accessed using an array subscript.

    for (int32_t index = 0; index < arguments->Length(); ++index) {
      if (arguments[index]->IsString()) {
        ::printf("%s\n", *v8::String::Utf8Value(arguments[index]->ToString()));
      }
    }
    

    You will find an online version of the v8::Arguments documentation at http://izs.me/v8-docs/classv8_1_1Arguments.html, however I make no guarantees that it is current or will remain online.

    Running the following command from the top of the V8 source tree will generate the documentation locally using doxygen.

    $ cd include && doxygen -g && doxygen
    

    If doxygen executes successfully you can access the documentation in include/html/index.html.