From e8eb3c5d5a1e7eb2f7c61f27dee659bfe00d7e6a Mon Sep 17 00:00:00 2001 From: Douglas Creager Date: Tue, 27 Apr 2021 09:21:38 -0400 Subject: [PATCH] binding_web: Add Query.didExceedMatchLimit This lets wasm clients check whether a query exceeded its maximum number of in-progress matches. --- lib/binding_web/binding.c | 7 +++++++ lib/binding_web/binding.js | 4 ++++ lib/binding_web/exports.json | 1 + lib/binding_web/test/query-test.js | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/lib/binding_web/binding.c b/lib/binding_web/binding.c index d020da7c..c605731e 100644 --- a/lib/binding_web/binding.c +++ b/lib/binding_web/binding.c @@ -670,3 +670,10 @@ void ts_query_captures_wasm( TRANSFER_BUFFER[0] = (const void *)(capture_count); TRANSFER_BUFFER[1] = result.contents; } + +bool ts_query_did_exceed_match_limit_wasm( + const TSQuery *self +) { + if (!scratch_query_cursor) return false; + return ts_query_cursor_did_exceed_match_limit(scratch_query_cursor); +} diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 62b147f1..a51a7aea 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -1048,6 +1048,10 @@ class Query { predicatesForPattern(patternIndex) { return this.predicates[patternIndex] } + + didExceedMatchLimit() { + return C._ts_query_did_exceed_match_limit_wasm(this[0]); + } } function getText(tree, startIndex, endIndex) { diff --git a/lib/binding_web/exports.json b/lib/binding_web/exports.json index b2b4449d..7e73a11a 100644 --- a/lib/binding_web/exports.json +++ b/lib/binding_web/exports.json @@ -76,6 +76,7 @@ "_ts_query_capture_name_for_id", "_ts_query_captures_wasm", "_ts_query_delete", + "_ts_query_did_exceed_match_limit_wasm", "_ts_query_matches_wasm", "_ts_query_new", "_ts_query_pattern_count", diff --git a/lib/binding_web/test/query-test.js b/lib/binding_web/test/query-test.js index 77937a45..b7b2e053 100644 --- a/lib/binding_web/test/query-test.js +++ b/lib/binding_web/test/query-test.js @@ -238,6 +238,26 @@ describe("Query", () => { refutedProperties: { bar: "baz" }, }, ]); + assert.ok(!query.didExceedMatchLimit()); + }); + + it("detects queries with too many permutations to track", () => { + tree = parser.parse(` + [ + hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, + hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, + hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, + hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, + hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, + ]; + `); + + query = JavaScript.query(` + (array (identifier) @pre (identifier) @post) + `); + + const captures = query.captures(tree.rootNode); + assert.ok(query.didExceedMatchLimit()); }); });