From 1925a70f7e6472dc5df8f6f4e1d69227f5255859 Mon Sep 17 00:00:00 2001 From: "tree-sitter-ci-bot[bot]" <180118140+tree-sitter-ci-bot[bot]@users.noreply.github.com> Date: Mon, 17 Feb 2025 18:52:32 -0800 Subject: [PATCH] Reset result_symbol field of lexer in wasm memory in between invocations (#4218) (#4220) (cherry picked from commit 2bd400dcee06e5965e3c83c56b27ac8459c09860) Co-authored-by: Max Brunsfeld --- lib/src/wasm_store.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/src/wasm_store.c b/lib/src/wasm_store.c index 3579fa6b..7a4320a0 100644 --- a/lib/src/wasm_store.c +++ b/lib/src/wasm_store.c @@ -1613,13 +1613,22 @@ static void ts_wasm_store__call( } } +// The data fields of TSLexer, without the function pointers. +// +// This portion of the struct needs to be copied in and out +// of wasm memory before and after calling a scan function. +typedef struct { + int32_t lookahead; + TSSymbol result_symbol; +} TSLexerDataPrefix; + static bool ts_wasm_store__call_lex_function(TSWasmStore *self, unsigned function_index, TSStateId state) { wasmtime_context_t *context = wasmtime_store_context(self->store); uint8_t *memory_data = wasmtime_memory_data(context, &self->memory); memcpy( &memory_data[self->lexer_address], - &self->current_lexer->lookahead, - sizeof(self->current_lexer->lookahead) + self->current_lexer, + sizeof(TSLexerDataPrefix) ); wasmtime_val_raw_t args[2] = { @@ -1631,9 +1640,9 @@ static bool ts_wasm_store__call_lex_function(TSWasmStore *self, unsigned functio bool result = args[0].i32; memcpy( - &self->current_lexer->lookahead, + self->current_lexer, &memory_data[self->lexer_address], - sizeof(self->current_lexer->lookahead) + sizeof(self->current_lexer->result_symbol) + sizeof(TSLexerDataPrefix) ); return result; } @@ -1678,8 +1687,8 @@ bool ts_wasm_store_call_scanner_scan( memcpy( &memory_data[self->lexer_address], - &self->current_lexer->lookahead, - sizeof(self->current_lexer->lookahead) + self->current_lexer, + sizeof(TSLexerDataPrefix) ); uint32_t valid_tokens_address = @@ -1694,9 +1703,9 @@ bool ts_wasm_store_call_scanner_scan( if (self->has_error) return false; memcpy( - &self->current_lexer->lookahead, + self->current_lexer, &memory_data[self->lexer_address], - sizeof(self->current_lexer->lookahead) + sizeof(self->current_lexer->result_symbol) + sizeof(TSLexerDataPrefix) ); return args[0].i32; }