Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent Playground: Explore OPFS support #544

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"json"
],
"files.associations": {
"*.embeddedhtml": "html",
"**/frontmatter.json": "jsonc",
"**/.frontmatter/config/*.json": "jsonc",
"api-extractor-base.json": "jsonc",
"*.embeddedhtml": "html",
"__string": "c",
"atomic": "c",
"cstring": "c",
Expand All @@ -16,7 +18,8 @@
"array": "c",
"string": "c",
"string_view": "c",
"vector": "c"
"vector": "c",
"exception": "c"
},
"autoImportFileExcludePatterns": [
"@wp-playground/client"
Expand Down
2 changes: 2 additions & 0 deletions packages/php-wasm/node/public/php_7_4.js
Original file line number Diff line number Diff line change
Expand Up @@ -7220,6 +7220,8 @@ if (PHPLoader.debug && typeof Asyncify !== "undefined") {
return originalHandleSleep(startAsync);
}
}



return PHPLoader;

Expand Down
48 changes: 48 additions & 0 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,50 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
};

this.#wasmErrorsTarget = improveWASMErrorReporting(runtime);

this.writeFile(
'/wordpress/index2.php',
`<?php
require('/wordpress/wp-load.php');
echo '<pre>';

// Create a new file with fopen(), write some data, seek,
// write more data, close it, and verify the consistency
// of the written data.
$fp = fopen('test.txt', 'w');
fwrite($fp, '1234567890');
fseek($fp, 3);
fwrite($fp, 'abc');
fclose($fp);
echo file_get_contents('test.txt');
// 123abc7890int as expected

var_dump($wpdb->query('SELECT 1'));
// This works fine

var_dump(
$wpdb->insert(
'wp_posts',
array(
'post_title' => 'Test',
'post_content' => 'Hello world!',
'post_status' => 'publish',
'post_excerpt' => 'excerpt',
'to_ping' => 0,
'pinged' => 0,
'post_content_filtered' => 0,
'post_type' => 'post',
'post_mime_type' => 'text/plain',
'comment_status' => 'closed',
'ping_status' => 'closed',

)
)
);
var_dump($wpdb->last_error);
// General error: 10 disk I/O error.
`
);
}

/** @inheritDoc */
Expand Down Expand Up @@ -204,6 +248,10 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
);
}

console.log(
headersFilePath + ': ',
this.readFileAsText(headersFilePath)
);
const headersData = JSON.parse(this.readFileAsText(headersFilePath));
const headers: PHPResponse['headers'] = {};
for (const line of headersData.headers) {
Expand Down
47 changes: 45 additions & 2 deletions packages/php-wasm/universal/src/lib/load-php-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,38 @@ export async function loadPHPRuntime(
}
},
});
for (const { default: loadDataModule } of dataDependenciesModules) {
loadDataModule(PHPRuntime);

// @TODO: Find a good strategy for conditionally loading data dependencies
const root = PHPRuntime.FS.filesystems.OPFS.opfs.root;
let loaded = false;
try {
root.getFile('/wordpress/wp-config.php', {});
// loaded = true;
} catch (e) {
loaded = false;
}
if (!loaded) {
try {
root.getDirectory('/wordpress', {
create: true,
}).removeRecursively();
root.getDirectory('/wordpress', { create: true });
} catch (e) {
console.error(e);
}
for (const { default: loadDataModule } of dataDependenciesModules) {
try {
const result = PHPRuntime.locateFile(
'/wordpress/wp-config.php'
);
console.log({ result });
// throw new Error();
} catch (e) {
console.error(e);
console.log('Loading data module');
}
loadDataModule(PHPRuntime);
}
}
if (!dataDependenciesModules.length) {
resolveDepsReady();
Expand All @@ -164,6 +194,19 @@ export async function loadPHPRuntime(
await depsReady;
await phpReady;

// console.log(PHPRuntime.FS.readdir('/wordpress'));
// // console.log(PHPRuntime.FS.writeFile('/phpinfo.php', 'test'));
// // console.log(PHPRuntime.FS.readFile('/phpinfo.php'));
// // Sleep 100
// const z = new Promise((resolve) => setTimeout(resolve, 100))
// .then(() => {
// console.log("Reading file");
// console.log(PHPRuntime.FS.readFile('/wordpress/phpinfo.php'));
// });
// console.log(z);

// throw new Error('a');

loadedRuntimes.push(PHPRuntime);
return loadedRuntimes.length - 1;
}
Expand Down
Loading