-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue_construct.rs
56 lines (47 loc) · 1.18 KB
/
issue_construct.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Demonstration of the issue where a foreign class' constructor is not called when
//! creating the instance from Rust.
use rust_wren::prelude::*;
#[wren_class]
struct GameObject {
#[getset]
id: i32,
}
#[wren_methods]
impl GameObject {
#[construct]
fn new(id: i32) -> Self {
println!("Rust factory function: {}", id);
GameObject { id }
}
#[method(name = createInstance)]
fn create_instance(id: i32) -> GameObject {
// The `construct` method declared in Wren will not be called.
GameObject::new(id)
}
}
const DECLARE_SCRIPT: &str = r#"
foreign class GameObject {
construct new(id) {
System.print("Wren constructor: %(id)")
}
foreign static createInstance(id)
}
"#;
fn main() {
let mut vm = WrenBuilder::new()
.with_module("main", |m| m.register::<GameObject>())
.build();
vm.interpret("main", DECLARE_SCRIPT).unwrap();
vm.interpret(
"example",
r#"
import "main" for GameObject
var obj1 = GameObject.new(1)
//> Rust factory function: 1
//> Wren constructor: 1
var obj2 = GameObject.createInstance(2)
//> Rust factory function: 2
"#,
)
.unwrap();
}