-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasmnastics.js
178 lines (161 loc) · 4.96 KB
/
wasmnastics.js
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
params_register_js_plugin = function (importObject) {
// === Object tools ===
importObject.env.null = function () {
return js_object(null);
}
importObject.env.type_of = function (obj) {
return js_object(typeof get_js_object(obj));
}
importObject.env.set_field_any = function (obj, key, val) {
let real_obj = get_js_object(obj);
let real_key = get_js_object(key);
let real_val = get_js_object(val);
real_obj[real_key] = real_val;
}
importObject.env.as_string = function (obj) {
return js_object(get_js_object(obj).toString());
}
// we need a seperate function for each primitive type
let primitives = [
["u8", "number"],
["u16", "number"],
["u32", "number"],
["u64", "number"],
["usize", "number"],
["i8", "number"],
["i16", "number"],
["i32", "number"],
["i64", "number"],
["isize", "number"],
["f32", "number"],
["f64", "number"],
["bool", "boolean"],
];
function primitive(p) {
return js_object(p);
}
for (let [prim, typeof_type] of primitives) {
importObject.env["primitive_to_js_" + prim] = primitive;
// make closures
function from_primitive(p) {
let inner = get_js_object(p);
let out;
if (typeof inner != typeof_type) {
out = null;
} else {
out = {
some: inner
};
}
return js_object(out);
}
importObject.env["primitive_from_js_" + prim] = from_primitive;
}
importObject.env.array = function () {
return js_object([]);
}
importObject.env.try_get_field = function (obj, key) {
try {
obj = get_js_object(obj);
key = get_js_object(key);
let val = obj[key];
/// Return a LongOption
if (val === undefined) {
return js_object(null);
} else {
return js_object({
some: val
});
}
} catch (e) {
return js_object(null);
}
}
importObject.env.equals = function (a, b, triple) {
a = get_js_object(a);
b = get_js_object(b);
if (triple) {
return a === b;
} else {
return a == b;
}
}
importObject.env.has_field = function (obj, buf, length) {
let field_name = UTF8ToString(buf, length);
return js_objects[obj][field_name] !== undefined;
}
// === Storage ===
importObject.env.storage_save = function (key, val) {
try {
key = get_js_object(key);
val = get_js_object(val);
localStorage.setItem(key, val);
return js_object({
ok: null
});
} catch (e) {
return js_object({
err: "Couldn't save to localstorage: " + e.toString()
});
}
}
importObject.env.storage_load = function (key) {
try {
key = get_js_object(key);
let found = localStorage.getItem(key);
let out = (found === null) ? {
err: `Couldn't find key \`${key}\` in localstorage`
} : {
ok: found
};
return js_object(out);
} catch (e) {
return js_object({
err: "Couldn't load from localstorage: " + e.toString()
});
}
}
// === Clipboard ===
importObject.env.clipboard_get = function () {
let waiter = waitify(navigator.clipboard.readText());
return js_object(waiter);
}
importObject.env.clipboard_set = function (text) {
let waiter = waitify(navigator.clipboard.writeText(get_js_object(text)));
return js_object(waiter);
}
importObject.env.console_log = function (obj) {
console.log(get_js_object(obj));
};
};
miniquad_add_plugin({
register_plugin: params_register_js_plugin,
name: "wasmnastics",
version: "0.1.0",
})
/**
* Turn a Promise into an object that will be updated later.
*
* The returned object starts with `waiting = true`. Once the promise
* resolves, `waiting = false` and `value = <the value>`.
*
* In case of error, it will be console logged and `waiting` will never be set to `false`.
*
* @param {Promise} promise Once this resolves, its value will be filled in.
*/
function waitify(promise) {
let out = {
waiting: true
};
promise.then(
(val) => {
out.waiting = false;
out.value = val;
},
(oh_no) => {
console.log(oh_no);
// pretend like it's still waiting.
}
);
return out;
}