Skip to content

Commit 2254fbe

Browse files
authored
[WasmFS][NFC] Move the jsimpl JS code to its own library file (#16893)
It was previously in library_wasmfs.js, but since it is the support code for a specific backend implementation, it makes more sense for it to live in a new library_wasmfs_jsimpl.js.
1 parent 5dfeb89 commit 2254fbe

File tree

3 files changed

+134
-124
lines changed

3 files changed

+134
-124
lines changed

src/library_wasmfs.js

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
var WasmFSLibrary = {
1+
/**
2+
* @license
3+
* Copyright 2022 The Emscripten Authors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
mergeInto(LibraryManager.library, {
28
$wasmFS$preloadedFiles: [],
39
$wasmFS$preloadedDirs: [],
410
#if USE_CLOSURE_COMPILER
@@ -161,128 +167,7 @@ var WasmFSLibrary = {
161167
},
162168
_wasmfs_copy_preloaded_file_data: function(index, buffer) {
163169
HEAPU8.set(wasmFS$preloadedFiles[index].fileData, buffer);
164-
},
165-
166-
// Backend support. wasmFS$backends will contain a mapping of backend IDs to
167-
// the JS code that implements them. This is the JS side of the JSImpl* class
168-
// in C++, together with the js_impl calls defined right after it.
169-
$wasmFS$backends: {},
170-
171-
// JSImpl
172-
173-
_wasmfs_jsimpl_alloc_file: function(backend, file) {
174-
#if ASSERTIONS
175-
assert(wasmFS$backends[backend]);
176-
#endif
177-
return wasmFS$backends[backend].allocFile(file);
178-
},
179-
180-
_wasmfs_jsimpl_free_file: function(backend, file) {
181-
#if ASSERTIONS
182-
assert(wasmFS$backends[backend]);
183-
#endif
184-
return wasmFS$backends[backend].freeFile(file);
185-
},
186-
187-
_wasmfs_jsimpl_write: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
188-
{{{ receiveI64ParamAsDouble('offset') }}}
189-
#if ASSERTIONS
190-
assert(wasmFS$backends[backend]);
191-
#endif
192-
return wasmFS$backends[backend].write(file, buffer, length, offset);
193-
},
194-
195-
_wasmfs_jsimpl_read: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
196-
{{{ receiveI64ParamAsDouble('offset') }}}
197-
#if ASSERTIONS
198-
assert(wasmFS$backends[backend]);
199-
#endif
200-
return wasmFS$backends[backend].read(file, buffer, length, offset);
201-
},
202-
203-
_wasmfs_jsimpl_get_size: function(backend, file) {
204-
#if ASSERTIONS
205-
assert(wasmFS$backends[backend]);
206-
#endif
207-
return wasmFS$backends[backend].getSize(file);
208-
},
209-
210-
// ProxiedAsyncJSImpl. Each function receives a function pointer and a
211-
// parameter. We convert those into a convenient Promise API for the
212-
// implementors of backends: the hooks we call should return Promises, which
213-
// we then connect to the calling C++.
214-
215-
// TODO: arg is void*, which for MEMORY64 will be 64-bit. we need a way to
216-
// declare arg in the function signature here (like defineI64Param,
217-
// but that varies for wasm32/wasm64), and a way to do makeDynCall that
218-
// adds a 'p' signature type for pointer, or something like that
219-
// (however, dyncalls might also just work, given in MEMORY64 we assume
220-
// WASM_BIGINT so the pointer is just a single argument, just like in
221-
// wasm32).
222-
_wasmfs_jsimpl_async_alloc_file__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
223-
_wasmfs_jsimpl_async_alloc_file: async function(backend, file, fptr, arg) {
224-
#if ASSERTIONS
225-
assert(wasmFS$backends[backend]);
226-
#endif
227-
{{{ runtimeKeepalivePush() }}}
228-
await wasmFS$backends[backend].allocFile(file);
229-
{{{ runtimeKeepalivePop() }}}
230-
{{{ makeDynCall('vi', 'fptr') }}}(arg);
231-
},
232-
233-
_wasmfs_jsimpl_async_free_file__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
234-
_wasmfs_jsimpl_async_free_file: async function(backend, file, fptr, arg) {
235-
#if ASSERTIONS
236-
assert(wasmFS$backends[backend]);
237-
#endif
238-
{{{ runtimeKeepalivePush() }}}
239-
await wasmFS$backends[backend].freeFile(file);
240-
{{{ runtimeKeepalivePop() }}}
241-
{{{ makeDynCall('vi', 'fptr') }}}(arg);
242-
},
243-
244-
_wasmfs_jsimpl_async_write__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
245-
_wasmfs_jsimpl_async_write: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
246-
{{{ receiveI64ParamAsDouble('offset') }}}
247-
#if ASSERTIONS
248-
assert(wasmFS$backends[backend]);
249-
#endif
250-
{{{ runtimeKeepalivePush() }}}
251-
var size = await wasmFS$backends[backend].write(file, buffer, length, offset);
252-
{{{ runtimeKeepalivePop() }}}
253-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
254-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
255-
{{{ makeDynCall('vi', 'fptr') }}}(arg);
256-
},
257-
258-
_wasmfs_jsimpl_async_read__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
259-
_wasmfs_jsimpl_async_read: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
260-
{{{ receiveI64ParamAsDouble('offset') }}}
261-
#if ASSERTIONS
262-
assert(wasmFS$backends[backend]);
263-
#endif
264-
{{{ runtimeKeepalivePush() }}}
265-
var size = await wasmFS$backends[backend].read(file, buffer, length, offset);
266-
{{{ runtimeKeepalivePop() }}}
267-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
268-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
269-
{{{ makeDynCall('vi', 'fptr') }}}(arg);
270-
},
271-
272-
_wasmfs_jsimpl_async_get_size__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
273-
_wasmfs_jsimpl_async_get_size: async function(backend, file, fptr, arg) {
274-
#if ASSERTIONS
275-
assert(wasmFS$backends[backend]);
276-
#endif
277-
{{{ runtimeKeepalivePush() }}}
278-
var size = await wasmFS$backends[backend].getSize(file);
279-
{{{ runtimeKeepalivePop() }}}
280-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
281-
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
282-
{{{ makeDynCall('vi', 'fptr') }}}(arg);
283-
},
284-
}
285-
286-
mergeInto(LibraryManager.library, WasmFSLibrary);
170+
}
171+
});
287172

288173
DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push('$FS');

src/library_wasmfs_jsimpl.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @license
3+
* Copyright 2022 The Emscripten Authors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
mergeInto(LibraryManager.library, {
8+
// Backend support. wasmFS$backends will contain a mapping of backend IDs to
9+
// the JS code that implements them. This is the JS side of the JSImpl* class
10+
// in C++, together with the js_impl calls defined right after it.
11+
$wasmFS$backends: {},
12+
13+
_wasmfs_jsimpl_alloc_file: function(backend, file) {
14+
#if ASSERTIONS
15+
assert(wasmFS$backends[backend]);
16+
#endif
17+
return wasmFS$backends[backend].allocFile(file);
18+
},
19+
20+
_wasmfs_jsimpl_free_file: function(backend, file) {
21+
#if ASSERTIONS
22+
assert(wasmFS$backends[backend]);
23+
#endif
24+
return wasmFS$backends[backend].freeFile(file);
25+
},
26+
27+
_wasmfs_jsimpl_write: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
28+
{{{ receiveI64ParamAsDouble('offset') }}}
29+
#if ASSERTIONS
30+
assert(wasmFS$backends[backend]);
31+
#endif
32+
return wasmFS$backends[backend].write(file, buffer, length, offset);
33+
},
34+
35+
_wasmfs_jsimpl_read: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
36+
{{{ receiveI64ParamAsDouble('offset') }}}
37+
#if ASSERTIONS
38+
assert(wasmFS$backends[backend]);
39+
#endif
40+
return wasmFS$backends[backend].read(file, buffer, length, offset);
41+
},
42+
43+
_wasmfs_jsimpl_get_size: function(backend, file) {
44+
#if ASSERTIONS
45+
assert(wasmFS$backends[backend]);
46+
#endif
47+
return wasmFS$backends[backend].getSize(file);
48+
},
49+
50+
// ProxiedAsyncJSImpl. Each function receives a function pointer and a
51+
// parameter. We convert those into a convenient Promise API for the
52+
// implementors of backends: the hooks we call should return Promises, which
53+
// we then connect to the calling C++.
54+
55+
// TODO: arg is void*, which for MEMORY64 will be 64-bit. we need a way to
56+
// declare arg in the function signature here (like defineI64Param,
57+
// but that varies for wasm32/wasm64), and a way to do makeDynCall that
58+
// adds a 'p' signature type for pointer, or something like that
59+
// (however, dyncalls might also just work, given in MEMORY64 we assume
60+
// WASM_BIGINT so the pointer is just a single argument, just like in
61+
// wasm32).
62+
_wasmfs_jsimpl_async_alloc_file__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
63+
_wasmfs_jsimpl_async_alloc_file: async function(backend, file, fptr, arg) {
64+
#if ASSERTIONS
65+
assert(wasmFS$backends[backend]);
66+
#endif
67+
{{{ runtimeKeepalivePush() }}}
68+
await wasmFS$backends[backend].allocFile(file);
69+
{{{ runtimeKeepalivePop() }}}
70+
{{{ makeDynCall('vi', 'fptr') }}}(arg);
71+
},
72+
73+
_wasmfs_jsimpl_async_free_file__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
74+
_wasmfs_jsimpl_async_free_file: async function(backend, file, fptr, arg) {
75+
#if ASSERTIONS
76+
assert(wasmFS$backends[backend]);
77+
#endif
78+
{{{ runtimeKeepalivePush() }}}
79+
await wasmFS$backends[backend].freeFile(file);
80+
{{{ runtimeKeepalivePop() }}}
81+
{{{ makeDynCall('vi', 'fptr') }}}(arg);
82+
},
83+
84+
_wasmfs_jsimpl_async_write__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
85+
_wasmfs_jsimpl_async_write: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
86+
{{{ receiveI64ParamAsDouble('offset') }}}
87+
#if ASSERTIONS
88+
assert(wasmFS$backends[backend]);
89+
#endif
90+
{{{ runtimeKeepalivePush() }}}
91+
var size = await wasmFS$backends[backend].write(file, buffer, length, offset);
92+
{{{ runtimeKeepalivePop() }}}
93+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
94+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
95+
{{{ makeDynCall('vi', 'fptr') }}}(arg);
96+
},
97+
98+
_wasmfs_jsimpl_async_read__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
99+
_wasmfs_jsimpl_async_read: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
100+
{{{ receiveI64ParamAsDouble('offset') }}}
101+
#if ASSERTIONS
102+
assert(wasmFS$backends[backend]);
103+
#endif
104+
{{{ runtimeKeepalivePush() }}}
105+
var size = await wasmFS$backends[backend].read(file, buffer, length, offset);
106+
{{{ runtimeKeepalivePop() }}}
107+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
108+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
109+
{{{ makeDynCall('vi', 'fptr') }}}(arg);
110+
},
111+
112+
_wasmfs_jsimpl_async_get_size__deps: ['$runtimeKeepalivePush', '$runtimeKeepalivePop'],
113+
_wasmfs_jsimpl_async_get_size: async function(backend, file, fptr, arg) {
114+
#if ASSERTIONS
115+
assert(wasmFS$backends[backend]);
116+
#endif
117+
{{{ runtimeKeepalivePush() }}}
118+
var size = await wasmFS$backends[backend].getSize(file);
119+
{{{ runtimeKeepalivePop() }}}
120+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
121+
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
122+
{{{ makeDynCall('vi', 'fptr') }}}(arg);
123+
},
124+
});

src/modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ global.LibraryManager = {
9292
} else if (WASMFS) {
9393
libraries.push('library_wasmfs.js');
9494
libraries.push('library_wasmfs_js_file.js');
95+
libraries.push('library_wasmfs_jsimpl.js');
9596
libraries.push('library_wasmfs_fetch.js');
9697
libraries.push('library_wasmfs_node.js');
9798
libraries.push('library_wasmfs_opfs.js');

0 commit comments

Comments
 (0)