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 5, 2024
1 parent cd34733 commit 8b1f000
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/koffi/src/ffi.hh
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,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
13 changes: 11 additions & 2 deletions src/koffi/src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,18 @@ void SetValueTag(const InstanceData *instance, Napi::Value value, const void *ma
{
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

0 comments on commit 8b1f000

Please sign in to comment.