From ab3688d01aa4dbbac1a56f868b40a95cc6414675 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 21 Sep 2018 15:17:19 -0700 Subject: [PATCH] Only generate JS null checks in debug mode In non-debug mode Rust is already checking these pointers, so let's only generate the relevant code in debug mode. --- crates/backend/src/codegen.rs | 2 +- crates/cli-support/src/js/js2rust.rs | 12 +++++++----- tests/wasm/imports.js | 3 ++- tests/wasm/validate_prt.js | 6 ++++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 28353d7f..d7617b6a 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -985,7 +985,7 @@ impl ToTokens for ast::Enum { _extra: &mut ::wasm_bindgen::convert::Stack, ) -> Self { #(#cast_clauses else)* { - ::wasm_bindgen::throw("invalid enum value passed") + ::wasm_bindgen::throw_str("invalid enum value passed") } } } diff --git a/crates/cli-support/src/js/js2rust.rs b/crates/cli-support/src/js/js2rust.rs index 35035d8c..7d9b23d1 100644 --- a/crates/cli-support/src/js/js2rust.rs +++ b/crates/cli-support/src/js/js2rust.rs @@ -68,11 +68,13 @@ impl<'a, 'b> Js2Rust<'a, 'b> { /// passed should be `this.ptr`. pub fn method(&mut self, method: bool, consumed: bool) -> &mut Self { if method { - self.prelude( - "if (this.ptr === 0) { - throw new Error('Attempt to use a moved value'); - }", - ); + if self.cx.config.debug { + self.prelude( + "if (this.ptr === 0) { + throw new Error('Attempt to use a moved value'); + }", + ); + } if consumed { self.prelude( "\ diff --git a/tests/wasm/imports.js b/tests/wasm/imports.js index a12d7b67..131ea9a3 100644 --- a/tests/wasm/imports.js +++ b/tests/wasm/imports.js @@ -85,7 +85,8 @@ exports.custom_type_return_2 = function() { }; exports.touch_custom_type = function() { - assert.throws(() => CUSTOM_TYPE.touch(), /Attempt to use a moved value/); + assert.throws(() => CUSTOM_TYPE.touch(), + /Attempt to use a moved value|null pointer passed to rust/); }; exports.interpret_2_as_custom_type = function() { diff --git a/tests/wasm/validate_prt.js b/tests/wasm/validate_prt.js index 4b8a3e73..4f16ca29 100644 --- a/tests/wasm/validate_prt.js +++ b/tests/wasm/validate_prt.js @@ -5,14 +5,16 @@ const useMoved = () => { const apple = new wasm.Fruit('apple'); apple.name(); wasm.eat(apple); - assert.throws(() => apple.name(), /Attempt to use a moved value/); + assert.throws(() => apple.name(), + /Attempt to use a moved value|null pointer passed to rust/); }; const moveMoved = () => { const pear = new wasm.Fruit('pear'); pear.name(); wasm.eat(pear); - assert.throws(() => wasm.eat(pear), /Attempt to use a moved value/); + assert.throws(() => wasm.eat(pear), + /Attempt to use a moved value|null pointer passed to rust/); }; exports.js_works = () => {