Skip to content

Commit

Permalink
fix: parsing issue when string is larger than buffer length
Browse files Browse the repository at this point in the history
This is caused by an annoying limitation of Node-API where you can't create a reference to a String.

Fixes tree-sitter#199
  • Loading branch information
segevfiner authored Aug 17, 2024
1 parent 5946971 commit 9ea6558
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CallbackInput final {
*bytes_read = 0;
String result;
if (!reader->partial_string.IsEmpty()) {
result = reader->partial_string.Value().As<String>();
result = reader->partial_string.Get("value").As<String>();
} else {
Function callback = reader->callback.Value();
Napi::Value result_value = callback({
Expand Down Expand Up @@ -92,7 +92,10 @@ class CallbackInput final {
reader->byte_offset += *bytes_read;

if (utf16_units_read < length) {
reader->partial_string.Reset(slice(result, utf16_units_read));
if (reader->partial_string.IsEmpty()) {
reader->partial_string = Napi::Persistent(Object::New(env));
}
reader->partial_string.Set("value", slice(result, utf16_units_read));
} else {
reader->partial_string.Reset();
}
Expand All @@ -103,7 +106,7 @@ class CallbackInput final {
FunctionReference callback;
std::vector<uint16_t> buffer;
size_t byte_offset {};
Reference<String> partial_string;
ObjectReference partial_string;
};

void Parser::Init(Napi::Env env, Napi::Object exports) {
Expand Down

0 comments on commit 9ea6558

Please sign in to comment.