Skip to content

Commit 9b5c5b9

Browse files
authored
Remove fastcomp-specific Asyncify code (#11868)
See #11860
1 parent aa130a4 commit 9b5c5b9

File tree

6 files changed

+15
-238
lines changed

6 files changed

+15
-238
lines changed

src/library_async.js

Lines changed: 1 addition & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -20,228 +20,6 @@ mergeInto(LibraryManager.library, {
2020
},
2121

2222
#if ASYNCIFY
23-
#if !WASM_BACKEND
24-
/*
25-
* The layout of normal and async stack frames
26-
*
27-
* --------------------- <-- saved sp for the current function
28-
* <last normal stack frame>
29-
* ---------------------
30-
* pointer to the previous frame <-- __async_cur_frame
31-
* saved sp
32-
* callback function <-- ctx, returned by alloc/reallloc, used by the program
33-
* saved local variable1
34-
* saved local variable2
35-
* ...
36-
* --------------------- <-- STACKTOP
37-
*
38-
*/
39-
__async: 0, // whether a truly async function has been called
40-
__async_unwind: 1, // whether to unwind the async stack frame
41-
__async_retval: '{{{ makeStaticAlloc(2) }}}', // store the return value for async functions
42-
__async_cur_frame: 0, // address to the current frame, which stores previous frame, stack pointer and async context
43-
44-
// __async_retval is not actually required in emscripten_async_resume
45-
// but we want it included when ASYNCIFY is enabled
46-
emscripten_async_resume__deps: ['__async', '__async_unwind', '__async_retval', '__async_cur_frame'],
47-
emscripten_async_resume__sig: 'v',
48-
emscripten_async_resume__asm: true,
49-
emscripten_async_resume: function() {
50-
var callback = 0;
51-
___async = 0;
52-
___async_unwind = 1;
53-
while (1) {
54-
if (!___async_cur_frame) return;
55-
callback = {{{ makeGetValueAsm('___async_cur_frame', 8, 'i32') }}};
56-
// the signature of callback is always vi
57-
// the only argument is ctx
58-
{{{ makeDynCall('vi') }}}(callback | 0, (___async_cur_frame + 8)|0);
59-
if (___async) return; // that was an async call
60-
if (!___async_unwind) {
61-
// keep the async stack
62-
___async_unwind = 1;
63-
continue;
64-
}
65-
// unwind normal stack frame
66-
stackRestore({{{ makeGetValueAsm('___async_cur_frame', 4, 'i32') }}});
67-
// pop the last async stack frame
68-
___async_cur_frame = {{{ makeGetValueAsm('___async_cur_frame', 0, 'i32') }}};
69-
}
70-
},
71-
72-
emscripten_sleep__deps: ['emscripten_async_resume', '$Browser'],
73-
emscripten_sleep: function(ms) {
74-
Module['setAsync'](); // tell the scheduler that we have a callback on hold
75-
Browser.safeSetTimeout(_emscripten_async_resume, ms);
76-
},
77-
78-
emscripten_alloc_async_context__deps: ['__async_cur_frame'],
79-
emscripten_alloc_async_context__sig: 'iii',
80-
emscripten_alloc_async_context__asm: true,
81-
emscripten_alloc_async_context: function(len, sp) {
82-
len = len|0;
83-
sp = sp|0;
84-
// len is the size of ctx
85-
// we also need to store prev_frame, stack pointer before ctx
86-
var new_frame = 0; new_frame = stackAlloc((len + 8)|0)|0;
87-
// save sp
88-
{{{ makeSetValueAsm('new_frame', 4, 'sp', 'i32') }}};
89-
// link the frame with previous one
90-
{{{ makeSetValueAsm('new_frame', 0, '___async_cur_frame', 'i32') }}};
91-
___async_cur_frame = new_frame;
92-
return (___async_cur_frame + 8)|0;
93-
},
94-
95-
emscripten_realloc_async_context__deps: ['__async_cur_frame'],
96-
emscripten_realloc_async_context__sig: 'ii',
97-
emscripten_realloc_async_context__asm: true,
98-
emscripten_realloc_async_context: function(len) {
99-
len = len|0;
100-
// assuming that we have on the stacktop
101-
stackRestore(___async_cur_frame | 0);
102-
return ((stackAlloc((len + 8)|0)|0) + 8)|0;
103-
},
104-
105-
emscripten_free_async_context__deps: ['__async_cur_frame'],
106-
emscripten_free_async_context__sig: 'vi',
107-
emscripten_free_async_context__asm: true,
108-
emscripten_free_async_context: function(ctx) {
109-
// this function is called when a possibly async function turned out to be sync
110-
// just undo a recent emscripten_alloc_async_context
111-
ctx = ctx|0;
112-
#if ASSERTIONS
113-
if ((((___async_cur_frame + 8)|0) != (ctx|0))|0) abort();
114-
#endif
115-
stackRestore(___async_cur_frame | 0);
116-
___async_cur_frame = {{{ makeGetValueAsm('___async_cur_frame', 0, 'i32') }}};
117-
},
118-
119-
emscripten_check_async: true,
120-
emscripten_do_not_unwind: true,
121-
emscripten_do_not_unwind_async: true,
122-
123-
emscripten_get_async_return_value_addr__deps: ['__async_retval'],
124-
emscripten_get_async_return_value_addr: true,
125-
126-
/*
127-
* Layout of an ASYNCIFY coroutine structure
128-
*
129-
* 0 callee's async ctx
130-
* 4 callee's STACKTOP
131-
* 8 callee's STACK_MAX
132-
* 12 my async ctx
133-
* 16 my STACKTOP
134-
* 20 my stack size
135-
* 24 coroutine function
136-
* 28 coroutine arg
137-
* 32 my stack:
138-
* ...
139-
*/
140-
emscripten_coroutine_create__sig: 'iiii',
141-
emscripten_coroutine_create__asm: true,
142-
emscripten_coroutine_create__deps: ['malloc', 'emscripten_alloc_async_context'],
143-
emscripten_coroutine_create: function(f, arg, stack_size) {
144-
f = f|0;
145-
arg = arg|0;
146-
stack_size = stack_size|0;
147-
var coroutine = 0;
148-
149-
if ((stack_size|0) <= 0) stack_size = 4096;
150-
151-
coroutine = _malloc(stack_size + 32 | 0) | 0;
152-
{{{ makeSetValueAsm('coroutine', 12, 0, 'i32') }}};
153-
{{{ makeSetValueAsm('coroutine', 16, '(coroutine+32)', 'i32') }}};
154-
{{{ makeSetValueAsm('coroutine', 20, 'stack_size', 'i32') }}};
155-
{{{ makeSetValueAsm('coroutine', 24, 'f', 'i32') }}};
156-
{{{ makeSetValueAsm('coroutine', 28, 'arg', 'i32') }}};
157-
return coroutine|0;
158-
},
159-
emscripten_coroutine_next__sig: 'ii',
160-
emscripten_coroutine_next__asm: true,
161-
emscripten_coroutine_next__deps: ['__async_cur_frame', '__async', 'emscripten_async_resume', 'free'],
162-
emscripten_coroutine_next: function(coroutine) {
163-
coroutine = coroutine|0;
164-
var coroutine_not_finished = 0, temp = 0;
165-
// switch context
166-
{{{ makeSetValueAsm('coroutine', 0, '___async_cur_frame', 'i32') }}};
167-
temp = stackSave() | 0;
168-
{{{ makeSetValueAsm('coroutine', 4, 'temp', 'i32') }}};
169-
{{{ makeSetValueAsm('coroutine', 8, 'STACK_MAX', 'i32') }}};
170-
___async_cur_frame = {{{ makeGetValueAsm('coroutine', 12, 'i32') }}};
171-
stackRestore({{{ makeGetValueAsm('coroutine', 16, 'i32') }}});
172-
STACK_MAX = coroutine + 32 + {{{ makeGetValueAsm('coroutine', 20, 'i32') }}} | 0;
173-
174-
if (!___async_cur_frame) {
175-
// first run
176-
{{{ makeDynCall('vi') }}}(
177-
{{{ makeGetValueAsm('coroutine', 24, 'i32') }}},
178-
{{{ makeGetValueAsm('coroutine', 28, 'i32') }}}
179-
);
180-
} else {
181-
_emscripten_async_resume();
182-
}
183-
184-
// switch context
185-
{{{ makeSetValueAsm('coroutine', 12, '___async_cur_frame', 'i32') }}};
186-
temp = stackSave() | 0;
187-
{{{ makeSetValueAsm('coroutine', 16, 'temp', 'i32') }}};
188-
___async_cur_frame = {{{ makeGetValueAsm('coroutine', 0, 'i32') }}};
189-
stackRestore({{{ makeGetValueAsm('coroutine', 4, 'i32') }}});
190-
STACK_MAX = {{{ makeGetValueAsm('coroutine', 8, 'i32') }}};
191-
192-
coroutine_not_finished = ___async;
193-
if (!coroutine_not_finished) {
194-
// coroutine has finished
195-
_free(coroutine);
196-
}
197-
// coroutine may be created during an async function
198-
// we do not want to affect the original async ctx
199-
// strictly we should backup and restore ___async, ___async_retval and ___async_unwind
200-
// but ___async=0 seems enough
201-
___async = 0;
202-
203-
return coroutine_not_finished|0;
204-
},
205-
emscripten_yield__sig: 'v',
206-
emscripten_yield__asm: true,
207-
emscripten_yield: function() {
208-
___async = 1;
209-
},
210-
211-
emscripten_wget__deps: ['emscripten_async_resume', '$PATH_FS', '$Browser'],
212-
emscripten_wget: function(url, file) {
213-
var _url = UTF8ToString(url);
214-
var _file = UTF8ToString(file);
215-
_file = PATH_FS.resolve(FS.cwd(), _file);
216-
Module['setAsync']();
217-
noExitRuntime = true;
218-
var destinationDirectory = PATH.dirname(_file);
219-
FS.createPreloadedFile(
220-
destinationDirectory,
221-
PATH.basename(_file),
222-
_url, true, true,
223-
_emscripten_async_resume,
224-
_emscripten_async_resume,
225-
undefined, // dontCreateFile
226-
undefined, // canOwn
227-
function() { // preFinish
228-
// if the destination directory does not yet exist, create it
229-
FS.mkdirTree(destinationDirectory);
230-
}
231-
);
232-
},
233-
234-
emscripten_fiber_init: function() {
235-
throw 'emscripten_fiber_init is not implemented for fastcomp ASYNCIFY';
236-
},
237-
emscripten_fiber_init_from_current_context: function() {
238-
throw 'emscripten_fiber_init_from_current_context is not implemented for fastcomp ASYNCIFY';
239-
},
240-
emscripten_fiber_swap: function() {
241-
throw 'emscripten_fiber_swap is not implemented for fastcomp ASYNCIFY';
242-
},
243-
244-
#else // !WASM_BACKEND
24523
$Asyncify__deps: ['$Browser', '$runAndAbortIfError'],
24624
$Asyncify: {
24725
State: {
@@ -719,7 +497,6 @@ mergeInto(LibraryManager.library, {
719497
emscripten_yield: function() {
720498
throw 'emscripten_yield has been removed. Please use the Fibers API';
721499
},
722-
#endif
723500
#else // ASYNCIFY
724501
emscripten_sleep: function() {
725502
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_sleep';
@@ -754,6 +531,6 @@ mergeInto(LibraryManager.library, {
754531
#endif // ASYNCIFY
755532
});
756533

757-
if (WASM_BACKEND && ASYNCIFY) {
534+
if (ASYNCIFY) {
758535
DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push('$Asyncify');
759536
}

src/library_idbstore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var LibraryIDBStore = {
5555
});
5656
},
5757

58-
#if WASM_BACKEND && ASYNCIFY
58+
#if ASYNCIFY
5959
emscripten_idb_load: function(db, id, pbuffer, pnum, perror) {
6060
Asyncify.handleSleep(function(wakeUp) {
6161
IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), function(error, byteArray) {
@@ -166,7 +166,7 @@ var LibraryIDBStore = {
166166
emscripten_idb_exists: function() {
167167
throw 'Please compile your program with async support in order to use synchronous operations like emscripten_idb_exists, etc.';
168168
},
169-
#endif // WASM_BACKEND && ASYNCIFY
169+
#endif // ASYNCIFY
170170
};
171171

172172
autoAddDeps(LibraryIDBStore, '$IDBStore');

src/library_sdl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ var LibrarySDL = {
17301730
// We actually do the whole screen in Unlock...
17311731
},
17321732

1733-
#if !(WASM_BACKEND && ASYNCIFY)
1733+
#if !ASYNCIFY
17341734
SDL_Delay: function(delay) {
17351735
if (!ENVIRONMENT_IS_WORKER) abort('SDL_Delay called on the main thread! Potential infinite loop, quitting. (consider building with async support like ASYNCIFY)');
17361736
// horrible busy-wait, but in a worker it at least does not block rendering
@@ -2504,7 +2504,7 @@ var LibrarySDL = {
25042504
}
25052505
}
25062506

2507-
#if (ASYNCIFY && WASM_BACKEND)
2507+
#if ASYNCIFY
25082508
var sleepCallback = function() {
25092509
if (SDL.audio && SDL.audio.queueNewAudioData) SDL.audio.queueNewAudioData();
25102510
};

src/library_syscall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ var SyscallsLibrary = {
15651565
fd_sync__sig: 'ii',
15661566
fd_sync: function(fd) {
15671567
var stream = SYSCALLS.getStreamFromFD(fd);
1568-
#if WASM_BACKEND && ASYNCIFY
1568+
#if ASYNCIFY
15691569
return Asyncify.handleSleep(function(wakeUp) {
15701570
var mount = stream.node.mount;
15711571
if (!mount.type.syncfs) {
@@ -1586,7 +1586,7 @@ var SyscallsLibrary = {
15861586
return -stream.stream_ops.fsync(stream);
15871587
}
15881588
return 0; // we can't do anything synchronously; the in-memory FS is already synced to
1589-
#endif // WASM_BACKEND && ASYNCIFY
1589+
#endif // ASYNCIFY
15901590
},
15911591

15921592
#if !WASM_BACKEND

src/postamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ function callMain(args) {
220220
// In PROXY_TO_PTHREAD builds, we should never exit the runtime below, as execution is asynchronously handed
221221
// off to a pthread.
222222
#if !PROXY_TO_PTHREAD
223-
#if WASM_BACKEND && ASYNCIFY
223+
#if ASYNCIFY
224224
// if we are saving the stack, then do not call exit, we are not
225225
// really exiting now, just unwinding the JS stack
226226
if (!noExitRuntime) {
227-
#endif // WASM_BACKEND && ASYNCIFY
227+
#endif // ASYNCIFY
228228
// if we're not running an evented main loop, it's time to exit
229229
exit(ret, /* implicit = */ true);
230-
#if WASM_BACKEND && ASYNCIFY
230+
#if ASYNCIFY
231231
}
232-
#endif // WASM_BACKEND && ASYNCIFY
232+
#endif // ASYNCIFY
233233
}
234234
catch(e) {
235235
if (e instanceof ExitStatus) {

src/preamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function ccall(ident, returnType, argTypes, args, opts) {
137137
}
138138
}
139139
var ret = func.apply(null, cArgs);
140-
#if ASYNCIFY && WASM_BACKEND
140+
#if ASYNCIFY
141141
var asyncMode = opts && opts.async;
142142
var runningAsync = typeof Asyncify === 'object' && Asyncify.currData;
143143
var prevRunningAsync = typeof Asyncify === 'object' && Asyncify.asyncFinalizers.length > 0;
@@ -163,7 +163,7 @@ function ccall(ident, returnType, argTypes, args, opts) {
163163

164164
ret = convertReturnValue(ret);
165165
if (stack !== 0) stackRestore(stack);
166-
#if ASYNCIFY && WASM_BACKEND
166+
#if ASYNCIFY
167167
// If this is an async ccall, ensure we return a promise
168168
if (opts && opts.async) return Promise.resolve(ret);
169169
#endif
@@ -906,7 +906,7 @@ function createWasm() {
906906
#if RELOCATABLE
907907
exports = relocateExports(exports, GLOBAL_BASE, 0);
908908
#endif
909-
#if WASM_BACKEND && ASYNCIFY
909+
#if ASYNCIFY
910910
exports = Asyncify.instrumentWasmExports(exports);
911911
#endif
912912
Module['asm'] = exports;
@@ -1122,7 +1122,7 @@ function createWasm() {
11221122
if (Module['instantiateWasm']) {
11231123
try {
11241124
var exports = Module['instantiateWasm'](info, receiveInstance);
1125-
#if WASM_BACKEND && ASYNCIFY
1125+
#if ASYNCIFY
11261126
exports = Asyncify.instrumentWasmExports(exports);
11271127
#endif
11281128
return exports;

0 commit comments

Comments
 (0)