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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#[macro_export]
macro_rules! main_app {
( $ app: ident) => {
#[cfg(not(target_arch = "wasm32"))]
fn main() {
let mut cx = Cx::new(std::any::TypeId::of::<$app>());
let mut app = $app::new(&mut cx);
let mut cxafterdraw = CxAfterDraw::new(&mut cx);
cx.set_finished_app_new();
cx.event_loop(|cx, mut event| {
match event {
Event::System(e) => {
match e {
SystemEvent::Draw => {
app.draw(cx);
cxafterdraw.after_draw(cx);
}
SystemEvent::WebRustCall(e) => {
let WebRustCallEvent { name, params, callback_id } = std::mem::take(e).unwrap();
let call_rust_async_fn =
cx.call_rust_async_fn.expect("`callRustAsync` called but no on_call_rust_async registered");
unsafe {
let func = Box::from_raw(
call_rust_async_fn
as *mut fn(
this: &mut $app,
cx: &mut Cx,
name: String,
params: Vec<ZapParam>,
) -> Vec<ZapParam>,
);
let mut return_params = func(&mut app, cx, name, params);
Box::into_raw(func);
cx.return_to_js(callback_id, return_params);
}
}
_ => {}
}
}
_ => {
app.handle(cx, &mut event);
}
};
});
}
#[cfg(target_arch = "wasm32")]
fn main() {}
#[cfg(target_arch = "wasm32")]
#[export_name = "createWasmApp"]
pub extern "C" fn create_wasm_app() -> u64 {
Cx::init_error_handlers();
let mut cx = Box::new(Cx::new(std::any::TypeId::of::<$app>()));
let app = Box::new($app::new(&mut cx));
let cxafterdraw = Box::new(CxAfterDraw::new(&mut cx));
cx.set_finished_app_new();
Box::into_raw(Box::new((Box::into_raw(app), Box::into_raw(cx), Box::into_raw(cxafterdraw)))) as u64
}
#[cfg(target_arch = "wasm32")]
#[export_name = "processWasmEvents"]
pub unsafe extern "C" fn process_wasm_events(appcx: u64, msg_bytes: u64) -> u64 {
let appcx = &*(appcx as *mut (*mut $app, *mut Cx, *mut CxAfterDraw));
(*appcx.1).process_wasm_events(msg_bytes, |cx, mut event| {
match event {
Event::System(e) => {
match e {
SystemEvent::Draw => {
(*appcx.0).draw(cx);
(*appcx.2).after_draw(cx);
}
SystemEvent::WebRustCall(e) => {
let WebRustCallEvent { name, params, callback_id } = std::mem::take(e).unwrap();
let call_rust_async_fn =
cx.call_rust_async_fn.expect("call_rust called but no on_call_rust_sync registered");
let func = Box::from_raw(
call_rust_async_fn
as *mut fn(
this: &mut $app,
cx: &mut Cx,
name: String,
params: Vec<ZapParam>,
) -> Vec<ZapParam>,
);
let mut return_params = func(&mut *appcx.0, cx, name, params);
Box::into_raw(func);
cx.return_to_js(callback_id, return_params);
}
_ => {}
}
}
_ => {
(*appcx.0).handle(cx, event);
}
}
})
}
#[cfg(all(target_arch = "wasm32"))]
#[export_name = "callRustSync"]
pub unsafe extern "C" fn call_rust_sync(appcx: u64, zerde_ptr: u64) -> u64 {
let appcx = &*(appcx as *mut (*mut $app, *mut Cx, *mut CxAfterDraw));
(*appcx.1).call_rust_sync(zerde_ptr)
}
};
}
#[macro_export]
macro_rules! register_call_rust {
( $ call_rust: ident) => {
struct App {}
impl App {
fn new(cx: &mut Cx) -> Self {
cx.on_call_rust_async(Self::on_call_rust_async);
cx.on_call_rust_sync(Self::on_call_rust_sync);
Self {}
}
fn handle(&mut self, cx: &mut Cx, event: &mut Event) {}
fn on_call_rust_async(&mut self, cx: &mut Cx, name: String, params: Vec<ZapParam>) -> Vec<ZapParam> {
call_rust(name, params)
}
fn on_call_rust_sync(name: String, params: Vec<ZapParam>) -> Vec<ZapParam> {
call_rust(name, params)
}
fn draw(&mut self, cx: &mut Cx) {}
}
main_app!(App);
};
}
#[macro_export]
macro_rules! location_hash {
() => {
LocationHash::new(file!(), line!() as u64, column!() as u64)
};
}
#[macro_export]
macro_rules! code_fragment {
( $ code: expr ) => {
CodeFragment::Static { filename: file!(), line: line!() as usize + 1, col: column!() as usize + 7, code: $code }
};
}
#[cfg(not(target_arch = "wasm32"))]
#[macro_export]
macro_rules! log {
( $ ( $t: tt) *) => {{
println!("{}:{} - {}",file!(),line!(),format!($($t)*));
std::io::Write::flush(&mut std::io::stdout()).unwrap();
}}
}
#[cfg(target_arch = "wasm32")]
#[macro_export]
macro_rules! log {
( $ ( $ t: tt) *) => {
console_log(&format!("{}:{} - {}", file!(), line!(), format!( $ ( $ t) *)))
}
}