How to parse object arguments in C ++ v8

I want to have javascript access to my C ++ function with google v8 engine and return the result according to the arguments provided in my javascript function.

My javascript function is as follows:

var result = MyFc( { 'stringData':'abc', 'numberData':123, 'arrData':[1,2,3], 'objData':{'a':true,'b':false,'c':true}, 'callback':function(){} } ); 

I used

 global->Set(v8::String::New("myFc"), v8::FunctionTemplate::New(MyFc)); v8::Handle<v8::Value> MyFc(const v8::Arguments& args) { obj = args[0]->... if( obj->stringData != 'abc' ){ //.... } if( obj->numberData != 123 ){ //.... } if( obj->arrData[2] != 3 ){ //.... } if( obj->objData->b == false ){ //.... } if( obj->callback !='abc' ){ //.... } } 

My question is how to parse argument objects in C ++ v8? I want to access all the key values ​​of an object in arguments, the values ​​can be a number, a string, an array, an anonymous function, or an object.

+7
source share
2 answers

Use type checking methods v8::Value ( IsBoolean() , IsArray() , IsObject() , etc.), and then assign values ​​to v8::Handle<T>::Cast(v8::Handle<S>) args[i] corresponding types.

Your code should look like this:

 if (args[0]->IsArray()) { Handle<Array> array = Handle<Array>::Cast(arg[0]); for (int i = 0; i < array->Length(); i++) { //... } } if (args[1]->IsObject()) { Handle<Object> object = Handle<Object>::Cast(args[i]); Handle<Value> fieldValue = object->Get(String::New("a")); Handle<Value> callback = object->Get(String::New("callback")); if (callback->IsFunction()) { Handle<Function> fn = Handle<Function>::Cast(callback); } } 
+13
source

Attention :

This method only works until Node JS version 0.8 . As of August 19, 2013, the v8 juice project is being abandoned due to the degree of incompatibility changes in the v8 core made in the first half of 2013.

Work on updating aponxi / npm-execxi code to interpret JavaScript variables in C ++ without using the cvv8 library.


This is how I handle the arguments. Hope this helps someone!

The full script is in execxi.cpp at aponxi / npm-execxi

Note. I am using v8 :: juice to convert from JS to C ++

 #include <node/node.h> #include <v8.h> #include <stdio.h> #include <stdlib.h> #include <cstdio> #include <cvv8/convert.hpp> #include <iostream> #include <string> #include <sstream> #include <set> namespace cv = cvv8; using namespace v8; Handle<Value> executeArray(const Arguments& args) { HandleScope scope; // here I define default options in c++ // later we will parse options that come from js // and convert values to C++ values, and replace // these default options with the one provided // for the sace of example I just copy pasted one // option here: // OPTIONS ///////////////////// // // Chained (bool) // If a command doesn't exit with 0 then we won't run the next command // default: true bool Chained = true; // there is code here that checks if first arguments is passed // and handles that argument // the second argument is options, as if // "myfunction("arg1", {chained: false})" // comes from javascript. // This part below checks if there are two arguments // since passing options is totally optional. // we begin by checking if two arguments are passed: if (args.Length() == 2) { if (args[1]->IsObject()) { // you must be passing options... // it must be an object Handle<Object> opt = Handle<Object>::Cast(args[1]); if (opt->Has(String::New("chained"))) { // you passed chained option if ((opt->Get(String::New("chained"))->IsBoolean())){ // the chained option must be bool Handle<Value> _chained = opt->Get(String::New("chained")); Chained = cv::CastFromJS<bool>(_chained); } else { // chained is not bool, throw error ThrowException(Exception::TypeError(String::New("not bool"))); return scope.Close(Undefined()); } } } } } 
0
source

All Articles