-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tinygo.wasm.html
More file actions
84 lines (77 loc) · 2.62 KB
/
Copy pathindex.tinygo.wasm.html
File metadata and controls
84 lines (77 loc) · 2.62 KB
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
<!DOCTYPE html>
<head>
<title>URLchess</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="preload" href="assets/URLchess.css?v0.10" as="style" onload="this.rel='stylesheet'">
</head>
<body>
<noscript>
<p>You need to enable JavaScript to run URLchess.</p>
</noscript>
<script type="text/javascript">
//<![CDATA[
window.addEventListener("load", function() {
// Backwards compatibility with URLchess links when version was 0.6 and lower.
var hash = window.location.hash;
var search = window.location.search;
if (search.startsWith("?")) {
search = search.slice(1);
}
if (
(search.length > 0 && hash.length == 0)
||
(search.length == 0 && hash.length == 0 && window.location.href.indexOf("?") != -1)
){
//window.alert("URL is in format for URLchess version <=0.6, transforming...");
window.location.href = window.location.href.replace("?", "#");
}
// If browser supports wasm, run URLchess.wasm.
if (typeof WebAssembly === "object") {
// Load and execute go wasm runtime.
var script = document.createElement('script');
script.src = "assets/wasm_exec.tinygo.js?v0.10";
script.onload = function() {
// After wasm runtime is loaded, continue to run the URLchess wasm app.
// Check if go is present.
if (typeof Go === "undefined") {
alert("Go runtime is not loaded.");
return;
}
// Start the Go WebAssembly runtime and run URLchess wasm app.
const go = new Go();
// Try to rewrite finalizeRef function according to https://github.com/tinygo-org/tinygo/issues/1140#issuecomment-2529705551
go.importObject.gojs["syscall/js.finalizeRef"] = function(v_ref) {
// Note: TinyGo does not support finalizers so this is only called
// for one specific case, by js.go:jsString. and can/might leak memory.
// magicValue taken from `unboxValue` function.
const magicValue = 0xffffffffn;
const id = v_ref & magicValue;
if (this._goRefCounts?.[id] !== undefined) {
this._goRefCounts[id]--;
if (this._goRefCounts[id] === 0) {
const v = this._values[id];
this._values[id] = null;
this._ids.delete(v);
this._idPool.push(id);
}
} else {
console.log("syscall/js.finalizeRef: unknown id", id);
}
};
WebAssembly.instantiateStreaming(fetch("assets/URLchess.tinygo.wasm?v0.10"), go.importObject).then((result) => {
go.run(result.instance);
}).catch((err) => {
alert("Failed to load WebAssembly", err);
});
};
// Append the script with wasm to body.
document.body.appendChild(script);
} else {
alert("No wasm in browser!");
}
});
//]]>
</script>
</body>
</html>