Skip to content

Commit 7ff112e

Browse files
authored
Fix for the NodeClass is not a constructor issue (#197)
1 parent 7fba7a4 commit 7ff112e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/node.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ Napi::Value GetMarshalNodes(const Napi::CallbackInfo &info,
6161
for (unsigned i = 0; i < node_count; i++) {
6262
TSNode node = nodes[i];
6363
const auto &cache_entry = tree->cached_nodes_.find(node.id);
64-
if (cache_entry == tree->cached_nodes_.end()) {
64+
Napi::Value value;
65+
if (cache_entry != tree->cached_nodes_.end() && (value = cache_entry->second->node.Value(), !value.IsEmpty())) {
66+
result[i] = value;
67+
} else {
6568
MarshalNodeId(node.id, p);
6669
p += 2;
6770
*(p++) = node.context[0];
@@ -73,8 +76,6 @@ Napi::Value GetMarshalNodes(const Napi::CallbackInfo &info,
7376
} else {
7477
result[i] = env.Null();
7578
}
76-
} else {
77-
result[i] = cache_entry->second->node.Value();
7879
}
7980
}
8081
return result;
@@ -84,7 +85,10 @@ Napi::Value GetMarshalNode(const Napi::CallbackInfo &info, const Tree *tree, TSN
8485
Env env = info.Env();
8586
auto* data = env.GetInstanceData<AddonData>();
8687
const auto &cache_entry = tree->cached_nodes_.find(node.id);
87-
if (cache_entry == tree->cached_nodes_.end()) {
88+
Napi::Value value;
89+
if (cache_entry != tree->cached_nodes_.end() && (value = cache_entry->second->node.Value(), !value.IsEmpty())) {
90+
return value;
91+
} else {
8892
setup_transfer_buffer(env, 1);
8993
uint32_t *p = data->transfer_buffer;
9094
MarshalNodeId(node.id, p);
@@ -96,8 +100,6 @@ Napi::Value GetMarshalNode(const Napi::CallbackInfo &info, const Tree *tree, TSN
96100
if (node.id != nullptr) {
97101
return Number::New(env, ts_node_symbol(node));
98102
}
99-
} else {
100-
return cache_entry->second->node.Value();
101103
}
102104
return env.Null();
103105
}

src/tree.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ Napi::Value Tree::Edit(const Napi::CallbackInfo &info) {
102102

103103
for (auto &entry : cached_nodes_) {
104104
Object js_node = entry.second->node.Value();
105+
if (js_node.IsEmpty()) {
106+
continue;
107+
}
105108
TSNode node;
106109
node.id = entry.first;
107110
for (unsigned i = 0; i < 4; i++) {
@@ -256,12 +259,15 @@ void CacheNodeForTree(Tree *tree, Napi::Env _env, Object js_node) {
256259
};
257260
const void *key = UnmarshalNodeId(key_parts);
258261

262+
// A Node could have been garbage collected but the finalizer has not yet run to remove its cache entry
263+
if (tree->cached_nodes_.count(key)) {
264+
return;
265+
}
266+
259267
auto *cache_entry = new Tree::NodeCacheEntry{tree, key, {}};
260-
cache_entry->node.Reset(js_node, 0);
268+
cache_entry->node = Napi::Weak(js_node);
261269
js_node.AddFinalizer(&FinalizeNode, cache_entry);
262270

263-
assert(!tree->cached_nodes_.count(key));
264-
265271
tree->cached_nodes_[key] = cache_entry;
266272
}
267273

0 commit comments

Comments
 (0)