Repository navigation
Expand file tree
/
Copy pathimports.js
More file actions
74 lines (51 loc) · 2 KB
/
Copy pathimports.js
File metadata and controls
74 lines (51 loc) · 2 KB
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
const fs = require('fs')
class NodeImport {
constructor() {
this._exports = null
this.wasmImports = {
FS: {
fsWriteFileSync: (path, data) => {
fs.writeFileSync(this._exports.__getString(path), Uint8Array.from(this._exports.__getArray(data)))
},
fsReadFileSync: (id, path) => {
const array = fs.readFileSync(this._exports.__getString(path))
return this._exports.__newArray(id, array)
},
fsMkDirSync: (path) => {
fs.mkdirSync(this._exports.__getString(path))
},
fsAppendFileSync: (path, data) => {
fs.appendFileSync(this._exports.__getString(path), Uint8Array.from(this._exports.__getArray(data)))
},
fsExistsSync: (path) => {
return fs.existsSync(this._exports.__getString(path)) ? 1 : 0
},
fsReadStreamCall: (path, pointer) => {
const stream = fs.createReadStream(this._exports.__getString(path))
const callback = this._exports.table.get(pointer)
stream.on('data', (chunk) => {
callback(this._exports.__newString(chunk.toString()))
})
}
}
}
}
get wasmExports() {
return this._exports
}
set wasmExports(e) {
this._exports = e
this._exports.__getString = e.__getString
this._exports.__newString = e.__newString
this._exports.__newArray = e.__newArray
this._exports.__getArray = e.__getArray
}
getFn(fnIndex) {
if (!this.wasmExports)
throw new Error(
'Make sure you set .wasmExports after instantiating the Wasm module but before running the Wasm module.',
)
return this._exports.table.get(fnIndex)
}
}
module.exports = NodeImport