Skip to content

Commit

Permalink
Work around N-API bug related to tags
Browse files Browse the repository at this point in the history
At least it seems to be a bug according to the N-API
documentation, or a documentation mistake, and probably comes
from here: nodejs/node#51149

This Node.js code change stores the napi_type_tag pointer and not
its value, but Koffi was using temporaries.
  • Loading branch information
Koromix committed Apr 4, 2024
1 parent 479a1ef commit de377f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/koffi/src/ffi.hh
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ struct InstanceData {
Size base_types_len;

bool debug;

uint64_t tag_lower;
BucketArray<napi_type_tag> tags;
HashMap<const void *, napi_type_tag *> tags_map;

const TypeInfo *void_type;
const TypeInfo *char_type;
Expand Down
16 changes: 13 additions & 3 deletions src/koffi/src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,22 @@ const char *GetValueType(const InstanceData *instance, Napi::Value value)
return "Unknown";
}

void SetValueTag(const InstanceData *instance, Napi::Value value, const void *marker)
void SetValueTag(InstanceData *instance, Napi::Value value, const void *marker)
{
RG_ASSERT(marker);

napi_type_tag tag = { instance->tag_lower, (uint64_t)marker };
napi_status status = napi_type_tag_object(value.Env(), value, &tag);
napi_type_tag *tag = instance->tags_map.FindValue(marker, nullptr);

if (!tag) {
tag = instance->tags.AppendDefault();

tag->lower = instance->tag_lower;
tag->upper = (uint64_t)marker;

instance->tags_map.Set(marker, tag);
}

napi_status status = napi_type_tag_object(value.Env(), value, tag);
RG_ASSERT(status == napi_ok);
}

Expand Down
2 changes: 1 addition & 1 deletion src/koffi/src/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bool CanStoreType(const TypeInfo *type);
// Can be slow, only use for error messages
const char *GetValueType(const InstanceData *instance, Napi::Value value);

void SetValueTag(const InstanceData *instance, Napi::Value value, const void *marker);
void SetValueTag(InstanceData *instance, Napi::Value value, const void *marker);
bool CheckValueTag(const InstanceData *instance, Napi::Value value, const void *marker);

static inline bool IsNullOrUndefined(Napi::Value value)
Expand Down

0 comments on commit de377f1

Please sign in to comment.