forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_wasmfs_js_file.js
52 lines (51 loc) · 2.02 KB
/
library_wasmfs_js_file.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
mergeInto(LibraryManager.library, {
// JSFile backend: Store a file's data in JS. We map File objects in C++ to
// entries here that contain typed arrays.
$wasmFS$JSMemoryFiles: {},
_wasmfs_create_js_file_backend_js__deps: [
'$wasmFS$backends',
'$wasmFS$JSMemoryFiles',
],
_wasmfs_create_js_file_backend_js: function(backend) {
wasmFS$backends[backend] = {
allocFile: (file) => {
// Do nothing: we allocate the typed array lazily, see write()
},
freeFile: (file) => {
// Release the memory, as it now has no references to it any more.
wasmFS$JSMemoryFiles[file] = undefined;
},
write: (file, buffer, length, offset) => {
try {
if (!wasmFS$JSMemoryFiles[file]) {
// Initialize typed array on first write operation.
wasmFS$JSMemoryFiles[file] = new Uint8Array(offset + length);
}
if (offset + length > wasmFS$JSMemoryFiles[file].length) {
// Resize the typed array if the length of the write buffer exceeds its capacity.
var oldContents = wasmFS$JSMemoryFiles[file];
var newContents = new Uint8Array(offset + length);
newContents.set(oldContents);
wasmFS$JSMemoryFiles[file] = newContents;
}
wasmFS$JSMemoryFiles[file].set(HEAPU8.subarray(buffer, buffer + length), offset);
return length;
} catch (err) {
return -{{{ cDefine('EIO') }}};
}
},
read: (file, buffer, length, offset) => {
var fileData = wasmFS$JSMemoryFiles[file];
// We can't read past the end of the file's data.
var dataAfterOffset = Math.max(0, fileData.length - offset);
// We only read as much as we were asked.
length = Math.min(length, dataAfterOffset);
HEAPU8.set(fileData.subarray(offset, offset + length), buffer);
return length;
},
getSize: (file) => {
return wasmFS$JSMemoryFiles[file] ? wasmFS$JSMemoryFiles[file].length : 0;
},
};
},
});