@php-wasm/node 1.1.2 → 1.1.4

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 (40) hide show
  1. package/asyncify/7_2_34/php_7_2.wasm +0 -0
  2. package/asyncify/7_3_33/php_7_3.wasm +0 -0
  3. package/asyncify/7_4_33/php_7_4.wasm +0 -0
  4. package/asyncify/8_0_30/php_8_0.wasm +0 -0
  5. package/asyncify/8_1_23/php_8_1.wasm +0 -0
  6. package/asyncify/8_2_10/php_8_2.wasm +0 -0
  7. package/asyncify/8_3_0/php_8_3.wasm +0 -0
  8. package/asyncify/8_4_0/php_8_4.wasm +0 -0
  9. package/asyncify/php_7_2.js +351 -157
  10. package/asyncify/php_7_3.js +350 -156
  11. package/asyncify/php_7_4.js +349 -155
  12. package/asyncify/php_8_0.js +353 -159
  13. package/asyncify/php_8_1.js +352 -158
  14. package/asyncify/php_8_2.js +353 -159
  15. package/asyncify/php_8_3.js +353 -159
  16. package/asyncify/php_8_4.js +352 -158
  17. package/fs_ext.node +0 -0
  18. package/index.cjs +9896 -2782
  19. package/index.js +9780 -2782
  20. package/jspi/7_2_34/php_7_2.wasm +0 -0
  21. package/jspi/7_3_33/php_7_3.wasm +0 -0
  22. package/jspi/7_4_33/php_7_4.wasm +0 -0
  23. package/jspi/8_0_30/php_8_0.wasm +0 -0
  24. package/jspi/8_1_23/php_8_1.wasm +0 -0
  25. package/jspi/8_2_10/php_8_2.wasm +0 -0
  26. package/jspi/8_3_0/php_8_3.wasm +0 -0
  27. package/jspi/8_4_0/php_8_4.wasm +0 -0
  28. package/jspi/php_7_2.js +1130 -340
  29. package/jspi/php_7_3.js +1130 -340
  30. package/jspi/php_7_4.js +1130 -340
  31. package/jspi/php_8_0.js +1130 -340
  32. package/jspi/php_8_1.js +1129 -339
  33. package/jspi/php_8_2.js +1129 -339
  34. package/jspi/php_8_3.js +1129 -339
  35. package/jspi/php_8_4.js +1129 -339
  36. package/lib/file-lock-manager-for-node.d.ts +149 -0
  37. package/lib/file-lock-manager.d.ts +96 -0
  38. package/lib/index.d.ts +2 -0
  39. package/lib/load-runtime.d.ts +31 -2
  40. package/package.json +10 -9
package/jspi/php_8_1.js CHANGED
@@ -8,7 +8,7 @@ import path from 'path';
8
8
 
9
9
  const dependencyFilename = path.join(__dirname, '8_1_23', 'php_8_1.wasm');
10
10
  export { dependencyFilename };
