Skip to content

Commit 97a2233

Browse files
committed
lib/wasm: eliminate global namespace pollution in wasm_exec.js
1 parent 2a71af1 commit 97a2233

File tree

1 file changed

+65
-69
lines changed

1 file changed

+65
-69
lines changed

lib/wasm/wasm_exec.js

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,71 @@
1111
return err;
1212
};
1313

14-
if (!globalThis.fs) {
15-
let outputBuf = "";
16-
globalThis.fs = {
17-
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_DIRECTORY: -1 }, // unused
18-
writeSync(fd, buf) {
19-
outputBuf += decoder.decode(buf);
20-
const nl = outputBuf.lastIndexOf("\n");
21-
if (nl != -1) {
22-
console.log(outputBuf.substring(0, nl));
23-
outputBuf = outputBuf.substring(nl + 1);
24-
}
25-
return buf.length;
26-
},
27-
write(fd, buf, offset, length, position, callback) {
28-
if (offset !== 0 || length !== buf.length || position !== null) {
29-
callback(enosys());
30-
return;
31-
}
32-
const n = this.writeSync(fd, buf);
33-
callback(null, n);
34-
},
35-
chmod(path, mode, callback) { callback(enosys()); },
36-
chown(path, uid, gid, callback) { callback(enosys()); },
37-
close(fd, callback) { callback(enosys()); },
38-
fchmod(fd, mode, callback) { callback(enosys()); },
39-
fchown(fd, uid, gid, callback) { callback(enosys()); },
40-
fstat(fd, callback) { callback(enosys()); },
41-
fsync(fd, callback) { callback(null); },
42-
ftruncate(fd, length, callback) { callback(enosys()); },
43-
lchown(path, uid, gid, callback) { callback(enosys()); },
44-
link(path, link, callback) { callback(enosys()); },
45-
lstat(path, callback) { callback(enosys()); },
46-
mkdir(path, perm, callback) { callback(enosys()); },
47-
open(path, flags, mode, callback) { callback(enosys()); },
48-
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
49-
readdir(path, callback) { callback(enosys()); },
50-
readlink(path, callback) { callback(enosys()); },
51-
rename(from, to, callback) { callback(enosys()); },
52-
rmdir(path, callback) { callback(enosys()); },
53-
stat(path, callback) { callback(enosys()); },
54-
symlink(path, link, callback) { callback(enosys()); },
55-
truncate(path, length, callback) { callback(enosys()); },
56-
unlink(path, callback) { callback(enosys()); },
57-
utimes(path, atime, mtime, callback) { callback(enosys()); },
58-
};
59-
}
14+
// Use local variables instead of polluting the global namespace.
15+
// These will be captured by closures in the importObject.
16+
let outputBuf = "";
17+
const fs = globalThis.fs || {
18+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_DIRECTORY: -1 }, // unused
19+
writeSync(fd, buf) {
20+
outputBuf += decoder.decode(buf);
21+
const nl = outputBuf.lastIndexOf("\n");
22+
if (nl != -1) {
23+
console.log(outputBuf.substring(0, nl));
24+
outputBuf = outputBuf.substring(nl + 1);
25+
}
26+
return buf.length;
27+
},
28+
write(fd, buf, offset, length, position, callback) {
29+
if (offset !== 0 || length !== buf.length || position !== null) {
30+
callback(enosys());
31+
return;
32+
}
33+
const n = this.writeSync(fd, buf);
34+
callback(null, n);
35+
},
36+
chmod(path, mode, callback) { callback(enosys()); },
37+
chown(path, uid, gid, callback) { callback(enosys()); },
38+
close(fd, callback) { callback(enosys()); },
39+
fchmod(fd, mode, callback) { callback(enosys()); },
40+
fchown(fd, uid, gid, callback) { callback(enosys()); },
41+
fstat(fd, callback) { callback(enosys()); },
42+
fsync(fd, callback) { callback(null); },
43+
ftruncate(fd, length, callback) { callback(enosys()); },
44+
lchown(path, uid, gid, callback) { callback(enosys()); },
45+
link(path, link, callback) { callback(enosys()); },
46+
lstat(path, callback) { callback(enosys()); },
47+
mkdir(path, perm, callback) { callback(enosys()); },
48+
open(path, flags, mode, callback) { callback(enosys()); },
49+
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
50+
readdir(path, callback) { callback(enosys()); },
51+
readlink(path, callback) { callback(enosys()); },
52+
rename(from, to, callback) { callback(enosys()); },
53+
rmdir(path, callback) { callback(enosys()); },
54+
stat(path, callback) { callback(enosys()); },
55+
symlink(path, link, callback) { callback(enosys()); },
56+
truncate(path, length, callback) { callback(enosys()); },
57+
unlink(path, callback) { callback(enosys()); },
58+
utimes(path, atime, mtime, callback) { callback(enosys()); },
59+
};
6060

61-
if (!globalThis.process) {
62-
globalThis.process = {
63-
getuid() { return -1; },
64-
getgid() { return -1; },
65-
geteuid() { return -1; },
66-
getegid() { return -1; },
67-
getgroups() { throw enosys(); },
68-
pid: -1,
69-
ppid: -1,
70-
umask() { throw enosys(); },
71-
cwd() { throw enosys(); },
72-
chdir() { throw enosys(); },
73-
}
74-
}
61+
const process = globalThis.process || {
62+
getuid() { return -1; },
63+
getgid() { return -1; },
64+
geteuid() { return -1; },
65+
getegid() { return -1; },
66+
getgroups() { throw enosys(); },
67+
pid: -1,
68+
ppid: -1,
69+
umask() { throw enosys(); },
70+
cwd() { throw enosys(); },
71+
chdir() { throw enosys(); },
72+
};
7573

76-
if (!globalThis.path) {
77-
globalThis.path = {
78-
resolve(...pathSegments) {
79-
return pathSegments.join("/");
80-
}
74+
const path = globalThis.path || {
75+
resolve(...pathSegments) {
76+
return pathSegments.join("/");
8177
}
82-
}
78+
};
8379

8480
if (!globalThis.crypto) {
8581
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
@@ -221,7 +217,7 @@
221217
return this._inst.exports.testExport(a, b);
222218
}
223219

224-
const timeOrigin = Date.now() - performance.now();
220+
const timeOrigin = Date.now() - globalThis.performance.now();
225221
this.importObject = {
226222
_gotest: {
227223
add: (a, b) => a + b,
@@ -264,7 +260,7 @@
264260
// func nanotime1() int64
265261
"runtime.nanotime1": (sp) => {
266262
sp >>>= 0;
267-
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
263+
setInt64(sp + 8, (timeOrigin + globalThis.performance.now()) * 1000000);
268264
},
269265

270266
// func walltime() (sec int64, nsec int32)
@@ -306,7 +302,7 @@
306302
// func getRandomData(r []byte)
307303
"runtime.getRandomData": (sp) => {
308304
sp >>>= 0;
309-
crypto.getRandomValues(loadSlice(sp + 8));
305+
globalThis.crypto.getRandomValues(loadSlice(sp + 8));
310306
},
311307

312308
// func finalizeRef(v ref)

0 commit comments

Comments
 (0)