From 2fee1956f42bb09504db2ef8eeec2c3b7da7b0c0 Mon Sep 17 00:00:00 2001 From: Corey Williamson Date: Thu, 18 May 2023 14:42:23 -0500 Subject: [PATCH] Stop storing query's source text in the query We can just pull it from the object table --- csrc/query.c | 25 +++++++++++++++++++------ include/ltreesitter/types.h | 2 -- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/csrc/query.c b/csrc/query.c index cf6c8ec..7dbf7aa 100644 --- a/csrc/query.c +++ b/csrc/query.c @@ -99,12 +99,15 @@ void ltreesitter_push_query( set_child(L, child_idx); // query ltreesitter_SourceText *source = ltreesitter_source_text_push(L, src_len, src); // query, source text + if (!source) { + ALLOC_FAIL(L); + return; + } lua_pushvalue(L, -2); // query, source text, query set_child(L, -2); // query, source text lua_pop(L, 1); // query *lq = (ltreesitter_Query){ - .source = source, .query = q, .lang = lang, }; @@ -123,8 +126,8 @@ static void push_query_copy(lua_State *L, int query_idx) { TSQueryError err_type; TSQuery *q = ts_query_new( orig->lang, - orig->source->text, - orig->source->length, + source_text->text, + source_text->length, &err_offset, &err_type); @@ -141,7 +144,6 @@ static void push_query_copy(lua_State *L, int query_idx) { *lq = (ltreesitter_Query){ .lang = orig->lang, .query = q, - .source = source_text, }; } @@ -677,8 +679,19 @@ static int query_exec(lua_State *L) { Gets the source that the query was initialized with ]]*/ static int query_source(lua_State *L) { - ltreesitter_Query *q = ltreesitter_check_query(L, 1); - lua_pushlstring(L, q->source->text, q->source->length); + ltreesitter_Query *q = ltreesitter_check_query(L, 1); // query + if (!q) { + int t = lua_type(L, 1); + luaL_error(L, "Expected an ltreesitter.Query, got %s", lua_typename(L, t)); + return 0; + } + push_child(L, -1); // query, source + ltreesitter_SourceText const *source_text = ltreesitter_check_source_text(L, -1); + if (!source_text) { + luaL_error(L, "Internal error: Query child was not a SourceText"); + return 0; + } + lua_pushlstring(L, source_text->text, source_text->length); // query, source, string return 1; } diff --git a/include/ltreesitter/types.h b/include/ltreesitter/types.h index 2cd2226..bc63454 100644 --- a/include/ltreesitter/types.h +++ b/include/ltreesitter/types.h @@ -47,8 +47,6 @@ struct ltreesitter_Query { TSQuery *query; const TSLanguage *lang; - // TODO: this doesn't need to be stored here - ltreesitter_SourceText const *source; }; #define LTREESITTER_QUERY_METATABLE_NAME "ltreesitter.Query"