11
- export const dependenciesTotalSize = 17182850;
11
+ export const dependenciesTotalSize = 17185468;
12
12
  export function init(RuntimeName, PHPLoader) {
13
13
  // The rest of the code comes from the built php.js file and esm-suffix.js
14
14
  // include: shell.js
@@ -5227,7 +5227,360 @@ export function init(RuntimeName, PHPLoader) {
5227
5227
 
5228
5228
  var syscallGetVarargP = syscallGetVarargI;
5229
5229
 
5230
- function ___syscall_fcntl64(fd, cmd, varargs) {
5230
+ var stringToUTF8 = (str, outPtr, maxBytesToWrite) =>
5231
+ stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);
5232
+
5233
+ Module['stringToUTF8'] = stringToUTF8;
5234
+
5235
+ var stackAlloc = (sz) => __emscripten_stack_alloc(sz);
5236
+
5237
+ /** @suppress {duplicate } */ var stringToUTF8OnStack = (str) => {
5238
+ var size = lengthBytesUTF8(str) + 1;
5239
+ var ret = stackAlloc(size);
5240
+ stringToUTF8(str, ret, size);
5241
+ return ret;
5242
+ };
5243
+
5244
+ var allocateUTF8OnStack = stringToUTF8OnStack;
5245
+
5246
+ var PHPWASM = {
5247
+ init: function () {
5248
+ // The /internal directory is required by the C module. It's where the
5249
+ // stdout, stderr, and headers information are written for the JavaScript
5250
+ // code to read later on.
5251
+ FS.mkdir('/internal');
5252
+ // The files from the shared directory are shared between all the
5253
+ // PHP processes managed by PHPProcessManager.
5254
+ FS.mkdir('/internal/shared');
5255
+ // The files from the preload directory are preloaded using the
5256
+ // auto_prepend_file php.ini directive.
5257
+ FS.mkdir('/internal/shared/preload');
5258
+ // Create stdout and stderr devices. We can't just use Emscripten's
5259
+ // default stdout and stderr devices because they stop processing data
5260
+ // on the first null byte. However, when dealing with binary data,
5261
+ // null bytes are valid and common.
5262
+ FS.registerDevice(FS.makedev(64, 0), {
5263
+ open: () => {},
5264
+ close: () => {},
5265
+ read: () => 0,
5266
+ write: (stream, buffer, offset, length, pos) => {
5267
+ const chunk = buffer.subarray(offset, offset + length);
5268
+ PHPWASM.onStdout(chunk);
5269
+ return length;
5270
+ },
5271
+ });
5272
+ FS.mkdev('/internal/stdout', FS.makedev(64, 0));
5273
+ FS.registerDevice(FS.makedev(63, 0), {
5274
+ open: () => {},
5275
+ close: () => {},
5276
+ read: () => 0,
5277
+ write: (stream, buffer, offset, length, pos) => {
5278
+ const chunk = buffer.subarray(offset, offset + length);
5279
+ PHPWASM.onStderr(chunk);
5280
+ return length;
5281
+ },
5282
+ });
5283
+ FS.mkdev('/internal/stderr', FS.makedev(63, 0));
5284
+ FS.registerDevice(FS.makedev(62, 0), {
5285
+ open: () => {},
5286
+ close: () => {},
5287
+ read: () => 0,
5288
+ write: (stream, buffer, offset, length, pos) => {
5289
+ const chunk = buffer.subarray(offset, offset + length);
5290
+ PHPWASM.onHeaders(chunk);
5291
+ return length;
5292
+ },
5293
+ });
5294
+ FS.mkdev('/internal/headers', FS.makedev(62, 0));
5295
+ // Handle events.
5296
+ PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
5297
+ ? require('events').EventEmitter
5298
+ : class EventEmitter {
5299
+ constructor() {
5300
+ this.listeners = {};
5301
+ }
5302
+ emit(eventName, data) {
5303
+ if (this.listeners[eventName]) {
5304
+ this.listeners[eventName].forEach(
5305
+ (callback) => {
5306
+ callback(data);
5307
+ }
5308
+ );
5309
+ }
5310
+ }
5311
+ once(eventName, callback) {
5312
+ const self = this;
5313
+ function removedCallback() {
5314
+ callback(...arguments);
5315
+ self.removeListener(eventName, removedCallback);
5316
+ }
5317
+ this.on(eventName, removedCallback);
5318
+ }
5319
+ removeAllListeners(eventName) {
5320
+ if (eventName) {
5321
+ delete this.listeners[eventName];
5322
+ } else {
5323
+ this.listeners = {};
5324
+ }
5325
+ }
5326
+ removeListener(eventName, callback) {
5327
+ if (this.listeners[eventName]) {
5328
+ const idx =
5329
+ this.listeners[eventName].indexOf(callback);
5330
+ if (idx !== -1) {
5331
+ this.listeners[eventName].splice(idx, 1);
5332
+ }
5333
+ }
5334
+ }
5335
+ };
5336
+ // Clean up the fd -> childProcess mapping when the fd is closed:
5337
+ const originalClose = FS.close;
5338
+ FS.close = function (stream) {
5339
+ originalClose(stream);
5340
+ delete PHPWASM.child_proc_by_fd[stream.fd];
5341
+ };
5342
+ PHPWASM.child_proc_by_fd = {};
5343
+ PHPWASM.child_proc_by_pid = {};
5344
+ PHPWASM.input_devices = {};
5345
+ const originalWrite = TTY.stream_ops.write;
5346
+ TTY.stream_ops.write = function (stream, ...rest) {
5347
+ const retval = originalWrite(stream, ...rest);
5348
+ // Implicit flush since PHP's fflush() doesn't seem to trigger the fsync event
5349
+ // @TODO: Fix this at the wasm level
5350
+ stream.tty.ops.fsync(stream.tty);
5351
+ return retval;
5352
+ };
5353
+ const originalPutChar = TTY.stream_ops.put_char;
5354
+ TTY.stream_ops.put_char = function (tty, val) {
5355
+ /**
5356
+ * Buffer newlines that Emscripten normally ignores.
5357
+ *
5358
+ * Emscripten doesn't do it by default because its default
5359
+ * print function is console.log that implicitly adds a newline. We are overwriting
5360
+ * it with an environment-specific function that outputs exaclty what it was given,
5361
+ * e.g. in Node.js it's process.stdout.write(). Therefore, we need to mak sure
5362
+ * all the newlines make it to the output buffer.
5363
+ */ if (val === 10) tty.output.push(val);
5364
+ return originalPutChar(tty, val);
5365
+ };
5366
+ },
5367
+ onHeaders: function (chunk) {
5368
+ if (Module['onHeaders']) {
5369
+ Module['onHeaders'](chunk);
5370
+ return;
5371
+ }
5372
+ console.log('headers', {
5373
+ chunk,
5374
+ });
5375
+ },
5376
+ onStdout: function (chunk) {
5377
+ if (Module['onStdout']) {
5378
+ Module['onStdout'](chunk);
5379
+ return;
5380
+ }
5381
+ if (ENVIRONMENT_IS_NODE) {
5382
+ process.stdout.write(chunk);
5383
+ } else {
5384
+ console.log('stdout', {
5385
+ chunk,
5386
+ });
5387
+ }
5388
+ },
5389
+ onStderr: function (chunk) {
5390
+ if (Module['onStderr']) {
5391
+ Module['onStderr'](chunk);
5392
+ return;
5393
+ }
5394
+ if (ENVIRONMENT_IS_NODE) {
5395
+ process.stderr.write(chunk);
5396
+ } else {
5397
+ console.warn('stderr', {
5398
+ chunk,
5399
+ });
5400
+ }
5401
+ },
5402
+ getAllWebSockets: function (sock) {
5403
+ const webSockets = new Set();
5404
+ if (sock.server) {
5405
+ sock.server.clients.forEach((ws) => {
5406
+ webSockets.add(ws);
5407
+ });
5408
+ }
5409
+ for (const peer of PHPWASM.getAllPeers(sock)) {
5410
+ webSockets.add(peer.socket);
5411
+ }
5412
+ return Array.from(webSockets);
5413
+ },
5414
+ getAllPeers: function (sock) {
5415
+ const peers = new Set();
5416
+ if (sock.server) {
5417
+ sock.pending
5418
+ .filter((pending) => pending.peers)
5419
+ .forEach((pending) => {
5420
+ for (const peer of Object.values(pending.peers)) {
5421
+ peers.add(peer);
5422
+ }
5423
+ });
5424
+ }
5425
+ if (sock.peers) {
5426
+ for (const peer of Object.values(sock.peers)) {
5427
+ peers.add(peer);
5428
+ }
5429
+ }
5430
+ return Array.from(peers);
5431
+ },
5432
+ awaitData: function (ws) {
5433
+ return PHPWASM.awaitEvent(ws, 'message');
5434
+ },
5435
+ awaitConnection: function (ws) {
5436
+ if (ws.OPEN === ws.readyState) {
5437
+ return [Promise.resolve(), PHPWASM.noop];
5438
+ }
5439
+ return PHPWASM.awaitEvent(ws, 'open');
5440
+ },
5441
+ awaitClose: function (ws) {
5442
+ if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) {
5443
+ return [Promise.resolve(), PHPWASM.noop];
5444
+ }
5445
+ return PHPWASM.awaitEvent(ws, 'close');
5446
+ },
5447
+ awaitError: function (ws) {
5448
+ if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) {
5449
+ return [Promise.resolve(), PHPWASM.noop];
5450
+ }
5451
+ return PHPWASM.awaitEvent(ws, 'error');
5452
+ },
5453
+ awaitEvent: function (ws, event) {
5454
+ let resolve;
5455
+ const listener = () => {
5456
+ resolve();
5457
+ };
5458
+ const promise = new Promise(function (_resolve) {
5459
+ resolve = _resolve;
5460
+ ws.once(event, listener);
5461
+ });
5462
+ const cancel = () => {
5463
+ ws.removeListener(event, listener);
5464
+ // Rejecting the promises bubbles up and kills the entire
5465
+ // node process. Let's resolve them on the next tick instead
5466
+ // to give the caller some space to unbind any handlers.
5467
+ setTimeout(resolve);
5468
+ };
5469
+ return [promise, cancel];
5470
+ },
5471
+ noop: function () {},
5472
+ spawnProcess: function (command, args, options) {
5473
+ if (Module['spawnProcess']) {
5474
+ const spawnedPromise = Module['spawnProcess'](
5475
+ command,
5476
+ args,
5477
+ options
5478
+ );
5479
+ return Promise.resolve(spawnedPromise).then(function (spawned) {
5480
+ if (!spawned || !spawned.on) {
5481
+ throw new Error(
5482
+ 'spawnProcess() must return an EventEmitter but returned a different type.'
5483
+ );
5484
+ }
5485
+ return spawned;
5486
+ });
5487
+ }
5488
+ if (ENVIRONMENT_IS_NODE) {
5489
+ return require('child_process').spawn(command, args, {
5490
+ ...options,
5491
+ shell: true,
5492
+ stdio: ['pipe', 'pipe', 'pipe'],
5493
+ timeout: 100,
5494
+ });
5495
+ }
5496
+ const e = new Error(
5497
+ 'popen(), proc_open() etc. are unsupported in the browser. Call php.setSpawnHandler() ' +
5498
+ 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' +
5499
+ 'and similar functions via php.ini.'
5500
+ );
5501
+ e.code = 'SPAWN_UNSUPPORTED';
5502
+ throw e;
5503
+ },
5504
+ shutdownSocket: function (socketd, how) {
5505
+ // This implementation only supports websockets at the moment
5506
+ const sock = getSocketFromFD(socketd);
5507
+ const peer = Object.values(sock.peers)[0];
5508
+ if (!peer) {
5509
+ return -1;
5510
+ }
5511
+ try {
5512
+ peer.socket.close();
5513
+ SOCKFS.websocket_sock_ops.removePeer(sock, peer);
5514
+ return 0;
5515
+ } catch (e) {
5516
+ console.log('Socket shutdown error', e);
5517
+ return -1;
5518
+ }
5519
+ },
5520
+ };
5521
+
5522
+ function _js_getpid() {
5523
+ return PHPLoader.processId ?? 42;
5524
+ }
5525
+
5526
+ function _js_wasm_trace(format, ...args) {
5527
+ if (PHPLoader.trace instanceof Function) {
5528
+ PHPLoader.trace(_js_getpid(), format, ...args);
5529
+ }
5530
+ }
5531
+
5532
+ function _fd_close(fd) {
5533
+ _js_wasm_trace('fd_close(%d)', fd);
5534
+ const [vfsPath, pathResolutionErrno] = locking.get_vfs_path_from_fd(fd);
5535
+ if (pathResolutionErrno !== 0) {
5536
+ _js_wasm_trace(
5537
+ 'fd_close(%d) get_vfs_path_from_fd error %d',
5538
+ fd,
5539
+ pathResolutionErrno
5540
+ );
5541
+ return -ERRNO_CODES.EBADF;
5542
+ }
5543
+ const result = _builtin_fd_close(fd);
5544
+ if (result === 0 && locking.maybeLockedFds.has(fd)) {
5545
+ const nativeFilePath =
5546
+ locking.get_native_path_from_vfs_path(vfsPath);
5547
+ return PHPLoader.fileLockManager
5548
+ .releaseLocksForProcessFd(
5549
+ PHPLoader.processId,
5550
+ fd,
5551
+ nativeFilePath
5552
+ )
5553
+ .then(() => {
5554
+ _js_wasm_trace('fd_close(%d) release locks success', fd);
5555
+ })
5556
+ .catch((e) => {
5557
+ _js_wasm_trace("fd_close(%d) error '%s'", fd, e);
5558
+ })
5559
+ .then(() => {
5560
+ _js_wasm_trace('fd_close(%d) result %d', fd, result);
5561
+ return result;
5562
+ })
5563
+ .finally(() => {
5564
+ locking.maybeLockedFds.delete(fd);
5565
+ });
5566
+ } else {
5567
+ _js_wasm_trace('fd_close(%d) result %d', fd, result);
5568
+ return result;
5569
+ }
5570
+ }
5571
+
5572
+ function _builtin_fd_close(fd) {
5573
+ try {
5574
+ var stream = SYSCALLS.getStreamFromFD(fd);
5575
+ FS.close(stream);
5576
+ return 0;
5577
+ } catch (e) {
5578
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
5579
+ return e.errno;
5580
+ }
5581
+ }
5582
+
5583
+ function _builtin_fcntl64(fd, cmd, varargs) {
5231
5584
  SYSCALLS.varargs = varargs;
5232
5585
  try {
5233
5586
  var stream = SYSCALLS.getStreamFromFD(fd);
@@ -5253,35 +5606,468 @@ export function init(RuntimeName, PHPLoader) {
5253
5606
  case 3:
5254
5607
  return stream.flags;
5255
5608
 
5256
- case 4: {
5257
- var arg = syscallGetVarargI();
5258
- stream.flags |= arg;
5259
- return 0;
5609
+ case 4: {
5610
+ var arg = syscallGetVarargI();
5611
+ stream.flags |= arg;
5612
+ return 0;
5613
+ }
5614
+
5615
+ case 12: {
5616
+ var arg = syscallGetVarargP();
5617
+ var offset = 0;
5618
+ // We're always unlocked.
5619
+ HEAP16[(arg + offset) >> 1] = 2;
5620
+ return 0;
5621
+ }
5622
+
5623
+ case 13:
5624
+ case 14:
5625
+ // Pretend that the locking is successful. These are process-level locks,
5626
+ // and Emscripten programs are a single process. If we supported linking a
5627
+ // filesystem between programs, we'd need to do more here.
5628
+ // See https://github.com/emscripten-core/emscripten/issues/23697
5629
+ return 0;
5630
+ }
5631
+ return -28;
5632
+ } catch (e) {
5633
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
5634
+ return -e.errno;
5635
+ }
5636
+ }
5637
+
5638
+ var locking = {
5639
+ maybeLockedFds: new Set(),
5640
+ F_RDLCK: 0,
5641
+ F_WRLCK: 1,
5642
+ F_UNLCK: 2,
5643
+ lockStateToFcntl: {
5644
+ shared: 0,
5645
+ exclusive: 1,
5646
+ unlocked: 2,
5647
+ },
5648
+ fcntlToLockState: {
5649
+ 0: 'shared',
5650
+ 1: 'exclusive',
5651
+ 2: 'unlocked',
5652
+ },
5653
+ is_shared_fs_node(node) {
5654
+ if (node?.isSharedFS) {
5655
+ return true;
5656
+ }
5657
+ // Handle PROXYFS nodes which wrap other nodes.
5658
+ if (!node?.mount?.opts?.fs?.lookupPath) {
5659
+ return false;
5660
+ }
5661
+ const vfsPath = NODEFS.realPath(node);
5662
+ const underlyingNode = node.mount.opts.fs.lookupPath(vfsPath)?.node;
5663
+ return !!underlyingNode?.isSharedFS;
5664
+ },
5665
+ is_path_to_shared_fs(path) {
5666
+ const { node } = FS.lookupPath(path);
5667
+ return locking.is_shared_fs_node(node);
5668
+ },
5669
+ get_fd_access_mode(fd) {
5670
+ const emscripten_F_GETFL = Number('3');
5671
+ const emscripten_O_ACCMODE = Number('2097155');
5672
+ return (
5673
+ _builtin_fcntl64(fd, emscripten_F_GETFL) & emscripten_O_ACCMODE
5674
+ );
5675
+ },
5676
+ get_vfs_path_from_fd(fd) {
5677
+ try {
5678
+ return [FS.readlink(`/proc/self/fd/${fd}`), 0];
5679
+ } catch (error) {
5680
+ return [null, ERRNO_CODES.EBADF];
5681
+ }
5682
+ },
5683
+ get_native_path_from_vfs_path(vfsPath) {
5684
+ const { node } = FS.lookupPath(vfsPath);
5685
+ return NODEFS.realPath(node);
5686
+ },
5687
+ check_lock_params(fd, l_type) {
5688
+ const emscripten_O_RDONLY = Number('0');
5689
+ const emscripten_O_WRONLY = Number('1');
5690
+ const accessMode = locking.get_fd_access_mode(fd);
5691
+ if (
5692
+ (l_type === locking.F_WRLCK &&
5693
+ accessMode === emscripten_O_RDONLY) ||
5694
+ (l_type === locking.F_RDLCK &&
5695
+ accessMode === emscripten_O_WRONLY)
5696
+ ) {
5697
+ return ERRNO_CODES.EBADF;
5698
+ }
5699
+ return 0;
5700
+ },
5701
+ };
5702
+
5703
+ async function ___syscall_fcntl64(fd, cmd, varargs) {
5704
+ // Necessary to use varargs accessor
5705
+ SYSCALLS.varargs = varargs;
5706
+ // These constants are replaced by Emscripten during the build process
5707
+ const emscripten_F_GETLK = Number('12');
5708
+ const emscripten_F_SETLK = Number('13');
5709
+ const emscripten_F_SETLKW = Number('14');
5710
+ const emscripten_SEEK_SET = Number('0');
5711
+ // NOTE: With the exception of l_type, these offsets are not exposed to
5712
+ // JS by Emscripten, so we hardcode them here.
5713
+ const emscripten_flock_l_type_offset = 0;
5714
+ const emscripten_flock_l_whence_offset = 2;
5715
+ const emscripten_flock_l_start_offset = 8;
5716
+ const emscripten_flock_l_len_offset = 16;
5717
+ const emscripten_flock_l_pid_offset = 24;
5718
+ /**
5719
+ * Read the flock struct at the given address.
5720
+ *
5721
+ * @param {bigint} flockStructAddress - the address of the flock struct
5722
+ * @returns the flock struct
5723
+ */ function read_flock_struct(flockStructAddress) {
5724
+ /*
5725
+ * NOTE: Since we are using HEAP<WORD_SIZE> vars like HEAP16 and HEAP64,
5726
+ * we need to adjust offsets to address the word size of each HEAP.
5727
+ *
5728
+ * For example, an offset of 64 bytes is the following for each HEAP:
5729
+ * - HEAP8: 64 (the 64th byte)
5730
+ * - HEAP16: 32 (the 32nd 16-bit word)
5731
+ * - HEAP32: 16 (the 16th 32-bit word)
5732
+ * - HEAP64: 8 (the 8th 64-bit word)
5733
+ *
5734
+ * We get a word offset by dividing the byte offset by the word size.
5735
+ */ return {
5736
+ l_type: HEAP16[ // Shift right by 1 to divide by 2^1.
5737
+ (flockStructAddress + emscripten_flock_l_type_offset) >> 1
5738
+ ],
5739
+ l_whence:
5740
+ HEAP16[ // Shift right by 1 to divide by 2^1.
5741
+ (flockStructAddress +
5742
+ emscripten_flock_l_whence_offset) >>
5743
+ 1
5744
+ ],
5745
+ l_start:
5746
+ HEAP64[ // Shift right by 3 to divide by 2^3.
5747
+ (flockStructAddress +
5748
+ emscripten_flock_l_start_offset) >>
5749
+ 3
5750
+ ],
5751
+ l_len: HEAP64[ // Shift right by 3 to divide by 2^3.
5752
+ (flockStructAddress + emscripten_flock_l_len_offset) >> 3
5753
+ ],
5754
+ l_pid: HEAP32[ // Shift right by 2 to divide by 2^2.
5755
+ (flockStructAddress + emscripten_flock_l_pid_offset) >> 2
5756
+ ],
5757
+ };
5758
+ }
5759
+ /**
5760
+ * Update the flock struct at the given address with the given fields.
5761
+ *
5762
+ * @param {bigint} flockStructAddress - the address of the flock struct
5763
+ * @param {object} fields - the fields to update
5764
+ */ function update_flock_struct(flockStructAddress, fields) {
5765
+ /*
5766
+ * NOTE: Since we are using HEAP<WORD_SIZE> vars like HEAP16 and HEAP64,
5767
+ * we need to adjust offsets to address the word size of each HEAP.
5768
+ *
5769
+ * For example, an offset of 64 bytes is the following for each HEAP:
5770
+ * - HEAP8: 64 (the 64th byte)
5771
+ * - HEAP16: 32 (the 32nd 16-bit word)
5772
+ * - HEAP32: 16 (the 16th 32-bit word)
5773
+ * - HEAP64: 8 (the 8th 64-bit word)
5774
+ *
5775
+ * We get a word offset by dividing the byte offset by the word size.
5776
+ */ if (fields.l_type !== undefined) {
5777
+ HEAP16[ // Shift right by 1 to divide by 2^1.
5778
+ (flockStructAddress + emscripten_flock_l_type_offset) >> 1
5779
+ ] = fields.l_type;
5780
+ }
5781
+ if (fields.l_whence !== undefined) {
5782
+ HEAP16[ // Shift right by 1 to divide by 2^1.
5783
+ (flockStructAddress + emscripten_flock_l_whence_offset) >> 1
5784
+ ] = fields.l_whence;
5785
+ }
5786
+ if (fields.l_start !== undefined) {
5787
+ HEAP64[ // Shift right by 3 to divide by 2^3.
5788
+ (flockStructAddress + emscripten_flock_l_start_offset) >> 3
5789
+ ] = fields.l_start;
5790
+ }
5791
+ if (fields.l_len !== undefined) {
5792
+ HEAP64[ // Shift right by 3 to divide by 2^3.
5793
+ (flockStructAddress + emscripten_flock_l_len_offset) >> 3
5794
+ ] = fields.l_len;
5795
+ }
5796
+ if (fields.l_pid !== undefined) {
5797
+ HEAP32[ // Shift right by 2 to divide by 2^2.
5798
+ (flockStructAddress + emscripten_flock_l_pid_offset) >> 2
5799
+ ] = fields.l_pid;
5800
+ }
5801
+ }
5802
+ /**
5803
+ * Resolve the base address of the range depending on the whence and start offset.
5804
+ *
5805
+ * @param {number} fd - the file descriptor
5806
+ * @param {number} whence - what the start offset is relative to
5807
+ * @param {bigint} startOffset - the offset from the whence
5808
+ * @returns The resolved offset and the errno. If there is an error,
5809
+ * the resolved offset is null, and the errno is non-zero.
5810
+ */ function get_base_address(fd, whence, startOffset) {
5811
+ let baseAddress;
5812
+ switch (whence) {
5813
+ case emscripten_SEEK_SET:
5814
+ baseAddress = 0n;
5815
+ break;
5816
+
5817
+ case emscripten_SEEK_CUR:
5818
+ baseAddress = FS.lseek(fd, 0, whence);
5819
+ break;
5820
+
5821
+ case emscripten_SEEK_END:
5822
+ baseAddress = _wasm_get_end_offset(fd);
5823
+ break;
5824
+
5825
+ default:
5826
+ return [null, ERRNO_CODES.EINVAL];
5827
+ }
5828
+ if (baseAddress == -1) {
5829
+ // We cannot resolve the offset within the file.
5830
+ // Let's treat this as a problem with the file descriptor.
5831
+ return [null, ERRNO_CODES.EBADF];
5832
+ }
5833
+ const resolvedOffset = baseAddress + startOffset;
5834
+ if (resolvedOffset < 0) {
5835
+ // This is not a valid offset. Report args as invalid.
5836
+ return [null, ERRNO_CODES.EINVAL];
5837
+ }
5838
+ return [resolvedOffset, 0];
5839
+ }
5840
+ const pid = PHPLoader.processId;
5841
+ switch (cmd) {
5842
+ case emscripten_F_GETLK: {
5843
+ _js_wasm_trace('fcntl(%d, F_GETLK)', fd);
5844
+ let vfsPath;
5845
+ let errno;
5846
+ [vfsPath, errno] = locking.get_vfs_path_from_fd(fd);
5847
+ if (errno !== 0) {
5848
+ _js_wasm_trace(
5849
+ 'fcntl(%d, F_GETLK) %s get_vfs_path_from_fd errno %d',
5850
+ fd,
5851
+ vfsPath,
5852
+ errno
5853
+ );
5854
+ return -ERRNO_CODES.EBADF;
5855
+ }
5856
+ if (!locking.is_path_to_shared_fs(vfsPath)) {
5857
+ _js_wasm_trace(
5858
+ "fcntl(%d, F_GETLK) locking is not implemented for non-NodeFS path '%s'",
5859
+ fd,
5860
+ vfsPath
5861
+ );
5862
+ // If not a NodeFS path, we can't lock it.
5863
+ // Default to succeeding as Emscripten does.
5864
+ update_flock_struct(flockStructAddr, {
5865
+ l_type: F_UNLCK,
5866
+ });
5867
+ return 0;
5868
+ }
5869
+ const flockStructAddr = syscallGetVarargP();
5870
+ const flockStruct = read_flock_struct(flockStructAddr);
5871
+ if (!(flockStruct.l_type in locking.fcntlToLockState)) {
5872
+ return -ERRNO_CODES.EINVAL;
5873
+ }
5874
+ errno = locking.check_lock_params(fd, flockStruct.l_type);
5875
+ if (errno !== 0) {
5876
+ _js_wasm_trace(
5877
+ 'fcntl(%d, F_GETLK) %s check_lock_params errno %d',
5878
+ fd,
5879
+ vfsPath,
5880
+ errno
5881
+ );
5882
+ return -ERRNO_CODES.EINVAL;
5883
+ }
5884
+ const requestedLockType =
5885
+ locking.fcntlToLockState[flockStruct.l_type];
5886
+ let absoluteStartOffset;
5887
+ [absoluteStartOffset, errno] = get_base_address(
5888
+ fd,
5889
+ flockStruct.l_whence,
5890
+ flockStruct.l_start
5891
+ );
5892
+ if (errno !== 0) {
5893
+ _js_wasm_trace(
5894
+ 'fcntl(%d, F_GETLK) %s get_base_address errno %d',
5895
+ fd,
5896
+ vfsPath,
5897
+ errno
5898
+ );
5899
+ return -ERRNO_CODES.EINVAL;
5900
+ }
5901
+ const nativeFilePath =
5902
+ locking.get_native_path_from_vfs_path(vfsPath);
5903
+ return PHPLoader.fileLockManager
5904
+ .findFirstConflictingByteRangeLock(nativeFilePath, {
5905
+ type: requestedLockType,
5906
+ start: absoluteStartOffset,
5907
+ end: absoluteStartOffset + flockStruct.l_len,
5908
+ pid,
5909
+ })
5910
+ .then((conflictingLock) => {
5911
+ if (conflictingLock === undefined) {
5912
+ _js_wasm_trace(
5913
+ 'fcntl(%d, F_GETLK) %s findFirstConflictingByteRangeLock type=unlocked start=0x%x end=0x%x',
5914
+ fd,
5915
+ vfsPath,
5916
+ absoluteStartOffset,
5917
+ absoluteStartOffset + flockStruct.l_len
5918
+ );
5919
+ update_flock_struct(flockStructAddr, {
5920
+ l_type: F_UNLCK,
5921
+ });
5922
+ return 0;
5923
+ }
5924
+ _js_wasm_trace(
5925
+ 'fcntl(%d, F_GETLK) %s findFirstConflictingByteRangeLock type=%s start=0x%x end=0x%x conflictingLock %d',
5926
+ fd,
5927
+ vfsPath,
5928
+ conflictingLock.type,
5929
+ conflictingLock.start,
5930
+ conflictingLock.end,
5931
+ conflictingLock.pid
5932
+ );
5933
+ const fcntlLockState =
5934
+ locking.lockStateToFcntl[conflictingLock.type];
5935
+ update_flock_struct(flockStructAddr, {
5936
+ l_type: fcntlLockState,
5937
+ l_whence: emscripten_SEEK_SET,
5938
+ l_start: conflictingLock.start,
5939
+ l_len: conflictingLock.end - conflictingLock.start,
5940
+ l_pid: conflictingLock.pid,
5941
+ });
5942
+ return 0;
5943
+ })
5944
+ .catch((e) => {
5945
+ _js_wasm_trace(
5946
+ 'fcntl(%d, F_GETLK) %s findFirstConflictingByteRangeLock error %s',
5947
+ fd,
5948
+ vfsPath,
5949
+ e
5950
+ );
5951
+ return -ERRNO_CODES.EINVAL;
5952
+ });
5953
+ }
5954
+
5955
+ case emscripten_F_SETLK: {
5956
+ _js_wasm_trace('fcntl(%d, F_SETLK)', fd);
5957
+ let vfsPath;
5958
+ let errno;
5959
+ [vfsPath, errno] = locking.get_vfs_path_from_fd(fd);
5960
+ if (errno !== 0) {
5961
+ _js_wasm_trace(
5962
+ 'fcntl(%d, F_SETLK) %s get_vfs_path_from_fd errno %d',
5963
+ fd,
5964
+ vfsPath,
5965
+ errno
5966
+ );
5967
+ return -errno;
5260
5968
  }
5261
-
5262
- case 12: {
5263
- var arg = syscallGetVarargP();
5264
- var offset = 0;
5265
- // We're always unlocked.
5266
- HEAP16[(arg + offset) >> 1] = 2;
5969
+ if (!locking.is_path_to_shared_fs(vfsPath)) {
5970
+ _js_wasm_trace(
5971
+ 'fcntl(%d, F_SETLK) locking is not implemented for non-NodeFS path %s',
5972
+ fd,
5973
+ vfsPath
5974
+ );
5975
+ // If not a NodeFS path, we can't lock it.
5976
+ // Default to succeeding as Emscripten does.
5267
5977
  return 0;
5268
5978
  }
5979
+ var flockStructAddr = syscallGetVarargP();
5980
+ const flockStruct = read_flock_struct(flockStructAddr);
5981
+ let absoluteStartOffset;
5982
+ [absoluteStartOffset, errno] = get_base_address(
5983
+ fd,
5984
+ flockStruct.l_whence,
5985
+ flockStruct.l_start
5986
+ );
5987
+ if (errno !== 0) {
5988
+ _js_wasm_trace(
5989
+ 'fcntl(%d, F_SETLK) %s get_base_address errno %d',
5990
+ fd,
5991
+ vfsPath,
5992
+ errno
5993
+ );
5994
+ return -errno;
5995
+ }
5996
+ if (!(flockStruct.l_type in locking.fcntlToLockState)) {
5997
+ _js_wasm_trace(
5998
+ 'fcntl(%d, F_SETLK) %s invalid lock type %d',
5999
+ fd,
6000
+ vfsPath,
6001
+ flockStruct.l_type
6002
+ );
6003
+ return -ERRNO_CODES.EINVAL;
6004
+ }
6005
+ errno = locking.check_lock_params(fd, flockStruct.l_type);
6006
+ if (errno !== 0) {
6007
+ _js_wasm_trace(
6008
+ 'fcntl(%d, F_SETLK) %s check_lock_params errno %d',
6009
+ fd,
6010
+ vfsPath,
6011
+ errno
6012
+ );
6013
+ return -errno;
6014
+ }
6015
+ locking.maybeLockedFds.add(fd);
6016
+ const requestedLockType =
6017
+ locking.fcntlToLockState[flockStruct.l_type];
6018
+ const rangeLock = {
6019
+ type: requestedLockType,
6020
+ start: absoluteStartOffset,
6021
+ end: absoluteStartOffset + flockStruct.l_len,
6022
+ pid,
6023
+ };
6024
+ const nativeFilePath =
6025
+ locking.get_native_path_from_vfs_path(vfsPath);
6026
+ _js_wasm_trace(
6027
+ 'fcntl(%d, F_SETLK) %s calling lockFileByteRange for range lock %s',
6028
+ fd,
6029
+ vfsPath,
6030
+ rangeLock
6031
+ );
6032
+ return PHPLoader.fileLockManager
6033
+ .lockFileByteRange(nativeFilePath, rangeLock)
6034
+ .then((succeeded) => {
6035
+ _js_wasm_trace(
6036
+ 'fcntl(%d, F_SETLK) %s lockFileByteRange returned %d for range lock %s',
6037
+ fd,
6038
+ vfsPath,
6039
+ succeeded,
6040
+ rangeLock
6041
+ );
6042
+ return succeeded ? 0 : -ERRNO_CODES.EAGAIN;
6043
+ })
6044
+ .catch((e) => {
6045
+ _js_wasm_trace(
6046
+ 'fcntl(%d, F_SETLK) %s lockFileByteRange error %s for range lock %s',
6047
+ fd,
6048
+ vfsPath,
6049
+ e,
6050
+ rangeLock
6051
+ );
6052
+ return -ERRNO_CODES.EINVAL;
6053
+ });
6054
+ }
5269
6055
 
5270
- case 13:
5271
- case 14:
5272
- // Pretend that the locking is successful. These are process-level locks,
5273
- // and Emscripten programs are a single process. If we supported linking a
5274
- // filesystem between programs, we'd need to do more here.
5275
- // See https://github.com/emscripten-core/emscripten/issues/23697
5276
- return 0;
6056
+ // @TODO: Implement waiting for lock
6057
+ case emscripten_F_SETLKW: {
6058
+ // We do not yet support the blocking form of flock().
6059
+ // We respond with EDEADLK to indicate failure
6060
+ // because it is a known errno for a failed F_SETLKW command.
6061
+ return -ERRNO_CODES.EDEADLK;
5277
6062
  }
5278
- return -28;
5279
- } catch (e) {
5280
- if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
5281
- return -e.errno;
6063
+
6064
+ default:
6065
+ return _builtin_fcntl64(fd, cmd, varargs);
5282
6066
  }
5283
6067
  }
5284
6068
 
6069
+ ___syscall_fcntl64.isAsync = true;
6070
+
5285
6071
  function ___syscall_fdatasync(fd) {
5286
6072
  try {
5287
6073
  var stream = SYSCALLS.getStreamFromFD(fd);
@@ -5313,11 +6099,6 @@ export function init(RuntimeName, PHPLoader) {
5313
6099
  }
5314
6100
  }
5315
6101
 
5316
- var stringToUTF8 = (str, outPtr, maxBytesToWrite) =>
5317
- stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);
5318
-
5319
- Module['stringToUTF8'] = stringToUTF8;
5320
-
5321
6102
  function ___syscall_getcwd(buf, size) {
5322
6103
  try {
5323
6104
  if (size === 0) return -28;
@@ -6577,17 +7358,6 @@ export function init(RuntimeName, PHPLoader) {
6577
7358
  return 0;
6578
7359
  };
6579
7360
 
6580
- function _fd_close(fd) {
6581
- try {
6582
- var stream = SYSCALLS.getStreamFromFD(fd);
6583
- FS.close(stream);
6584
- return 0;
6585
- } catch (e) {
6586
- if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
6587
- return e.errno;
6588
- }
6589
- }
6590
-
6591
7361
  function _fd_fdstat_get(fd, pbuf) {
6592
7362
  try {
6593
7363
  var rightsBase = 0;
@@ -6873,291 +7643,104 @@ export function init(RuntimeName, PHPLoader) {
6873
7643
  return 0;
6874
7644
  };
6875
7645
 
6876
- var _getcontext = () => abort('missing function: ${name}');
6877
-
6878
- var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => {
6879
- var info = readSockaddr(sa, salen);
6880
- if (info.errno) {
6881
- return -6;
6882
- }
6883
- var port = info.port;
6884
- var addr = info.addr;
6885
- var overflowed = false;
6886
- if (node && nodelen) {
6887
- var lookup;
6888
- if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) {
6889
- if (flags & 8) {
6890
- return -2;
6891
- }
6892
- } else {
6893
- addr = lookup;
6894
- }
6895
- var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen);
6896
- if (numBytesWrittenExclNull + 1 >= nodelen) {
6897
- overflowed = true;
6898
- }
6899
- }
6900
- if (serv && servlen) {
6901
- port = '' + port;
6902
- var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen);
6903
- if (numBytesWrittenExclNull + 1 >= servlen) {
6904
- overflowed = true;
6905
- }
6906
- }
6907
- if (overflowed) {
6908
- // Note: even when we overflow, getnameinfo() is specced to write out the truncated results.
6909
- return -12;
6910
- }
6911
- return 0;
6912
- };
6913
-
6914
- var Protocols = {
6915
- list: [],
6916
- map: {},
6917
- };
6918
-
6919
- var _setprotoent = (stayopen) => {
6920
- // void setprotoent(int stayopen);
6921
- // Allocate and populate a protoent structure given a name, protocol number and array of aliases
6922
- function allocprotoent(name, proto, aliases) {
6923
- // write name into buffer
6924
- var nameBuf = _malloc(name.length + 1);
6925
- stringToAscii(name, nameBuf);
6926
- // write aliases into buffer
6927
- var j = 0;
6928
- var length = aliases.length;
6929
- var aliasListBuf = _malloc((length + 1) * 4);
6930
- // Use length + 1 so we have space for the terminating NULL ptr.
6931
- for (var i = 0; i < length; i++, j += 4) {
6932
- var alias = aliases[i];
6933
- var aliasBuf = _malloc(alias.length + 1);
6934
- stringToAscii(alias, aliasBuf);
6935
- HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf;
6936
- }
6937
- HEAPU32[(aliasListBuf + j) >> 2] = 0;
6938
- // Terminating NULL pointer.
6939
- // generate protoent
6940
- var pe = _malloc(12);
6941
- HEAPU32[pe >> 2] = nameBuf;
6942
- HEAPU32[(pe + 4) >> 2] = aliasListBuf;
6943
- HEAP32[(pe + 8) >> 2] = proto;
6944
- return pe;
6945
- }
6946
- // Populate the protocol 'database'. The entries are limited to tcp and udp, though it is fairly trivial
6947
- // to add extra entries from /etc/protocols if desired - though not sure if that'd actually be useful.
6948
- var list = Protocols.list;
6949
- var map = Protocols.map;
6950
- if (list.length === 0) {
6951
- var entry = allocprotoent('tcp', 6, ['TCP']);
6952
- list.push(entry);
6953
- map['tcp'] = map['6'] = entry;
6954
- entry = allocprotoent('udp', 17, ['UDP']);
6955
- list.push(entry);
6956
- map['udp'] = map['17'] = entry;
6957
- }
6958
- _setprotoent.index = 0;
6959
- };
6960
-
6961
- var _getprotobyname = (name) => {
6962
- // struct protoent *getprotobyname(const char *);
6963
- name = UTF8ToString(name);
6964
- _setprotoent(true);
6965
- var result = Protocols.map[name];
6966
- return result;
6967
- };
6968
-
6969
- var _getprotobynumber = (number) => {
6970
- // struct protoent *getprotobynumber(int proto);
6971
- _setprotoent(true);
6972
- var result = Protocols.map[number];
6973
- return result;
6974
- };
6975
-
6976
- var stackAlloc = (sz) => __emscripten_stack_alloc(sz);
6977
-
6978
- /** @suppress {duplicate } */ var stringToUTF8OnStack = (str) => {
6979
- var size = lengthBytesUTF8(str) + 1;
6980
- var ret = stackAlloc(size);
6981
- stringToUTF8(str, ret, size);
6982
- return ret;
6983
- };
6984
-
6985
- var allocateUTF8OnStack = stringToUTF8OnStack;
6986
-
6987
- var PHPWASM = {
6988
- init: function () {
6989
- // The /internal directory is required by the C module. It's where the
6990
- // stdout, stderr, and headers information are written for the JavaScript
6991
- // code to read later on.
6992
- FS.mkdir('/internal');
6993
- // The files from the shared directory are shared between all the
6994
- // PHP processes managed by PHPProcessManager.
6995
- FS.mkdir('/internal/shared');
6996
- // The files from the preload directory are preloaded using the
6997
- // auto_prepend_file php.ini directive.
6998
- FS.mkdir('/internal/shared/preload');
6999
- PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
7000
- ? require('events').EventEmitter
7001
- : class EventEmitter {
7002
- constructor() {
7003
- this.listeners = {};
7004
- }
7005
- emit(eventName, data) {
7006
- if (this.listeners[eventName]) {
7007
- this.listeners[eventName].forEach(
7008
- (callback) => {
7009
- callback(data);
7010
- }
7011
- );
7012
- }
7013
- }
7014
- once(eventName, callback) {
7015
- const self = this;
7016
- function removedCallback() {
7017
- callback(...arguments);
7018
- self.removeListener(eventName, removedCallback);
7019
- }
7020
- this.on(eventName, removedCallback);
7021
- }
7022
- removeAllListeners(eventName) {
7023
- if (eventName) {
7024
- delete this.listeners[eventName];
7025
- } else {
7026
- this.listeners = {};
7027
- }
7028
- }
7029
- removeListener(eventName, callback) {
7030
- if (this.listeners[eventName]) {
7031
- const idx =
7032
- this.listeners[eventName].indexOf(callback);
7033
- if (idx !== -1) {
7034
- this.listeners[eventName].splice(idx, 1);
7035
- }
7036
- }
7037
- }
7038
- };
7039
- PHPWASM.child_proc_by_fd = {};
7040
- PHPWASM.child_proc_by_pid = {};
7041
- PHPWASM.input_devices = {};
7042
- },
7043
- getAllWebSockets: function (sock) {
7044
- const webSockets = new Set();
7045
- if (sock.server) {
7046
- sock.server.clients.forEach((ws) => {
7047
- webSockets.add(ws);
7048
- });
7049
- }
7050
- for (const peer of PHPWASM.getAllPeers(sock)) {
7051
- webSockets.add(peer.socket);
7052
- }
7053
- return Array.from(webSockets);
7054
- },
7055
- getAllPeers: function (sock) {
7056
- const peers = new Set();
7057
- if (sock.server) {
7058
- sock.pending
7059
- .filter((pending) => pending.peers)
7060
- .forEach((pending) => {
7061
- for (const peer of Object.values(pending.peers)) {
7062
- peers.add(peer);
7063
- }
7064
- });
7065
- }
7066
- if (sock.peers) {
7067
- for (const peer of Object.values(sock.peers)) {
7068
- peers.add(peer);
7646
+ var _getcontext = () => abort('missing function: ${name}');
7647
+
7648
+ var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => {
7649
+ var info = readSockaddr(sa, salen);
7650
+ if (info.errno) {
7651
+ return -6;
7652
+ }
7653
+ var port = info.port;
7654
+ var addr = info.addr;
7655
+ var overflowed = false;
7656
+ if (node && nodelen) {
7657
+ var lookup;
7658
+ if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) {
7659
+ if (flags & 8) {
7660
+ return -2;
7069
7661
  }
7662
+ } else {
7663
+ addr = lookup;
7070
7664
  }
7071
- return Array.from(peers);
7072
- },
7073
- awaitData: function (ws) {
7074
- return PHPWASM.awaitEvent(ws, 'message');
7075
- },
7076
- awaitConnection: function (ws) {
7077
- if (ws.OPEN === ws.readyState) {
7078
- return [Promise.resolve(), PHPWASM.noop];
7079
- }
7080
- return PHPWASM.awaitEvent(ws, 'open');
7081
- },
7082
- awaitClose: function (ws) {
7083
- if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) {
7084
- return [Promise.resolve(), PHPWASM.noop];
7085
- }
7086
- return PHPWASM.awaitEvent(ws, 'close');
7087
- },
7088
- awaitError: function (ws) {
7089
- if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) {
7090
- return [Promise.resolve(), PHPWASM.noop];
7091
- }
7092
- return PHPWASM.awaitEvent(ws, 'error');
7093
- },
7094
- awaitEvent: function (ws, event) {
7095
- let resolve;
7096
- const listener = () => {
7097
- resolve();
7098
- };
7099
- const promise = new Promise(function (_resolve) {
7100
- resolve = _resolve;
7101
- ws.once(event, listener);
7102
- });
7103
- const cancel = () => {
7104
- ws.removeListener(event, listener);
7105
- // Rejecting the promises bubbles up and kills the entire
7106
- // node process. Let's resolve them on the next tick instead
7107
- // to give the caller some space to unbind any handlers.
7108
- setTimeout(resolve);
7109
- };
7110
- return [promise, cancel];
7111
- },
7112
- noop: function () {},
7113
- spawnProcess: function (command, args, options) {
7114
- if (Module['spawnProcess']) {
7115
- const spawnedPromise = Module['spawnProcess'](
7116
- command,
7117
- args,
7118
- options
7119
- );
7120
- return Promise.resolve(spawnedPromise).then(function (spawned) {
7121
- if (!spawned || !spawned.on) {
7122
- throw new Error(
7123
- 'spawnProcess() must return an EventEmitter but returned a different type.'
7124
- );
7125
- }
7126
- return spawned;
7127
- });
7128
- }
7129
- if (ENVIRONMENT_IS_NODE) {
7130
- return require('child_process').spawn(command, args, {
7131
- ...options,
7132
- shell: true,
7133
- stdio: ['pipe', 'pipe', 'pipe'],
7134
- timeout: 100,
7135
- });
7665
+ var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen);
7666
+ if (numBytesWrittenExclNull + 1 >= nodelen) {
7667
+ overflowed = true;
7136
7668
  }
7137
- const e = new Error(
7138
- 'popen(), proc_open() etc. are unsupported in the browser. Call php.setSpawnHandler() ' +
7139
- 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' +
7140
- 'and similar functions via php.ini.'
7141
- );
7142
- e.code = 'SPAWN_UNSUPPORTED';
7143
- throw e;
7144
- },
7145
- shutdownSocket: function (socketd, how) {
7146
- // This implementation only supports websockets at the moment
7147
- const sock = getSocketFromFD(socketd);
7148
- const peer = Object.values(sock.peers)[0];
7149
- if (!peer) {
7150
- return -1;
7669
+ }
7670
+ if (serv && servlen) {
7671
+ port = '' + port;
7672
+ var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen);
7673
+ if (numBytesWrittenExclNull + 1 >= servlen) {
7674
+ overflowed = true;
7151
7675
  }
7152
- try {
7153
- peer.socket.close();
7154
- SOCKFS.websocket_sock_ops.removePeer(sock, peer);
7155
- return 0;
7156
- } catch (e) {
7157
- console.log('Socket shutdown error', e);
7158
- return -1;
7676
+ }
7677
+ if (overflowed) {
7678
+ // Note: even when we overflow, getnameinfo() is specced to write out the truncated results.
7679
+ return -12;
7680
+ }
7681
+ return 0;
7682
+ };
7683
+
7684
+ var Protocols = {
7685
+ list: [],
7686
+ map: {},
7687
+ };
7688
+
7689
+ var _setprotoent = (stayopen) => {
7690
+ // void setprotoent(int stayopen);
7691
+ // Allocate and populate a protoent structure given a name, protocol number and array of aliases
7692
+ function allocprotoent(name, proto, aliases) {
7693
+ // write name into buffer
7694
+ var nameBuf = _malloc(name.length + 1);
7695
+ stringToAscii(name, nameBuf);
7696
+ // write aliases into buffer
7697
+ var j = 0;
7698
+ var length = aliases.length;
7699
+ var aliasListBuf = _malloc((length + 1) * 4);
7700
+ // Use length + 1 so we have space for the terminating NULL ptr.
7701
+ for (var i = 0; i < length; i++, j += 4) {
7702
+ var alias = aliases[i];
7703
+ var aliasBuf = _malloc(alias.length + 1);
7704
+ stringToAscii(alias, aliasBuf);
7705
+ HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf;
7159
7706
  }
7160
- },
7707
+ HEAPU32[(aliasListBuf + j) >> 2] = 0;
7708
+ // Terminating NULL pointer.
7709
+ // generate protoent
7710
+ var pe = _malloc(12);
7711
+ HEAPU32[pe >> 2] = nameBuf;
7712
+ HEAPU32[(pe + 4) >> 2] = aliasListBuf;
7713
+ HEAP32[(pe + 8) >> 2] = proto;
7714
+ return pe;
7715
+ }
7716
+ // Populate the protocol 'database'. The entries are limited to tcp and udp, though it is fairly trivial
7717
+ // to add extra entries from /etc/protocols if desired - though not sure if that'd actually be useful.
7718
+ var list = Protocols.list;
7719
+ var map = Protocols.map;
7720
+ if (list.length === 0) {
7721
+ var entry = allocprotoent('tcp', 6, ['TCP']);
7722
+ list.push(entry);
7723
+ map['tcp'] = map['6'] = entry;
7724
+ entry = allocprotoent('udp', 17, ['UDP']);
7725
+ list.push(entry);
7726
+ map['udp'] = map['17'] = entry;
7727
+ }
7728
+ _setprotoent.index = 0;
7729
+ };
7730
+
7731
+ var _getprotobyname = (name) => {
7732
+ // struct protoent *getprotobyname(const char *);
7733
+ name = UTF8ToString(name);
7734
+ _setprotoent(true);
7735
+ var result = Protocols.map[name];
7736
+ return result;
7737
+ };
7738
+
7739
+ var _getprotobynumber = (number) => {
7740
+ // struct protoent *getprotobynumber(int proto);
7741
+ _setprotoent(true);
7742
+ var result = Protocols.map[number];
7743
+ return result;
7161
7744
  };
7162
7745
 
7163
7746
  function _js_create_input_device(deviceId) {
@@ -7195,6 +7778,98 @@ export function init(RuntimeName, PHPLoader) {
7195
7778
  return allocateUTF8OnStack(devicePath);
7196
7779
  }
7197
7780
 
7781
+ async function _js_flock(fd, op) {
7782
+ _js_wasm_trace('js_flock(%d, %d)', fd, op);
7783
+ // Emscripten does not expose these constants to JS, so we hardcode them here.
7784
+ // Based on
7785
+ // https://github.com/emscripten-core/emscripten/blob/76860cc47cef67f5712a7a03a247bc1baabf7ba4/system/lib/libc/musl/include/sys/file.h#L7-L10
7786
+ const emscripten_LOCK_SH = 1;
7787
+ const emscripten_LOCK_EX = 2;
7788
+ const emscripten_LOCK_NB = 4;
7789
+ const emscripten_LOCK_UN = 8;
7790
+ const flockToLockOpType = {
7791
+ [emscripten_LOCK_SH]: 'shared',
7792
+ [emscripten_LOCK_EX]: 'exclusive',
7793
+ [emscripten_LOCK_UN]: 'unlocked',
7794
+ };
7795
+ let vfsPath;
7796
+ let errno;
7797
+ [vfsPath, errno] = locking.get_vfs_path_from_fd(fd);
7798
+ if (errno !== 0) {
7799
+ _js_wasm_trace(
7800
+ 'js_flock(%d, %d) get_vfs_path_from_fd errno %d',
7801
+ fd,
7802
+ op,
7803
+ vfsPath,
7804
+ errno
7805
+ );
7806
+ return -errno;
7807
+ }
7808
+ if (!locking.is_path_to_shared_fs(vfsPath)) {
7809
+ _js_wasm_trace(
7810
+ 'flock(%d, %d) locking is not implemented for non-NodeFS path %s',
7811
+ fd,
7812
+ op,
7813
+ vfsPath
7814
+ );
7815
+ // If not a NodeFS path, we can't lock it.
7816
+ // Default to succeeding as Emscripten does.
7817
+ return 0;
7818
+ }
7819
+ errno = locking.check_lock_params(fd, op);
7820
+ if (errno !== 0) {
7821
+ _js_wasm_trace(
7822
+ 'js_flock(%d, %d) check_lock_params errno %d',
7823
+ fd,
7824
+ op,
7825
+ errno
7826
+ );
7827
+ return -errno;
7828
+ }
7829
+ // @TODO: Consider supporting blocking mode of flock()
7830
+ if (op & (emscripten_LOCK_NB === 0)) {
7831
+ _js_wasm_trace(
7832
+ 'js_flock(%d, %d) blocking mode of flock() is not implemented',
7833
+ fd,
7834
+ op
7835
+ );
7836
+ // We do not yet support the blocking form of flock().
7837
+ // We respond with EINVAL to indicate failure
7838
+ // because it is a known errno for a failed blocking flock().
7839
+ return -ERRNO_CODES.EINVAL;
7840
+ }
7841
+ const maskedOp =
7842
+ op & (emscripten_LOCK_SH | emscripten_LOCK_EX | emscripten_LOCK_UN);
7843
+ const lockOpType = flockToLockOpType[maskedOp];
7844
+ if (lockOpType === undefined) {
7845
+ _js_wasm_trace(
7846
+ 'js_flock(%d, %d) invalid flock() operation',
7847
+ fd,
7848
+ op
7849
+ );
7850
+ return -ERRNO_CODES.EINVAL;
7851
+ }
7852
+ const nativeFilePath = locking.get_native_path_from_vfs_path(vfsPath);
7853
+ const obtainedLock = await PHPLoader.fileLockManager.lockWholeFile(
7854
+ nativeFilePath,
7855
+ {
7856
+ type: lockOpType,
7857
+ pid: PHPLoader.processId,
7858
+ fd,
7859
+ }
7860
+ );
7861
+ _js_wasm_trace(
7862
+ 'js_flock(%d, %d) lockWholeFile %s returned %d',
7863
+ fd,
7864
+ op,
7865
+ vfsPath,
7866
+ obtainedLock
7867
+ );
7868
+ return obtainedLock ? 0 : -ERRNO_CODES.EWOULDBLOCK;
7869
+ }
7870
+
7871
+ _js_flock.isAsync = true;
7872
+
7198
7873
  function _js_open_process(
7199
7874
  command,
7200
7875
  argsPtr,
@@ -7220,7 +7895,7 @@ export function init(RuntimeName, PHPLoader) {
7220
7895
  argsArray.push(UTF8ToString(HEAPU32[charPointer >> 2]));
7221
7896
  }
7222
7897
  }
7223
- const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : null;
7898
+ const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : FS.cwd();
7224
7899
  let envObject = null;
7225
7900
  if (envLength) {
7226
7901
  envObject = {};
@@ -7293,6 +7968,15 @@ export function init(RuntimeName, PHPLoader) {
7293
7968
  PHPWASM.child_proc_by_fd[ProcInfo.stderrParentFd] = ProcInfo;
7294
7969
  PHPWASM.child_proc_by_pid[ProcInfo.pid] = ProcInfo;
7295
7970
  cp.on('exit', function (code) {
7971
+ for (const fd of [
7972
+ // The child process exited. Let's clean up its output streams:
7973
+ ProcInfo.stdoutChildFd,
7974
+ ProcInfo.stderrChildFd,
7975
+ ]) {
7976
+ if (FS.streams[fd] && !FS.isClosed(FS.streams[fd])) {
7977
+ FS.close(FS.streams[fd]);
7978
+ }
7979
+ }
7296
7980
  ProcInfo.exitCode = code;
7297
7981
  ProcInfo.exited = true;
7298
7982
  // Emit events for the wasm_poll_socket function.
@@ -7343,12 +8027,52 @@ export function init(RuntimeName, PHPLoader) {
7343
8027
  * listen to the 'exit' event.
7344
8028
  */ try {
7345
8029
  await new Promise((resolve, reject) => {
7346
- cp.on('spawn', resolve);
7347
- cp.on('error', reject);
8030
+ /**
8031
+ * There was no `await` between the `spawnProcess` call
8032
+ * and the `await` below so the process haven't had a chance
8033
+ * to run any of the exit-related callbacks yet.
8034
+ *
8035
+ * Good.
8036
+ *
8037
+ * Let's listen to all the lifecycle events and resolve
8038
+ * the promise when the process starts or immediately crashes.
8039
+ */ let resolved = false;
8040
+ cp.on('spawn', () => {
8041
+ if (resolved) return;
8042
+ resolved = true;
8043
+ resolve();
8044
+ });
8045
+ cp.on('error', (e) => {
8046
+ if (resolved) return;
8047
+ resolved = true;
8048
+ reject(e);
8049
+ });
8050
+ cp.on('exit', function (code) {
8051
+ if (resolved) return;
8052
+ resolved = true;
8053
+ if (code === 0) {
8054
+ resolve();
8055
+ } else {
8056
+ reject(
8057
+ new Error(`Process exited with code ${code}`)
8058
+ );
8059
+ }
8060
+ });
8061
+ /**
8062
+ * If the process haven't even started after 5 seconds, something
8063
+ * is wrong. Perhaps we're missing an event listener, or perhaps
8064
+ * the `spawnProcess` implementation failed to dispatch the relevant
8065
+ * event. Either way, let's crash to avoid blocking the proc_open()
8066
+ * call indefinitely.
8067
+ */ setTimeout(() => {
8068
+ if (resolved) return;
8069
+ resolved = true;
8070
+ reject(new Error('Process timed out'));
8071
+ }, 5e3);
7348
8072
  });
7349
8073
  } catch (e) {
7350
8074
  console.error(e);
7351
- wakeUp(1);
8075
+ wakeUp(ProcInfo.pid);
7352
8076
  return;
7353
8077
  }
7354
8078
  // Now we want to pass data from the STDIN source supplied by PHP
@@ -7418,6 +8142,21 @@ export function init(RuntimeName, PHPLoader) {
7418
8142
  return 0;
7419
8143
  }
7420
8144
 
8145
+ var _js_release_file_locks = async function js_release_file_locks() {
8146
+ _js_wasm_trace('js_release_file_locks()');
8147
+ const pid = PHPLoader.processId;
8148
+ return await PHPLoader.fileLockManager
8149
+ .releaseLocksForProcess(pid)
8150
+ .then(() => {
8151
+ _js_wasm_trace('js_release_file_locks succeeded');
8152
+ })
8153
+ .catch((e) => {
8154
+ _js_wasm_trace('js_release_file_locks error %s', e);
8155
+ });
8156
+ };
8157
+
8158
+ _js_release_file_locks.isAsync = true;
8159
+
7421
8160
  function _js_waitpid(pid, exitCodePtr) {
7422
8161
  if (!PHPWASM.child_proc_by_pid[pid]) {
7423
8162
  return -1;
@@ -8062,14 +8801,7 @@ export function init(RuntimeName, PHPLoader) {
8062
8801
  const POLLNVAL = 32;
8063
8802
  return returnCallback((wakeUp) => {
8064
8803
  const polls = [];
8065
- if (socketd in PHPWASM.child_proc_by_fd) {
8066
- const procInfo = PHPWASM.child_proc_by_fd[socketd];
8067
- if (procInfo.exited) {
8068
- wakeUp(0);
8069
- return;
8070
- }
8071
- polls.push(PHPWASM.awaitEvent(procInfo.stdout, 'data'));
8072
- } else if (FS.isSocket(FS.getStream(socketd)?.node.mode)) {
8804
+ if (FS.isSocket(FS.getStream(socketd)?.node.mode)) {
8073
8805
  const sock = getSocketFromFD(socketd);
8074
8806
  if (!sock) {
8075
8807
  wakeUp(0);
@@ -8103,7 +8835,12 @@ export function init(RuntimeName, PHPLoader) {
8103
8835
  polls.push(PHPWASM.awaitConnection(ws));
8104
8836
  lookingFor.add('POLLOUT');
8105
8837
  }
8106
- if (events & POLLHUP) {
8838
+ if (
8839
+ events & POLLHUP ||
8840
+ events & POLLIN ||
8841
+ events & POLLOUT ||
8842
+ events & POLLERR
8843
+ ) {
8107
8844
  polls.push(PHPWASM.awaitClose(ws));
8108
8845
  lookingFor.add('POLLHUP');
8109
8846
  }
@@ -8112,6 +8849,13 @@ export function init(RuntimeName, PHPLoader) {
8112
8849
  lookingFor.add('POLLERR');
8113
8850
  }
8114
8851
  }
8852
+ } else if (socketd in PHPWASM.child_proc_by_fd) {
8853
+ const procInfo = PHPWASM.child_proc_by_fd[socketd];
8854
+ if (procInfo.exited) {
8855
+ wakeUp(0);
8856
+ return;
8857
+ }
8858
+ polls.push(PHPWASM.awaitEvent(procInfo.stdout, 'data'));
8115
8859
  } else {
8116
8860
  setTimeout(function () {
8117
8861
  wakeUp(1);
@@ -8345,9 +9089,13 @@ export function init(RuntimeName, PHPLoader) {
8345
9089
  /** @export */ getprotobyname: _getprotobyname,
8346
9090
  /** @export */ getprotobynumber: _getprotobynumber,
8347
9091
  /** @export */ js_create_input_device: _js_create_input_device,
9092
+ /** @export */ js_flock: _js_flock,
9093
+ /** @export */ js_getpid: _js_getpid,
8348
9094
  /** @export */ js_open_process: _js_open_process,
8349
9095
  /** @export */ js_process_status: _js_process_status,
9096
+ /** @export */ js_release_file_locks: _js_release_file_locks,
8350
9097
  /** @export */ js_waitpid: _js_waitpid,
9098
+ /** @export */ js_wasm_trace: _js_wasm_trace,
8351
9099
  /** @export */ makecontext: _makecontext,
8352
9100
  /** @export */ proc_exit: _proc_exit,
8353
9101
  /** @export */ strptime: _strptime,
@@ -8375,6 +9123,12 @@ export function init(RuntimeName, PHPLoader) {
8375
9123
 
8376
9124
  var _fflush = (a0) => (_fflush = wasmExports['fflush'])(a0);
8377
9125
 
9126
+ var _flock = (Module['_flock'] = (a0, a1) =>
9127
+ (_flock = Module['_flock'] = wasmExports['flock'])(a0, a1));
9128
+
9129
+ var _getpid = (Module['_getpid'] = () =>
9130
+ (_getpid = Module['_getpid'] = wasmExports['getpid'])());
9131
+
8378
9132
  var _wasm_popen = (Module['_wasm_popen'] = (a0, a1) =>
8379
9133
  (_wasm_popen = Module['_wasm_popen'] = wasmExports['wasm_popen'])(
8380
9134
  a0,
@@ -8402,13 +9156,6 @@ export function init(RuntimeName, PHPLoader) {
8402
9156
  (___wrap_select = Module['___wrap_select'] =
8403
9157
  wasmExports['__wrap_select'])(a0, a1, a2, a3, a4));
8404
9158
 
8405
- var _wasm_add_cli_arg = (Module['_wasm_add_cli_arg'] = (a0) =>
8406
- (_wasm_add_cli_arg = Module['_wasm_add_cli_arg'] =
8407
- wasmExports['wasm_add_cli_arg'])(a0));
8408
-
8409
- var _run_cli = (Module['_run_cli'] = () =>
8410
- (_run_cli = Module['_run_cli'] = wasmExports['run_cli'])());
8411
-
8412
9159
  var _wasm_set_sapi_name = (Module['_wasm_set_sapi_name'] = (a0) =>
8413
9160
  (_wasm_set_sapi_name = Module['_wasm_set_sapi_name'] =
8414
9161
  wasmExports['wasm_set_sapi_name'])(a0));
@@ -8417,6 +9164,13 @@ export function init(RuntimeName, PHPLoader) {
8417
9164
  (_wasm_set_phpini_path = Module['_wasm_set_phpini_path'] =
8418
9165
  wasmExports['wasm_set_phpini_path'])(a0));
8419
9166
 
9167
+ var _wasm_add_cli_arg = (Module['_wasm_add_cli_arg'] = (a0) =>
9168
+ (_wasm_add_cli_arg = Module['_wasm_add_cli_arg'] =
9169
+ wasmExports['wasm_add_cli_arg'])(a0));
9170
+
9171
+ var _run_cli = (Module['_run_cli'] = () =>
9172
+ (_run_cli = Module['_run_cli'] = wasmExports['run_cli'])());
9173
+
8420
9174
  var _wasm_add_SERVER_entry = (Module['_wasm_add_SERVER_entry'] = (a0, a1) =>
8421
9175
  (_wasm_add_SERVER_entry = Module['_wasm_add_SERVER_entry'] =
8422
9176
  wasmExports['wasm_add_SERVER_entry'])(a0, a1));
@@ -8489,6 +9243,16 @@ export function init(RuntimeName, PHPLoader) {
8489
9243
  var _wasm_free = (Module['_wasm_free'] = (a0) =>
8490
9244
  (_wasm_free = Module['_wasm_free'] = wasmExports['wasm_free'])(a0));
8491
9245
 
9246
+ var _wasm_get_end_offset = (Module['_wasm_get_end_offset'] = (a0) =>
9247
+ (_wasm_get_end_offset = Module['_wasm_get_end_offset'] =
9248
+ wasmExports['wasm_get_end_offset'])(a0));
9249
+
9250
+ var _wasm_trace = (Module['_wasm_trace'] = (a0, a1) =>
9251
+ (_wasm_trace = Module['_wasm_trace'] = wasmExports['wasm_trace'])(
9252
+ a0,
9253
+ a1
9254
+ ));
9255
+
8492
9256
  var ___funcs_on_exit = () =>
8493
9257
  (___funcs_on_exit = wasmExports['__funcs_on_exit'])();
8494
9258
 
@@ -8636,6 +9400,32 @@ export function init(RuntimeName, PHPLoader) {
8636
9400
  PHPLoader['free'] =
8637
9401
  typeof _free === 'function' ? _free : PHPLoader['_wasm_free'];
8638
9402
 
9403
+ if (typeof NODEFS === 'object') {
9404
+ // We override NODEFS.createNode() to add an `isSharedFS` flag to all NODEFS
9405
+ // nodes. This way we can tell whether file-locking is needed and possible
9406
+ // for an FS node, even if wrapped with PROXYFS.
9407
+ const originalCreateNode = NODEFS.createNode;
9408
+ NODEFS.createNode = function createNodeWithSharedFlag() {
9409
+ const node = originalCreateNode.apply(NODEFS, arguments);
9410
+ node.isSharedFS = true;
9411
+ return node;
9412
+ };
9413
+
9414
+ var originalHashAddNode = FS.hashAddNode;
9415
+ FS.hashAddNode = function hashAddNodeIfNotSharedFS(node) {
9416
+ if (
9417
+ typeof locking === 'object' &&
9418
+ locking?.is_shared_fs_node(node)
9419
+ ) {
9420
+ // Avoid caching shared VFS nodes so multiple instances
9421
+ // can access the same underlying filesystem without
9422
+ // conflicting caches.
9423
+ return;
9424
+ }
9425
+ return originalHashAddNode.apply(FS, arguments);
9426
+ };
9427
+ }
9428
+
8639
9429
  return PHPLoader;
8640
9430
 
8641
9431
  // Close the opening bracket from esm-prefix.js: