#1491·napi-rs

Add type tags to `napi-sys`.

Author: cutsoyCreated Feb 19, 2023Updated Jun 3, 2026
Labelsnode-api

Would it be possible to add the following two functions to napi-sys? Added since N-API v8 (in Node v14.8.0, v12.19.0).

c
napi_status napi_type_tag_object(napi_env env,
                                 napi_value js_object,
                                 const napi_type_tag* type_tag);
 
napi_status napi_check_object_type_tag(napi_env env,
                                       napi_value js_object,
                                       const napi_type_tag* type_tag,
                                       bool* result);

I'm using the napi::sys for some FFI-safe bindings (long story), so if these land in napi::sys that would be enough for me. I can submit a PR for this.


I can also imagine it would be useful to use this in TaggedObject on v8 since it's a bit safer. E.g. the following example segfaults right now:

  1. Assume a JS script like this:
    javascript
    const { one } = require("objc-module");
    const { sum } = require("napi-rs-module");
    
    sum(one(), one());
  2. The Objective-C library calls napi_create_external(...) with data = [NSNumber numberWithInt:1]. This is a tagged pointer [^1], so it doesn't actually point to anything but it also doesn't violate the NAPI contract.
  3. The Rust library segfaults because napi-rs::JsExternal attempts to read the first sizeof(TypeId) bytes at the tagged pointer's address.

Granted, this example isn't supposed to "work" anyway, but I think it would be better to have it throw an exception instead of segfault.


Anyway, summarizing: would it be okay if I submit a PR adding these two functions to napi::sys?

[^1]: NB: Apple's tagged pointers in Objective-C are unrelated to type tagging in NAPI.