7
7
mergeInto ( LibraryManager . library , {
8
8
$wasmFSPreloadedFiles : [ ] ,
9
9
$wasmFSPreloadedDirs : [ ] ,
10
+ // We must note when preloading has been "flushed", that is, the time at which
11
+ // WasmFS has started up and read the preloaded data. After that time, no more
12
+ // data needs to be preloaded (and it would be invalid to do so, as any
13
+ // further additions to wasmFSPreloadedFiles|Dirs would be ignored).
14
+ $wasmFSPreloadingFlushed : false ,
15
+
10
16
$FS__postset : `
11
17
FS.init();
12
18
FS.createPreloadedFile = FS_createPreloadedFile;
13
19
` ,
14
20
$FS__deps : [
15
21
'$wasmFSPreloadedFiles' ,
16
22
'$wasmFSPreloadedDirs' ,
23
+ '$wasmFSPreloadingFlushed' ,
17
24
'$PATH' ,
18
25
'$stringToUTF8OnStack' ,
19
26
'$withStackSave' ,
@@ -55,10 +62,19 @@ FS.createPreloadedFile = FS_createPreloadedFile;
55
62
FS . ErrnoError . prototype . constructor = FS . ErrnoError ;
56
63
} ,
57
64
createDataFile : ( parent , name , fileData , canRead , canWrite , canOwn ) => {
58
- // Data files must be cached until the file system itself has been initialized.
59
- var mode = FS_getMode ( canRead , canWrite ) ;
60
65
var pathName = name ? parent + '/' + name : parent ;
61
- wasmFSPreloadedFiles . push ( { pathName, fileData, mode} ) ;
66
+ var mode = FS_getMode ( canRead , canWrite ) ;
67
+
68
+ if ( ! wasmFSPreloadingFlushed ) {
69
+ // WasmFS code in the wasm is not ready to be called yet. Cache the
70
+ // files we want to create here in JS, and WasmFS will read them
71
+ // later.
72
+ wasmFSPreloadedFiles . push ( { pathName, fileData, mode} ) ;
73
+ } else {
74
+ // WasmFS is already running, so create the file normally.
75
+ FS . create ( pathName , mode ) ;
76
+ FS . writeFile ( pathName , fileData ) ;
77
+ }
62
78
} ,
63
79
createPath : ( parent , path , canRead , canWrite ) => {
64
80
// Cache file path directory names.
@@ -67,7 +83,11 @@ FS.createPreloadedFile = FS_createPreloadedFile;
67
83
var part = parts . pop ( ) ;
68
84
if ( ! part ) continue ;
69
85
var current = PATH . join2 ( parent , part ) ;
70
- wasmFSPreloadedDirs . push ( { parentPath : parent , childName : part } ) ;
86
+ if ( ! wasmFSPreloadingFlushed ) {
87
+ wasmFSPreloadedDirs . push ( { parentPath : parent , childName : part } ) ;
88
+ } else {
89
+ FS . mkdir ( current ) ;
90
+ }
71
91
parent = current ;
72
92
}
73
93
return current ;
@@ -322,8 +342,17 @@ FS.createPreloadedFile = FS_createPreloadedFile;
322
342
323
343
#endif
324
344
} ,
325
- _wasmfs_get_num_preloaded_files__deps : [ '$wasmFSPreloadedFiles' ] ,
345
+
346
+ _wasmfs_get_num_preloaded_files__deps : [
347
+ '$wasmFSPreloadedFiles' ,
348
+ '$wasmFSPreloadingFlushed' ] ,
326
349
_wasmfs_get_num_preloaded_files : function ( ) {
350
+ // When this method is called from WasmFS it means that we are about to
351
+ // flush all the preloaded data, so mark that. (There is no call that
352
+ // occurs at the end of that flushing, which would be more natural, but it
353
+ // is fine to mark the flushing here as during the flushing itself no user
354
+ // code can run, so nothing will check whether we have flushed or not.)
355
+ wasmFSPreloadingFlushed = true ;
327
356
return wasmFSPreloadedFiles . length ;
328
357
} ,
329
358
_wasmfs_get_num_preloaded_dirs__deps : [ '$wasmFSPreloadedDirs '] ,
0 commit comments