@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.fac8cd9 → 0.0.0-insiders.fc63ce7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +175 -129
  2. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +58 -2
  3. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -1
  4. package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +58 -2
  5. package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +58 -2
  6. package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +210 -131
  7. package/node_modules/@emnapi/core/dist/emnapi-core.js +266 -160
  8. package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +58 -2
  9. package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -1
  10. package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -1
  11. package/node_modules/@emnapi/core/dist/emnapi-core.mjs +175 -129
  12. package/node_modules/@emnapi/core/package.json +2 -2
  13. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +13 -10
  14. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +4 -4
  15. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -1
  16. package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +4 -4
  17. package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +4 -4
  18. package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +13 -10
  19. package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +4 -4
  20. package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +13 -10
  21. package/node_modules/@emnapi/runtime/dist/emnapi.js +13 -10
  22. package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +4 -4
  23. package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -1
  24. package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -1
  25. package/node_modules/@emnapi/runtime/dist/emnapi.mjs +13 -10
  26. package/node_modules/@emnapi/runtime/package.json +1 -1
  27. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +46 -30
  28. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +57 -1
  29. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -1
  30. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +57 -1
  31. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +57 -1
  32. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +46 -30
  33. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +46 -30
  34. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +57 -1
  35. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -1
  36. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -1
  37. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +46 -30
  38. package/node_modules/@emnapi/wasi-threads/package.json +1 -1
  39. package/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +264 -0
  40. package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +14895 -13935
  41. package/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +259 -0
  42. package/node_modules/@napi-rs/wasm-runtime/package.json +13 -12
  43. package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +1 -1
  44. package/node_modules/@napi-rs/wasm-runtime/runtime.js +8 -0
  45. package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +6 -0
  46. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +51 -31
  47. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +51 -31
  48. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -1
  49. package/node_modules/@tybys/wasm-util/dist/wasm-util.js +51 -31
  50. package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -1
  51. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +12 -14
  52. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +39 -17
  53. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +12 -14
  54. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +39 -17
  55. package/node_modules/@tybys/wasm-util/package.json +1 -1
  56. package/node_modules/tslib/package.json +1 -1
  57. package/node_modules/tslib/tslib.es6.js +10 -1
  58. package/node_modules/tslib/tslib.es6.mjs +10 -1
  59. package/node_modules/tslib/tslib.js +42 -33
  60. package/package.json +7 -7
  61. package/tailwindcss-oxide.wasi-browser.js +6 -3
  62. package/tailwindcss-oxide.wasi.cjs +26 -3
  63. package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
  64. package/node_modules/@napi-rs/wasm-runtime/LICENSE +0 -43
  65. package/node_modules/@napi-rs/wasm-runtime/dist/runtime.js +0 -12285
  66. package/node_modules/@napi-rs/wasm-runtime/fs-proxy.cjs +0 -209
