Skip to content

Commit e9ea2da

Browse files
author
Robert Masen
committed
add Validate ptr test
1 parent 749ac65 commit e9ea2da

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,4 @@ mod structural;
538538
mod u64;
539539
mod webidl;
540540
mod comments;
541+
mod validate_prt;

tests/all/validate_prt.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use super::project;
2+
3+
#[test]
4+
fn works() {
5+
project()
6+
.file("src/lib.rs", r#"
7+
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
8+
extern crate wasm_bindgen;
9+
use wasm_bindgen::prelude::*;
10+
#[wasm_bindgen]
11+
pub struct Fruit {
12+
name: String,
13+
}
14+
#[wasm_bindgen]
15+
impl Fruit {
16+
#[wasm_bindgen(method)]
17+
pub fn name(&self) -> String {
18+
self.name.clone()
19+
}
20+
#[wasm_bindgen(constructor)]
21+
pub fn new(name: String) -> Self {
22+
Fruit {
23+
name,
24+
}
25+
}
26+
}
27+
#[wasm_bindgen]
28+
pub fn eat(_fruit: Fruit) { }
29+
"#)
30+
.file("test.js", r#"
31+
import * as wasm from './out';
32+
const targetMessage = 'Attempt to use a moved value';
33+
function assertEq(a, b) {
34+
console.log(a, '?=', b);
35+
if (a === b)
36+
return;
37+
throw new Error('not equal');
38+
}
39+
export function test() {
40+
useMoved();
41+
moveMoved();
42+
}
43+
export function useMoved() {
44+
// create a new struct
45+
let apple = new wasm.Fruit('apple');
46+
// sanity check that this method works
47+
let name = apple.name();
48+
// consume the struct
49+
wasm.eat(apple);
50+
// try and use the moved apple again
51+
try {
52+
let movedName = apple.name();
53+
} catch (e) {
54+
assertEq(e.message, targetMessage);
55+
}
56+
}
57+
export function moveMoved() {
58+
let pear = new wasm.Fruit('pear');
59+
let name = pear.name();
60+
wasm.eat(pear);
61+
try {
62+
wasm.eat(pear);
63+
} catch (e) {
64+
assertEq(e.message, targetMessage);
65+
}
66+
}
67+
"#)
68+
.test();
69+
}

0 commit comments

Comments
 (0)