-
Notifications
You must be signed in to change notification settings - Fork 12
/
asyncify.mjs
198 lines (163 loc) · 5.01 KB
/
asyncify.mjs
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
/**
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Put `__asyncify_data` somewhere at the start.
// This address is pretty hand-wavy and we might want to make it configurable in future.
// See https://github.com/WebAssembly/binaryen/blob/6371cf63687c3f638b599e086ca668c04a26cbbb/src/passes/Asyncify.cpp#L106-L113
// for structure details.
const DATA_ADDR = 16;
// Place actual data right after the descriptor (which is 2 * sizeof(i32) = 8 bytes).
const DATA_START = DATA_ADDR + 8;
// End data at 1024 bytes. This is where the unused area by Clang ends and real stack / data begins.
// Because this might differ between languages and parameters passed to wasm-ld, ideally we would
// use `__stack_pointer` here, but, sadly, it's not exposed via exports yet.
const DATA_END = 1024;
const WRAPPED_EXPORTS = new WeakMap();
const State = {
None: 0,
Unwinding: 1,
Rewinding: 2
};
function isPromise(obj) {
return (
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
}
function proxyGet(obj, transform) {
return new Proxy(obj, {
get: (obj, name) => transform(obj[name])
});
}
class Asyncify {
constructor() {
this.value = undefined;
this.exports = null;
}
getState() {
return this.exports.asyncify_get_state();
}
assertNoneState() {
let state = this.getState();
if (state !== State.None) {
throw new Error(`Invalid async state ${state}, expected 0.`);
}
}
wrapImportFn(fn) {
return (...args) => {
if (this.getState() === State.Rewinding) {
this.exports.asyncify_stop_rewind();
return this.value;
}
this.assertNoneState();
let value = fn(...args);
if (!isPromise(value)) {
return value;
}
this.exports.asyncify_start_unwind(DATA_ADDR);
this.value = value;
};
}
wrapModuleImports(module) {
return proxyGet(module, value => {
if (typeof value === 'function') {
return this.wrapImportFn(value);
}
return value;
});
}
wrapImports(imports) {
if (imports === undefined) return;
return proxyGet(imports, (moduleImports = Object.create(null)) =>
this.wrapModuleImports(moduleImports)
);
}
wrapExportFn(fn) {
let newExport = WRAPPED_EXPORTS.get(fn);
if (newExport !== undefined) {
return newExport;
}
newExport = async (...args) => {
this.assertNoneState();
let result = fn(...args);
while (this.getState() === State.Unwinding) {
this.exports.asyncify_stop_unwind();
this.value = await this.value;
this.assertNoneState();
this.exports.asyncify_start_rewind(DATA_ADDR);
result = fn(...args);
}
this.assertNoneState();
return result;
};
WRAPPED_EXPORTS.set(fn, newExport);
return newExport;
}
wrapExports(exports) {
let newExports = Object.create(null);
for (let exportName in exports) {
let value = exports[exportName];
if (typeof value === 'function' && !exportName.startsWith('asyncify_')) {
value = this.wrapExportFn(value);
}
Object.defineProperty(newExports, exportName, {
enumerable: true,
value
});
}
WRAPPED_EXPORTS.set(exports, newExports);
return newExports;
}
init(instance, imports) {
const { exports } = instance;
const memory = exports.memory || (imports.env && imports.env.memory);
new Int32Array(memory.buffer, DATA_ADDR).set([DATA_START, DATA_END]);
this.exports = this.wrapExports(exports);
Object.setPrototypeOf(instance, Instance.prototype);
}
}
export class Instance extends WebAssembly.Instance {
constructor(module, imports) {
let state = new Asyncify();
super(module, state.wrapImports(imports));
state.init(this, imports);
}
get exports() {
return WRAPPED_EXPORTS.get(super.exports);
}
}
Object.defineProperty(Instance.prototype, 'exports', { enumerable: true });
export async function instantiate(source, imports) {
let state = new Asyncify();
let result = await WebAssembly.instantiate(
source,
state.wrapImports(imports)
);
state.init(
result instanceof WebAssembly.Instance ? result : result.instance,
imports
);
return result;
}
export async function instantiateStreaming(source, imports) {
let state = new Asyncify();
let result = await WebAssembly.instantiateStreaming(
source,
state.wrapImports(imports)
);
state.init(result.instance, imports);
return result;
}