node.js - v8 c++ - how to get object key values provided as arguments -


i'm passing js object function this

myfunc.do({'1':2}); 

i want std::cout key values pairs object in c++

here have

handle<object> object = handle<object>::cast(args[i]); local<array> objarray = object->getpropertynames();   for(uint o=0; o < objarray->length(); o++) {    local<value> val = objarray->get(o);    v8::string::utf8value str(val->tostring());    std::string foo = std::string(*str);        std::cout << "val is= " << foo;  } return; 

i'm doing wrong object->getpropertynames();, because not gets me passed values

update here useless try

local<context> context = isolate->getcurrentcontext(); local<object> object = local<object>::cast(args[i]); local<array> props; if (!object->getownpropertynames(context).tolocal(&props)) {   std::cout << "error";   return; }  (uint32_t p = 0; p < props->length(); ++p) {   local<value> name;   local<value> property_value;   props->get(context, p).tolocal(&name);   v8::string::utf8value str(name->tostring());   std::string foo = std::string(*str);       std::cout << "val is= " << foo; // outputs 0 } 

thanks help

a verifiable example, tested on node 7, should work on 6 , 4 too, other versions of node need nan:

// logger.cc #include <string> #include <iostream> #include <node.h>  namespace demo {  using v8::context; using v8::function; using v8::functioncallbackinfo; using v8::functiontemplate; using v8::isolate; using v8::local; using v8::number; using v8::object; using v8::persistent; using v8::string; using v8::value; using v8::array; using v8::exception;  // logging function objects void log(const functioncallbackinfo<value>& args) {   isolate* isolate = args.getisolate();    if(args.length() < 1 || !args[0]->isobject()) {     isolate->throwexception(exception::typeerror(     string::newfromutf8(isolate, "error: 1 object expected")));     return;   }    local<context> context = isolate->getcurrentcontext();   local<object> obj = args[0]->toobject(context).tolocalchecked();   local<array> props = obj->getownpropertynames(context).tolocalchecked();    for(int = 0, l = props->length(); < l; i++) {     local<value> localkey = props->get(i);     local<value> localval = obj->get(context, localkey).tolocalchecked();     std::string key = *string::utf8value(localkey);     std::string val = *string::utf8value(localval);     std::cout << key << ":" << val << std::endl;   } }  void createfunction(const functioncallbackinfo<value>& args) {   isolate* isolate = args.getisolate();    local<functiontemplate> tpl = functiontemplate::new(isolate, log);   local<function> fn = tpl->getfunction();    // omit make anonymous   fn->setname(string::newfromutf8(isolate, "loggerfunction"));    args.getreturnvalue().set(fn); }  void init(local<object> exports, local<object> module) {   node_set_method(module, "exports", createfunction); }  node_module(logger, init)  }  // namespace demo 

bindings:

{   "targets": [     {       "target_name": "logger",       "sources": [         "logger.cc"       ]     }   ] } 

tester:

const logger = require('./build/release/logger'); var log = logger();  log({'cave' :'johnson'}); log({'1':2}); log({a: 1}); 

output:

cave:johnson 1:2 a:1 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -