【问题标题】:v8 c++ - how to get object key values provided as argumentsv8 c++ - 如何获取作为参数提供的对象键值
【发布时间】:2017-03-24 19:28:12
【问题描述】:

我像这样将一个 js 对象传递给我的函数

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

我想std::cout c++ 中我的对象中的那些键值对

这就是我所拥有的

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;

我对 object-&gt;GetPropertyNames(); 做错了,因为它没有让我得到传递的值

更新 这是另一个无用的尝试

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;
}

for (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
}

感谢帮助

【问题讨论】:

    标签: c++ node.js object v8


    【解决方案1】:

    一个可验证的示例,在节点 7 上测试,应该也适用于 6 和 4,对于其他版本的节点,您可能需要 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 for 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: One 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 i = 0, l = props->Length(); i < 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 this to make it 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
    

    绑定:

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

    测试人员:

    const logger = require('./build/Release/logger');
    var log = logger();
    
    log({'Cave' :'Johnson'});
    log({'1':2});
    log({a: 1});
    

    输出:

    Cave:Johnson
    1:2
    a:1
    

    【讨论】:

    • 你先生是个天才:)
    【解决方案2】:

    这是我的实现:

    void do_handler(const FunctionCallbackInfo<Value>& args)
    {
      Isolate* isolate = args.GetIsolate();
      if (args.Length() < 1 && !args[0]->IsObject()) {
        isolate->ThrowException(Exception::TypeError(
          String::NewFromUtf8(isolate, "Wrong arguments: object expected")));
        return;
      }
      auto obj = args[0]->ToObject(isolate);
      Local<Array> props = obj->GetOwnPropertyNames();
      for (uint32_t i = 0; i < props->Length(); ++i) {
        const Local<Value> key_local = props->Get(i);
        const Local<Value> val_local = obj->Get(key_local);
        std::string key = *String::Utf8Value(key_local);
        std::string val = *String::Utf8Value(val_local);
        std::cout << "\"" << key << "\"" << ": \"" << val << "\"" << std::endl;
      }
    }
    

    【讨论】:

    • 这对我不起作用输出是"0": "[object Object]" for console.log(datatypes.inArray([{'1':1}],{'1':1}));
    • 我希望得到类似 "1": "1"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多