From faed98b8439c5041a8f56a6827c458727f1f5bd9 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 25 Apr 2018 21:57:44 -0700 Subject: [PATCH] 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 --- crates/cli-support/src/js/mod.rs | 5 ++--- tests/all/jsobjects.rs | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index f77f54e10..b2656f732 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -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; }} diff --git a/tests/all/jsobjects.rs b/tests/all/jsobjects.rs index 404a3e198..18619125e 100644 --- a/tests/all/jsobjects.rs +++ b/tests/all/jsobjects.rs @@ -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 { + 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(); +}