@@ -0,0 +1,259 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @param {unknown} value
5
+ */
6
+ const getType = (value) => {
7
+ if (value === undefined) return 0
8
+ if (value === null) return 1
9
+ const t = typeof value
10
+ if (t === 'boolean') return 2
11
+ if (t === 'number') return 3
12
+ if (t === 'string') return 4
13
+ if (t === 'object') return 6
14
+ if (t === 'bigint') return 9
15
+ return -1
16
+ }
17
+
18
+ /**
19
+ * @param {import('memfs').IFs} memfs
20
+ * @param {any} value
21
+ * @param {ReturnType<typeof getType>} type
22
+ * @returns {Uint8Array}
23
+ */
24
+ const encodeValue = (memfs, value, type) => {
25
+ switch (type) {
26
+ case 0:
27
+ case 1:
28
+ return new Uint8Array(0)
29
+ case 2: {
30
+ const view = new Int32Array(1)
31
+ view[0] = value ? 1 : 0
32
+ return new Uint8Array(view.buffer)
33
+ }
34
+ case 3: {
35
+ const view = new Float64Array(1)
36
+ view[0] = value
37
+ return new Uint8Array(view.buffer)
38
+ }
39
+ case 4: {
40
+ const view = new TextEncoder().encode(value)
41
+ return view
42
+ }
43
+ case 6: {
44
+ function storeConstructor(obj, memfs, processed = new WeakSet()) {
45
+ if (!obj || typeof obj !== 'object') {
46
+ return
47
+ }
48
+
49
+ if (processed.has(obj)) {
50
+ return
51
+ }
52
+
53
+ processed.add(obj)
54
+
55
+ const [entry] =
56
+ Object.entries(memfs).filter(([_, v]) => v === obj.constructor)[0] ??
57
+ []
58
+ if (entry) {
59
+ Object.defineProperty(obj, '__constructor__', {
60
+ configurable: true,
61
+ writable: true,
62
+ enumerable: true,
63
+ value: entry,
64
+ })
65
+ }
66
+
67
+ for (const value of Object.values(obj)) {
68
+ storeConstructor(value, memfs, processed)
69
+ }
70
+ }
71
+
72
+ storeConstructor(value, memfs)
73
+
74
+ const json = JSON.stringify(value, (_, value) => {
75
+ if (typeof value === 'bigint') {
76
+ return `BigInt(${String(value)})`
77
+ }
78
+ if (value instanceof Error) {
79
+ return {
80
+ ...value,
81
+ message: value.message,
82
+ stack: value.stack,
83
+ __error__: value.constructor.name,
84
+ }
85
+ }
86
+ return value
87
+ })
88
+ const view = new TextEncoder().encode(json)
89
+ return view
90
+ }
91
+ case 9: {
92
+ const view = new BigInt64Array(1)
93
+ view[0] = value
94
+ return new Uint8Array(view.buffer)
95
+ }
96
+ case -1:
97
+ default:
98
+ throw new Error('unsupported data')
99
+ }
100
+ }
101
+
102
+ /**
103
+ * @param {typeof import('memfs')} memfs
104
+ * @param {Uint8Array} payload
105
+ * @param {number} type
106
+ * @returns {any}
107
+ */
108
+ const decodeValue = (memfs, payload, type) => {
109
+ if (type === 0) return undefined
110
+ if (type === 1) return null
111
+ if (type === 2)
112
+ return Boolean(new Int32Array(payload.buffer, payload.byteOffset, 1)[0])
113
+ if (type === 3)
114
+ return new Float64Array(payload.buffer, payload.byteOffset, 1)[0]
115
+ if (type === 4) return new TextDecoder().decode(payload.slice())
116
+ if (type === 6) {
117
+ const obj = JSON.parse(
118
+ new TextDecoder().decode(payload.slice()),
119
+ (_key, value) => {
120
+ if (typeof value === 'string') {
121
+ const matched = value.match(/^BigInt\((-?\d+)\)$/)
122
+ if (matched && matched[1]) {
123
+ return BigInt(matched[1])
124
+ }
125
+ }
126
+ return value
127
+ },
128
+ )
129
+
130
+ function loadConstructor(obj, memfs, processed = new WeakSet()) {
131
+ if (!obj || typeof obj !== 'object') {
132
+ return
133
+ }
134
+
135
+ if (processed.has(obj)) {
136
+ return
137
+ }
138
+
139
+ processed.add(obj)
140
+
141
+ if (obj.__constructor__) {
142
+ const ctor = obj.__constructor__
143
+ delete obj.__constructor__
144
+ Object.setPrototypeOf(obj, memfs[ctor].prototype)
145
+ }
146
+
147
+ for (const value of Object.values(obj)) {
148
+ loadConstructor(value, memfs, processed)
149
+ }
150
+ }
151
+
152
+ loadConstructor(obj, memfs)
153
+
154
+ if (obj.__error__) {
155
+ const name = obj.__error__
156
+ const ErrorConstructor = globalThis[name] || Error
157
+ delete obj.__error__
158
+ const err = new ErrorConstructor(obj.message)
159
+ Object.defineProperty(err, 'stack', {
160
+ configurable: true,
161
+ enumerable: false,
162
+ writable: true,
163
+ value: err.stack,
164
+ })
165
+ Object.defineProperty(err, Symbol.toStringTag, {
166
+ configurable: true,
167
+ enumerable: false,
168
+ writable: true,
169
+ value: name,
170
+ })
171
+ for (const [k, v] of Object.entries(obj)) {
172
+ if (k === 'message' || k === 'stack') continue
173
+ err[k] = v
174
+ }
175
+ return err
176
+ }
177
+ return obj
178
+ }
179
+ if (type === 9)
180
+ return new BigInt64Array(payload.buffer, payload.byteOffset, 1)[0]
181
+ throw new Error('unsupported data')
182
+ }
183
+
184
+ /**
185
+ * @param {import('memfs').IFs} fs
186
+ * @returns {(e: { data: { __fs__: { sab: Int32Array, type: keyof import('memfs').IFs, payload: any[] } } }) => void}
187
+ */
188
+ // oxlint-disable-next-line no-unused-vars -- fixed in an upcoming release
189
+ export const createOnMessage = (fs) =>
190
+ function onMessage(e) {
191
+ if (e.data.__fs__) {
192
+ /**
193
+ * 0..4 status(int32_t): 21(waiting) 0(success) 1(error)
194
+ * 5..8 type(napi_valuetype): 0(undefined) 1(null) 2(boolean) 3(number) 4(string) 6(jsonstring) 9(bigint) -1(unsupported)
195
+ * 9..16 payload_size(uint32_t) <= 1024
196
+ * 16..16 + payload_size payload_content
197
+ */
198
+ const { sab, type, payload } = e.data.__fs__
199
+ const fn = fs[type]
200
+ try {
201
+ const ret = fn.apply(fs, payload)
202
+ const t = getType(ret)
203
+ Atomics.store(sab, 1, t)
204
+ const v = encodeValue(fs, ret, t)
205
+ Atomics.store(sab, 2, v.length)
206
+ new Uint8Array(sab.buffer).set(v, 16)
207
+ Atomics.store(sab, 0, 0) // success
208
+ } catch (/** @type {any} */ err) {
209
+ const t = getType(err)
210
+ Atomics.store(sab, 1, t)
211
+ const v = encodeValue(fs, err, t)
212
+ Atomics.store(sab, 2, v.length)
213
+ new Uint8Array(sab.buffer).set(v, 16)
214
+ Atomics.store(sab, 0, 1) // error
215
+ } finally {
216
+ Atomics.notify(sab, 0)
217
+ }
218
+ }
219
+ }
220
+
221
+ /**
222
+ * @param {typeof import('memfs')} memfs
223
+ */
224
+ export const createFsProxy = (memfs) =>
225
+ new Proxy(
226
+ {},
227
+ {
228
+ get(_target, p, _receiver) {
229
+ /**
230
+ * @param {any[]} args
231
+ */
232
+ return function (...args) {
233
+ const sab = new SharedArrayBuffer(16 + 10240)
234
+ const i32arr = new Int32Array(sab)
235
+ Atomics.store(i32arr, 0, 21)
236
+
237
+ postMessage({
238
+ __fs__: {
239
+ sab: i32arr,
240
+ type: p,
241
+ payload: args,
242
+ },
243
+ })
244
+
245
+ Atomics.wait(i32arr, 0, 21)
246
+
247
+ const status = Atomics.load(i32arr, 0)
248
+ const type = Atomics.load(i32arr, 1)
249
+ const size = Atomics.load(i32arr, 2)
250
+ const content = new Uint8Array(sab, 16, size)
251
+ const value = decodeValue(memfs, content, type)
252
+ if (status === 1) {
253
+ throw value
254
+ }
255
+ return value
256
+ }
257
+ },
258
+ },
259
+ )
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@napi-rs/wasm-runtime",
3
- "version": "0.2.11",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "Runtime and polyfill for wasm targets",
6
6
  "author": {
@@ -19,30 +19,32 @@
19
19
  },
