mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-19 16:01:23 +00:00
Fix consuming a struct and returning a slice
This came up in a [recent comment][1] and it turns out we're accidentally generating two `const ptr = ...` declarations, invalid JS! While Node doesn't catch this it looks like firefox does. [1]: https://github.com/rustwasm/wasm-bindgen/issues/329#issuecomment-411082013
This commit is contained in:
@ -48,11 +48,19 @@ matrix:
|
|||||||
env: JOB=test-bindgen
|
env: JOB=test-bindgen
|
||||||
install:
|
install:
|
||||||
- *INSTALL_NODE_VIA_NVM
|
- *INSTALL_NODE_VIA_NVM
|
||||||
|
- *INSTALL_GECKODRIVER
|
||||||
|
- export GECKODRIVER=`pwd`/geckodriver
|
||||||
script:
|
script:
|
||||||
|
# Run a test or two that makes sure `#[wasm_bindgen]` works "reasonably"
|
||||||
|
# on non-wasm platforms
|
||||||
- cargo test
|
- cargo test
|
||||||
|
# Run the main body of the test suite
|
||||||
- cargo test --target wasm32-unknown-unknown
|
- cargo test --target wasm32-unknown-unknown
|
||||||
|
# Rerun the test suite but disable `--debug` in generated JS
|
||||||
- WASM_BINDGEN_NO_DEBUG=1 cargo test --target wasm32-unknown-unknown
|
- WASM_BINDGEN_NO_DEBUG=1 cargo test --target wasm32-unknown-unknown
|
||||||
|
# Make sure our serde tests work
|
||||||
- cargo test --target wasm32-unknown-unknown --features serde-serialize
|
- cargo test --target wasm32-unknown-unknown --features serde-serialize
|
||||||
|
# Make sure the `std` feature works if disabled
|
||||||
- cargo test --target wasm32-unknown-unknown -p no-std
|
- cargo test --target wasm32-unknown-unknown -p no-std
|
||||||
addons:
|
addons:
|
||||||
firefox: latest
|
firefox: latest
|
||||||
|
@ -417,16 +417,16 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
"\
|
"\
|
||||||
RET;\n\
|
RET;\n\
|
||||||
const mem = getUint32Memory();\n\
|
const mem = getUint32Memory();\n\
|
||||||
const ptr = mem[retptr / 4];\n\
|
const rustptr = mem[retptr / 4];\n\
|
||||||
const len = mem[retptr / 4 + 1];\n\
|
const rustlen = mem[retptr / 4 + 1];\n\
|
||||||
{guard}
|
{guard}
|
||||||
const realRet = {}(ptr, len).slice();\n\
|
const realRet = {}(rustptr, rustlen).slice();\n\
|
||||||
wasm.__wbindgen_free(ptr, len * {});\n\
|
wasm.__wbindgen_free(rustptr, rustlen * {});\n\
|
||||||
return realRet;\n\
|
return realRet;\n\
|
||||||
",
|
",
|
||||||
f,
|
f,
|
||||||
ty.size(),
|
ty.size(),
|
||||||
guard = if optional { "if (ptr === 0) return;" } else { "" },
|
guard = if optional { "if (rustptr === 0) return;" } else { "" },
|
||||||
);
|
);
|
||||||
return Ok(self);
|
return Ok(self);
|
||||||
}
|
}
|
||||||
|
27
tests/headless.rs
Normal file
27
tests/headless.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#![cfg(target_arch = "wasm32")]
|
||||||
|
#![feature(use_extern_macros)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen_test;
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
|
||||||
|
use wasm_bindgen_test::*;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
wasm_bindgen_test_configure!(run_in_browser);
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct ConsumeRetString;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl ConsumeRetString {
|
||||||
|
// https://github.com/rustwasm/wasm-bindgen/issues/329#issuecomment-411082013
|
||||||
|
//
|
||||||
|
// This used to cause two `const ptr = ...` declarations, which is invalid
|
||||||
|
// JS.
|
||||||
|
pub fn consume(self) -> String { String::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn works() {
|
||||||
|
ConsumeRetString.consume();
|
||||||
|
}
|
Reference in New Issue
Block a user