Skip to content

Commit fd6903a

Browse files
committed
Experiment with real /internal/shared dir
1 parent 4726892 commit fd6903a

File tree

5 files changed

+61
-24
lines changed

5 files changed

+61
-24
lines changed

packages/php-wasm/compile/php/phpwasm-emscripten-library.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
const LibraryExample = {
1111
// Emscripten dependencies:
1212
$PHPWASM__deps: ['$allocateUTF8OnStack'],
13-
$PHPWASM__postset: 'PHPWASM.init();',
13+
$PHPWASM__postset: 'PHPWASM.init(PHPLoader?.phpWasmInitOptions);',
1414

1515
// Functions not exposed to C but available in the generated
1616
// JavaScript library under the PHPWASM object:
@@ -28,7 +28,7 @@ const LibraryExample = {
2828
// emscripten_O_NDELAY |
2929
// emscripten_O_DIRECT |
3030
// emscripten_O_NOATIME
31-
init: function () {
31+
init: function (phpWasmInitOptions) {
3232
Module['ENV'] = Module['ENV'] || {};
3333
// Ensure a platform-level bin directory for a fallback `php` binary.
3434
Module['ENV']['PATH'] = [
@@ -42,9 +42,18 @@ const LibraryExample = {
4242
// stdout, stderr, and headers information are written for the JavaScript
4343
// code to read later on.
4444
FS.mkdir('/internal');
45-
// The files from the shared directory are shared between all the
45+
// The files from the shared directory are shared between all th
4646
// PHP processes managed by PHPProcessManager.
4747
FS.mkdir('/internal/shared');
48+
49+
if (phpWasmInitOptions?.nativeInternalDirPath) {
50+
FS.mount(
51+
FS.filesystems.NODEFS,
52+
{ root: phpWasmInitOptions.nativeInternalDirPath },
53+
'/internal/shared'
54+
);
55+
}
56+
4857
// The files from the preload directory are preloaded using the
4958
// auto_prepend_file php.ini directive.
5059
FS.mkdir('/internal/shared/preload');

packages/php-wasm/node/src/lib/load-runtime.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ type PHPLoaderOptionsForNode = PHPLoaderOptions & {
5656
* @param args - Arguments to the format string.
5757
*/
5858
trace?: (processId: number, format: string, ...args: any[]) => void;
59+
60+
/**
61+
* An optional object to pass to the PHP-WASM library's `init` function.
62+
*
63+
* phpWasmInitOptions.nativeInternalDirPath is used to mount a
64+
* real, native directory as the php-wasm /internal directory.
65+
*
66+
* @see https://github.com/php-wasm/php-wasm/blob/main/compile/php/phpwasm-emscripten-library.js#L100
67+
*/
68+
phpWasmInitOptions?: {
69+
nativeInternalDirPath?: string;
70+
};
5971
};
6072
};
6173

packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export class BlueprintsV1Handler {
5555

5656
async bootPrimaryWorker(
5757
phpPort: NodeMessagePort,
58-
fileLockManagerPort: NodeMessagePort
58+
fileLockManagerPort: NodeMessagePort,
59+
nativeInternalDirPath: string
5960
) {
6061
const compiledBlueprint = await this.compileInputBlueprint(
6162
this.args['additional-blueprint-steps'] || []
@@ -149,6 +150,7 @@ export class BlueprintsV1Handler {
149150
trace,
150151
internalCookieStore: this.args.internalCookieStore,
151152
withXdebug: this.args.xdebug,
153+
nativeInternalDirPath,
152154
});
153155

154156
if (
@@ -171,10 +173,12 @@ export class BlueprintsV1Handler {
171173
worker,
172174
fileLockManagerPort,
173175
firstProcessId,
176+
nativeInternalDirPath,
174177
}: {
175178
worker: SpawnedWorker;
176179
fileLockManagerPort: NodeMessagePort;
177180
firstProcessId: number;
181+
nativeInternalDirPath: string;
178182
}) {
179183
const additionalPlayground = consumeAPI<PlaygroundCliBlueprintV1Worker>(
180184
worker.phpPort
@@ -202,6 +206,7 @@ export class BlueprintsV1Handler {
202206
// will have a separate cookie store.
203207
internalCookieStore: this.args.internalCookieStore,
204208
withXdebug: this.args.xdebug,
209+
nativeInternalDirPath,
205210
});
206211
await additionalPlayground.isReady();
207212
return additionalPlayground;

packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export type WorkerBootOptions = {
4444
*/
4545
internalCookieStore?: boolean;
4646
withXdebug?: boolean;
47+
nativeInternalDirPath?: string;
4748
};
4849

4950
/**
@@ -117,6 +118,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker {
117118
trace,
118119
internalCookieStore,
119120
withXdebug,
121+
nativeInternalDirPath,
120122
}: WorkerBootOptions) {
121123
if (this.booted) {
122124
throw new Error('Playground already booted');
@@ -151,6 +153,9 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker {
151153
fileLockManager: this.fileLockManager!,
152154
processId,
153155
trace: trace ? tracePhpWasm : undefined,
156+
phpWasmInitOptions: {
157+
nativeInternalDirPath,
158+
},
154159
},
155160
followSymlinks,
156161
withXdebug,

packages/playground/cli/src/run-cli.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { resolveBlueprint } from './resolve-blueprint';
4848
import { BlueprintsV2Handler } from './blueprints-v2/blueprints-v2-handler';
4949
import { BlueprintsV1Handler } from './blueprints-v1/blueprints-v1-handler';
5050
import { startBridge } from '@php-wasm/xdebug-bridge';
51+
import { dir as tmpDir } from 'tmp-promise';
5152

5253
export async function parseOptionsAndRunCLI() {
5354
try {
@@ -444,10 +445,13 @@ export async function runCLI(args: RunCLIArgs): Promise<RunCLIServer> {
444445
fileLockManager
445446
);
446447

448+
const nativeInternalDirPath = (await tmpDir()).path;
449+
447450
// Boot the primary worker using the handler
448451
playground = await handler.bootPrimaryWorker(
449452
initialWorker.phpPort,
450-
fileLockManagerPort
453+
fileLockManagerPort,
454+
nativeInternalDirPath
451455
);
452456
playgroundsToCleanUp.push({
453457
playground,
@@ -489,17 +493,18 @@ export async function runCLI(args: RunCLIArgs): Promise<RunCLIServer> {
489493
) {
490494
logger.log(`Preparing additional workers...`);
491495

492-
// Save /internal directory from initial worker so we can replicate it
493-
// in each additional worker.
494-
const internalZip = await zipDirectory(
495-
playground,
496-
'/internal'
497-
);
496+
// // Save /internal directory from initial worker so we can replicate it
497+
// // in each additional worker.
498+
// const internalZip = await zipDirectory(
499+
// playground,
500+
// '/internal'
501+
// );
498502

499503
// Boot additional workers using the handler
500504
const initialWorkerProcessIdSpace = processIdSpaceLength;
501505
await Promise.all(
502506
additionalWorkers.map(async (worker, index) => {
507+
console.error('worker', index);
503508
const firstProcessId =
504509
initialWorkerProcessIdSpace +
505510
index * processIdSpaceLength;
@@ -512,26 +517,27 @@ export async function runCLI(args: RunCLIArgs): Promise<RunCLIServer> {
512517
worker,
513518
fileLockManagerPort,
514519
firstProcessId,
520+
nativeInternalDirPath,
515521
});
516522

517523
playgroundsToCleanUp.push({
518524
playground: additionalPlayground,
519525
worker: worker.worker,
520526
});
521527

522-
// Replicate the Blueprint-initialized /internal directory
523-
await additionalPlayground.writeFile(
524-
'/tmp/internal.zip',
525-
internalZip
526-
);
527-
await unzipFile(
528-
additionalPlayground,
529-
'/tmp/internal.zip',
530-
'/internal'
531-
);
532-
await additionalPlayground.unlink(
533-
'/tmp/internal.zip'
534-
);
528+
// // Replicate the Blueprint-initialized /internal directory
529+
// await additionalPlayground.writeFile(
530+
// '/tmp/internal.zip',
531+
// internalZip
532+
// );
533+
// await unzipFile(
534+
// additionalPlayground,
535+
// '/tmp/internal.zip',
536+
// '/internal'
537+
// );
538+
// await additionalPlayground.unlink(
539+
// '/tmp/internal.zip'
540+
// );
535541

536542
loadBalancer.addWorker(additionalPlayground);
537543
})

0 commit comments

Comments
 (0)