Skip to content

Instantly share code, notes, and snippets.

@brandonpayton
Created March 22, 2024 17:08
Show Gist options
  • Save brandonpayton/e73ae3b261ab2b2302398abf5068d840 to your computer and use it in GitHub Desktop.
Save brandonpayton/e73ae3b261ab2b2302398abf5068d840 to your computer and use it in GitHub Desktop.
WP Playground Exercise
diff --git a/packages/php-wasm/compile/php/Dockerfile b/packages/php-wasm/compile/php/Dockerfile
index fdc458d0..c97f8606 100644
--- a/packages/php-wasm/compile/php/Dockerfile
+++ b/packages/php-wasm/compile/php/Dockerfile
@@ -809,20 +809,21 @@ RUN set -euxo pipefail; \
"_wasm_set_content_type", \n\
"_wasm_set_cookies", \n\
"_wasm_set_path_translated", \n\
"_wasm_set_php_code", \n\
"_wasm_set_query_string", \n\
"_wasm_set_request_body", \n\
"_wasm_set_request_host", \n\
"_wasm_set_request_method", \n\
"_wasm_set_request_port", \n\
"_wasm_set_request_uri", \n\
+"_wasm_return_this_number", \n\
"_wasm_set_skip_shebang" '"$(cat /root/.EXPORTED_FUNCTIONS)"']'; \
export OPTIMIZATION_FLAGS="-O3"; \
if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \
export OPTIMIZATION_FLAGS="-O0"; \
fi; \
emcc $OPTIMIZATION_FLAGS \
--js-library /root/phpwasm-emscripten-library.js \
-I . \
-I ext \
-I ext/json \
diff --git a/packages/php-wasm/compile/php/php_wasm.c b/packages/php-wasm/compile/php/php_wasm.c
index ff8d510c..9f3aaf42 100644
--- a/packages/php-wasm/compile/php/php_wasm.c
+++ b/packages/php-wasm/compile/php/php_wasm.c
@@ -824,20 +824,28 @@ void wasm_set_path_translated(char *path_translated)
/**
* Function: wasm_set_skip_shebang
* ----------------------------
*/
void wasm_set_skip_shebang(int should_skip_shebang)
{
wasm_server_context->skip_shebang = should_skip_shebang;
}
+/**
+ * Function: wasm_return_this_number
+*/
+int wasm_return_this_number(int number)
+{
+ return number;
+}
+
/**
* Function: wasm_set_request_uri
* ----------------------------
* Sets the request path (without the query string) for the next request.
*
* path_translated: the request path, e.g. "/index.php"
*/
void wasm_set_request_uri(char *request_uri)
{
wasm_server_context->request_uri = strdup(request_uri);
diff --git a/packages/php-wasm/universal/src/lib/base-php.ts b/packages/php-wasm/universal/src/lib/base-php.ts
index 03fdaa20..af00f557 100644
--- a/packages/php-wasm/universal/src/lib/base-php.ts
+++ b/packages/php-wasm/universal/src/lib/base-php.ts
@@ -575,20 +575,29 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
}
this.writeFile(
'/internal/consts.json',
JSON.stringify({
...consts,
[key]: value,
})
);
}
+ returnThisNumber(num: number): number {
+ return this[__private__dont__use].ccall(
+ 'wasm_return_this_number',
+ NUMBER,
+ [NUMBER],
+ [num]
+ );
+ }
+
#setPHPCode(code: string) {
this[__private__dont__use].ccall(
'wasm_set_php_code',
null,
[STRING],
[code]
);
}
async #handleRequest(): Promise<PHPResponse> {
diff --git a/packages/php-wasm/universal/src/lib/universal-php.ts b/packages/php-wasm/universal/src/lib/universal-php.ts
index dee30b2a..5d19f891 100644
--- a/packages/php-wasm/universal/src/lib/universal-php.ts
+++ b/packages/php-wasm/universal/src/lib/universal-php.ts
@@ -175,20 +175,21 @@ export interface RequestHandler {
absoluteUrl: string;
/**
* The directory in the PHP filesystem where the server will look
* for the files to serve. Default: `/var/www`.
*/
documentRoot: string;
}
export interface IsomorphicLocalPHP extends RequestHandler {
+ returnThisNumber(num: number): number;
/**
* Sets the SAPI name exposed by the PHP module.
* @param newName - The new SAPI name.
*/
setSapiName(newName: string): void;
/**
* Defines a constant in the PHP runtime.
* @param key - The name of the constant.
* @param value - The value of the constant.
diff --git a/packages/php-wasm/web/src/lib/web-php-endpoint.ts b/packages/php-wasm/web/src/lib/web-php-endpoint.ts
index 75d93684..0595e83d 100644
--- a/packages/php-wasm/web/src/lib/web-php-endpoint.ts
+++ b/packages/php-wasm/web/src/lib/web-php-endpoint.ts
@@ -176,20 +176,24 @@ export class WebPHPEndpoint implements IsomorphicLocalPHP {
/** @inheritDoc @php-wasm/web!WebPHP.onMessage */
onMessage(listener: MessageListener): void {
_private.get(this)!.php.onMessage(listener);
}
/** @inheritDoc @php-wasm/web!WebPHP.defineConstant */
defineConstant(key: string, value: string | boolean | number | null): void {
_private.get(this)!.php.defineConstant(key, value);
}
+ returnThisNumber(num: number): number {
+ return _private.get(this)!.php.returnThisNumber(num);
+ }
+
/** @inheritDoc @php-wasm/web!WebPHP.addEventListener */
addEventListener(
eventType: PHPEvent['type'],
listener: PHPEventListener
): void {
_private.get(this)!.php.addEventListener(eventType, listener);
}
/** @inheritDoc @php-wasm/web!WebPHP.removeEventListener */
removeEventListener(
diff --git a/packages/playground/blueprints/public/blueprint-schema.json b/packages/playground/blueprints/public/blueprint-schema.json
index 3668dc3e..b8023ed1 100644
--- a/packages/playground/blueprints/public/blueprint-schema.json
+++ b/packages/playground/blueprints/public/blueprint-schema.json
@@ -1,17 +1,21 @@
{
"$schema": "http://json-schema.org/schema",
"$ref": "#/definitions/Blueprint",
"definitions": {
"Blueprint": {
"type": "object",
"properties": {
+ "logThisNumber": {
+ "type": "number",
+ "description": "A number to pass through Wasm and then log to the console."
+ },
"landingPage": {
"type": "string",
"description": "The URL to navigate to after the blueprint has been run."
},
"description": {
"type": "string",
"description": "Optional description. It doesn't do anything but is exposed as a courtesy to developers who may want to document which blueprint file does what.",
"deprecated": "Use meta.description instead."
},
"meta": {
diff --git a/packages/playground/blueprints/src/lib/blueprint.ts b/packages/playground/blueprints/src/lib/blueprint.ts
index 72445cd8..1eeaf525 100644
--- a/packages/playground/blueprints/src/lib/blueprint.ts
+++ b/packages/playground/blueprints/src/lib/blueprint.ts
@@ -1,18 +1,19 @@
import {
SupportedPHPExtensionBundle,
SupportedPHPVersion,
} from '@php-wasm/universal';
import { StepDefinition } from './steps';
import { FileReference } from './resources';
export interface Blueprint {
+ logThisNumber?: number;
/**
* The URL to navigate to after the blueprint has been run.
*/
landingPage?: string;
/**
* Optional description. It doesn't do anything but is exposed as
* a courtesy to developers who may want to document which blueprint
* file does what.
*
* @deprecated Use meta.description instead.
diff --git a/packages/playground/blueprints/src/lib/compile.ts b/packages/playground/blueprints/src/lib/compile.ts
index f7a7d4a9..318d65d4 100644
--- a/packages/playground/blueprints/src/lib/compile.ts
+++ b/packages/playground/blueprints/src/lib/compile.ts
@@ -213,20 +213,26 @@ export function compileBlueprint(
},
phpExtensions: compilePHPExtensions(
[],
blueprint.phpExtensionBundles || []
),
features: {
// Disable networking by default
networking: blueprint.features?.networking ?? false,
},
run: async (playground: UniversalPHP) => {
+ if (blueprint.logThisNumber !== undefined) {
+ console.log(
+ `The number ${await playground.returnThisNumber(blueprint.logThisNumber)} was blessed by Wasm!`
+ );
+ }
try {
// Start resolving resources early
for (const { resources } of compiled) {
for (const resource of resources) {
resource.setPlayground(playground);
if (resource.isAsync) {
resource.resolve();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment