neon-bindings / rfcs

RFCs for changes to Neon
Apache License 2.0
14 stars 9 forks source link

Incorrect constructor functions for typed arrays #48

Closed sockmaster27 closed 8 months ago

sockmaster27 commented 8 months ago

When returning a typed array, like JsInt16Array for example, the following JavaScript expression will evaluate to false:

x instanceof Int16Array

Using Jest, this assertion

expect(arr).toBeInstanceOf(Int16Array)

fails and reports the following:

Expected constructor: Int16Array
Received constructor: Int16Array

This makes me suspect that Neon is using a constructor function with an identical name, but different equality identity.

kjvalencik commented 8 months ago

@sockmaster27 are you using any transpilers that could be polyfilling or transforming typed arrays?

I am not ables to reproduce with a simple test:

use neon::prelude::*;

#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
    cx.export_function("array", |mut cx| {
        JsInt16Array::from_slice(&mut cx, &[0, 1, 2, 3])
    })?;

    Ok(())
}
npm install
node -e 'console.log(require(".").array() instanceof Int16Array)'
# true

Internally, Neon is calling napi_create_typed_array.

sockmaster27 commented 8 months ago

It appears to be a problem with Jest. Sorry for the inconvenience.