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
This commit is contained in:
Alex Crichton 2018-04-25 21:57:44 -07:00
parent b2ecf56cb6
commit faed98b843
2 changed files with 36 additions and 3 deletions

View File

@ -225,7 +225,6 @@ impl<'a> Context<'a> {
Ok(format!("
function(ptr, len) {{
let a;
console.log(ptr, len);
if (ptr === 0) {{
a = Symbol();
}} else {{
@ -889,8 +888,8 @@ impl<'a> Context<'a> {
const mem = getUint32Memory();
const slice = mem.slice(ptr / 4, ptr / 4 + len);
const result = [];
for (ptr in slice) {{
result.push(getObject(ptr))
for (let i = 0; i < slice.length; i++) {{
result.push(getObject(slice[i]))
}}
return result;
}}

View File

@ -210,3 +210,37 @@ fn returning_vector() {
"#)
.test();
}
#[test]
fn another_vector_return() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_array() -> Vec<JsValue> {
vec![
JsValue::from(1),
JsValue::from(2),
JsValue::from(3),
JsValue::from(4),
JsValue::from(5),
JsValue::from(6),
]
}
"#)
.file("test.ts", r#"
import { get_array } from "./out";
import * as assert from "assert";
export function test() {
assert.deepStrictEqual(get_array(), [1, 2, 3, 4, 5, 6]);
}
"#)
.test();
}