forked from LeNPaul/Lagrange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
151 lines (124 loc) · 3.97 KB
/
lib.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
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
use wasm_bindgen::prelude::{wasm_bindgen, Closure};
use wasm_bindgen::JsCast;
mod utils;
use web_sys::Element;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn init() {
utils::set_panic_hook();
// window and document have to be fetched from JS world
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let main = document.get_elements_by_tag_name("main").item(0).unwrap();
let dynamic_text: Element = document.create_element("p").unwrap();
main.prepend_with_node_1(&dynamic_text).unwrap();
unsafe {
DYNAMIC_TEXT = Some(dynamic_text);
update_text();
}
set_timer();
}
static mut DYNAMIC_TEXT: Option<Element> = None;
static mut APPLES: i32 = 1;
static mut TREES: i32 = 0;
pub unsafe fn update_text() {
if let Some(dynamic_text) = DYNAMIC_TEXT.as_mut() {
dynamic_text.set_inner_html(&format!("You have {} apples and {} trees.", APPLES, TREES));
}
}
#[wasm_bindgen]
pub fn buy() {
unsafe {
if APPLES > 0 {
TREES += 1;
APPLES -= 1;
update_text();
}
}
}
fn collect_apples() {
unsafe {
APPLES += TREES;
update_text();
}
}
fn set_timer() {
let window = web_sys::window().unwrap();
let boxed_function = Box::new(collect_apples);
let closure = Closure::wrap(boxed_function as Box<dyn Fn()>);
// setInterval() has many different names in Rust because there is no overloading
window
.set_interval_with_callback_and_timeout_and_arguments_0(
closure.as_ref().unchecked_ref(),
5000,
)
.unwrap();
// Leak memory on purpose
closure.forget();
}
/*
* Safe implementation of the same as above
*/
use std::cell::RefCell;
use std::sync::atomic::{AtomicI32, Ordering};
use std::thread_local;
thread_local!(
static SAFE_DYNAMIC_TEXT: RefCell<Option<Element>> = RefCell::new(None);
);
static SAFE_APPLES: AtomicI32 = AtomicI32::new(1);
static SAFE_TREES: AtomicI32 = AtomicI32::new(0);
#[wasm_bindgen]
pub fn safe_init() {
utils::set_panic_hook();
// window and document have to be fetched from JS world
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let main = document.get_elements_by_tag_name("main").item(0).unwrap();
let dynamic_text: Element = document.create_element("p").unwrap();
main.prepend_with_node_1(&dynamic_text).unwrap();
SAFE_DYNAMIC_TEXT
.with(|maybe_dynamic_text| *maybe_dynamic_text.borrow_mut() = Some(dynamic_text));
safe_update_text();
safe_set_timer();
}
pub fn safe_update_text() {
SAFE_DYNAMIC_TEXT.with(|maybe_dynamic_text| {
if let Some(dynamic_text) = maybe_dynamic_text.borrow().as_ref() {
dynamic_text.set_inner_html(&format!(
"You have {} apples and {} trees.",
SAFE_APPLES.load(Ordering::Relaxed),
SAFE_TREES.load(Ordering::Relaxed)
))
}
})
}
#[wasm_bindgen]
pub fn safe_buy() {
if SAFE_APPLES.load(Ordering::Relaxed) > 0 {
SAFE_TREES.fetch_add(1, Ordering::Relaxed);
SAFE_APPLES.fetch_add(-1, Ordering::Relaxed);
safe_update_text();
}
}
fn safe_collect_apples() {
SAFE_APPLES.fetch_add(SAFE_TREES.load(Ordering::Relaxed), Ordering::Relaxed);
safe_update_text();
}
fn safe_set_timer() {
let window = web_sys::window().unwrap();
let boxed_function = Box::new(safe_collect_apples);
let closure = Closure::wrap(boxed_function as Box<dyn Fn()>);
// setInterval() has many different names in Rust because there is no overloading
window
.set_interval_with_callback_and_timeout_and_arguments_0(
closure.as_ref().unchecked_ref(),
5000,
)
.unwrap();
// Leak memory on purpose
closure.forget();
}