mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 15:31:25 +00:00
Assert empty JS heap/stack in tests
Turns out there was a bug when passing a vector of `JsValue` instances back to JS all objects were leaked rather than correctly removed from the global slab.
This commit is contained in:
@ -633,6 +633,15 @@ impl<'a> Context<'a> {
|
|||||||
self.global(&format!("
|
self.global(&format!("
|
||||||
let stack = [];
|
let stack = [];
|
||||||
"));
|
"));
|
||||||
|
if self.config.debug {
|
||||||
|
self.export("assertStackEmpty", "
|
||||||
|
function() {
|
||||||
|
if (stack.length === 0)
|
||||||
|
return;
|
||||||
|
throw new Error('stack is not currently empty');
|
||||||
|
}
|
||||||
|
");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expose_global_slab(&mut self) {
|
fn expose_global_slab(&mut self) {
|
||||||
@ -640,6 +649,17 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.global(&format!("let slab = [];"));
|
self.global(&format!("let slab = [];"));
|
||||||
|
if self.config.debug {
|
||||||
|
self.export("assertSlabEmpty", "
|
||||||
|
function() {
|
||||||
|
for (let i = 0; i < slab.length; i++) {
|
||||||
|
if (typeof(slab[i]) === 'number')
|
||||||
|
continue;
|
||||||
|
throw new Error('slab is not currently empty');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expose_global_slab_next(&mut self) {
|
fn expose_global_slab_next(&mut self) {
|
||||||
@ -882,14 +902,14 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.expose_get_array_u32_from_wasm();
|
self.expose_get_array_u32_from_wasm();
|
||||||
self.expose_get_object();
|
self.expose_take_object();
|
||||||
self.global(&format!("
|
self.global(&format!("
|
||||||
function getArrayJsValueFromWasm(ptr, len) {{
|
function getArrayJsValueFromWasm(ptr, len) {{
|
||||||
const mem = getUint32Memory();
|
const mem = getUint32Memory();
|
||||||
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
||||||
const result = [];
|
const result = [];
|
||||||
for (let i = 0; i < slice.length; i++) {{
|
for (let i = 0; i < slice.length; i++) {{
|
||||||
result.push(getObject(slice[i]))
|
result.push(takeObject(slice[i]))
|
||||||
}}
|
}}
|
||||||
return result;
|
return result;
|
||||||
}}
|
}}
|
||||||
|
@ -283,6 +283,7 @@ fn free_imports() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn import_a_field() {
|
fn import_a_field() {
|
||||||
project()
|
project()
|
||||||
|
.debug(false)
|
||||||
.file("src/lib.rs", r#"
|
.file("src/lib.rs", r#"
|
||||||
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
||||||
|
|
||||||
|
@ -48,11 +48,18 @@ fn project() -> Project {
|
|||||||
|
|
||||||
("run.js".to_string(), r#"
|
("run.js".to_string(), r#"
|
||||||
import * as process from "process";
|
import * as process from "process";
|
||||||
|
let wasm = import('./out');
|
||||||
|
|
||||||
const test = import("./test");
|
const test = import("./test");
|
||||||
|
|
||||||
test.then(test => {
|
Promise.all([test, wasm]).then(results => {
|
||||||
|
let [test, wasm] = results;
|
||||||
test.test();
|
test.test();
|
||||||
|
|
||||||
|
if (wasm.assertStackEmpty)
|
||||||
|
wasm.assertStackEmpty();
|
||||||
|
if (wasm.assertSlabEmpty)
|
||||||
|
wasm.assertSlabEmpty();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -216,7 +216,7 @@ fn no_std() {
|
|||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen(module = "./foo")]
|
||||||
extern {
|
extern {
|
||||||
fn test(a: &str);
|
fn test(a: &str);
|
||||||
|
|
||||||
@ -238,6 +238,12 @@ fn no_std() {
|
|||||||
wasm.foo(1);
|
wasm.foo(1);
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
|
.file("foo.js", r#"
|
||||||
|
export class Js {
|
||||||
|
init() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#)
|
||||||
.test();
|
.test();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user