Skip to content

Commit

Permalink
Fix for the NodeClass is not a constructor issue (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner authored Mar 26, 2024
1 parent 7fba7a4 commit 7ff112e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ Napi::Value GetMarshalNodes(const Napi::CallbackInfo &info,
for (unsigned i = 0; i < node_count; i++) {
TSNode node = nodes[i];
const auto &cache_entry = tree->cached_nodes_.find(node.id);
if (cache_entry == tree->cached_nodes_.end()) {
Napi::Value value;
if (cache_entry != tree->cached_nodes_.end() && (value = cache_entry->second->node.Value(), !value.IsEmpty())) {
result[i] = value;
} else {
MarshalNodeId(node.id, p);
p += 2;
*(p++) = node.context[0];
Expand All @@ -73,8 +76,6 @@ Napi::Value GetMarshalNodes(const Napi::CallbackInfo &info,
} else {
result[i] = env.Null();
}
} else {
result[i] = cache_entry->second->node.Value();
}
}
return result;
Expand All @@ -84,7 +85,10 @@ Napi::Value GetMarshalNode(const Napi::CallbackInfo &info, const Tree *tree, TSN
Env env = info.Env();
auto* data = env.GetInstanceData<AddonData>();
const auto &cache_entry = tree->cached_nodes_.find(node.id);
if (cache_entry == tree->cached_nodes_.end()) {
Napi::Value value;
if (cache_entry != tree->cached_nodes_.end() && (value = cache_entry->second->node.Value(), !value.IsEmpty())) {
return value;
} else {
setup_transfer_buffer(env, 1);
uint32_t *p = data->transfer_buffer;
MarshalNodeId(node.id, p);
Expand All @@ -96,8 +100,6 @@ Napi::Value GetMarshalNode(const Napi::CallbackInfo &info, const Tree *tree, TSN
if (node.id != nullptr) {
return Number::New(env, ts_node_symbol(node));
}
} else {
return cache_entry->second->node.Value();
}
return env.Null();
}
Expand Down
12 changes: 9 additions & 3 deletions src/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ Napi::Value Tree::Edit(const Napi::CallbackInfo &info) {

for (auto &entry : cached_nodes_) {
Object js_node = entry.second->node.Value();
if (js_node.IsEmpty()) {
continue;
}
TSNode node;
node.id = entry.first;
for (unsigned i = 0; i < 4; i++) {
Expand Down Expand Up @@ -256,12 +259,15 @@ void CacheNodeForTree(Tree *tree, Napi::Env _env, Object js_node) {
};
const void *key = UnmarshalNodeId(key_parts);

// A Node could have been garbage collected but the finalizer has not yet run to remove its cache entry
if (tree->cached_nodes_.count(key)) {
return;
}

auto *cache_entry = new Tree::NodeCacheEntry{tree, key, {}};
cache_entry->node.Reset(js_node, 0);
cache_entry->node = Napi::Weak(js_node);
js_node.AddFinalizer(&FinalizeNode, cache_entry);

assert(!tree->cached_nodes_.count(key));

tree->cached_nodes_[key] = cache_entry;
}

Expand Down

0 comments on commit 7ff112e

Please sign in to comment.