20
20
  "files": [
21
21
  "runtime.cjs",
22
- "fs-proxy.cjs",
22
+ "runtime.js",
23
+ "fs-proxy.js",
24
+ "dist/*.cjs",
23
25
  "dist/*.js"
24
26
  ],
25
27
  "devDependencies": {
26
28
  "@rollup/plugin-alias": "^5.1.1",
27
- "@rollup/plugin-commonjs": "^28.0.3",
29
+ "@rollup/plugin-commonjs": "^28.0.6",
28
30
  "@rollup/plugin-inject": "^5.0.5",
29
31
  "@rollup/plugin-json": "^6.1.0",
30
32
  "@rollup/plugin-node-resolve": "^16.0.1",
31
33
  "@rollup/plugin-replace": "^6.0.2",
32
34
  "buffer": "^6.0.3",
33
- "memfs": "^4.17.0",
34
- "node-inspect-extracted": "^3.0.2",
35
+ "memfs": "^4.39.0",
36
+ "node-inspect-extracted": "^3.2.1",
35
37
  "path-browserify": "^1.0.1",
36
38
  "process": "^0.11.10",
37
39
  "readable-stream": "^4.7.0",
38
- "rollup": "^4.38.0",
40
+ "rollup": "^4.50.1",
39
41
  "rollup-plugin-polyfill-node": "^0.13.0",
40
42
  "tslib": "^2.8.1"
41
43
  },
