Skip to content

Commit

Permalink
feat: implement Query.timeoutMicros
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Sep 1, 2024
1 parent 268f633 commit 9103f93
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class QueryTest :
query.captureCount shouldBe 3U
}

test("timeoutMicros") {
val query = Query(language, source)
query.timeoutMicros shouldBe 0UL
query.timeoutMicros = 10UL
query.timeoutMicros shouldBe 10UL
}

test("matchLimit") {
val query = Query(language, source)
query.matchLimit shouldBe UInt.MAX_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,24 @@ actual class Query @Throws(QueryError::class) actual constructor(
}
}

/**
* The maximum duration in microseconds that query
* execution should be allowed to take before halting.
*
* Default: `0`
*/
@get:JvmName("getTimeoutMicros")
@set:JvmName("setTimeoutMicros")
actual var timeoutMicros: ULong
@FastNative external get

@FastNative external set

/**
* The maximum number of in-progress matches.
*
* Default: `UInt.MAX_VALUE`
*
* @throws [IllegalArgumentException] If the match limit is set to `0`.
*/
@get:JvmName("getMatchLimit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ expect class Query @Throws(QueryError::class) constructor(language: Language, so
/** The number of captures in the query. */
val captureCount: UInt

/**
* The maximum duration in microseconds that query
* execution should be allowed to take before halting.
*
* Default: `0`
*/
var timeoutMicros: ULong

/**
* The maximum number of in-progress matches.
*
* Default: `UInt.MAX_VALUE`
*
* @throws [IllegalArgumentException] If the match limit is set to `0`.
*/
var matchLimit: UInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class QueryTest :
query.captureCount shouldBe 3U
}

test("timeoutMicros") {
val query = Query(language, source)
query.timeoutMicros shouldBe 0UL
query.timeoutMicros = 10UL
query.timeoutMicros shouldBe 10UL
}

test("matchLimit") {
val query = Query(language, source)
query.matchLimit shouldBe UInt.MAX_VALUE
Expand Down
13 changes: 13 additions & 0 deletions ktreesitter/src/jni/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ jint query_get_capture_count(JNIEnv *env, jobject this) {
return (jint)ts_query_capture_count(self);
}

jlong query_get_timeout_micros(JNIEnv *env, jobject this) {
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
return (jlong)ts_query_cursor_timeout_micros(cursor);
}

void query_set_timeout_micros(JNIEnv *env, jobject this, jlong value) {
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
ts_query_cursor_set_timeout_micros(cursor, (uint64_t)value);
(*env)->SetLongField(env, this, global_field_cache.Query_timeoutMicros, value);
}

jint query_get_match_limit(JNIEnv *env, jobject this) {
TSQueryCursor *cursor = GET_POINTER(TSQueryCursor, this, Query_cursor);
return (jint)ts_query_cursor_match_limit(cursor);
Expand Down Expand Up @@ -338,6 +349,8 @@ const JNINativeMethod Query_methods[] = {
{"delete", "(JJ)V", (void *)&query_delete},
{"getPatternCount", "()I", (void *)&query_get_pattern_count},
{"getCaptureCount", "()I", (void *)&query_get_capture_count},
{"getTimeoutMicros", "()J", (void *)&query_get_timeout_micros},
{"setTimeoutMicros", "(J)V", (void *)&query_set_timeout_micros},
{"getMatchLimit", "()I", (void *)&query_get_match_limit},
{"setMatchLimit", "(I)V", (void *)&query_set_match_limit},
{"setMaxStartDepth", "(I)V", (void *)&query_set_max_start_depth},
Expand Down
1 change: 1 addition & 0 deletions ktreesitter/src/jni/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef struct {
jfieldID Query_maxStartDepth;
jfieldID Query_self;
jfieldID Query_source;
jfieldID Query_timeoutMicros;
jfieldID Range_endByte;
jfieldID Range_endPoint;
jfieldID Range_startByte;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ actual class Parser actual constructor() {

override fun toString() = "Parser(language=$language)"

// private external fun nativeParse(oldTree: Tree?, callback: (Int, Point) -> CharSequence?): Tree

/** The type of a log message. */
@Suppress("unused")
actual enum class LogType { LEX, PARSE }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,23 @@ actual class Query @Throws(QueryError::class) actual constructor(
}
}

/**
* The maximum duration in microseconds that query
* execution should be allowed to take before halting.
*
* Default: `0`
*/
@get:JvmName("getTimeoutMicros")
@set:JvmName("setTimeoutMicros")
actual var timeoutMicros: ULong
external get
external set

/**
* The maximum number of in-progress matches.
*
* Default: `UInt.MAX_VALUE`
*
* @throws [IllegalArgumentException] If the match limit is set to `0`.
*/
@get:JvmName("getMatchLimit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,23 @@ actual class Query @Throws(QueryError::class) actual constructor(
@OptIn(ExperimentalNativeApi::class)
private val cursorCleaner = createCleaner(cursor, ::ts_query_cursor_delete)

/**
* The maximum duration in microseconds that query
* execution should be allowed to take before halting.
*
* Default: `0`
*/
actual var timeoutMicros: ULong
get() = ts_query_cursor_timeout_micros(cursor)
set(value) {
ts_query_cursor_set_timeout_micros(cursor, value)
}

/**
* The maximum number of in-progress matches.
*
* Default: `UInt.MAX_VALUE`
*
* @throws [IllegalArgumentException] If the match limit is set to `0`.
*/
actual var matchLimit: UInt
Expand Down

0 comments on commit 9103f93

Please sign in to comment.