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
|
addToLibrary({
cbfl: [],
validateCallbackId__deps: [ 'cbfl' ],
validateCallbackId: function(i) {
if (i < 0) {
console.log("--- WARNING ---");
console.log("Trying to access callback function of invalid id " + i);
console.log("--- WARNING ---");
return false;
}
if (i >= _cbfl.length) {
console.log("--- WARNING ---");
console.log("Trying to access callback function " + i + ", but only "
+ _cbfl.length + " have been registered. This may be caused by a "
+ "call outside of the main thread.");
console.log("--- WARNING ---");
return false;
}
return true;
},
addCallbackFunction__deps: [ 'cbfl' ],
addCallbackFunction: function(f) {
_cbfl.push(f)
return _cbfl.length - 1
},
callFunction__deps: [ 'cbfl', 'validateCallbackId' ],
callFunction: function(id, arg) {
// This is a workaround related to usign WASM64
// JavaScript's UTF8ToString expects a pointer argument, which for JS is
// of type "number", but WASM64 is passing a BigInt. See also:
// https://github.com/emscripten-core/emscripten/issues/21541
// (but I could not make the suggested solution work in this case).
// TODO: check if there is a better workaround.
const non64_arg = Number(arg);
if (_validateCallbackId(id))
_cbfl[id](UTF8ToString(non64_arg));
},
callFunctionInt__deps: [ 'cbfl', 'validateCallbackId' ],
callFunctionInt: function(id) {
if (_validateCallbackId(id))
return _cbfl[id]();
return 0;
},
});
|