Search code examples
node.jsv8node.js-nan

Nan::To<T> accepts all conversions


I'm trying to write an argument verification helper function but apparently Nan::To never fails to convert and object to T, even if the conversion is impossible.

//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)
})

})

In the code above if my understanding is correct, the Maybe object returned from Nan::To should be empty. There shouldn't be a legal conversion from json to u_int32_t, function to u_int32_t or undefined to u_int32_t.

But I still get as if all 3 cases are just.

Am I misunderstanding the function Nan::To?


Solution

  • See discussion:

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

    The short answer:

    Assuming u_int32_t is the same as uint32_t, then yes, there is. The converters follow JS semantics; e.g. undefined >>> 0 === 0