From d7a23cf0173b490d55db0d9f1e4d642be40bda99 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 24 Apr 2024 16:29:44 -0700 Subject: [PATCH] fix: Correctly load field data from wasm languages Previously, if the last production id in a language did not have a unique set of fields, the field maps weren't loaded correctly from wasm. Co-authored-by: Marshall --- lib/src/wasm_store.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/src/wasm_store.c b/lib/src/wasm_store.c index 7137a7fb..34c39e8f 100644 --- a/lib/src/wasm_store.c +++ b/lib/src/wasm_store.c @@ -1276,10 +1276,21 @@ const TSLanguage *ts_wasm_store_load_language( &memory[wasm_language.field_map_slices], wasm_language.production_id_count * sizeof(TSFieldMapSlice) ); - const TSFieldMapSlice last_field_map_slice = language->field_map_slices[language->production_id_count - 1]; + + // Determine the number of field map entries by finding the greatest index + // in any of the slices. + uint32_t field_map_entry_count = 0; + for (uint32_t i = 0; i < wasm_language.production_id_count; i++) { + TSFieldMapSlice slice = language->field_map_slices[i]; + uint32_t slice_end = slice.index + slice.length; + if (slice_end > field_map_entry_count) { + field_map_entry_count = slice_end; + } + } + language->field_map_entries = copy( &memory[wasm_language.field_map_entries], - (last_field_map_slice.index + last_field_map_slice.length) * sizeof(TSFieldMapEntry) + field_map_entry_count * sizeof(TSFieldMapEntry) ); language->field_names = copy_strings( memory,