42
44
  "dependencies": {
43
- "@emnapi/core": "^1.4.3",
44
- "@emnapi/runtime": "^1.4.3",
45
- "@tybys/wasm-util": "^0.9.0"
45
+ "@emnapi/core": "^1.5.0",
46
+ "@emnapi/runtime": "^1.5.0",
47
+ "@tybys/wasm-util": "^0.10.1"
46
48
  },
47
49
  "scripts": {
48
50
  "build": "rollup -c rollup.config.js",
@@ -50,12 +52,11 @@
50
52
  },
51
53
  "exports": {
52
54
  ".": {
53
- "import": "./dist/runtime.js",
55
+ "import": "./runtime.js",
54
56
  "require": "./runtime.cjs"
55
57
  },
56
58
  "./fs": {
57
59
  "import": "./dist/fs.js"
58
60
  }
59
- },
60
- "gitHead": "984d166275f29e24e4bcf9d6189fe9bbe2e96bfe"
61
+ }
61
62
  }
@@ -2,7 +2,7 @@ const { MessageHandler, instantiateNapiModuleSync, instantiateNapiModule } = req
2
2
  const { getDefaultContext } = require('@emnapi/runtime')
3
3
  const { WASI } = require('@tybys/wasm-util')
4
4
 
5
- const { createFsProxy, createOnMessage } = require('./fs-proxy.cjs')
5
+ const { createFsProxy, createOnMessage } = require('./dist/fs-proxy.cjs')
6
6
 
