forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibrary_wasmfs.js
283 lines (263 loc) · 10.2 KB
/
library_wasmfs.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var WasmFSLibrary = {
$wasmFS$preloadedFiles: [],
$wasmFS$preloadedDirs: [],
#if USE_CLOSURE_COMPILER
// Declare variable for Closure, FS.createPreloadedFile() below calls Browser.handledByPreloadPlugin()
$FS__postset: '/**@suppress {duplicate, undefinedVars}*/var Browser;',
#endif
$FS__deps: [
'$wasmFS$preloadedFiles',
'$wasmFS$preloadedDirs',
'$asyncLoad',
],
$FS : {
// TODO: Clean up the following functions - currently copied from library_fs.js directly.
createPreloadedFile: (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
// TODO: use WasmFS code to resolve and join the path here?
var fullname = name ? parent + '/' + name : parent;
var dep = getUniqueRunDependency('cp ' + fullname); // might have several active requests for the same fullname
function processData(byteArray) {
function finish(byteArray) {
if (preFinish) preFinish();
if (!dontCreateFile) {
FS.createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
}
if (onload) onload();
removeRunDependency(dep);
}
#if !MINIMAL_RUNTIME
if (Browser.handledByPreloadPlugin(byteArray, fullname, finish, () => {
if (onerror) onerror();
removeRunDependency(dep);
})) {
return;
}
#endif
finish(byteArray);
}
addRunDependency(dep);
if (typeof url == 'string') {
asyncLoad(url, (byteArray) => {
processData(byteArray);
}, onerror);
} else {
processData(url);
}
},
getMode: (canRead, canWrite) => {
var mode = 0;
if (canRead) mode |= {{{ cDefine('S_IRUGO') }}} | {{{ cDefine('S_IXUGO') }}};
if (canWrite) mode |= {{{ cDefine('S_IWUGO') }}};
return mode;
},
createDataFile: (parent, name, data, canRead, canWrite, canOwn) => {
// Data files must be cached until the file system itself has been initialized.
var mode = FS.getMode(canRead, canWrite);
var pathName = name ? parent + '/' + name : parent;
wasmFS$preloadedFiles.push({pathName: pathName, fileData: data, mode: mode});
},
createPath: (parent, path, canRead, canWrite) => {
// Cache file path directory names.
wasmFS$preloadedDirs.push({parentPath: parent, childName: path});
},
readFile: (path, opts) => {
opts = opts || {};
opts.encoding = opts.encoding || 'binary';
if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
throw new Error('Invalid encoding type "' + opts.encoding + '"');
}
var pathName = allocateUTF8(path);
// Copy the file into a JS buffer on the heap.
var buf = __wasmfs_read_file(pathName);
// The integer length is returned in the first 8 bytes of the buffer.
var length = {{{ makeGetValue('buf', '0', 'i64') }}};
// Default return type is binary.
// The buffer contents exist 8 bytes after the returned pointer.
var ret = new Uint8Array(HEAPU8.subarray(buf + 8, buf + 8 + length));
if (opts.encoding === 'utf8') {
ret = UTF8ArrayToString(ret, 0);
}
_free(pathName);
_free(buf);
return ret;
},
cwd: () => {
// TODO: Remove dependency on FS.cwd().
// User code should not be using FS.cwd().
// For file preloading, cwd should be '/' to begin with.
return '/';
},
#if FORCE_FILESYSTEM
// Full JS API support
mkdir: (path, mode) => {
mode = mode !== undefined ? mode : 511 /* 0777 */;
var buffer = allocateUTF8OnStack(path);
__wasmfs_mkdir(buffer, mode);
},
chdir: (path) => {
var buffer = allocateUTF8OnStack(path);
return __wasmfs_chdir(buffer);
},
writeFile: (path, data) => {
var pathBuffer = allocateUTF8OnStack(path);
var dataBuffer = _malloc(data);
__wasmfs_write_file(pathBuffer, dataBuffer, data.length);
_free(dataBuffer);
},
symlink: (target, linkpath) => {
var targetBuffer = allocateUTF8OnStack(target);
var linkpathBuffer = allocateUTF8OnStack(linkpath);
__wasmfs_symlink(targetBuffer, linkpathBuffer);
},
chmod: (path, mode) => {
var buffer = allocateUTF8OnStack(path);
return __wasmfs_chmod(buffer, mode);
},
findObject: (path) => {
var result = __wasmfs_identify(path);
if (result == {{{ cDefine('ENOENT') }}}) {
return null;
}
return {
isFolder: result == {{{ cDefine('EISDIR') }}},
isDevice: false, // TODO: wasmfs support for devices
};
},
#endif
},
_wasmfs_get_num_preloaded_files__deps: ['$wasmFS$preloadedFiles'],
_wasmfs_get_num_preloaded_files: function() {
return wasmFS$preloadedFiles.length;
},
_wasmfs_get_num_preloaded_dirs__deps: ['$wasmFS$preloadedDirs'],
_wasmfs_get_num_preloaded_dirs: function() {
return wasmFS$preloadedDirs.length;
},
_wasmfs_get_preloaded_file_mode: function(index) {
return wasmFS$preloadedFiles[index].mode;
},
_wasmfs_get_preloaded_parent_path: function(index, parentPathBuffer) {
var s = wasmFS$preloadedDirs[index].parentPath;
var len = lengthBytesUTF8(s) + 1;
stringToUTF8(s, parentPathBuffer, len);
},
_wasmfs_get_preloaded_child_path: function(index, childNameBuffer) {
var s = wasmFS$preloadedDirs[index].childName;
var len = lengthBytesUTF8(s) + 1;
stringToUTF8(s, childNameBuffer, len);
},
_wasmfs_get_preloaded_path_name: function(index, fileNameBuffer) {
var s = wasmFS$preloadedFiles[index].pathName;
var len = lengthBytesUTF8(s) + 1;
stringToUTF8(s, fileNameBuffer, len);
},
_wasmfs_get_preloaded_file_size: function(index) {
return wasmFS$preloadedFiles[index].fileData.length;
},
_wasmfs_copy_preloaded_file_data: function(index, buffer) {
HEAPU8.set(wasmFS$preloadedFiles[index].fileData, buffer);
},
// Backend support. wasmFS$backends will contain a mapping of backend IDs to
// the JS code that implements them. This is the JS side of the JSImpl* class
// in C++, together with the js_impl calls defined right after it.
$wasmFS$backends: {},
// JSImpl
_wasmfs_jsimpl_alloc_file: function(backend, file) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
return wasmFS$backends[backend].allocFile(file);
},
_wasmfs_jsimpl_free_file: function(backend, file) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
return wasmFS$backends[backend].freeFile(file);
},
_wasmfs_jsimpl_write: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
{{{ receiveI64ParamAsDouble('offset') }}}
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
return wasmFS$backends[backend].write(file, buffer, length, offset);
},
_wasmfs_jsimpl_read: function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}) {
{{{ receiveI64ParamAsDouble('offset') }}}
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
return wasmFS$backends[backend].read(file, buffer, length, offset);
},
_wasmfs_jsimpl_get_size: function(backend, file) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
return wasmFS$backends[backend].getSize(file);
},
// ProxiedAsyncJSImpl. Each function receives a function pointer and a
// parameter. We convert those into a convenient Promise API for the
// implementors of backends: the hooks we call should return Promises, which
// we then connect to the calling C++.
// TODO: arg is void*, which for MEMORY64 will be 64-bit. we need a way to
// declare arg in the function signature here (like defineI64Param,
// but that varies for wasm32/wasm64), and a way to do makeDynCall that
// adds a 'p' signature type for pointer, or something like that
// (however, dyncalls might also just work, given in MEMORY64 we assume
// WASM_BIGINT so the pointer is just a single argument, just like in
// wasm32).
_wasmfs_jsimpl_async_alloc_file: async function(backend, file, fptr, arg) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
{{{ runtimeKeepalivePush() }}}
await wasmFS$backends[backend].allocFile(file);
{{{ runtimeKeepalivePop() }}}
{{{ makeDynCall('vi', 'fptr') }}}(arg);
},
_wasmfs_jsimpl_async_free_file: async function(backend, file, fptr, arg) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
{{{ runtimeKeepalivePush() }}}
await wasmFS$backends[backend].freeFile(file);
{{{ runtimeKeepalivePop() }}}
{{{ makeDynCall('vi', 'fptr') }}}(arg);
},
_wasmfs_jsimpl_async_write: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
{{{ receiveI64ParamAsDouble('offset') }}}
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
{{{ runtimeKeepalivePush() }}}
var size = await wasmFS$backends[backend].write(file, buffer, length, offset);
{{{ runtimeKeepalivePop() }}}
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
{{{ makeDynCall('vi', 'fptr') }}}(arg);
},
_wasmfs_jsimpl_async_read: async function(backend, file, buffer, length, {{{ defineI64Param('offset') }}}, fptr, arg) {
{{{ receiveI64ParamAsDouble('offset') }}}
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
{{{ runtimeKeepalivePush() }}}
var size = await wasmFS$backends[backend].read(file, buffer, length, offset);
{{{ runtimeKeepalivePop() }}}
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
{{{ makeDynCall('vi', 'fptr') }}}(arg);
},
_wasmfs_jsimpl_async_get_size: async function(backend, file, fptr, arg) {
#if ASSERTIONS
assert(wasmFS$backends[backend]);
#endif
{{{ runtimeKeepalivePush() }}}
var size = await wasmFS$backends[backend].getSize(file);
{{{ runtimeKeepalivePop() }}}
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.result, '0', 'i32') }}};
{{{ makeSetValue('arg', C_STRUCTS.CallbackState.offset, 'size', 'i64') }}};
{{{ makeDynCall('vi', 'fptr') }}}(arg);
},
}
mergeInto(LibraryManager.library, WasmFSLibrary);
DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push('$FS');