Skip to content

Commit d7a23cf

Browse files
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 <[email protected]>
1 parent 90e0e28 commit d7a23cf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/src/wasm_store.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,10 +1276,21 @@ const TSLanguage *ts_wasm_store_load_language(
12761276
&memory[wasm_language.field_map_slices],
12771277
wasm_language.production_id_count * sizeof(TSFieldMapSlice)
12781278
);
1279-
const TSFieldMapSlice last_field_map_slice = language->field_map_slices[language->production_id_count - 1];
1279+
1280+
// Determine the number of field map entries by finding the greatest index
1281+
// in any of the slices.
1282+
uint32_t field_map_entry_count = 0;
1283+
for (uint32_t i = 0; i < wasm_language.production_id_count; i++) {
1284+
TSFieldMapSlice slice = language->field_map_slices[i];
1285+
uint32_t slice_end = slice.index + slice.length;
1286+
if (slice_end > field_map_entry_count) {
1287+
field_map_entry_count = slice_end;
1288+
}
1289+
}
1290+
12801291
language->field_map_entries = copy(
12811292
&memory[wasm_language.field_map_entries],
1282-
(last_field_map_slice.index + last_field_map_slice.length) * sizeof(TSFieldMapEntry)
1293+
field_map_entry_count * sizeof(TSFieldMapEntry)
12831294
);
12841295
language->field_names = copy_strings(
12851296
memory,

0 commit comments

Comments
 (0)