7
7
  module.exports = {
8
8
  MessageHandler,
@@ -0,0 +1,8 @@
1
+ export {
2
+ instantiateNapiModuleSync,
3
+ instantiateNapiModule,
4
+ MessageHandler,
5
+ } from '@emnapi/core'
6
+ export { getDefaultContext } from '@emnapi/runtime'
7
+ export * from '@tybys/wasm-util'
8
+ export { createOnMessage, createFsProxy } from './fs-proxy.js'
@@ -111,6 +111,11 @@ export declare interface FileHandle {
111
111
  close(): Promise<void>;
112
112
  }
113
113
 
114
+ /** @public */
115
+ export declare interface FinalizeBindingsOptions {
116
+ memory?: WebAssembly.Memory;
117
+ }
118
+
114
119
  /** @public */
115
120
  export declare interface IDirent {
116
121
  isFile(): boolean;
@@ -375,6 +380,7 @@ export declare class WASI {
375
380
  private [kBindingName];
376
381
  readonly wasiImport: Record<string, any>;
377
382
  constructor(options?: SyncWASIOptions);
383
+ finalizeBindings(instance: WebAssembly.Instance, { memory }?: FinalizeBindingsOptions): void;
378
384
  start(instance: WebAssembly.Instance): number | undefined | Promise<number> | Promise<undefined>;
379
385
  initialize(instance: WebAssembly.Instance): void | Promise<void>;
380
386
  getImportObject(): Record<string, Record<string, any>>;
@@ -1075,26 +1075,43 @@ function defineName(name, f) {
1075
1075
  Object.defineProperty(f, 'name', { value: name });
1076
1076
  return f;
1077
1077
  }
1078
+ function tryCall(f, wasi, args) {
1079
+ let r;
1080
+ try {
1081
+ r = f.apply(wasi, args);
1082
+ }
1083
+ catch (err) {
1084
+ return handleError(err);
1085
+ }
1086
+ if (isPromiseLike(r)) {
1087
+ return r.then(_ => _, handleError);
1088
+ }
1089
+ return r;
1090
+ }
1078
1091
  function syscallWrap(self, name, f) {
1079
- return defineName(name, function () {
1080
- if (process.env.NODE_DEBUG_NATIVE === 'wasi') {
1092
+ let debug = false;
1093
+ const NODE_DEBUG_NATIVE = (() => {
1094
+ try {
1095
+ return process.env.NODE_DEBUG_NATIVE;
1096
+ }
1097
+ catch (_) {
1098
+ return undefined;
1099
+ }
1100
+ })();
1101
+ if (typeof NODE_DEBUG_NATIVE === 'string' && NODE_DEBUG_NATIVE.split(',').includes('wasi')) {
1102
+ debug = true;
1103
+ }
1104
+ return debug
1105
+ ? defineName(name, function () {
1081
1106
  const args = Array.prototype.slice.call(arguments);
1082
1107
  let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
1083
1108
  debugArgs = debugArgs.concat(args);
1084
1109
  console.debug.apply(console, debugArgs);
1085
- }
1086
- let r;
1087
- try {
1088
- r = f.apply(self, arguments);
1089
- }
1090
- catch (err) {
1091
- return handleError(err);
1092
- }
1093
- if (isPromiseLike(r)) {
1094
- return r.then(_ => _, handleError);
1095
- }
1096
- return r;
1097
- });
1110
+ return tryCall(f, self, args);
1111
+ })
1112
+ : defineName(name, function () {
1113
+ return tryCall(f, self, arguments);
1114
+ });
1098
1115
  }
1099
1116
  function resolvePathSync(fs, fileDescriptor, path, flags) {
1100
1117
  let resolvedPath = resolve(fileDescriptor.realPath, path);
@@ -1240,7 +1257,7 @@ class WASI$1 {
1240
1257
  case 1 /* WasiClockid.MONOTONIC */:
1241
1258
  case 2 /* WasiClockid.PROCESS_CPUTIME_ID */:
1242
1259
  case 3 /* WasiClockid.THREAD_CPUTIME_ID */: {
1243
- const t = performance.now();
1260
+ const t = performance.now() / 1000;
1244
1261
  const s = Math.trunc(t);
1245
1262
  const ms = Math.floor((t - s) * 1000);
1246
1263
  const result = BigInt(s) * BigInt(1000000000) + BigInt(ms) * BigInt(1000000);
@@ -2258,7 +2275,9 @@ class WASI$1 {
2258
2275
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2259
2276
  const r = fs.openSync(resolved_path, flagsRes, 0o666);
2260
2277
  const filetype = wasi.fds.getFileTypeByFd(r);
2261
- if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2278
+ if ((filetype !== 3 /* WasiFileType.DIRECTORY */) &&
2279
+ ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 ||
2280
+ (resolved_path.endsWith('/')))) {
2262
2281
  return 54 /* WasiErrno.ENOTDIR */;
2263
2282
  }
2264
2283
  const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
@@ -2447,6 +2466,9 @@ class WASI$1 {
2447
2466
  const wasi = _wasi.get(this);
2448
2467
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2449
2468
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
2469
+ if (oldPath.length > 0 && oldPath[0] === '/') {
2470
+ return 63 /* WasiErrno.EPERM */;
2471
+ }
2450
2472
  let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
2451
2473
  newPath = resolve(fileDescriptor.realPath, newPath);
2452
2474
  const fs = getFs(this);
@@ -2555,12 +2577,6 @@ const kSetMemory = Symbol('kSetMemory');
2555
2577
  const kStarted = Symbol('kStarted');
2556
2578
  const kInstance = Symbol('kInstance');
2557
2579
  const kBindingName = Symbol('kBindingName');
2558
- function setupInstance(self, instance) {
2559
- validateObject(instance, 'instance');
2560
- validateObject(instance.exports, 'instance.exports');
2561
- self[kInstance] = instance;
2562
- self[kSetMemory](instance.exports.memory);
2563
- }
2564
2580
  function validateOptions(options) {
2565
2581
  var _a;
2566
2582
  validateObject(options, 'options');
@@ -2665,13 +2681,21 @@ class WASI {
2665
2681
  wrap.proc_exit = wasiReturnOnProcExit.bind(this);
2666
2682
  }
2667
2683
  }
2668
- // Must not export _initialize, must export _start
2669
- start(instance) {
2684
+ finalizeBindings(instance, _a) {
2685
+ var _b;
2686
+ var { memory = (_b = instance === null || instance === void 0 ? void 0 : instance.exports) === null || _b === void 0 ? void 0 : _b.memory } = _a === void 0 ? {} : _a;
2670
2687
  if (this[kStarted]) {
2671
2688
  throw new Error('WASI instance has already started');
2672
2689
  }
2690
+ validateObject(instance, 'instance');
2691
+ validateObject(instance.exports, 'instance.exports');
2692
+ this[kSetMemory](memory);
2693
+ this[kInstance] = instance;
2673
2694
  this[kStarted] = true;
2674
- setupInstance(this, instance);
2695
+ }
2696
+ // Must not export _initialize, must export _start
2697
+ start(instance) {
2698
+ this.finalizeBindings(instance);
2675
2699
  const { _start, _initialize } = this[kInstance].exports;
2676
2700
  validateFunction(_start, 'instance.exports._start');
2677
2701
  validateUndefined(_initialize, 'instance.exports._initialize');
@@ -2696,11 +2720,7 @@ class WASI {
2696
2720
  }
2697
2721
  // Must not export _start, may optionally export _initialize
2698
2722
  initialize(instance) {
2699
- if (this[kStarted]) {
2700
- throw new Error('WASI instance has already started');
2701
- }
2702
- this[kStarted] = true;
2703
- setupInstance(this, instance);
2723
+ this.finalizeBindings(instance);
2704
2724
  const { _start, _initialize } = this[kInstance].exports;
2705
2725
  validateUndefined(_start, 'instance.exports._start');
2706
2726
  if (_initialize !== undefined) {
@@ -1075,26 +1075,43 @@ function defineName(name, f) {
1075
1075
  Object.defineProperty(f, 'name', { value: name });
1076
1076
  return f;
1077
1077
  }
1078
+ function tryCall(f, wasi, args) {
1079
+ let r;
1080
+ try {
1081
+ r = f.apply(wasi, args);
1082
+ }
1083
+ catch (err) {
1084
+ return handleError(err);
1085
+ }
1086
+ if (isPromiseLike(r)) {
1087
+ return r.then(_ => _, handleError);
1088
+ }
1089
+ return r;
1090
+ }
1078
1091
  function syscallWrap(self, name, f) {
1079
- return defineName(name, function () {
1080
- {
1092
+ let debug = false;
1093
+ const NODE_DEBUG_NATIVE = (() => {
1094
+ try {
1095
+ return "wasi";
1096
+ }
1097
+ catch (_) {
1098
+ return undefined;
1099
+ }
1100
+ })();
1101
+ if (typeof NODE_DEBUG_NATIVE === 'string' && NODE_DEBUG_NATIVE.split(',').includes('wasi')) {
1102
+ debug = true;
1103
+ }
1104
+ return debug
1105
+ ? defineName(name, function () {
1081
1106
  const args = Array.prototype.slice.call(arguments);
1082
1107
  let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
1083
1108
  debugArgs = debugArgs.concat(args);
1084
1109
  console.debug.apply(console, debugArgs);
1085
- }
1086
- let r;
1087
- try {
1088
- r = f.apply(self, arguments);
1089
- }
1090
- catch (err) {
1091
- return handleError(err);
1092
- }
1093
- if (isPromiseLike(r)) {
1094
- return r.then(_ => _, handleError);
1095
- }
1096
- return r;
1097
- });
1110
+ return tryCall(f, self, args);
1111
+ })
1112
+ : defineName(name, function () {
1113
+ return tryCall(f, self, arguments);
1114
+ });
1098
1115
  }
1099
1116
  function resolvePathSync(fs, fileDescriptor, path, flags) {
1100
1117
  let resolvedPath = resolve(fileDescriptor.realPath, path);
@@ -1240,7 +1257,7 @@ class WASI$1 {
1240
1257
  case 1 /* WasiClockid.MONOTONIC */:
1241
1258
  case 2 /* WasiClockid.PROCESS_CPUTIME_ID */:
1242
1259
  case 3 /* WasiClockid.THREAD_CPUTIME_ID */: {
1243
- const t = performance.now();
1260
+ const t = performance.now() / 1000;
1244
1261
  const s = Math.trunc(t);
1245
1262
  const ms = Math.floor((t - s) * 1000);
1246
1263
  const result = BigInt(s) * BigInt(1000000000) + BigInt(ms) * BigInt(1000000);
@@ -2258,7 +2275,9 @@ class WASI$1 {
2258
2275
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2259
2276
  const r = fs.openSync(resolved_path, flagsRes, 0o666);
2260
2277
  const filetype = wasi.fds.getFileTypeByFd(r);
2261
- if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2278
+ if ((filetype !== 3 /* WasiFileType.DIRECTORY */) &&
2279
+ ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 ||
2280
+ (resolved_path.endsWith('/')))) {
2262
2281
  return 54 /* WasiErrno.ENOTDIR */;
2263
2282
  }
2264
2283
  const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
@@ -2447,6 +2466,9 @@ class WASI$1 {
2447
2466
  const wasi = _wasi.get(this);
2448
2467
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2449
2468
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
2469
+ if (oldPath.length > 0 && oldPath[0] === '/') {
2470
+ return 63 /* WasiErrno.EPERM */;
2471
+ }
2450
2472
  let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
2451
2473
  newPath = resolve(fileDescriptor.realPath, newPath);
2452
2474
  const fs = getFs(this);
@@ -2555,12 +2577,6 @@ const kSetMemory = Symbol('kSetMemory');
2555
2577
  const kStarted = Symbol('kStarted');
2556
2578
  const kInstance = Symbol('kInstance');
2557
2579
  const kBindingName = Symbol('kBindingName');
2558
- function setupInstance(self, instance) {
2559
- validateObject(instance, 'instance');
2560
- validateObject(instance.exports, 'instance.exports');
2561
- self[kInstance] = instance;
2562
- self[kSetMemory](instance.exports.memory);
2563
- }
2564
2580
  function validateOptions(options) {
2565
2581
  var _a;
2566
2582
  validateObject(options, 'options');
@@ -2665,13 +2681,21 @@ class WASI {
2665
2681
  wrap.proc_exit = wasiReturnOnProcExit.bind(this);
2666
2682
  }
2667
2683
  }
2668
- // Must not export _initialize, must export _start
2669
- start(instance) {
2684
+ finalizeBindings(instance, _a) {
2685
+ var _b;
2686
+ var { memory = (_b = instance === null || instance === void 0 ? void 0 : instance.exports) === null || _b === void 0 ? void 0 : _b.memory } = _a === void 0 ? {} : _a;
2670
2687
  if (this[kStarted]) {
2671
2688
  throw new Error('WASI instance has already started');
2672
2689
  }
2690
+ validateObject(instance, 'instance');
2691
+ validateObject(instance.exports, 'instance.exports');
2692
+ this[kSetMemory](memory);
2693
+ this[kInstance] = instance;
2673
2694
  this[kStarted] = true;
2674
- setupInstance(this, instance);
2695
+ }
2696
+ // Must not export _initialize, must export _start
2697
+ start(instance) {
2698
+ this.finalizeBindings(instance);
2675
2699
  const { _start, _initialize } = this[kInstance].exports;
2676
2700
  validateFunction(_start, 'instance.exports._start');
2677
2701
  validateUndefined(_initialize, 'instance.exports._initialize');
@@ -2696,11 +2720,7 @@ class WASI {
2696
2720
  }
2697
2721
  // Must not export _start, may optionally export _initialize
2698
2722
  initialize(instance) {
2699
- if (this[kStarted]) {
2700
- throw new Error('WASI instance has already started');
2701
- }
2702
- this[kStarted] = true;
2703
- setupInstance(this, instance);
2723
+ this.finalizeBindings(instance);
2704
2724
  const { _start, _initialize } = this[kInstance].exports;
2705
2725
  validateUndefined(_start, 'instance.exports._start');
2706
2726
  if (_initialize !== undefined) {