Skip to content

Commit faed98b

Browse files
committed
Correct how slices are iterated over
This commit fixes how the `getArrayJsValueFromWasm` function is defined to correctly iterate over the slice by looking at the values rather than the indices. Closes #169
1 parent b2ecf56 commit faed98b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ impl<'a> Context<'a> {
225225
Ok(format!("
226226
function(ptr, len) {{
227227
let a;
228-
console.log(ptr, len);
229228
if (ptr === 0) {{
230229
a = Symbol();
231230
}} else {{
@@ -889,8 +888,8 @@ impl<'a> Context<'a> {
889888
const mem = getUint32Memory();
890889
const slice = mem.slice(ptr / 4, ptr / 4 + len);
891890
const result = [];
892-
for (ptr in slice) {{
893-
result.push(getObject(ptr))
891+
for (let i = 0; i < slice.length; i++) {{
892+
result.push(getObject(slice[i]))
894893
}}
895894
return result;
896895
}}

tests/all/jsobjects.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,37 @@ fn returning_vector() {
210210
"#)
211211
.test();
212212
}
213+
214+
#[test]
215+
fn another_vector_return() {
216+
project()
217+
.file("src/lib.rs", r#"
218+
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
219+
220+
extern crate wasm_bindgen;
221+
222+
use wasm_bindgen::prelude::*;
223+
224+
225+
#[wasm_bindgen]
226+
pub fn get_array() -> Vec<JsValue> {
227+
vec![
228+
JsValue::from(1),
229+
JsValue::from(2),
230+
JsValue::from(3),
231+
JsValue::from(4),
232+
JsValue::from(5),
233+
JsValue::from(6),
234+
]
235+
}
236+
"#)
237+
.file("test.ts", r#"
238+
import { get_array } from "./out";
239+
import * as assert from "assert";
240+
241+
export function test() {
242+
assert.deepStrictEqual(get_array(), [1, 2, 3, 4, 5, 6]);
243+
}
244+
"#)
245+
.test();
246+
}

0 commit comments

Comments
 (0)