Fix error when wrong language name is passed to load_language

This commit is contained in:
Max Brunsfeld 2023-11-24 20:25:28 -08:00
parent bd0796d11d
commit e9289d3b30
3 changed files with 38 additions and 22 deletions

View file

@ -15,32 +15,48 @@ fn test_wasm_store() {
let wasm_cpp = fs::read(&WASM_DIR.join(format!("tree-sitter-cpp.wasm"))).unwrap();
let wasm_rs = fs::read(&WASM_DIR.join(format!("tree-sitter-rust.wasm"))).unwrap();
let wasm_rb = fs::read(&WASM_DIR.join(format!("tree-sitter-ruby.wasm"))).unwrap();
let wasm_typescript = fs::read(&WASM_DIR.join(format!("tree-sitter-typescript.wasm"))).unwrap();
let language_rust = store.load_language("rust", &wasm_rs);
let language_cpp = store.load_language("cpp", &wasm_cpp);
let language_ruby = store.load_language("ruby", &wasm_rb);
let language_typescript = store.load_language("typescript", &wasm_typescript);
parser.set_wasm_store(store).unwrap();
for _ in 0..2 {
parser.set_language(language_cpp).unwrap();
let tree = parser.parse("A<B> c = d();", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(translation_unit (declaration type: (template_type name: (type_identifier) arguments: (template_argument_list (type_descriptor type: (type_identifier)))) declarator: (init_declarator declarator: (identifier) value: (call_expression function: (identifier) arguments: (argument_list)))))"
);
let mut parser2 = Parser::new();
parser2
.set_wasm_store(WasmStore::new(ENGINE.clone()))
.unwrap();
parser.set_language(language_rust).unwrap();
let tree = parser.parse("const A: B = c();", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(source_file (const_item name: (identifier) type: (type_identifier) value: (call_expression function: (identifier) arguments: (arguments))))"
);
for mut parser in [parser, parser2] {
for _ in 0..2 {
parser.set_language(language_cpp).unwrap();
let tree = parser.parse("A<B> c = d();", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(translation_unit (declaration type: (template_type name: (type_identifier) arguments: (template_argument_list (type_descriptor type: (type_identifier)))) declarator: (init_declarator declarator: (identifier) value: (call_expression function: (identifier) arguments: (argument_list)))))"
);
parser.set_language(language_ruby).unwrap();
let tree = parser.parse("class A; end", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(program (class name: (constant)))"
);
parser.set_language(language_rust).unwrap();
let tree = parser.parse("const A: B = c();", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(source_file (const_item name: (identifier) type: (type_identifier) value: (call_expression function: (identifier) arguments: (arguments))))"
);
parser.set_language(language_ruby).unwrap();
let tree = parser.parse("class A; end", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(program (class name: (constant)))"
);
parser.set_language(language_typescript).unwrap();
let tree = parser.parse("class A {}", None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(program (class_declaration name: (type_identifier) body: (class_body)))"
);
}
}
}

View file

@ -1,6 +1,4 @@
#include "./language.h"
#include "./subtree.h"
#include "./error_costs.h"
#include <string.h>
uint32_t ts_language_symbol_count(const TSLanguage *self) {

View file

@ -773,6 +773,7 @@ static bool ts_wasm_store__instantiate(
self->current_function_table_offset += dylink_info->table_size;
// Process the module's exports.
bool found_language = false;
wasmtime_extern_t language_extern;
wasm_exporttype_vec_t export_types = WASM_EMPTY_VEC;
wasmtime_module_exports(module, &export_types);
@ -801,11 +802,12 @@ static bool ts_wasm_store__instantiate(
// Find the main language function for the module.
else if (name_eq(name, language_function_name)) {
language_extern = export;
found_language = true;
}
}
wasm_exporttype_vec_delete(&export_types);
if (language_extern.kind != WASMTIME_EXTERN_FUNC) {
if (!found_language) {
printf("failed to find function %s\n", language_function_name);
goto error;
}