From ad3907c2a6f9aee249677493f9444de868ed06bd Mon Sep 17 00:00:00 2001 From: Douglas Creager Date: Wed, 2 Jun 2021 13:51:00 -0400 Subject: [PATCH] wasm: Add matchLimit option to query methods This exposes the new configurable match limits for query cursors. --- lib/binding_web/binding.c | 16 ++++++++++++++-- lib/binding_web/binding.js | 26 ++++++++++++++++++++++---- lib/binding_web/test/query-test.js | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/binding_web/binding.c b/lib/binding_web/binding.c index 8adaec75..27292911 100644 --- a/lib/binding_web/binding.c +++ b/lib/binding_web/binding.c @@ -594,9 +594,15 @@ void ts_query_matches_wasm( uint32_t start_row, uint32_t start_column, uint32_t end_row, - uint32_t end_column + uint32_t end_column, + uint32_t match_limit ) { if (!scratch_query_cursor) scratch_query_cursor = ts_query_cursor_new(); + if (match_limit == 0) { + ts_query_cursor_set_match_limit(scratch_query_cursor, UINT32_MAX); + } else { + ts_query_cursor_set_match_limit(scratch_query_cursor, match_limit); + } TSNode node = unmarshal_node(tree); TSPoint start_point = {start_row, code_unit_to_byte(start_column)}; @@ -635,9 +641,15 @@ void ts_query_captures_wasm( uint32_t start_row, uint32_t start_column, uint32_t end_row, - uint32_t end_column + uint32_t end_column, + uint32_t match_limit ) { if (!scratch_query_cursor) scratch_query_cursor = ts_query_cursor_new(); + if (match_limit == 0) { + ts_query_cursor_set_match_limit(scratch_query_cursor, UINT32_MAX); + } else { + ts_query_cursor_set_match_limit(scratch_query_cursor, match_limit); + } TSNode node = unmarshal_node(tree); TSPoint start_point = {start_row, code_unit_to_byte(start_column)}; diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 1f9ef412..bf0a91ce 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -953,9 +953,17 @@ class Query { this[0] = 0; } - matches(node, startPosition, endPosition) { + matches(node, startPosition, endPosition, options) { if (!startPosition) startPosition = ZERO_POINT; if (!endPosition) endPosition = ZERO_POINT; + if (!options) options = {}; + + let matchLimit = options.matchLimit; + if (typeof matchLimit === 'undefined') { + matchLimit = 0; + } else if (typeof matchLimit !== 'number') { + throw new Error('Arguments must be numbers'); + } marshalNode(node); @@ -965,7 +973,8 @@ class Query { startPosition.row, startPosition.column, endPosition.row, - endPosition.column + endPosition.column, + matchLimit ); const rawCount = getValue(TRANSFER_BUFFER, 'i32'); @@ -1000,9 +1009,17 @@ class Query { return result; } - captures(node, startPosition, endPosition) { + captures(node, startPosition, endPosition, options) { if (!startPosition) startPosition = ZERO_POINT; if (!endPosition) endPosition = ZERO_POINT; + if (!options) options = {}; + + let matchLimit = options.matchLimit; + if (typeof matchLimit === 'undefined') { + matchLimit = 0; + } else if (typeof matchLimit !== 'number') { + throw new Error('Arguments must be numbers'); + } marshalNode(node); @@ -1012,7 +1029,8 @@ class Query { startPosition.row, startPosition.column, endPosition.row, - endPosition.column + endPosition.column, + matchLimit ); const count = getValue(TRANSFER_BUFFER, 'i32'); diff --git a/lib/binding_web/test/query-test.js b/lib/binding_web/test/query-test.js index b7b2e053..2b2aebe0 100644 --- a/lib/binding_web/test/query-test.js +++ b/lib/binding_web/test/query-test.js @@ -256,7 +256,7 @@ describe("Query", () => { (array (identifier) @pre (identifier) @post) `); - const captures = query.captures(tree.rootNode); + const captures = query.captures(tree.rootNode, null, null, {matchLimit: 32}); assert.ok(query.didExceedMatchLimit()); }); });