mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-16 06:21:22 +00:00
Catch errors in Table and Module constructors
This commit is contained in:
@ -2953,8 +2953,8 @@ pub mod WebAssembly {
|
|||||||
/// efficiently shared with Workers, and instantiated multiple times.
|
/// efficiently shared with Workers, and instantiated multiple times.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
|
||||||
#[wasm_bindgen(constructor, js_namespace = WebAssembly)]
|
#[wasm_bindgen(constructor, js_namespace = WebAssembly, catch)]
|
||||||
pub fn new(buffer_source: &JsValue) -> Module;
|
pub fn new(buffer_source: &JsValue) -> Result<Module, JsValue>;
|
||||||
|
|
||||||
/// The `WebAssembly.customSections()` function returns a copy of the
|
/// The `WebAssembly.customSections()` function returns a copy of the
|
||||||
/// contents of all custom sections in the given module with the given
|
/// contents of all custom sections in the given module with the given
|
||||||
@ -2994,8 +2994,8 @@ pub mod WebAssembly {
|
|||||||
/// of the given size and element type.
|
/// of the given size and element type.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table
|
||||||
#[wasm_bindgen(constructor, js_namespace = WebAssembly)]
|
#[wasm_bindgen(constructor, js_namespace = WebAssembly, catch)]
|
||||||
pub fn new(table_descriptor: &Object) -> Table;
|
pub fn new(table_descriptor: &Object) -> Result<Table, JsValue>;
|
||||||
|
|
||||||
/// The `length` prototype property of the `WebAssembly.Table` object
|
/// The `length` prototype property of the `WebAssembly.Table` object
|
||||||
/// returns the length of the table, i.e. the number of elements in the
|
/// returns the length of the table, i.e. the number of elements in the
|
||||||
|
@ -17,7 +17,12 @@ function getTableObject() {
|
|||||||
return { element: "anyfunc", initial: 1 }
|
return { element: "anyfunc", initial: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getInvalidTableObject() {
|
||||||
|
return { element: "anyfunc", initial: 1, maximum: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getWasmArray,
|
getInvalidTableObject,
|
||||||
getTableObject,
|
getTableObject,
|
||||||
|
getWasmArray,
|
||||||
};
|
};
|
||||||
|
@ -11,6 +11,9 @@ extern {
|
|||||||
|
|
||||||
#[wasm_bindgen(js_name = getTableObject)]
|
#[wasm_bindgen(js_name = getTableObject)]
|
||||||
fn get_table_object() -> Object;
|
fn get_table_object() -> Object;
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = getInvalidTableObject)]
|
||||||
|
fn get_invalid_table_object() -> Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_invalid_wasm() -> JsValue {
|
fn get_invalid_wasm() -> JsValue {
|
||||||
@ -66,46 +69,61 @@ fn compile_valid() -> impl Future<Item = (), Error = JsValue> {
|
|||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn module_inheritance() {
|
fn module_inheritance() {
|
||||||
let module = WebAssembly::Module::new(&get_valid_wasm());
|
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
|
||||||
assert!(module.is_instance_of::<WebAssembly::Module>());
|
assert!(module.is_instance_of::<WebAssembly::Module>());
|
||||||
assert!(module.is_instance_of::<Object>());
|
assert!(module.is_instance_of::<Object>());
|
||||||
|
|
||||||
let _: &Object = module.as_ref();
|
let _: &Object = module.as_ref();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn module_error() {
|
||||||
|
let error = WebAssembly::Module::new(&get_invalid_wasm()).err().unwrap();
|
||||||
|
assert!(error.is_instance_of::<WebAssembly::CompileError>());
|
||||||
|
|
||||||
|
let error = WebAssembly::Module::new(&get_bad_type_wasm()).err().unwrap();
|
||||||
|
assert!(error.is_instance_of::<TypeError>());
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn module_custom_sections() {
|
fn module_custom_sections() {
|
||||||
let module = WebAssembly::Module::new(&get_valid_wasm());
|
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
|
||||||
let cust_sec = WebAssembly::Module::custom_sections(&module, "abcd");
|
let cust_sec = WebAssembly::Module::custom_sections(&module, "abcd");
|
||||||
assert_eq!(cust_sec.length(), 0);
|
assert_eq!(cust_sec.length(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn module_exports() {
|
fn module_exports() {
|
||||||
let module = WebAssembly::Module::new(&get_valid_wasm());
|
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
|
||||||
let exports = WebAssembly::Module::exports(&module);
|
let exports = WebAssembly::Module::exports(&module);
|
||||||
assert_eq!(exports.length(), 1);
|
assert_eq!(exports.length(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn module_imports() {
|
fn module_imports() {
|
||||||
let module = WebAssembly::Module::new(&get_valid_wasm());
|
let module = WebAssembly::Module::new(&get_valid_wasm()).unwrap();
|
||||||
let imports = WebAssembly::Module::imports(&module);
|
let imports = WebAssembly::Module::imports(&module);
|
||||||
assert_eq!(imports.length(), 1);
|
assert_eq!(imports.length(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn table_inheritance() {
|
fn table_inheritance() {
|
||||||
let table = WebAssembly::Table::new(&get_table_object().into());
|
let table = WebAssembly::Table::new(&get_table_object().into()).unwrap();
|
||||||
assert!(table.is_instance_of::<WebAssembly::Table>());
|
assert!(table.is_instance_of::<WebAssembly::Table>());
|
||||||
assert!(table.is_instance_of::<Object>());
|
assert!(table.is_instance_of::<Object>());
|
||||||
|
|
||||||
let _: &Object = table.as_ref();
|
let _: &Object = table.as_ref();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn table_error() {
|
||||||
|
let error = WebAssembly::Table::new(&get_invalid_table_object()).err().unwrap();
|
||||||
|
assert!(error.is_instance_of::<RangeError>());
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn table() {
|
fn table() {
|
||||||
let table = WebAssembly::Table::new(&get_table_object().into());
|
let table = WebAssembly::Table::new(&get_table_object().into()).unwrap();
|
||||||
assert_eq!(table.length(), 1);
|
assert_eq!(table.length(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user