【问题标题】:Nan::To<T> accepts all conversionsNan::To<T> 接受所有转换
【发布时间】:2018-07-01 13:07:45
【问题描述】:

我正在尝试编写一个参数验证辅助函数,但显然 Nan::To 永远不会失败转换并反对 T,即使转换是不可能的。

//convert.cpp

template <typename T>
bool argument_verify(Local<Value> const& in_value, T & out_value)
{
    auto maybe_value = Nan::To<T>(in_value);
    if (maybe_value.IsJust()) return true;
    else return false;
}

void test(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    u_int32_t   param1;
    bool verify_param1 = argument_verify(args[0], param1);

    args.GetReturnValue().Set(verify_param1);

}

//test.js

describe('Sandbox - This is just a place for testing stuff', function() {
it('Testing new library argument verification', function() {

    const out_buffer = cil.test(function(){})
    console.log(out_buffer)
})
it('Testing new library argument verification', function() {

    const out_buffer = cil.test({"this_is":"json"})
    console.log(out_buffer)
})
it('Testing new library argument verification', function() {

    const out_buffer = cil.test()
    console.log(out_buffer)
})

})

在上面的代码中,如果我的理解是正确的,从 Nan::To 返回的 Maybe 对象应该是空的。不应该有从 json 到 u_int32_t、函数到 u_int32_t 或 undefined 到 u_int32_t 的合法转换。

但我仍然觉得这 3 个案例都是公正的。

我是不是误解了 Nan::To 函数?

【问题讨论】:

    标签: node.js v8 node.js-nan


    【解决方案1】:

    查看讨论:

    https://github.com/nodejs/nan/issues/726

    简短的回答:

    假设 u_int32_t 与 uint32_t 相同,那么是的,有。这 转换器遵循 JS 语义;例如未定义 >>> 0 === 0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2010-12-02
      • 2018-10-27
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多