Skip to content

Commit

Permalink
Merge pull request #1020 from fitzgen/move-by-value-debug-asserts
Browse files Browse the repository at this point in the history
Only emit JS glue assertions for move arguments in debug mode
  • Loading branch information
fitzgen authored Nov 8, 2018
2 parents d646b29 + 75c18f0 commit 227e836
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions crates/cli-support/src/js/js2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
if self.cx.config.debug {
self.prelude(
"if (this.ptr === 0) {
throw new Error('Attempt to use a moved value');
throw new Error('Attempt to use a moved value');
}",
);
}
Expand Down Expand Up @@ -330,14 +330,26 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.prelude(&format!(
"\
const ptr{i} = {arg}.ptr;\n\
if (ptr{i} === 0) {{
throw new Error('Attempt to use a moved value');
}}
{arg}.ptr = 0;\n\
",
",
i = i,
arg = name
));
if self.cx.config.debug {
self.prelude(&format!(
"\
if (ptr{i} === 0) {{
throw new Error('Attempt to use a moved value');
}}
",
i = i,
));
}
self.prelude(&format!(
"\
{arg}.ptr = 0;\n\
",
arg = name
));
self.rust_arguments.push(format!("ptr{}", i));
}
return Ok(self);
Expand Down
2 changes: 1 addition & 1 deletion tests/wasm/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ exports.js_readonly_fields = () => {

exports.js_double_consume = () => {
const r = new wasm.DoubleConsume();
assert.throws(() => r.consume(r), /Attempt to use a moved value/);
assert.throws(() => r.consume(r));
};


Expand Down

0 comments on commit 227e836

Please sign in to comment.