| 1 |
#include "tree_sitter/api.h" |
| 2 |
#include "./parser.h" |
| 3 |
#include <stdint.h> |
| 4 |
|
| 5 |
#ifdef TREE_SITTER_FEATURE_WASM |
| 6 |
|
| 7 |
#include "./alloc.h" |
| 8 |
#include "./array.h" |
| 9 |
#include "./atomic.h" |
| 10 |
#include "./language.h" |
| 11 |
#include "./lexer.h" |
| 12 |
#include "./wasm/wasm-stdlib.h" |
| 13 |
#include "./wasm_store.h" |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
#include <wasm.h> |
| 17 |
#include <wasmtime.h> |
| 18 |
|
| 19 |
#ifdef _MSC_VER |
| 20 |
#pragma warning(push) |
| 21 |
#pragma warning(disable : 4100) |
| 22 |
#elif defined(__GNUC__) || defined(__clang__) |
| 23 |
#pragma GCC diagnostic push |
| 24 |
#pragma GCC diagnostic ignored "-Wunused-parameter" |
| 25 |
#endif |
| 26 |
|
| 27 |
#define array_len(a) (sizeof(a) / sizeof(a[0])) |
| 28 |
|
| 29 |
// The following symbols from the C and C++ standard libraries are available |
| 30 |
// for external scanners to use. |
| 31 |
const char *STDLIB_SYMBOLS[] = { |
| 32 |
#include "./stdlib-symbols.txt" |
| 33 |
}; |
| 34 |
|
| 35 |
// The contents of the `dylink.0` custom section of a Wasm module, |
| 36 |
// as specified by the current WebAssembly dynamic linking ABI proposal. |
| 37 |
typedef struct { |
| 38 |
uint32_t memory_size; |
| 39 |
uint32_t memory_align; |
| 40 |
uint32_t table_size; |
| 41 |
uint32_t table_align; |
| 42 |
} WasmDylinkInfo; |
| 43 |
|
| 44 |
// WasmLanguageId - A pointer used to identify a language. This language id is |
| 45 |
// reference-counted, so that its ownership can be shared between the language |
| 46 |
// itself and the instances of the language that are held in Wasm stores. |
| 47 |
typedef struct { |
| 48 |
volatile uint32_t ref_count; |
| 49 |
volatile uint32_t is_language_deleted; |
| 50 |
} WasmLanguageId; |
| 51 |
|
| 52 |
// LanguageWasmModule - Additional data associated with a Wasm-backed |
| 53 |
// `TSLanguage`. This data is read-only and does not reference a particular |
| 54 |
// Wasm store, so it can be shared by all users of a `TSLanguage`. A pointer to |
| 55 |
// this is stored on the language itself. |
| 56 |
typedef struct { |
| 57 |
volatile uint32_t ref_count; |
| 58 |
WasmLanguageId *language_id; |
| 59 |
wasmtime_module_t *module; |
| 60 |
const char *name; |
| 61 |
char *symbol_name_buffer; |
| 62 |
char *field_name_buffer; |
| 63 |
WasmDylinkInfo dylink_info; |
| 64 |
} LanguageWasmModule; |
| 65 |
|
| 66 |
// LanguageWasmInstance - Additional data associated with an instantiation of |
| 67 |
// a `TSLanguage` in a particular Wasm store. The Wasm store holds one of |
| 68 |
// these structs for each language that it has instantiated. |
| 69 |
typedef struct { |
| 70 |
WasmLanguageId *language_id; |
| 71 |
wasmtime_instance_t instance; |
| 72 |
int32_t external_states_address; |
| 73 |
int32_t lex_main_fn_index; |
| 74 |
int32_t lex_keyword_fn_index; |
| 75 |
int32_t scanner_create_fn_index; |
| 76 |
int32_t scanner_destroy_fn_index; |
| 77 |
int32_t scanner_serialize_fn_index; |
| 78 |
int32_t scanner_deserialize_fn_index; |
| 79 |
int32_t scanner_scan_fn_index; |
| 80 |
} LanguageWasmInstance; |
| 81 |
|
| 82 |
typedef struct { |
| 83 |
wasmtime_func_t reset_heap; |
| 84 |
wasmtime_func_t proc_exit; |
| 85 |
wasmtime_func_t abort; |
| 86 |
wasmtime_func_t assert_fail; |
| 87 |
wasmtime_func_t notify_memory_growth; |
| 88 |
wasmtime_func_t debug_message; |
| 89 |
wasmtime_func_t at_exit; |
| 90 |
wasmtime_func_t args_get; |
| 91 |
wasmtime_func_t args_sizes_get; |
| 92 |
} BuiltinFunctionIndices; |
| 93 |
|
| 94 |
// TSWasmStore - A struct that allows a given `Parser` to use Wasm-backed |
| 95 |
// languages. This struct is mutable, and can only be used by one parser at a |
| 96 |
// time. |
| 97 |
struct TSWasmStore { |
| 98 |
wasm_engine_t *engine; |
| 99 |
wasmtime_store_t *store; |
| 100 |
wasmtime_table_t function_table; |
| 101 |
wasmtime_memory_t memory; |
| 102 |
TSLexer *current_lexer; |
| 103 |
LanguageWasmInstance *current_instance; |
| 104 |
Array(LanguageWasmInstance) language_instances; |
| 105 |
uint32_t current_memory_offset; |
| 106 |
uint32_t current_function_table_offset; |
| 107 |
wasmtime_func_t *stdlib_fn_indices; |
| 108 |
BuiltinFunctionIndices builtin_fn_indices; |
| 109 |
wasmtime_global_t stack_pointer_global; |
| 110 |
wasm_globaltype_t *const_i32_type; |
| 111 |
bool has_error; |
| 112 |
uint32_t lexer_address; |
| 113 |
}; |
| 114 |
|
| 115 |
typedef Array(char) StringData; |
| 116 |
|
| 117 |
// LanguageInWasmMemory - The memory layout of a `TSLanguage` when compiled to |
| 118 |
// wasm32. This is used to copy static language data out of the Wasm memory. |
| 119 |
typedef struct { |
| 120 |
uint32_t abi_version; |
| 121 |
uint32_t symbol_count; |
| 122 |
uint32_t alias_count; |
| 123 |
uint32_t token_count; |
| 124 |
uint32_t external_token_count; |
| 125 |
uint32_t state_count; |
| 126 |
uint32_t large_state_count; |
| 127 |
uint32_t production_id_count; |
| 128 |
uint32_t field_count; |
| 129 |
uint16_t max_alias_sequence_length; |
| 130 |
int32_t parse_table; |
| 131 |
int32_t small_parse_table; |
| 132 |
int32_t small_parse_table_map; |
| 133 |
int32_t parse_actions; |
| 134 |
int32_t symbol_names; |
| 135 |
int32_t field_names; |
| 136 |
int32_t field_map_slices; |
| 137 |
int32_t field_map_entries; |
| 138 |
int32_t symbol_metadata; |
| 139 |
int32_t public_symbol_map; |
| 140 |
int32_t alias_map; |
| 141 |
int32_t alias_sequences; |
| 142 |
int32_t lex_modes; |
| 143 |
int32_t lex_fn; |
| 144 |
int32_t keyword_lex_fn; |
| 145 |
TSSymbol keyword_capture_token; |
| 146 |
struct { |
| 147 |
int32_t states; |
| 148 |
int32_t symbol_map; |
| 149 |
int32_t create; |
| 150 |
int32_t destroy; |
| 151 |
int32_t scan; |
| 152 |
int32_t serialize; |
| 153 |
int32_t deserialize; |
| 154 |
} external_scanner; |
| 155 |
int32_t primary_state_ids; |
| 156 |
int32_t name; |
| 157 |
int32_t reserved_words; |
| 158 |
uint16_t max_reserved_word_set_size; |
| 159 |
uint32_t supertype_count; |
| 160 |
int32_t supertype_symbols; |
| 161 |
int32_t supertype_map_slices; |
| 162 |
int32_t supertype_map_entries; |
| 163 |
TSLanguageMetadata metadata; |
| 164 |
} LanguageInWasmMemory; |
| 165 |
|
| 166 |
// LexerInWasmMemory - The memory layout of a `TSLexer` when compiled to wasm32. |
| 167 |
// This is used to copy mutable lexing state in and out of the Wasm memory. |
| 168 |
typedef struct { |
| 169 |
int32_t lookahead; |
| 170 |
TSSymbol result_symbol; |
| 171 |
int32_t advance; |
| 172 |
int32_t mark_end; |
| 173 |
int32_t get_column; |
| 174 |
int32_t is_at_included_range_start; |
| 175 |
int32_t eof; |
| 176 |
} LexerInWasmMemory; |
| 177 |
|
| 178 |
// Linear memory layout: |
| 179 |
// [ <-- stack | stdlib statics | lexer | language statics --> | serialization_buffer | heap --> ] |
| 180 |
#define MAX_MEMORY_SIZE (128 * 1024 * 1024 / MEMORY_PAGE_SIZE) |
| 181 |
|
| 182 |
/************************ |
| 183 |
* WasmDylinkMemoryInfo |
| 184 |
***********************/ |
| 185 |
|
| 186 |
static uint8_t read_u8(const uint8_t **p) { |
| 187 |
return *(*p)++; |
| 188 |
} |
| 189 |
|
| 190 |
static inline uint64_t read_uleb128(const uint8_t **p, const uint8_t *end) { |
| 191 |
uint64_t value = 0; |
| 192 |
unsigned shift = 0; |
| 193 |
do { |
| 194 |
if (*p == end) return UINT64_MAX; |
| 195 |
value += (uint64_t)(**p & 0x7f) << shift; |
| 196 |
shift += 7; |
| 197 |
} while (*((*p)++) >= 128); |
| 198 |
return value; |
| 199 |
} |
| 200 |
|
| 201 |
static bool wasm_dylink_info__parse( |
| 202 |
const uint8_t *bytes, |
| 203 |
size_t length, |
| 204 |
WasmDylinkInfo *info |
| 205 |
) { |
| 206 |
const uint8_t WASM_MAGIC_NUMBER[4] = {0, 'a', 's', 'm'}; |
| 207 |
const uint8_t WASM_VERSION[4] = {1, 0, 0, 0}; |
| 208 |
const uint8_t WASM_CUSTOM_SECTION = 0x0; |
| 209 |
const uint8_t WASM_DYLINK_MEM_INFO = 0x1; |
| 210 |
|
| 211 |
const uint8_t *p = bytes; |
| 212 |
const uint8_t *end = bytes + length; |
| 213 |
|
| 214 |
if (length < 8) return false; |
| 215 |
if (memcmp(p, WASM_MAGIC_NUMBER, 4) != 0) return false; |
| 216 |
p += 4; |
| 217 |
if (memcmp(p, WASM_VERSION, 4) != 0) return false; |
| 218 |
p += 4; |
| 219 |
|
| 220 |
while (p < end) { |
| 221 |
uint8_t section_id = read_u8(&p); |
| 222 |
uint32_t section_length = read_uleb128(&p, end); |
| 223 |
const uint8_t *section_end = p + section_length; |
| 224 |
if (section_end > end) return false; |
| 225 |
|
| 226 |
if (section_id == WASM_CUSTOM_SECTION) { |
| 227 |
uint32_t name_length = read_uleb128(&p, section_end); |
| 228 |
const uint8_t *name_end = p + name_length; |
| 229 |
if (name_end > section_end) return false; |
| 230 |
|
| 231 |
if (name_length == 8 && memcmp(p, "dylink.0", 8) == 0) { |
| 232 |
p = name_end; |
| 233 |
while (p < section_end) { |
| 234 |
uint8_t subsection_type = read_u8(&p); |
| 235 |
uint32_t subsection_size = read_uleb128(&p, section_end); |
| 236 |
const uint8_t *subsection_end = p + subsection_size; |
| 237 |
if (subsection_end > section_end) return false; |
| 238 |
if (subsection_type == WASM_DYLINK_MEM_INFO) { |
| 239 |
info->memory_size = read_uleb128(&p, subsection_end); |
| 240 |
info->memory_align = read_uleb128(&p, subsection_end); |
| 241 |
info->table_size = read_uleb128(&p, subsection_end); |
| 242 |
info->table_align = read_uleb128(&p, subsection_end); |
| 243 |
return true; |
| 244 |
} |
| 245 |
p = subsection_end; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
p = section_end; |
| 250 |
} |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
/******************************************* |
| 255 |
* Native callbacks exposed to Wasm modules |
| 256 |
*******************************************/ |
| 257 |
|
| 258 |
static wasm_trap_t *callback__abort( |
| 259 |
void *env, |
| 260 |
wasmtime_caller_t* caller, |
| 261 |
wasmtime_val_raw_t *args_and_results, |
| 262 |
size_t args_and_results_len |
| 263 |
) { |
| 264 |
return wasmtime_trap_new("Wasm module called abort", 24); |
| 265 |
} |
| 266 |
|
| 267 |
static wasm_trap_t *callback__debug_message( |
| 268 |
void *env, |
| 269 |
wasmtime_caller_t* caller, |
| 270 |
wasmtime_val_raw_t *args_and_results, |
| 271 |
size_t args_and_results_len |
| 272 |
) { |
| 273 |
wasmtime_context_t *context = wasmtime_caller_context(caller); |
| 274 |
TSWasmStore *store = env; |
| 275 |
ts_assert(args_and_results_len == 2); |
| 276 |
uint32_t string_address = args_and_results[0].i32; |
| 277 |
uint32_t value = args_and_results[1].i32; |
| 278 |
uint8_t *memory = wasmtime_memory_data(context, &store->memory); |
| 279 |
printf("DEBUG: %s %u\n", &memory[string_address], value); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
static wasm_trap_t *callback__noop( |
| 284 |
void *env, |
| 285 |
wasmtime_caller_t* caller, |
| 286 |
wasmtime_val_raw_t *args_and_results, |
| 287 |
size_t args_and_results_len |
| 288 |
) { |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
static wasm_trap_t *callback__lexer_advance( |
| 293 |
void *env, |
| 294 |
wasmtime_caller_t* caller, |
| 295 |
wasmtime_val_raw_t *args_and_results, |
| 296 |
size_t args_and_results_len |
| 297 |
) { |
| 298 |
wasmtime_context_t *context = wasmtime_caller_context(caller); |
| 299 |
ts_assert(args_and_results_len == 2); |
| 300 |
|
| 301 |
TSWasmStore *store = env; |
| 302 |
TSLexer *lexer = store->current_lexer; |
| 303 |
bool skip = args_and_results[1].i32; |
| 304 |
lexer->advance(lexer, skip); |
| 305 |
|
| 306 |
uint8_t *memory = wasmtime_memory_data(context, &store->memory); |
| 307 |
memcpy(&memory[store->lexer_address], &lexer->lookahead, sizeof(lexer->lookahead)); |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
static wasm_trap_t *callback__lexer_mark_end( |
| 312 |
void *env, |
| 313 |
wasmtime_caller_t* caller, |
| 314 |
wasmtime_val_raw_t *args_and_results, |
| 315 |
size_t args_and_results_len |
| 316 |
) { |
| 317 |
TSWasmStore *store = env; |
| 318 |
TSLexer *lexer = store->current_lexer; |
| 319 |
lexer->mark_end(lexer); |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
static wasm_trap_t *callback__lexer_get_column( |
| 324 |
void *env, |
| 325 |
wasmtime_caller_t* caller, |
| 326 |
wasmtime_val_raw_t *args_and_results, |
| 327 |
size_t args_and_results_len |
| 328 |
) { |
| 329 |
TSWasmStore *store = env; |
| 330 |
TSLexer *lexer = store->current_lexer; |
| 331 |
uint32_t result = lexer->get_column(lexer); |
| 332 |
args_and_results[0].i32 = result; |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
|
| 336 |
static wasm_trap_t *callback__lexer_is_at_included_range_start( |
| 337 |
void *env, |
| 338 |
wasmtime_caller_t* caller, |
| 339 |
wasmtime_val_raw_t *args_and_results, |
| 340 |
size_t args_and_results_len |
| 341 |
) { |
| 342 |
TSWasmStore *store = env; |
| 343 |
TSLexer *lexer = store->current_lexer; |
| 344 |
bool result = lexer->is_at_included_range_start(lexer); |
| 345 |
args_and_results[0].i32 = result; |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
static wasm_trap_t *callback__lexer_eof( |
| 350 |
void *env, |
| 351 |
wasmtime_caller_t* caller, |
| 352 |
wasmtime_val_raw_t *args_and_results, |
| 353 |
size_t args_and_results_len |
| 354 |
) { |
| 355 |
TSWasmStore *store = env; |
| 356 |
TSLexer *lexer = store->current_lexer; |
| 357 |
bool result = lexer->eof(lexer); |
| 358 |
args_and_results[0].i32 = result; |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
typedef struct { |
| 363 |
void *storage_location; |
| 364 |
wasmtime_func_unchecked_callback_t callback; |
| 365 |
wasm_functype_t *type; |
| 366 |
} FunctionDefinition; |
| 367 |
|
| 368 |
typedef struct { |
| 369 |
const uint8_t *data; |
| 370 |
size_t size; |
| 371 |
} WasmMemory; |
| 372 |
|
| 373 |
static bool wasm_memory__contains(const WasmMemory *memory, int32_t address, size_t size) { |
| 374 |
if (address < 0) return false; |
| 375 |
size_t start = (size_t)address; |
| 376 |
return start <= memory->size && size <= memory->size - start; |
| 377 |
} |
| 378 |
|
| 379 |
static bool wasm_memory__read(const WasmMemory *memory, int32_t address, void *result, size_t size) { |
| 380 |
if (!wasm_memory__contains(memory, address, size)) return false; |
| 381 |
memcpy(result, &memory->data[address], size); |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
static bool wasm_memory__string_length(const WasmMemory *memory, int32_t address, size_t *length) { |
| 386 |
if (address < 0 || (size_t)address >= memory->size) return false; |
| 387 |
const uint8_t *data = &memory->data[address]; |
| 388 |
size_t limit = memory->size - (size_t)address; |
| 389 |
for (size_t i = 0; i < limit; i++) { |
| 390 |
if (data[i] == 0) { |
| 391 |
*length = i; |
| 392 |
return true; |
| 393 |
} |
| 394 |
} |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
static void *copy(const WasmMemory *memory, int32_t address, size_t size, bool *ok) { |
| 399 |
if (!*ok || size == 0) return NULL; |
| 400 |
if (!wasm_memory__contains(memory, address, size)) { |
| 401 |
*ok = false; |
| 402 |
return NULL; |
| 403 |
} |
| 404 |
void *result = ts_malloc(size); |
| 405 |
memcpy(result, &memory->data[address], size); |
| 406 |
return result; |
| 407 |
} |
| 408 |
|
| 409 |
static void *copy_unsized_static_array( |
| 410 |
const WasmMemory *memory, |
| 411 |
int32_t start_address, |
| 412 |
const int32_t all_addresses[], |
| 413 |
size_t address_count, |
| 414 |
bool *ok |
| 415 |
) { |
| 416 |
if (!*ok || start_address == 0) return NULL; |
| 417 |
if (start_address < 0) { |
| 418 |
*ok = false; |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
int32_t end_address = 0; |
| 423 |
for (unsigned i = 0; i < address_count; i++) { |
| 424 |
if (all_addresses[i] > start_address) { |
| 425 |
if (!end_address || all_addresses[i] < end_address) { |
| 426 |
end_address = all_addresses[i]; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if (!end_address) return NULL; |
| 432 |
size_t size = end_address - start_address; |
| 433 |
if (!wasm_memory__contains(memory, start_address, size)) { |
| 434 |
*ok = false; |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
void *result = ts_malloc(size); |
| 438 |
memcpy(result, &memory->data[start_address], size); |
| 439 |
return result; |
| 440 |
} |
| 441 |
|
| 442 |
static void *copy_strings( |
| 443 |
const WasmMemory *memory, |
| 444 |
int32_t array_address, |
| 445 |
size_t count, |
| 446 |
StringData *string_data, |
| 447 |
bool *ok |
| 448 |
) { |
| 449 |
if (!*ok) return NULL; |
| 450 |
if (count > SIZE_MAX / sizeof(char *)) { |
| 451 |
*ok = false; |
| 452 |
return NULL; |
| 453 |
} |
| 454 |
if (count > (SIZE_MAX / sizeof(int32_t)) || |
| 455 |
!wasm_memory__contains(memory, array_address, count * sizeof(int32_t))) { |
| 456 |
*ok = false; |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
|
| 460 |
const char **result = ts_malloc(count * sizeof(char *)); |
| 461 |
for (unsigned i = 0; i < count; i++) { |
| 462 |
int32_t address; |
| 463 |
memcpy(&address, &memory->data[array_address + i * sizeof(address)], sizeof(address)); |
| 464 |
if (address == 0) { |
| 465 |
result[i] = (const char *)-1; |
| 466 |
} else { |
| 467 |
size_t len; |
| 468 |
if (!wasm_memory__string_length(memory, address, &len) || len > UINT32_MAX) { |
| 469 |
ts_free(result); |
| 470 |
*ok = false; |
| 471 |
return NULL; |
| 472 |
} |
| 473 |
result[i] = (const char *)(uintptr_t)string_data->size; |
| 474 |
array_extend(string_data, len + 1, &memory->data[address]); |
| 475 |
} |
| 476 |
} |
| 477 |
for (unsigned i = 0; i < count; i++) { |
| 478 |
if (result[i] == (const char *)-1) { |
| 479 |
result[i] = NULL; |
| 480 |
} else { |
| 481 |
result[i] = string_data->contents + (uintptr_t)result[i]; |
| 482 |
} |
| 483 |
} |
| 484 |
return result; |
| 485 |
} |
| 486 |
|
| 487 |
static void *copy_string( |
| 488 |
const WasmMemory *memory, |
| 489 |
int32_t address, |
| 490 |
bool *ok |
| 491 |
) { |
| 492 |
if (!*ok) return NULL; |
| 493 |
size_t len; |
| 494 |
if (!wasm_memory__string_length(memory, address, &len)) { |
| 495 |
*ok = false; |
| 496 |
return NULL; |
| 497 |
} |
| 498 |
const char *string = (const char *)&memory->data[address]; |
| 499 |
char *result = ts_malloc(len + 1); |
| 500 |
memcpy(result, string, len + 1); |
| 501 |
return result; |
| 502 |
} |
| 503 |
|
| 504 |
static void delete_partially_loaded_language( |
| 505 |
TSLanguage *language, |
| 506 |
StringData *symbol_name_buffer, |
| 507 |
StringData *field_name_buffer |
| 508 |
) { |
| 509 |
if (language) { |
| 510 |
ts_free((void *)language->alias_map); |
| 511 |
ts_free((void *)language->alias_sequences); |
| 512 |
ts_free((void *)language->external_scanner.symbol_map); |
| 513 |
ts_free((void *)language->field_map_entries); |
| 514 |
ts_free((void *)language->field_map_slices); |
| 515 |
ts_free((void *)language->field_names); |
| 516 |
ts_free((void *)language->lex_modes); |
| 517 |
ts_free((void *)language->name); |
| 518 |
ts_free((void *)language->parse_actions); |
| 519 |
ts_free((void *)language->parse_table); |
| 520 |
ts_free((void *)language->primary_state_ids); |
| 521 |
ts_free((void *)language->public_symbol_map); |
| 522 |
ts_free((void *)language->reserved_words); |
| 523 |
ts_free((void *)language->small_parse_table); |
| 524 |
ts_free((void *)language->small_parse_table_map); |
| 525 |
ts_free((void *)language->supertype_map_entries); |
| 526 |
ts_free((void *)language->supertype_map_slices); |
| 527 |
ts_free((void *)language->supertype_symbols); |
| 528 |
ts_free((void *)language->symbol_metadata); |
| 529 |
ts_free((void *)language->symbol_names); |
| 530 |
ts_free(language); |
| 531 |
} |
| 532 |
array_delete(symbol_name_buffer); |
| 533 |
array_delete(field_name_buffer); |
| 534 |
} |
| 535 |
|
| 536 |
static bool name_eq(const wasm_name_t *name, const char *string) { |
| 537 |
return strncmp(string, name->data, name->size) == 0; |
| 538 |
} |
| 539 |
|
| 540 |
static inline wasm_functype_t* wasm_functype_new_4_0( |
| 541 |
wasm_valtype_t* p1, |
| 542 |
wasm_valtype_t* p2, |
| 543 |
wasm_valtype_t* p3, |
| 544 |
wasm_valtype_t* p4 |
| 545 |
) { |
| 546 |
wasm_valtype_t* ps[4] = {p1, p2, p3, p4}; |
| 547 |
wasm_valtype_vec_t params, results; |
| 548 |
wasm_valtype_vec_new(¶ms, 4, ps); |
| 549 |
wasm_valtype_vec_new_empty(&results); |
| 550 |
return wasm_functype_new(¶ms, &results); |
| 551 |
} |
| 552 |
|
| 553 |
#define format(output, ...) \ |
| 554 |
do { \ |
| 555 |
size_t message_length = snprintf((char *)NULL, 0, __VA_ARGS__); \ |
| 556 |
*output = ts_malloc(message_length + 1); \ |
| 557 |
snprintf(*output, message_length + 1, __VA_ARGS__); \ |
| 558 |
} while (0) |
| 559 |
|
| 560 |
WasmLanguageId *language_id_new(void) { |
| 561 |
WasmLanguageId *self = ts_malloc(sizeof(WasmLanguageId)); |
| 562 |
self->is_language_deleted = false; |
| 563 |
self->ref_count = 1; |
| 564 |
return self; |
| 565 |
} |
| 566 |
|
| 567 |
WasmLanguageId *language_id_clone(WasmLanguageId *self) { |
| 568 |
atomic_inc(&self->ref_count); |
| 569 |
return self; |
| 570 |
} |
| 571 |
|
| 572 |
void language_id_delete(WasmLanguageId *self) { |
| 573 |
if (atomic_dec(&self->ref_count) == 0) { |
| 574 |
ts_free(self); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
static wasmtime_extern_t get_builtin_extern( |
| 579 |
wasmtime_func_t *func |
| 580 |
) { |
| 581 |
return (wasmtime_extern_t) { |
| 582 |
.kind = WASMTIME_EXTERN_FUNC, |
| 583 |
.of.func = *func |
| 584 |
}; |
| 585 |
} |
| 586 |
|
| 587 |
static bool ts_wasm_store__provide_builtin_import( |
| 588 |
TSWasmStore *self, |
| 589 |
const wasm_name_t *import_name, |
| 590 |
wasmtime_extern_t *import |
| 591 |
) { |
| 592 |
wasmtime_error_t *error = NULL; |
| 593 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 594 |
|
| 595 |
// Dynamic linking parameters |
| 596 |
if (name_eq(import_name, "__memory_base")) { |
| 597 |
wasmtime_val_t value = WASM_I32_VAL(self->current_memory_offset); |
| 598 |
wasmtime_global_t global; |
| 599 |
error = wasmtime_global_new(context, self->const_i32_type, &value, &global); |
| 600 |
ts_assert(!error); |
| 601 |
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = global}; |
| 602 |
} else if (name_eq(import_name, "__table_base")) { |
| 603 |
wasmtime_val_t value = WASM_I32_VAL(self->current_function_table_offset); |
| 604 |
wasmtime_global_t global; |
| 605 |
error = wasmtime_global_new(context, self->const_i32_type, &value, &global); |
| 606 |
ts_assert(!error); |
| 607 |
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = global}; |
| 608 |
} else if (name_eq(import_name, "__stack_pointer")) { |
| 609 |
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = self->stack_pointer_global}; |
| 610 |
} else if (name_eq(import_name, "__indirect_function_table")) { |
| 611 |
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_TABLE, .of.table = self->function_table}; |
| 612 |
} else if (name_eq(import_name, "memory")) { |
| 613 |
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_MEMORY, .of.memory = self->memory}; |
| 614 |
} |
| 615 |
|
| 616 |
// Builtin functions |
| 617 |
else if (name_eq(import_name, "__assert_fail")) { |
| 618 |
*import = get_builtin_extern(&self->builtin_fn_indices.assert_fail); |
| 619 |
} else if (name_eq(import_name, "__cxa_atexit")) { |
| 620 |
*import = get_builtin_extern(&self->builtin_fn_indices.at_exit); |
| 621 |
} else if (name_eq(import_name, "args_get")) { |
| 622 |
*import = get_builtin_extern(&self->builtin_fn_indices.args_get); |
| 623 |
} else if (name_eq(import_name, "args_sizes_get")) { |
| 624 |
*import = get_builtin_extern(&self->builtin_fn_indices.args_sizes_get); |
| 625 |
} else if (name_eq(import_name, "abort")) { |
| 626 |
*import = get_builtin_extern(&self->builtin_fn_indices.abort); |
| 627 |
} else if (name_eq(import_name, "proc_exit")) { |
| 628 |
*import = get_builtin_extern(&self->builtin_fn_indices.proc_exit); |
| 629 |
} else if (name_eq(import_name, "emscripten_notify_memory_growth")) { |
| 630 |
*import = get_builtin_extern(&self->builtin_fn_indices.notify_memory_growth); |
| 631 |
} else if (name_eq(import_name, "tree_sitter_debug_message")) { |
| 632 |
*import = get_builtin_extern(&self->builtin_fn_indices.debug_message); |
| 633 |
} else { |
| 634 |
return false; |
| 635 |
} |
| 636 |
|
| 637 |
return true; |
| 638 |
} |
| 639 |
|
| 640 |
static bool ts_wasm_store__call_module_initializer( |
| 641 |
TSWasmStore *self, |
| 642 |
const wasm_name_t *export_name, |
| 643 |
wasmtime_extern_t *export, |
| 644 |
wasm_trap_t **trap |
| 645 |
) { |
| 646 |
if ( |
| 647 |
name_eq(export_name, "_initialize") || |
| 648 |
name_eq(export_name, "__wasm_apply_data_relocs") || |
| 649 |
name_eq(export_name, "__wasm_call_ctors") |
| 650 |
) { |
| 651 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 652 |
wasmtime_func_t initialization_func = export->of.func; |
| 653 |
wasmtime_error_t *error = wasmtime_func_call(context, &initialization_func, NULL, 0, NULL, 0, trap); |
| 654 |
ts_assert(!error); |
| 655 |
return true; |
| 656 |
} else { |
| 657 |
return false; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
TSWasmStore *ts_wasm_store_new(TSWasmEngine *engine, TSWasmError *wasm_error) { |
| 662 |
TSWasmStore *self = ts_calloc(1, sizeof(TSWasmStore)); |
| 663 |
wasmtime_store_t *store = wasmtime_store_new(engine, self, NULL); |
| 664 |
wasmtime_context_t *context = wasmtime_store_context(store); |
| 665 |
wasmtime_error_t *error = NULL; |
| 666 |
wasm_trap_t *trap = NULL; |
| 667 |
wasm_message_t message = WASM_EMPTY_VEC; |
| 668 |
wasm_exporttype_vec_t export_types = WASM_EMPTY_VEC; |
| 669 |
wasm_importtype_vec_t import_types = WASM_EMPTY_VEC; |
| 670 |
wasmtime_extern_t *imports = NULL; |
| 671 |
wasmtime_module_t *stdlib_module = NULL; |
| 672 |
wasm_memorytype_t *memory_type = NULL; |
| 673 |
wasm_tabletype_t *table_type = NULL; |
| 674 |
wasmtime_func_t *lexer_funcs = NULL; |
| 675 |
|
| 676 |
// Define functions called by scanners via function pointers on the lexer. |
| 677 |
LexerInWasmMemory lexer = { |
| 678 |
.lookahead = 0, |
| 679 |
.result_symbol = 0, |
| 680 |
}; |
| 681 |
FunctionDefinition lexer_definitions[] = { |
| 682 |
{ |
| 683 |
&lexer.advance, |
| 684 |
callback__lexer_advance, |
| 685 |
wasm_functype_new_2_0(wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 686 |
}, |
| 687 |
{ |
| 688 |
&lexer.mark_end, |
| 689 |
callback__lexer_mark_end, |
| 690 |
wasm_functype_new_1_0(wasm_valtype_new_i32()) |
| 691 |
}, |
| 692 |
{ |
| 693 |
&lexer.get_column, |
| 694 |
callback__lexer_get_column, |
| 695 |
wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 696 |
}, |
| 697 |
{ |
| 698 |
&lexer.is_at_included_range_start, |
| 699 |
callback__lexer_is_at_included_range_start, |
| 700 |
wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 701 |
}, |
| 702 |
{ |
| 703 |
&lexer.eof, |
| 704 |
callback__lexer_eof, |
| 705 |
wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 706 |
}, |
| 707 |
}; |
| 708 |
|
| 709 |
// Define builtin functions that can be imported by scanners. |
| 710 |
BuiltinFunctionIndices builtin_fn_indices = {0}; |
| 711 |
FunctionDefinition builtin_definitions[] = { |
| 712 |
{ |
| 713 |
&builtin_fn_indices.proc_exit, |
| 714 |
callback__abort, |
| 715 |
wasm_functype_new_1_0(wasm_valtype_new_i32()) |
| 716 |
}, |
| 717 |
{ |
| 718 |
&builtin_fn_indices.abort, |
| 719 |
callback__abort, |
| 720 |
wasm_functype_new_0_0() |
| 721 |
}, |
| 722 |
{ |
| 723 |
&builtin_fn_indices.assert_fail, |
| 724 |
callback__abort, |
| 725 |
wasm_functype_new_4_0(wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 726 |
}, |
| 727 |
{ |
| 728 |
&builtin_fn_indices.notify_memory_growth, |
| 729 |
callback__noop, |
| 730 |
wasm_functype_new_1_0(wasm_valtype_new_i32()) |
| 731 |
}, |
| 732 |
{ |
| 733 |
&builtin_fn_indices.debug_message, |
| 734 |
callback__debug_message, |
| 735 |
wasm_functype_new_2_0(wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 736 |
}, |
| 737 |
{ |
| 738 |
&builtin_fn_indices.at_exit, |
| 739 |
callback__noop, |
| 740 |
wasm_functype_new_3_1(wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 741 |
}, |
| 742 |
{ |
| 743 |
&builtin_fn_indices.args_get, |
| 744 |
callback__noop, |
| 745 |
wasm_functype_new_2_1(wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 746 |
}, |
| 747 |
{ |
| 748 |
&builtin_fn_indices.args_sizes_get, |
| 749 |
callback__noop, |
| 750 |
wasm_functype_new_2_1(wasm_valtype_new_i32(), wasm_valtype_new_i32(), wasm_valtype_new_i32()) |
| 751 |
}, |
| 752 |
}; |
| 753 |
|
| 754 |
// Create all of the Wasm functions. |
| 755 |
unsigned builtin_definitions_len = array_len(builtin_definitions); |
| 756 |
unsigned lexer_definitions_len = array_len(lexer_definitions); |
| 757 |
lexer_funcs = ts_calloc(lexer_definitions_len, sizeof(wasmtime_func_t)); |
| 758 |
for (unsigned i = 0; i < builtin_definitions_len; i++) { |
| 759 |
FunctionDefinition *definition = &builtin_definitions[i]; |
| 760 |
wasmtime_func_t *func = (wasmtime_func_t *)definition->storage_location; |
| 761 |
wasmtime_func_new_unchecked(context, definition->type, definition->callback, self, NULL, func); |
| 762 |
wasm_functype_delete(definition->type); |
| 763 |
} |
| 764 |
for (unsigned i = 0; i < lexer_definitions_len; i++) { |
| 765 |
FunctionDefinition *definition = &lexer_definitions[i]; |
| 766 |
wasmtime_func_new_unchecked(context, definition->type, definition->callback, self, NULL, &lexer_funcs[i]); |
| 767 |
wasm_functype_delete(definition->type); |
| 768 |
} |
| 769 |
|
| 770 |
// Compile the stdlib module. |
| 771 |
error = wasmtime_module_new(engine, STDLIB_WASM, STDLIB_WASM_LEN, &stdlib_module); |
| 772 |
if (error) { |
| 773 |
wasmtime_error_message(error, &message); |
| 774 |
wasm_error->kind = TSWasmErrorKindCompile; |
| 775 |
format( |
| 776 |
&wasm_error->message, |
| 777 |
"failed to compile Wasm stdlib: %.*s", |
| 778 |
(int)message.size, message.data |
| 779 |
); |
| 780 |
goto error; |
| 781 |
} |
| 782 |
|
| 783 |
// Retrieve the stdlib module's imports. |
| 784 |
wasmtime_module_imports(stdlib_module, &import_types); |
| 785 |
|
| 786 |
// Find the initial number of memory pages needed by the stdlib. |
| 787 |
const wasm_memorytype_t *stdlib_memory_type = NULL; |
| 788 |
for (unsigned i = 0; i < import_types.size; i++) { |
| 789 |
wasm_importtype_t *import_type = import_types.data[i]; |
| 790 |
const wasm_name_t *import_name = wasm_importtype_name(import_type); |
| 791 |
if (name_eq(import_name, "memory")) { |
| 792 |
const wasm_externtype_t *type = wasm_importtype_type(import_type); |
| 793 |
stdlib_memory_type = wasm_externtype_as_memorytype_const(type); |
| 794 |
} |
| 795 |
} |
| 796 |
if (!stdlib_memory_type) { |
| 797 |
wasm_error->kind = TSWasmErrorKindCompile; |
| 798 |
format( |
| 799 |
&wasm_error->message, |
| 800 |
"Wasm stdlib is missing the 'memory' import" |
| 801 |
); |
| 802 |
goto error; |
| 803 |
} |
| 804 |
|
| 805 |
// Initialize store's memory |
| 806 |
uint64_t initial_memory_pages = wasmtime_memorytype_minimum(stdlib_memory_type); |
| 807 |
wasm_limits_t memory_limits = {.min = initial_memory_pages, .max = MAX_MEMORY_SIZE}; |
| 808 |
memory_type = wasm_memorytype_new(&memory_limits); |
| 809 |
wasmtime_memory_t memory; |
| 810 |
error = wasmtime_memory_new(context, memory_type, &memory); |
| 811 |
if (error) { |
| 812 |
wasmtime_error_message(error, &message); |
| 813 |
wasm_error->kind = TSWasmErrorKindAllocate; |
| 814 |
format( |
| 815 |
&wasm_error->message, |
| 816 |
"failed to allocate Wasm memory: %.*s", |
| 817 |
(int)message.size, message.data |
| 818 |
); |
| 819 |
goto error; |
| 820 |
} |
| 821 |
wasm_memorytype_delete(memory_type); |
| 822 |
memory_type = NULL; |
| 823 |
|
| 824 |
// Initialize store's function table |
| 825 |
wasm_limits_t table_limits = {.min = 1, .max = wasm_limits_max_default}; |
| 826 |
table_type = wasm_tabletype_new(wasm_valtype_new(WASM_FUNCREF), &table_limits); |
| 827 |
wasmtime_val_t initializer = {.kind = WASMTIME_FUNCREF}; |
| 828 |
wasmtime_table_t function_table; |
| 829 |
error = wasmtime_table_new(context, table_type, &initializer, &function_table); |
| 830 |
if (error) { |
| 831 |
wasmtime_error_message(error, &message); |
| 832 |
wasm_error->kind = TSWasmErrorKindAllocate; |
| 833 |
format( |
| 834 |
&wasm_error->message, |
| 835 |
"failed to allocate Wasm table: %.*s", |
| 836 |
(int)message.size, message.data |
| 837 |
); |
| 838 |
goto error; |
| 839 |
} |
| 840 |
wasm_tabletype_delete(table_type); |
| 841 |
table_type = NULL; |
| 842 |
|
| 843 |
unsigned stdlib_symbols_len = array_len(STDLIB_SYMBOLS); |
| 844 |
|
| 845 |
// Define globals for the stack and heap start addresses. |
| 846 |
wasm_globaltype_t *const_i32_type = wasm_globaltype_new(wasm_valtype_new_i32(), WASM_CONST); |
| 847 |
wasm_globaltype_t *var_i32_type = wasm_globaltype_new(wasm_valtype_new_i32(), WASM_VAR); |
| 848 |
|
| 849 |
wasmtime_val_t stack_pointer_value = WASM_I32_VAL(0); |
| 850 |
wasmtime_global_t stack_pointer_global; |
| 851 |
error = wasmtime_global_new(context, var_i32_type, &stack_pointer_value, &stack_pointer_global); |
| 852 |
wasm_globaltype_delete(var_i32_type); |
| 853 |
ts_assert(!error); |
| 854 |
|
| 855 |
*self = (TSWasmStore) { |
| 856 |
.engine = wasmtime_engine_clone(engine), |
| 857 |
.store = store, |
| 858 |
.memory = memory, |
| 859 |
.function_table = function_table, |
| 860 |
.language_instances = array_new(), |
| 861 |
.stdlib_fn_indices = ts_calloc(stdlib_symbols_len, sizeof(wasmtime_func_t)), |
| 862 |
.builtin_fn_indices = builtin_fn_indices, |
| 863 |
.stack_pointer_global = stack_pointer_global, |
| 864 |
.current_memory_offset = 0, |
| 865 |
.current_function_table_offset = 0, |
| 866 |
.const_i32_type = const_i32_type, |
| 867 |
}; |
| 868 |
|
| 869 |
// Set up the imports for the stdlib module. |
| 870 |
imports = ts_calloc(import_types.size, sizeof(wasmtime_extern_t)); |
| 871 |
for (unsigned i = 0; i < import_types.size; i++) { |
| 872 |
wasm_importtype_t *type = import_types.data[i]; |
| 873 |
const wasm_name_t *import_name = wasm_importtype_name(type); |
| 874 |
if (!ts_wasm_store__provide_builtin_import(self, import_name, &imports[i])) { |
| 875 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 876 |
format( |
| 877 |
&wasm_error->message, |
| 878 |
"unexpected import in Wasm stdlib: %.*s\n", |
| 879 |
(int)import_name->size, import_name->data |
| 880 |
); |
| 881 |
goto error; |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
// Instantiate the stdlib module. |
| 886 |
wasmtime_instance_t instance; |
| 887 |
error = wasmtime_instance_new(context, stdlib_module, imports, import_types.size, &instance, &trap); |
| 888 |
ts_free(imports); |
| 889 |
imports = NULL; |
| 890 |
if (error) { |
| 891 |
wasmtime_error_message(error, &message); |
| 892 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 893 |
format( |
| 894 |
&wasm_error->message, |
| 895 |
"failed to instantiate Wasm stdlib module: %.*s", |
| 896 |
(int)message.size, message.data |
| 897 |
); |
| 898 |
goto error; |
| 899 |
} |
| 900 |
if (trap) { |
| 901 |
wasm_trap_message(trap, &message); |
| 902 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 903 |
format( |
| 904 |
&wasm_error->message, |
| 905 |
"trapped when instantiating Wasm stdlib module: %.*s", |
| 906 |
(int)message.size, message.data |
| 907 |
); |
| 908 |
goto error; |
| 909 |
} |
| 910 |
wasm_importtype_vec_delete(&import_types); |
| 911 |
|
| 912 |
// Process the stdlib module's exports. |
| 913 |
for (unsigned i = 0; i < stdlib_symbols_len; i++) { |
| 914 |
self->stdlib_fn_indices[i] = (wasmtime_func_t){.store_id = 0}; |
| 915 |
} |
| 916 |
wasmtime_module_exports(stdlib_module, &export_types); |
| 917 |
for (unsigned i = 0; i < export_types.size; i++) { |
| 918 |
wasm_exporttype_t *export_type = export_types.data[i]; |
| 919 |
const wasm_name_t *name = wasm_exporttype_name(export_type); |
| 920 |
|
| 921 |
char *export_name; |
| 922 |
size_t name_len; |
| 923 |
wasmtime_extern_t export = {.kind = WASM_EXTERN_GLOBAL}; |
| 924 |
bool exists = wasmtime_instance_export_nth(context, &instance, i, &export_name, &name_len, &export); |
| 925 |
ts_assert(exists); |
| 926 |
|
| 927 |
if (export.kind == WASMTIME_EXTERN_GLOBAL) { |
| 928 |
if (name_eq(name, "__stack_pointer")) { |
| 929 |
self->stack_pointer_global = export.of.global; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
if (export.kind == WASMTIME_EXTERN_FUNC) { |
| 934 |
if (ts_wasm_store__call_module_initializer(self, name, &export, &trap)) { |
| 935 |
if (trap) { |
| 936 |
wasm_trap_message(trap, &message); |
| 937 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 938 |
format( |
| 939 |
&wasm_error->message, |
| 940 |
"trap when calling stdlib relocation function: %.*s\n", |
| 941 |
(int)message.size, message.data |
| 942 |
); |
| 943 |
goto error; |
| 944 |
} |
| 945 |
continue; |
| 946 |
} |
| 947 |
|
| 948 |
if (name_eq(name, "reset_heap")) { |
| 949 |
self->builtin_fn_indices.reset_heap = export.of.func; |
| 950 |
continue; |
| 951 |
} |
| 952 |
|
| 953 |
for (unsigned j = 0; j < stdlib_symbols_len; j++) { |
| 954 |
if (name_eq(name, STDLIB_SYMBOLS[j])) { |
| 955 |
self->stdlib_fn_indices[j] = export.of.func; |
| 956 |
break; |
| 957 |
} |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
if (self->builtin_fn_indices.reset_heap.store_id == 0) { |
| 963 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 964 |
format( |
| 965 |
&wasm_error->message, |
| 966 |
"missing malloc reset function in Wasm stdlib" |
| 967 |
); |
| 968 |
goto error; |
| 969 |
} |
| 970 |
|
| 971 |
for (unsigned i = 0; i < stdlib_symbols_len; i++) { |
| 972 |
if (self->stdlib_fn_indices[i].store_id == 0) { |
| 973 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 974 |
format( |
| 975 |
&wasm_error->message, |
| 976 |
"missing exported symbol in Wasm stdlib: %s", |
| 977 |
STDLIB_SYMBOLS[i] |
| 978 |
); |
| 979 |
goto error; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
wasm_exporttype_vec_delete(&export_types); |
| 984 |
wasmtime_module_delete(stdlib_module); |
| 985 |
|
| 986 |
// Add all of the lexer callback functions to the function table. Store their function table |
| 987 |
// indices on the in-memory lexer. |
| 988 |
uint64_t table_index; |
| 989 |
error = wasmtime_table_grow(context, &function_table, lexer_definitions_len, &initializer, &table_index); |
| 990 |
if (error) { |
| 991 |
wasmtime_error_message(error, &message); |
| 992 |
wasm_error->kind = TSWasmErrorKindAllocate; |
| 993 |
format( |
| 994 |
&wasm_error->message, |
| 995 |
"failed to grow Wasm table to initial size: %.*s", |
| 996 |
(int)message.size, message.data |
| 997 |
); |
| 998 |
goto error; |
| 999 |
} |
| 1000 |
for (unsigned i = 0; i < lexer_definitions_len; i++) { |
| 1001 |
FunctionDefinition *definition = &lexer_definitions[i]; |
| 1002 |
wasmtime_val_t func_val = {.kind = WASMTIME_FUNCREF, .of.funcref = lexer_funcs[i]}; |
| 1003 |
error = wasmtime_table_set(context, &function_table, table_index, &func_val); |
| 1004 |
ts_assert(!error); |
| 1005 |
*(int32_t *)(definition->storage_location) = table_index; |
| 1006 |
table_index++; |
| 1007 |
} |
| 1008 |
ts_free(lexer_funcs); |
| 1009 |
|
| 1010 |
self->current_function_table_offset = table_index; |
| 1011 |
self->lexer_address = initial_memory_pages * MEMORY_PAGE_SIZE; |
| 1012 |
self->current_memory_offset = self->lexer_address + sizeof(LexerInWasmMemory); |
| 1013 |
|
| 1014 |
// Grow the memory enough to hold the builtin lexer and serialization buffer. |
| 1015 |
uint32_t new_pages_needed = (self->current_memory_offset - self->lexer_address - 1) / MEMORY_PAGE_SIZE + 1; |
| 1016 |
uint64_t prev_memory_size; |
| 1017 |
wasmtime_memory_grow(context, &memory, new_pages_needed, &prev_memory_size); |
| 1018 |
|
| 1019 |
uint8_t *memory_data = wasmtime_memory_data(context, &memory); |
| 1020 |
memcpy(&memory_data[self->lexer_address], &lexer, sizeof(lexer)); |
| 1021 |
return self; |
| 1022 |
|
| 1023 |
error: |
| 1024 |
ts_free(self); |
| 1025 |
if (stdlib_module) wasmtime_module_delete(stdlib_module); |
| 1026 |
if (store) wasmtime_store_delete(store); |
| 1027 |
if (import_types.size) wasm_importtype_vec_delete(&import_types); |
| 1028 |
if (memory_type) wasm_memorytype_delete(memory_type); |
| 1029 |
if (table_type) wasm_tabletype_delete(table_type); |
| 1030 |
if (trap) wasm_trap_delete(trap); |
| 1031 |
if (error) wasmtime_error_delete(error); |
| 1032 |
if (message.size) wasm_byte_vec_delete(&message); |
| 1033 |
if (export_types.size) wasm_exporttype_vec_delete(&export_types); |
| 1034 |
if (imports) ts_free(imports); |
| 1035 |
ts_free(lexer_funcs); |
| 1036 |
return NULL; |
| 1037 |
} |
| 1038 |
|
| 1039 |
void ts_wasm_store_delete(TSWasmStore *self) { |
| 1040 |
if (!self) return; |
| 1041 |
ts_free(self->stdlib_fn_indices); |
| 1042 |
wasm_globaltype_delete(self->const_i32_type); |
| 1043 |
wasmtime_store_delete(self->store); |
| 1044 |
wasm_engine_delete(self->engine); |
| 1045 |
for (unsigned i = 0; i < self->language_instances.size; i++) { |
| 1046 |
LanguageWasmInstance *instance = array_get(&self->language_instances, i); |
| 1047 |
language_id_delete(instance->language_id); |
| 1048 |
} |
| 1049 |
array_delete(&self->language_instances); |
| 1050 |
ts_free(self); |
| 1051 |
} |
| 1052 |
|
| 1053 |
size_t ts_wasm_store_language_count(const TSWasmStore *self) { |
| 1054 |
size_t result = 0; |
| 1055 |
for (unsigned i = 0; i < self->language_instances.size; i++) { |
| 1056 |
const WasmLanguageId *id = array_get(&self->language_instances, i)->language_id; |
| 1057 |
if (!id->is_language_deleted) { |
| 1058 |
result++; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
return result; |
| 1062 |
} |
| 1063 |
|
| 1064 |
static uint32_t ts_wasm_store__heap_address(TSWasmStore *self) { |
| 1065 |
return self->current_memory_offset + TREE_SITTER_SERIALIZATION_BUFFER_SIZE; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static uint32_t ts_wasm_store__serialization_buffer_address(TSWasmStore *self) { |
| 1069 |
return self->current_memory_offset; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static bool ts_wasm_store__instantiate( |
| 1073 |
TSWasmStore *self, |
| 1074 |
wasmtime_module_t *module, |
| 1075 |
const char *language_name, |
| 1076 |
const WasmDylinkInfo *dylink_info, |
| 1077 |
wasmtime_instance_t *result, |
| 1078 |
int32_t *language_address, |
| 1079 |
char **error_message |
| 1080 |
) { |
| 1081 |
wasmtime_error_t *error = NULL; |
| 1082 |
wasm_trap_t *trap = NULL; |
| 1083 |
wasm_message_t message = WASM_EMPTY_VEC; |
| 1084 |
char *language_function_name = NULL; |
| 1085 |
wasmtime_extern_t *imports = NULL; |
| 1086 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1087 |
|
| 1088 |
// Grow the function table to make room for the new functions. |
| 1089 |
wasmtime_val_t initializer = {.kind = WASMTIME_FUNCREF}; |
| 1090 |
uint64_t prev_table_size; |
| 1091 |
error = wasmtime_table_grow(context, &self->function_table, dylink_info->table_size, &initializer, &prev_table_size); |
| 1092 |
if (error) { |
| 1093 |
format(error_message, "invalid function table size %u", dylink_info->table_size); |
| 1094 |
goto error; |
| 1095 |
} |
| 1096 |
|
| 1097 |
// Grow the memory to make room for the new data. |
| 1098 |
uint32_t needed_memory_size = ts_wasm_store__heap_address(self) + dylink_info->memory_size; |
| 1099 |
uint32_t current_memory_size = wasmtime_memory_data_size(context, &self->memory); |
| 1100 |
if (needed_memory_size > current_memory_size) { |
| 1101 |
uint32_t pages_to_grow = ( |
| 1102 |
needed_memory_size - current_memory_size + MEMORY_PAGE_SIZE - 1) / |
| 1103 |
MEMORY_PAGE_SIZE; |
| 1104 |
uint64_t prev_memory_size; |
| 1105 |
error = wasmtime_memory_grow(context, &self->memory, pages_to_grow, &prev_memory_size); |
| 1106 |
if (error) { |
| 1107 |
format(error_message, "invalid memory size %u", dylink_info->memory_size); |
| 1108 |
goto error; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Construct the language function name as string. |
| 1113 |
format(&language_function_name, "tree_sitter_%s", language_name); |
| 1114 |
|
| 1115 |
// Build the imports list for the module. |
| 1116 |
wasm_importtype_vec_t import_types = WASM_EMPTY_VEC; |
| 1117 |
wasmtime_module_imports(module, &import_types); |
| 1118 |
imports = ts_calloc(import_types.size, sizeof(wasmtime_extern_t)); |
| 1119 |
|
| 1120 |
for (unsigned i = 0; i < import_types.size; i++) { |
| 1121 |
const wasm_importtype_t *import_type = import_types.data[i]; |
| 1122 |
const wasm_name_t *import_name = wasm_importtype_name(import_type); |
| 1123 |
if (import_name->size == 0) { |
| 1124 |
format(error_message, "empty import name"); |
| 1125 |
goto error; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (ts_wasm_store__provide_builtin_import(self, import_name, &imports[i])) { |
| 1129 |
continue; |
| 1130 |
} |
| 1131 |
|
| 1132 |
bool defined_in_stdlib = false; |
| 1133 |
for (unsigned j = 0; j < array_len(STDLIB_SYMBOLS); j++) { |
| 1134 |
if (name_eq(import_name, STDLIB_SYMBOLS[j])) { |
| 1135 |
imports[i] = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_FUNC, .of.func = self->stdlib_fn_indices[j]}; |
| 1136 |
defined_in_stdlib = true; |
| 1137 |
break; |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (!defined_in_stdlib) { |
| 1142 |
format( |
| 1143 |
error_message, |
| 1144 |
"invalid import '%.*s'\n", |
| 1145 |
(int)import_name->size, import_name->data |
| 1146 |
); |
| 1147 |
goto error; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
wasmtime_instance_t instance; |
| 1152 |
error = wasmtime_instance_new(context, module, imports, import_types.size, &instance, &trap); |
| 1153 |
wasm_importtype_vec_delete(&import_types); |
| 1154 |
ts_free(imports); |
| 1155 |
imports = NULL; |
| 1156 |
if (error) { |
| 1157 |
wasmtime_error_message(error, &message); |
| 1158 |
format( |
| 1159 |
error_message, |
| 1160 |
"error instantiating Wasm module: %.*s\n", |
| 1161 |
(int)message.size, message.data |
| 1162 |
); |
| 1163 |
goto error; |
| 1164 |
} |
| 1165 |
if (trap) { |
| 1166 |
wasm_trap_message(trap, &message); |
| 1167 |
format( |
| 1168 |
error_message, |
| 1169 |
"trap when instantiating Wasm module: %.*s\n", |
| 1170 |
(int)message.size, message.data |
| 1171 |
); |
| 1172 |
goto error; |
| 1173 |
} |
| 1174 |
|
| 1175 |
self->current_memory_offset += dylink_info->memory_size; |
| 1176 |
self->current_function_table_offset += dylink_info->table_size; |
| 1177 |
|
| 1178 |
// Process the module's exports. |
| 1179 |
bool found_language = false; |
| 1180 |
wasmtime_extern_t language_extern; |
| 1181 |
wasm_exporttype_vec_t export_types = WASM_EMPTY_VEC; |
| 1182 |
wasmtime_module_exports(module, &export_types); |
| 1183 |
for (unsigned i = 0; i < export_types.size; i++) { |
| 1184 |
wasm_exporttype_t *export_type = export_types.data[i]; |
| 1185 |
const wasm_name_t *name = wasm_exporttype_name(export_type); |
| 1186 |
|
| 1187 |
size_t name_len; |
| 1188 |
char *export_name; |
| 1189 |
wasmtime_extern_t export = {.kind = WASM_EXTERN_GLOBAL}; |
| 1190 |
bool exists = wasmtime_instance_export_nth(context, &instance, i, &export_name, &name_len, &export); |
| 1191 |
ts_assert(exists); |
| 1192 |
|
| 1193 |
// If the module exports an initialization or data-relocation function, call it. |
| 1194 |
if (ts_wasm_store__call_module_initializer(self, name, &export, &trap)) { |
| 1195 |
if (trap) { |
| 1196 |
wasm_trap_message(trap, &message); |
| 1197 |
format( |
| 1198 |
error_message, |
| 1199 |
"trap when calling data relocation function: %.*s\n", |
| 1200 |
(int)message.size, message.data |
| 1201 |
); |
| 1202 |
goto error; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
// Find the main language function for the module. |
| 1207 |
else if (name_eq(name, language_function_name)) { |
| 1208 |
language_extern = export; |
| 1209 |
found_language = true; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
wasm_exporttype_vec_delete(&export_types); |
| 1213 |
|
| 1214 |
if (!found_language) { |
| 1215 |
format( |
| 1216 |
error_message, |
| 1217 |
"module did not contain language function: %s", |
| 1218 |
language_function_name |
| 1219 |
); |
| 1220 |
goto error; |
| 1221 |
} |
| 1222 |
|
| 1223 |
// Invoke the language function to get the static address of the language object. |
| 1224 |
wasmtime_func_t language_func = language_extern.of.func; |
| 1225 |
wasmtime_val_t language_address_val; |
| 1226 |
error = wasmtime_func_call(context, &language_func, NULL, 0, &language_address_val, 1, &trap); |
| 1227 |
ts_assert(!error); |
| 1228 |
if (trap) { |
| 1229 |
wasm_trap_message(trap, &message); |
| 1230 |
format( |
| 1231 |
error_message, |
| 1232 |
"trapped when calling language function: %s: %.*s\n", |
| 1233 |
language_function_name, (int)message.size, message.data |
| 1234 |
); |
| 1235 |
goto error; |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (language_address_val.kind != WASMTIME_I32) { |
| 1239 |
format( |
| 1240 |
error_message, |
| 1241 |
"language function did not return an integer: %s\n", |
| 1242 |
language_function_name |
| 1243 |
); |
| 1244 |
goto error; |
| 1245 |
} |
| 1246 |
|
| 1247 |
ts_free(language_function_name); |
| 1248 |
*result = instance; |
| 1249 |
*language_address = language_address_val.of.i32; |
| 1250 |
return true; |
| 1251 |
|
| 1252 |
error: |
| 1253 |
if (language_function_name) ts_free(language_function_name); |
| 1254 |
if (message.size) wasm_byte_vec_delete(&message); |
| 1255 |
if (error) wasmtime_error_delete(error); |
| 1256 |
if (trap) wasm_trap_delete(trap); |
| 1257 |
if (imports) ts_free(imports); |
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
|
| 1261 |
static bool ts_wasm_store__sentinel_lex_fn(TSLexer *_lexer, TSStateId state) { |
| 1262 |
return false; |
| 1263 |
} |
| 1264 |
|
| 1265 |
const TSLanguage *ts_wasm_store_load_language( |
| 1266 |
TSWasmStore *self, |
| 1267 |
const char *language_name, |
| 1268 |
const char *wasm, |
| 1269 |
uint32_t wasm_len, |
| 1270 |
TSWasmError *wasm_error |
| 1271 |
) { |
| 1272 |
WasmDylinkInfo dylink_info; |
| 1273 |
wasmtime_module_t *module = NULL; |
| 1274 |
wasmtime_error_t *error = NULL; |
| 1275 |
TSLanguage *language = NULL; |
| 1276 |
StringData symbol_name_buffer = array_new(); |
| 1277 |
StringData field_name_buffer = array_new(); |
| 1278 |
wasm_error->kind = TSWasmErrorKindNone; |
| 1279 |
|
| 1280 |
if (!wasm_dylink_info__parse((const unsigned char *)wasm, wasm_len, &dylink_info)) { |
| 1281 |
wasm_error->kind = TSWasmErrorKindParse; |
| 1282 |
format(&wasm_error->message, "failed to parse dylink section of Wasm module"); |
| 1283 |
goto error; |
| 1284 |
} |
| 1285 |
|
| 1286 |
// Compile the Wasm code. |
| 1287 |
error = wasmtime_module_new(self->engine, (const uint8_t *)wasm, wasm_len, &module); |
| 1288 |
if (error) { |
| 1289 |
wasm_message_t message; |
| 1290 |
wasmtime_error_message(error, &message); |
| 1291 |
wasm_error->kind = TSWasmErrorKindCompile; |
| 1292 |
format(&wasm_error->message, "error compiling Wasm module: %.*s", (int)message.size, message.data); |
| 1293 |
wasm_byte_vec_delete(&message); |
| 1294 |
goto error; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// Instantiate the module in this store. |
| 1298 |
wasmtime_instance_t instance; |
| 1299 |
int32_t language_address; |
| 1300 |
if (!ts_wasm_store__instantiate( |
| 1301 |
self, |
| 1302 |
module, |
| 1303 |
language_name, |
| 1304 |
&dylink_info, |
| 1305 |
&instance, |
| 1306 |
&language_address, |
| 1307 |
&wasm_error->message |
| 1308 |
)) { |
| 1309 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 1310 |
goto error; |
| 1311 |
} |
| 1312 |
|
| 1313 |
// Copy all of the static data out of the language object in Wasm memory, |
| 1314 |
// constructing a native language object. |
| 1315 |
LanguageInWasmMemory wasm_language; |
| 1316 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1317 |
const uint8_t *memory = wasmtime_memory_data(context, &self->memory); |
| 1318 |
WasmMemory wasm_memory = { |
| 1319 |
.data = memory, |
| 1320 |
.size = wasmtime_memory_data_size(context, &self->memory), |
| 1321 |
}; |
| 1322 |
bool valid_wasm_memory = true; |
| 1323 |
if (!wasm_memory__read(&wasm_memory, language_address, &wasm_language, sizeof(LanguageInWasmMemory))) { |
| 1324 |
goto invalid_language_memory; |
| 1325 |
} |
| 1326 |
|
| 1327 |
bool has_supertypes = |
| 1328 |
wasm_language.abi_version >= LANGUAGE_VERSION_WITH_RESERVED_WORDS && |
| 1329 |
wasm_language.supertype_count > 0; |
| 1330 |
|
| 1331 |
int32_t addresses[] = { |
| 1332 |
wasm_language.parse_table, |
| 1333 |
wasm_language.small_parse_table, |
| 1334 |
wasm_language.small_parse_table_map, |
| 1335 |
wasm_language.parse_actions, |
| 1336 |
wasm_language.symbol_names, |
| 1337 |
wasm_language.field_names, |
| 1338 |
wasm_language.field_map_slices, |
| 1339 |
wasm_language.field_map_entries, |
| 1340 |
wasm_language.symbol_metadata, |
| 1341 |
wasm_language.public_symbol_map, |
| 1342 |
wasm_language.alias_map, |
| 1343 |
wasm_language.alias_sequences, |
| 1344 |
wasm_language.lex_modes, |
| 1345 |
wasm_language.lex_fn, |
| 1346 |
wasm_language.keyword_lex_fn, |
| 1347 |
wasm_language.primary_state_ids, |
| 1348 |
wasm_language.name, |
| 1349 |
wasm_language.reserved_words, |
| 1350 |
has_supertypes ? wasm_language.supertype_symbols : 0, |
| 1351 |
has_supertypes ? wasm_language.supertype_map_entries : 0, |
| 1352 |
has_supertypes ? wasm_language.supertype_map_slices : 0, |
| 1353 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.states : 0, |
| 1354 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.symbol_map : 0, |
| 1355 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.create : 0, |
| 1356 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.destroy : 0, |
| 1357 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.scan : 0, |
| 1358 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.serialize : 0, |
| 1359 |
wasm_language.external_token_count > 0 ? wasm_language.external_scanner.deserialize : 0, |
| 1360 |
language_address, |
| 1361 |
self->current_memory_offset, |
| 1362 |
}; |
| 1363 |
uint32_t address_count = array_len(addresses); |
| 1364 |
|
| 1365 |
language = ts_calloc(1, sizeof(TSLanguage)); |
| 1366 |
*language = (TSLanguage) { |
| 1367 |
.abi_version = wasm_language.abi_version, |
| 1368 |
.symbol_count = wasm_language.symbol_count, |
| 1369 |
.alias_count = wasm_language.alias_count, |
| 1370 |
.token_count = wasm_language.token_count, |
| 1371 |
.external_token_count = wasm_language.external_token_count, |
| 1372 |
.state_count = wasm_language.state_count, |
| 1373 |
.large_state_count = wasm_language.large_state_count, |
| 1374 |
.production_id_count = wasm_language.production_id_count, |
| 1375 |
.field_count = wasm_language.field_count, |
| 1376 |
.supertype_count = wasm_language.supertype_count, |
| 1377 |
.max_alias_sequence_length = wasm_language.max_alias_sequence_length, |
| 1378 |
.keyword_capture_token = wasm_language.keyword_capture_token, |
| 1379 |
.metadata = wasm_language.metadata, |
| 1380 |
.parse_table = copy( |
| 1381 |
&wasm_memory, |
| 1382 |
wasm_language.parse_table, |
| 1383 |
wasm_language.large_state_count * wasm_language.symbol_count * sizeof(uint16_t), |
| 1384 |
&valid_wasm_memory |
| 1385 |
), |
| 1386 |
.parse_actions = copy_unsized_static_array( |
| 1387 |
&wasm_memory, |
| 1388 |
wasm_language.parse_actions, |
| 1389 |
addresses, |
| 1390 |
address_count, |
| 1391 |
&valid_wasm_memory |
| 1392 |
), |
| 1393 |
.symbol_names = copy_strings( |
| 1394 |
&wasm_memory, |
| 1395 |
wasm_language.symbol_names, |
| 1396 |
wasm_language.symbol_count + wasm_language.alias_count, |
| 1397 |
&symbol_name_buffer, |
| 1398 |
&valid_wasm_memory |
| 1399 |
), |
| 1400 |
.symbol_metadata = copy( |
| 1401 |
&wasm_memory, |
| 1402 |
wasm_language.symbol_metadata, |
| 1403 |
(wasm_language.symbol_count + wasm_language.alias_count) * sizeof(TSSymbolMetadata), |
| 1404 |
&valid_wasm_memory |
| 1405 |
), |
| 1406 |
.public_symbol_map = copy( |
| 1407 |
&wasm_memory, |
| 1408 |
wasm_language.public_symbol_map, |
| 1409 |
(wasm_language.symbol_count + wasm_language.alias_count) * sizeof(TSSymbol), |
| 1410 |
&valid_wasm_memory |
| 1411 |
), |
| 1412 |
.lex_modes = copy( |
| 1413 |
&wasm_memory, |
| 1414 |
wasm_language.lex_modes, |
| 1415 |
wasm_language.state_count * sizeof(TSLexerMode), |
| 1416 |
&valid_wasm_memory |
| 1417 |
), |
| 1418 |
}; |
| 1419 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1420 |
|
| 1421 |
if (language->field_count > 0 && language->production_id_count > 0) { |
| 1422 |
language->field_map_slices = copy( |
| 1423 |
&wasm_memory, |
| 1424 |
wasm_language.field_map_slices, |
| 1425 |
wasm_language.production_id_count * sizeof(TSMapSlice), |
| 1426 |
&valid_wasm_memory |
| 1427 |
); |
| 1428 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1429 |
|
| 1430 |
// Determine the number of field map entries by finding the greatest index |
| 1431 |
// in any of the slices. |
| 1432 |
uint32_t field_map_entry_count = 0; |
| 1433 |
for (uint32_t i = 0; i < wasm_language.production_id_count; i++) { |
| 1434 |
TSMapSlice slice = language->field_map_slices[i]; |
| 1435 |
uint32_t slice_end = slice.index + slice.length; |
| 1436 |
if (slice_end > field_map_entry_count) { |
| 1437 |
field_map_entry_count = slice_end; |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
language->field_map_entries = copy( |
| 1442 |
&wasm_memory, |
| 1443 |
wasm_language.field_map_entries, |
| 1444 |
field_map_entry_count * sizeof(TSFieldMapEntry), |
| 1445 |
&valid_wasm_memory |
| 1446 |
); |
| 1447 |
language->field_names = copy_strings( |
| 1448 |
&wasm_memory, |
| 1449 |
wasm_language.field_names, |
| 1450 |
wasm_language.field_count + 1, |
| 1451 |
&field_name_buffer, |
| 1452 |
&valid_wasm_memory |
| 1453 |
); |
| 1454 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1455 |
} |
| 1456 |
|
| 1457 |
if (has_supertypes) { |
| 1458 |
language->supertype_symbols = copy( |
| 1459 |
&wasm_memory, |
| 1460 |
wasm_language.supertype_symbols, |
| 1461 |
wasm_language.supertype_count * sizeof(TSSymbol), |
| 1462 |
&valid_wasm_memory |
| 1463 |
); |
| 1464 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1465 |
|
| 1466 |
// Determine the number of supertype map slices by finding the greatest |
| 1467 |
// supertype ID. |
| 1468 |
int largest_supertype = 0; |
| 1469 |
for (unsigned i = 0; i < language->supertype_count; i++) { |
| 1470 |
TSSymbol supertype = language->supertype_symbols[i]; |
| 1471 |
if (supertype > largest_supertype) { |
| 1472 |
largest_supertype = supertype; |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
language->supertype_map_slices = copy( |
| 1477 |
&wasm_memory, |
| 1478 |
wasm_language.supertype_map_slices, |
| 1479 |
(largest_supertype + 1) * sizeof(TSMapSlice), |
| 1480 |
&valid_wasm_memory |
| 1481 |
); |
| 1482 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1483 |
|
| 1484 |
TSSymbol last_supertype = language->supertype_symbols[language->supertype_count - 1]; |
| 1485 |
TSMapSlice last_slice = language->supertype_map_slices[last_supertype]; |
| 1486 |
uint32_t supertype_map_entry_count = last_slice.index + last_slice.length; |
| 1487 |
|
| 1488 |
language->supertype_map_entries = copy( |
| 1489 |
&wasm_memory, |
| 1490 |
wasm_language.supertype_map_entries, |
| 1491 |
supertype_map_entry_count * sizeof(char *), |
| 1492 |
&valid_wasm_memory |
| 1493 |
); |
| 1494 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1495 |
} |
| 1496 |
|
| 1497 |
if (language->max_alias_sequence_length > 0 && language->production_id_count > 0) { |
| 1498 |
// The alias map contains symbols, alias counts, and aliases, terminated by a null symbol. |
| 1499 |
int32_t alias_map_size = 0; |
| 1500 |
for (;;) { |
| 1501 |
TSSymbol symbol; |
| 1502 |
if (!wasm_memory__read(&wasm_memory, wasm_language.alias_map + alias_map_size, &symbol, sizeof(symbol))) { |
| 1503 |
goto invalid_language_memory; |
| 1504 |
} |
| 1505 |
alias_map_size += sizeof(TSSymbol); |
| 1506 |
if (symbol == 0) break; |
| 1507 |
uint16_t value_count; |
| 1508 |
if (!wasm_memory__read(&wasm_memory, wasm_language.alias_map + alias_map_size, &value_count, sizeof(value_count))) { |
| 1509 |
goto invalid_language_memory; |
| 1510 |
} |
| 1511 |
alias_map_size += sizeof(uint16_t); |
| 1512 |
alias_map_size += value_count * sizeof(TSSymbol); |
| 1513 |
} |
| 1514 |
language->alias_map = copy( |
| 1515 |
&wasm_memory, |
| 1516 |
wasm_language.alias_map, |
| 1517 |
alias_map_size, |
| 1518 |
&valid_wasm_memory |
| 1519 |
); |
| 1520 |
language->alias_sequences = copy( |
| 1521 |
&wasm_memory, |
| 1522 |
wasm_language.alias_sequences, |
| 1523 |
wasm_language.production_id_count * wasm_language.max_alias_sequence_length * sizeof(TSSymbol), |
| 1524 |
&valid_wasm_memory |
| 1525 |
); |
| 1526 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1527 |
} |
| 1528 |
|
| 1529 |
if (language->state_count > language->large_state_count) { |
| 1530 |
uint32_t small_state_count = wasm_language.state_count - wasm_language.large_state_count; |
| 1531 |
language->small_parse_table_map = copy( |
| 1532 |
&wasm_memory, |
| 1533 |
wasm_language.small_parse_table_map, |
| 1534 |
small_state_count * sizeof(uint32_t), |
| 1535 |
&valid_wasm_memory |
| 1536 |
); |
| 1537 |
language->small_parse_table = copy_unsized_static_array( |
| 1538 |
&wasm_memory, |
| 1539 |
wasm_language.small_parse_table, |
| 1540 |
addresses, |
| 1541 |
address_count, |
| 1542 |
&valid_wasm_memory |
| 1543 |
); |
| 1544 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (language->abi_version >= LANGUAGE_VERSION_WITH_PRIMARY_STATES) { |
| 1548 |
language->primary_state_ids = copy( |
| 1549 |
&wasm_memory, |
| 1550 |
wasm_language.primary_state_ids, |
| 1551 |
wasm_language.state_count * sizeof(TSStateId), |
| 1552 |
&valid_wasm_memory |
| 1553 |
); |
| 1554 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (language->abi_version >= LANGUAGE_VERSION_WITH_RESERVED_WORDS) { |
| 1558 |
language->name = copy_string(&wasm_memory, wasm_language.name, &valid_wasm_memory); |
| 1559 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1560 |
language->max_reserved_word_set_size = wasm_language.max_reserved_word_set_size; |
| 1561 |
|
| 1562 |
// Determine the number of reserved word sets by finding the maximum |
| 1563 |
// reserved_word_set_id across all lex modes. |
| 1564 |
uint16_t max_reserved_word_set_id = 0; |
| 1565 |
for (uint32_t i = 0; i < wasm_language.state_count; i++) { |
| 1566 |
uint16_t id = language->lex_modes[i].reserved_word_set_id; |
| 1567 |
if (id > max_reserved_word_set_id) max_reserved_word_set_id = id; |
| 1568 |
} |
| 1569 |
|
| 1570 |
if (max_reserved_word_set_id > 0 && language->max_reserved_word_set_size > 0) { |
| 1571 |
uint32_t reserved_word_count = |
| 1572 |
(max_reserved_word_set_id + 1) * language->max_reserved_word_set_size; |
| 1573 |
language->reserved_words = copy( |
| 1574 |
&wasm_memory, |
| 1575 |
wasm_language.reserved_words, |
| 1576 |
reserved_word_count * sizeof(TSSymbol), |
| 1577 |
&valid_wasm_memory |
| 1578 |
); |
| 1579 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
if (language->external_token_count > 0) { |
| 1584 |
language->external_scanner.symbol_map = copy( |
| 1585 |
&wasm_memory, |
| 1586 |
wasm_language.external_scanner.symbol_map, |
| 1587 |
wasm_language.external_token_count * sizeof(TSSymbol), |
| 1588 |
&valid_wasm_memory |
| 1589 |
); |
| 1590 |
if (!valid_wasm_memory) goto invalid_language_memory; |
| 1591 |
language->external_scanner.states = (void *)(uintptr_t)wasm_language.external_scanner.states; |
| 1592 |
} |
| 1593 |
|
| 1594 |
unsigned name_len = strlen(language_name); |
| 1595 |
char *name = ts_malloc(name_len + 1); |
| 1596 |
memcpy(name, language_name, name_len); |
| 1597 |
name[name_len] = '\0'; |
| 1598 |
|
| 1599 |
LanguageWasmModule *language_module = ts_malloc(sizeof(LanguageWasmModule)); |
| 1600 |
*language_module = (LanguageWasmModule) { |
| 1601 |
.language_id = language_id_new(), |
| 1602 |
.module = module, |
| 1603 |
.name = name, |
| 1604 |
.symbol_name_buffer = symbol_name_buffer.contents, |
| 1605 |
.field_name_buffer = field_name_buffer.contents, |
| 1606 |
.dylink_info = dylink_info, |
| 1607 |
.ref_count = 1, |
| 1608 |
}; |
| 1609 |
|
| 1610 |
// The lex functions are not used for Wasm languages. Use those two fields |
| 1611 |
// to mark this language as Wasm-based and to store the language's |
| 1612 |
// Wasm-specific data. |
| 1613 |
language->lex_fn = ts_wasm_store__sentinel_lex_fn; |
| 1614 |
language->keyword_lex_fn = (bool (*)(TSLexer *, TSStateId))language_module; |
| 1615 |
|
| 1616 |
// Clear out any instances of languages that have been deleted. |
| 1617 |
for (unsigned i = 0; i < self->language_instances.size; i++) { |
| 1618 |
WasmLanguageId *id = array_get(&self->language_instances, i)->language_id; |
| 1619 |
if (id->is_language_deleted) { |
| 1620 |
language_id_delete(id); |
| 1621 |
array_erase(&self->language_instances, i); |
| 1622 |
i--; |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
// Store this store's instance of this language module. |
| 1627 |
array_push(&self->language_instances, ((LanguageWasmInstance) { |
| 1628 |
.language_id = language_id_clone(language_module->language_id), |
| 1629 |
.instance = instance, |
| 1630 |
.external_states_address = wasm_language.external_scanner.states, |
| 1631 |
.lex_main_fn_index = wasm_language.lex_fn, |
| 1632 |
.lex_keyword_fn_index = wasm_language.keyword_lex_fn, |
| 1633 |
.scanner_create_fn_index = wasm_language.external_scanner.create, |
| 1634 |
.scanner_destroy_fn_index = wasm_language.external_scanner.destroy, |
| 1635 |
.scanner_serialize_fn_index = wasm_language.external_scanner.serialize, |
| 1636 |
.scanner_deserialize_fn_index = wasm_language.external_scanner.deserialize, |
| 1637 |
.scanner_scan_fn_index = wasm_language.external_scanner.scan, |
| 1638 |
})); |
| 1639 |
|
| 1640 |
return language; |
| 1641 |
|
| 1642 |
invalid_language_memory: |
| 1643 |
wasm_error->kind = TSWasmErrorKindInstantiate; |
| 1644 |
format(&wasm_error->message, "invalid language memory address"); |
| 1645 |
goto error; |
| 1646 |
|
| 1647 |
error: |
| 1648 |
delete_partially_loaded_language(language, &symbol_name_buffer, &field_name_buffer); |
| 1649 |
if (module) wasmtime_module_delete(module); |
| 1650 |
return NULL; |
| 1651 |
} |
| 1652 |
|
| 1653 |
bool ts_wasm_store_add_language( |
| 1654 |
TSWasmStore *self, |
| 1655 |
const TSLanguage *language, |
| 1656 |
uint32_t *index |
| 1657 |
) { |
| 1658 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1659 |
const LanguageWasmModule *language_module = (void *)language->keyword_lex_fn; |
| 1660 |
|
| 1661 |
// Search for this store's instance of the language module. Also clear out any |
| 1662 |
// instances of languages that have been deleted. |
| 1663 |
bool exists = false; |
| 1664 |
for (unsigned i = 0; i < self->language_instances.size; i++) { |
| 1665 |
WasmLanguageId *id = array_get(&self->language_instances, i)->language_id; |
| 1666 |
if (id->is_language_deleted) { |
| 1667 |
language_id_delete(id); |
| 1668 |
array_erase(&self->language_instances, i); |
| 1669 |
i--; |
| 1670 |
} else if (id == language_module->language_id) { |
| 1671 |
exists = true; |
| 1672 |
*index = i; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
// If the language module has not been instantiated in this store, then add |
| 1677 |
// it to this store. |
| 1678 |
if (!exists) { |
| 1679 |
*index = self->language_instances.size; |
| 1680 |
char *message; |
| 1681 |
wasmtime_instance_t instance; |
| 1682 |
int32_t language_address; |
| 1683 |
if (!ts_wasm_store__instantiate( |
| 1684 |
self, |
| 1685 |
language_module->module, |
| 1686 |
language_module->name, |
| 1687 |
&language_module->dylink_info, |
| 1688 |
&instance, |
| 1689 |
&language_address, |
| 1690 |
&message |
| 1691 |
)) { |
| 1692 |
ts_free(message); |
| 1693 |
return false; |
| 1694 |
} |
| 1695 |
|
| 1696 |
LanguageInWasmMemory wasm_language; |
| 1697 |
const uint8_t *memory = wasmtime_memory_data(context, &self->memory); |
| 1698 |
WasmMemory wasm_memory = { |
| 1699 |
.data = memory, |
| 1700 |
.size = wasmtime_memory_data_size(context, &self->memory), |
| 1701 |
}; |
| 1702 |
if (!wasm_memory__read(&wasm_memory, language_address, &wasm_language, sizeof(LanguageInWasmMemory))) { |
| 1703 |
return false; |
| 1704 |
} |
| 1705 |
array_push(&self->language_instances, ((LanguageWasmInstance) { |
| 1706 |
.language_id = language_id_clone(language_module->language_id), |
| 1707 |
.instance = instance, |
| 1708 |
.external_states_address = wasm_language.external_scanner.states, |
| 1709 |
.lex_main_fn_index = wasm_language.lex_fn, |
| 1710 |
.lex_keyword_fn_index = wasm_language.keyword_lex_fn, |
| 1711 |
.scanner_create_fn_index = wasm_language.external_scanner.create, |
| 1712 |
.scanner_destroy_fn_index = wasm_language.external_scanner.destroy, |
| 1713 |
.scanner_serialize_fn_index = wasm_language.external_scanner.serialize, |
| 1714 |
.scanner_deserialize_fn_index = wasm_language.external_scanner.deserialize, |
| 1715 |
.scanner_scan_fn_index = wasm_language.external_scanner.scan, |
| 1716 |
})); |
| 1717 |
} |
| 1718 |
|
| 1719 |
return true; |
| 1720 |
} |
| 1721 |
|
| 1722 |
void ts_wasm_store_reset_heap(TSWasmStore *self) { |
| 1723 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1724 |
wasmtime_func_t *func = &self->builtin_fn_indices.reset_heap; |
| 1725 |
wasm_trap_t *trap = NULL; |
| 1726 |
wasmtime_val_t args[1] = { |
| 1727 |
{.of.i32 = ts_wasm_store__heap_address(self), .kind = WASMTIME_I32}, |
| 1728 |
}; |
| 1729 |
|
| 1730 |
wasmtime_error_t *error = wasmtime_func_call(context, func, args, 1, NULL, 0, &trap); |
| 1731 |
ts_assert(!error); |
| 1732 |
ts_assert(!trap); |
| 1733 |
} |
| 1734 |
|
| 1735 |
bool ts_wasm_store_start(TSWasmStore *self, TSLexer *lexer, const TSLanguage *language) { |
| 1736 |
uint32_t instance_index; |
| 1737 |
if (!ts_wasm_store_add_language(self, language, &instance_index)) return false; |
| 1738 |
self->current_lexer = lexer; |
| 1739 |
self->current_instance = array_get(&self->language_instances, instance_index); |
| 1740 |
self->has_error = false; |
| 1741 |
ts_wasm_store_reset_heap(self); |
| 1742 |
return true; |
| 1743 |
} |
| 1744 |
|
| 1745 |
void ts_wasm_store_reset(TSWasmStore *self) { |
| 1746 |
self->current_lexer = NULL; |
| 1747 |
self->current_instance = NULL; |
| 1748 |
self->has_error = false; |
| 1749 |
ts_wasm_store_reset_heap(self); |
| 1750 |
} |
| 1751 |
|
| 1752 |
static void ts_wasm_store__call( |
| 1753 |
TSWasmStore *self, |
| 1754 |
int32_t function_index, |
| 1755 |
wasmtime_val_raw_t *args_and_results, |
| 1756 |
size_t args_and_results_len |
| 1757 |
) { |
| 1758 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1759 |
wasmtime_val_t value; |
| 1760 |
bool succeeded = wasmtime_table_get(context, &self->function_table, function_index, &value); |
| 1761 |
ts_assert(succeeded); |
| 1762 |
ts_assert(value.kind == WASMTIME_FUNCREF); |
| 1763 |
wasmtime_func_t func = value.of.funcref; |
| 1764 |
|
| 1765 |
wasm_trap_t *trap = NULL; |
| 1766 |
wasmtime_error_t *error = wasmtime_func_call_unchecked(context, &func, args_and_results, args_and_results_len, &trap); |
| 1767 |
if (error) { |
| 1768 |
// wasm_message_t message; |
| 1769 |
// wasmtime_error_message(error, &message); |
| 1770 |
// fprintf( |
| 1771 |
// stderr, |
| 1772 |
// "error in Wasm module: %.*s\n", |
| 1773 |
// (int)message.size, message.data |
| 1774 |
// ); |
| 1775 |
wasmtime_error_delete(error); |
| 1776 |
self->has_error = true; |
| 1777 |
} else if (trap) { |
| 1778 |
// wasm_message_t message; |
| 1779 |
// wasm_trap_message(trap, &message); |
| 1780 |
// fprintf( |
| 1781 |
// stderr, |
| 1782 |
// "trap in Wasm module: %.*s\n", |
| 1783 |
// (int)message.size, message.data |
| 1784 |
// ); |
| 1785 |
wasm_trap_delete(trap); |
| 1786 |
self->has_error = true; |
| 1787 |
} |
| 1788 |
} |
| 1789 |
|
| 1790 |
// The data fields of TSLexer, without the function pointers. |
| 1791 |
// |
| 1792 |
// This portion of the struct needs to be copied in and out |
| 1793 |
// of Wasm memory before and after calling a scan function. |
| 1794 |
typedef struct { |
| 1795 |
int32_t lookahead; |
| 1796 |
TSSymbol result_symbol; |
| 1797 |
} TSLexerDataPrefix; |
| 1798 |
|
| 1799 |
static bool ts_wasm_store__call_lex_function(TSWasmStore *self, unsigned function_index, TSStateId state) { |
| 1800 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1801 |
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory); |
| 1802 |
memcpy( |
| 1803 |
&memory_data[self->lexer_address], |
| 1804 |
self->current_lexer, |
| 1805 |
sizeof(TSLexerDataPrefix) |
| 1806 |
); |
| 1807 |
|
| 1808 |
wasmtime_val_raw_t args[2] = { |
| 1809 |
{.i32 = self->lexer_address}, |
| 1810 |
{.i32 = state}, |
| 1811 |
}; |
| 1812 |
ts_wasm_store__call(self, function_index, args, 2); |
| 1813 |
if (self->has_error) return false; |
| 1814 |
bool result = args[0].i32; |
| 1815 |
|
| 1816 |
memcpy( |
| 1817 |
self->current_lexer, |
| 1818 |
&memory_data[self->lexer_address], |
| 1819 |
sizeof(TSLexerDataPrefix) |
| 1820 |
); |
| 1821 |
return result; |
| 1822 |
} |
| 1823 |
|
| 1824 |
bool ts_wasm_store_call_lex_main(TSWasmStore *self, TSStateId state) { |
| 1825 |
return ts_wasm_store__call_lex_function( |
| 1826 |
self, |
| 1827 |
self->current_instance->lex_main_fn_index, |
| 1828 |
state |
| 1829 |
); |
| 1830 |
} |
| 1831 |
|
| 1832 |
bool ts_wasm_store_call_lex_keyword(TSWasmStore *self, TSStateId state) { |
| 1833 |
return ts_wasm_store__call_lex_function( |
| 1834 |
self, |
| 1835 |
self->current_instance->lex_keyword_fn_index, |
| 1836 |
state |
| 1837 |
); |
| 1838 |
} |
| 1839 |
|
| 1840 |
uint32_t ts_wasm_store_call_scanner_create(TSWasmStore *self) { |
| 1841 |
wasmtime_val_raw_t args[1] = {{.i32 = 0}}; |
| 1842 |
ts_wasm_store__call(self, self->current_instance->scanner_create_fn_index, args, 1); |
| 1843 |
if (self->has_error) return 0; |
| 1844 |
return args[0].i32; |
| 1845 |
} |
| 1846 |
|
| 1847 |
void ts_wasm_store_call_scanner_destroy(TSWasmStore *self, uint32_t scanner_address) { |
| 1848 |
if (self->current_instance) { |
| 1849 |
wasmtime_val_raw_t args[1] = {{.i32 = scanner_address}}; |
| 1850 |
ts_wasm_store__call(self, self->current_instance->scanner_destroy_fn_index, args, 1); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
|
| 1854 |
bool ts_wasm_store_call_scanner_scan( |
| 1855 |
TSWasmStore *self, |
| 1856 |
uint32_t scanner_address, |
| 1857 |
uint32_t valid_tokens_ix |
| 1858 |
) { |
| 1859 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1860 |
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory); |
| 1861 |
|
| 1862 |
memcpy( |
| 1863 |
&memory_data[self->lexer_address], |
| 1864 |
self->current_lexer, |
| 1865 |
sizeof(TSLexerDataPrefix) |
| 1866 |
); |
| 1867 |
|
| 1868 |
uint32_t valid_tokens_address = |
| 1869 |
self->current_instance->external_states_address + |
| 1870 |
(valid_tokens_ix * sizeof(bool)); |
| 1871 |
wasmtime_val_raw_t args[3] = { |
| 1872 |
{.i32 = scanner_address}, |
| 1873 |
{.i32 = self->lexer_address}, |
| 1874 |
{.i32 = valid_tokens_address} |
| 1875 |
}; |
| 1876 |
ts_wasm_store__call(self, self->current_instance->scanner_scan_fn_index, args, 3); |
| 1877 |
if (self->has_error) return false; |
| 1878 |
|
| 1879 |
memcpy( |
| 1880 |
self->current_lexer, |
| 1881 |
&memory_data[self->lexer_address], |
| 1882 |
sizeof(TSLexerDataPrefix) |
| 1883 |
); |
| 1884 |
return args[0].i32; |
| 1885 |
} |
| 1886 |
|
| 1887 |
uint32_t ts_wasm_store_call_scanner_serialize( |
| 1888 |
TSWasmStore *self, |
| 1889 |
uint32_t scanner_address, |
| 1890 |
char *buffer |
| 1891 |
) { |
| 1892 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1893 |
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory); |
| 1894 |
uint32_t serialization_buffer_address = ts_wasm_store__serialization_buffer_address(self); |
| 1895 |
|
| 1896 |
wasmtime_val_raw_t args[2] = { |
| 1897 |
{.i32 = scanner_address}, |
| 1898 |
{.i32 = serialization_buffer_address}, |
| 1899 |
}; |
| 1900 |
ts_wasm_store__call(self, self->current_instance->scanner_serialize_fn_index, args, 2); |
| 1901 |
if (self->has_error) return 0; |
| 1902 |
|
| 1903 |
uint32_t length = args[0].i32; |
| 1904 |
if (length > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) { |
| 1905 |
self->has_error = true; |
| 1906 |
return 0; |
| 1907 |
} |
| 1908 |
|
| 1909 |
if (length > 0) { |
| 1910 |
memcpy( |
| 1911 |
((Lexer *)self->current_lexer)->debug_buffer, |
| 1912 |
&memory_data[serialization_buffer_address], |
| 1913 |
length |
| 1914 |
); |
| 1915 |
} |
| 1916 |
return length; |
| 1917 |
} |
| 1918 |
|
| 1919 |
void ts_wasm_store_call_scanner_deserialize( |
| 1920 |
TSWasmStore *self, |
| 1921 |
uint32_t scanner_address, |
| 1922 |
const char *buffer, |
| 1923 |
unsigned length |
| 1924 |
) { |
| 1925 |
wasmtime_context_t *context = wasmtime_store_context(self->store); |
| 1926 |
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory); |
| 1927 |
uint32_t serialization_buffer_address = ts_wasm_store__serialization_buffer_address(self); |
| 1928 |
|
| 1929 |
if (length > 0) { |
| 1930 |
memcpy( |
| 1931 |
&memory_data[serialization_buffer_address], |
| 1932 |
buffer, |
| 1933 |
length |
| 1934 |
); |
| 1935 |
} |
| 1936 |
|
| 1937 |
wasmtime_val_raw_t args[3] = { |
| 1938 |
{.i32 = scanner_address}, |
| 1939 |
{.i32 = serialization_buffer_address}, |
| 1940 |
{.i32 = length}, |
| 1941 |
}; |
| 1942 |
ts_wasm_store__call(self, self->current_instance->scanner_deserialize_fn_index, args, 3); |
| 1943 |
} |
| 1944 |
|
| 1945 |
bool ts_wasm_store_has_error(const TSWasmStore *self) { |
| 1946 |
return self->has_error; |
| 1947 |
} |
| 1948 |
|
| 1949 |
bool ts_language_is_wasm(const TSLanguage *self) { |
| 1950 |
return self->lex_fn == ts_wasm_store__sentinel_lex_fn; |
| 1951 |
} |
| 1952 |
|
| 1953 |
static inline LanguageWasmModule *ts_language__wasm_module(const TSLanguage *self) { |
| 1954 |
return (LanguageWasmModule *)self->keyword_lex_fn; |
| 1955 |
} |
| 1956 |
|
| 1957 |
void ts_wasm_language_retain(const TSLanguage *self) { |
| 1958 |
LanguageWasmModule *module = ts_language__wasm_module(self); |
| 1959 |
ts_assert(module->ref_count > 0); |
| 1960 |
atomic_inc(&module->ref_count); |
| 1961 |
} |
| 1962 |
|
| 1963 |
void ts_wasm_language_release(const TSLanguage *self) { |
| 1964 |
LanguageWasmModule *module = ts_language__wasm_module(self); |
| 1965 |
ts_assert(module->ref_count > 0); |
| 1966 |
if (atomic_dec(&module->ref_count) == 0) { |
| 1967 |
// Update the language id to reflect that the language is deleted. This allows any Wasm stores |
| 1968 |
// that hold Wasm instances for this language to delete those instances. |
| 1969 |
atomic_inc(&module->language_id->is_language_deleted); |
| 1970 |
language_id_delete(module->language_id); |
| 1971 |
|
| 1972 |
ts_free((void *)module->field_name_buffer); |
| 1973 |
ts_free((void *)module->symbol_name_buffer); |
| 1974 |
ts_free((void *)module->name); |
| 1975 |
wasmtime_module_delete(module->module); |
| 1976 |
ts_free(module); |
| 1977 |
|
| 1978 |
ts_free((void *)self->alias_map); |
| 1979 |
ts_free((void *)self->alias_sequences); |
| 1980 |
ts_free((void *)self->external_scanner.symbol_map); |
| 1981 |
ts_free((void *)self->field_map_entries); |
| 1982 |
ts_free((void *)self->field_map_slices); |
| 1983 |
ts_free((void *)self->supertype_symbols); |
| 1984 |
ts_free((void *)self->supertype_map_entries); |
| 1985 |
ts_free((void *)self->supertype_map_slices); |
| 1986 |
ts_free((void *)self->field_names); |
| 1987 |
ts_free((void *)self->lex_modes); |
| 1988 |
ts_free((void *)self->name); |
| 1989 |
ts_free((void *)self->reserved_words); |
| 1990 |
ts_free((void *)self->parse_actions); |
| 1991 |
ts_free((void *)self->parse_table); |
| 1992 |
ts_free((void *)self->primary_state_ids); |
| 1993 |
ts_free((void *)self->public_symbol_map); |
| 1994 |
ts_free((void *)self->small_parse_table); |
| 1995 |
ts_free((void *)self->small_parse_table_map); |
| 1996 |
ts_free((void *)self->symbol_metadata); |
| 1997 |
ts_free((void *)self->symbol_names); |
| 1998 |
ts_free((void *)self); |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
#ifdef _MSC_VER |
| 2003 |
#pragma warning(pop) |
| 2004 |
#elif defined(__GNUC__) || defined(__clang__) |
| 2005 |
#pragma GCC diagnostic pop |
| 2006 |
#endif |
| 2007 |
|
| 2008 |
#else |
| 2009 |
|
| 2010 |
// If the Wasm feature is not enabled, define dummy versions of all of the |
| 2011 |
// Wasm-related functions. |
| 2012 |
|
| 2013 |
void ts_wasm_store_delete(TSWasmStore *self) { |
| 2014 |
(void)self; |
| 2015 |
} |
| 2016 |
|
| 2017 |
bool ts_wasm_store_start( |
| 2018 |
TSWasmStore *self, |
| 2019 |
TSLexer *lexer, |
| 2020 |
const TSLanguage *language |
| 2021 |
) { |
| 2022 |
(void)self; |
| 2023 |
(void)lexer; |
| 2024 |
(void)language; |
| 2025 |
return false; |
| 2026 |
} |
| 2027 |
|
| 2028 |
void ts_wasm_store_reset(TSWasmStore *self) { |
| 2029 |
(void)self; |
| 2030 |
} |
| 2031 |
|
| 2032 |
bool ts_wasm_store_call_lex_main(TSWasmStore *self, TSStateId state) { |
| 2033 |
(void)self; |
| 2034 |
(void)state; |
| 2035 |
return false; |
| 2036 |
} |
| 2037 |
|
| 2038 |
bool ts_wasm_store_call_lex_keyword(TSWasmStore *self, TSStateId state) { |
| 2039 |
(void)self; |
| 2040 |
(void)state; |
| 2041 |
return false; |
| 2042 |
} |
| 2043 |
|
| 2044 |
uint32_t ts_wasm_store_call_scanner_create(TSWasmStore *self) { |
| 2045 |
(void)self; |
| 2046 |
return 0; |
| 2047 |
} |
| 2048 |
|
| 2049 |
void ts_wasm_store_call_scanner_destroy( |
| 2050 |
TSWasmStore *self, |
| 2051 |
uint32_t scanner_address |
| 2052 |
) { |
| 2053 |
(void)self; |
| 2054 |
(void)scanner_address; |
| 2055 |
} |
| 2056 |
|
| 2057 |
bool ts_wasm_store_call_scanner_scan( |
| 2058 |
TSWasmStore *self, |
| 2059 |
uint32_t scanner_address, |
| 2060 |
uint32_t valid_tokens_ix |
| 2061 |
) { |
| 2062 |
(void)self; |
| 2063 |
(void)scanner_address; |
| 2064 |
(void)valid_tokens_ix; |
| 2065 |
return false; |
| 2066 |
} |
| 2067 |
|
| 2068 |
uint32_t ts_wasm_store_call_scanner_serialize( |
| 2069 |
TSWasmStore *self, |
| 2070 |
uint32_t scanner_address, |
| 2071 |
char *buffer |
| 2072 |
) { |
| 2073 |
(void)self; |
| 2074 |
(void)scanner_address; |
| 2075 |
(void)buffer; |
| 2076 |
return 0; |
| 2077 |
} |
| 2078 |
|
| 2079 |
void ts_wasm_store_call_scanner_deserialize( |
| 2080 |
TSWasmStore *self, |
| 2081 |
uint32_t scanner_address, |
| 2082 |
const char *buffer, |
| 2083 |
unsigned length |
| 2084 |
) { |
| 2085 |
(void)self; |
| 2086 |
(void)scanner_address; |
| 2087 |
(void)buffer; |
| 2088 |
(void)length; |
| 2089 |
} |
| 2090 |
|
| 2091 |
bool ts_wasm_store_has_error(const TSWasmStore *self) { |
| 2092 |
(void)self; |
| 2093 |
return false; |
| 2094 |
} |
| 2095 |
|
| 2096 |
bool ts_language_is_wasm(const TSLanguage *self) { |
| 2097 |
(void)self; |
| 2098 |
return false; |
| 2099 |
} |
| 2100 |
|
| 2101 |
void ts_wasm_language_retain(const TSLanguage *self) { |
| 2102 |
(void)self; |
| 2103 |
} |
| 2104 |
|
| 2105 |
void ts_wasm_language_release(const TSLanguage *self) { |
| 2106 |
(void)self; |
| 2107 |
} |
| 2108 |
|
| 2109 |
#endif |