简体   繁体   English

如何从C ++模块获取node.js哈希的键?

[英]How can I get the keys of a node.js hash from a C++ module?

I have a C++ module that's being called from node.js. 我有一个从node.js调用的C ++模块。 I would like to pass in a hash and retrieve both the keys and values from the C++ code but I cannot figure out how to get them. 我想传递一个散列并从C ++代码中检索键和值,但是我不知道如何获取它们。 Here's what I would like to handle: 这是我要处理的内容:

var mod = require('MyModule')
var conn = mod.createConnection()
conn.connect( { 'uid': 'graeme',
                'pwd': 'mypassword' } )

Inside my C++ method, I can use args[0]->IsObject() to determine that the parameter is a hash, but I can't find a way to get the keys ('uid', 'pwd') or the values ('graeme', 'mypassword') from it. 在我的C ++方法中,我可以使用args[0]->IsObject()确定该参数是一个哈希,但是我找不到找到键('uid','pwd')或值的方法(“ graeme”,“ mypassword”)。

Is there a way to get the keys and values from these kinds of objects? 有没有办法从这类对象中获取键和值?

Your second example of passing in an array doesn't really make sense, but to your general point, you can use GetOwnPropertyNames to read the names of properties. 您传递数组的第二个示例实际上没有任何意义,但是一般而言,您可以使用GetOwnPropertyNames读取属性的名称。

Local<Object> obj = args[0].As<Object>();
Local<Array> props = obj->GetOwnPropertyNames();

for (int i = 0, len = props->Length(); i < len; i++){
    Local<String> key = props->Get(i).As<String>();
    Local<Value> val = obj->Get(key);
}

equivalent to: 相当于:

var props = Object.getOwnPropertyNames(arguments[0]);
for (var i = 0, len = props.length; i < len; i++){
    var key = props[i];
    var value = arguments[0][key];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM