Skip to content

Instantly share code, notes, and snippets.

View brandonpayton's full-sized avatar

Brandon Payton brandonpayton

View GitHub Profile
@brandonpayton
brandonpayton / blueprint.json
Created April 24, 2024 15:23
Blueprint: Test Material Board plugin
{
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
"landingPage": "https://playground.wordpress.net/wp-admin/",
"preferredVersions": {
"php": "8.3",
"wp": "6.5"
},
"phpExtensionBundles": [
"kitchen-sink"
],
@brandonpayton
brandonpayton / custom-redirects.php
Last active April 18, 2024 22:39
Serving playground.wordpress.net from nginx-based host with no customization of nginx config
<?php
/**
* ATTENTION: Please update Playground's .htaccess file as necessary
* whenever making changes here.
*/
define('PLAYGROUND_DEBUG', false);
playground_handle_request();
function playground_handle_request() {
@brandonpayton
brandonpayton / Output
Created April 12, 2024 03:58
Expected OOM log
Initial memory usage: 0.35476684570312 MB
Memory limit: 128M
* memory_get_usage(false): 1.3586730957031 MB
* memory_get_usage(true): 20 MB
* memory_get_usage(false): 2.4172821044922 MB
* memory_get_usage(true): 22.0625 MB
* memory_get_usage(false): 3.4172821044922 MB
* memory_get_usage(true): 23.0625 MB
* memory_get_usage(false): 4.4172821044922 MB
* memory_get_usage(true): 24.0625 MB
@brandonpayton
brandonpayton / alloc and free by wasm_memory_storage
Last active April 8, 2024 14:21
site-data.spec.ts log from instrumented PHP 8.0 Wasm
wasm_memory_storage_chunk_alloc: attempt
wasm_memory_storage_chunk_alloc: addr: 0x1200000 size: 2097152, align: 2097152
wasm_memory_storage_chunk_alloc: attempt
wasm_memory_storage_chunk_alloc: addr: 0x1a00000 size: 2097152, align: 2097152
wasm_memory_storage_chunk_alloc: attempt
wasm_memory_storage_chunk_alloc: addr: 0x1e00000 size: 2097152, align: 2097152
wasm_memory_storage_chunk_alloc: attempt
wasm_memory_storage_chunk_alloc: addr: 0x2600000 size: 2097152, align: 2097152
wasm_memory_storage_chunk_alloc: attempt
wasm_memory_storage_chunk_alloc: addr: 0x2a00000 size: 2097152, align: 2097152
@brandonpayton
brandonpayton / sw.js
Created April 4, 2024 19:07
Sample of transpiled Service Worker script for playground.wordpress.net
function isURLScoped(e){return e.pathname.startsWith("/scope:")}function getURLScope(e){return isURLScoped(e)?e.pathname.split("/")[1].split(":")[1]:null}function setURLScope(e,t){let r=new URL(e);if(isURLScoped(r))if(t){const s=r.pathname.split("/");s[1]=`scope:${t}`,r.pathname=s.join("/")}else r=removeURLScope(r);else if(t){const s=r.pathname==="/"?"":r.pathname;r.pathname=`/scope:${t}${s}`}return r}function removeURLScope(e){if(!isURLScoped(e))return e;const t=new URL(e),r=t.pathname.split("/");return t.pathname="/"+r.slice(2).join("/"),t}const currentJsRuntime=function(){return typeof process<"u"&&process.release?.name==="node"?"NODE":typeof window<"u"?"WEB":typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope?"WORKER":"NODE"}();if(currentJsRuntime==="NODE"){let e=function(r){return new Promise(function(s,n){r.onload=r.onerror=function(i){r.onload=r.onerror=null,i.type==="load"?s(r.result):n(new Error("Failed to read the blob/file"))}})},t=function(){const r=new Uint8Array([1,2,3,4]),n=new File
@brandonpayton
brandonpayton / index.php
Last active April 3, 2024 06:55
Script that revealed OOM condition due to memory leak
<?php
ini_set('memory_limit', '1G');
function useAllMemory() {
echo "Initial memory limit: " . ini_get( 'memory_limit' ) . '<br>';
echo "Initial memory usage: " . (memory_get_usage()/(1024*1024)) . ' MB <br>';
echo "<br>";
$data = '';
@brandonpayton
brandonpayton / PHP OOM log
Created March 27, 2024 01:57
Log of php-wasm leaking memory via partial munmap() attempts which fail
Initial memory limit: 64M
Initial memory usage: 0.38388824462891 MB
* iteration: 0
* strlen(): 0.0625 MB
* memory_get_usage(): 0.45029449462891 MB
* aggregate strlen()'s: 0.0625 MB
* iteration: 1
* strlen(): 0.125 MB
@brandonpayton
brandonpayton / exercise.diff
Created March 22, 2024 17:08
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\
@brandonpayton
brandonpayton / main.rs
Created April 11, 2020 07:41
Rust generic type inference
fn get<T, U: From<T>>(s: T) -> U {
U::from(s)
}
fn main() {
let x: u64 = get(123u8);
println!("{:064b}", x);
}
@brandonpayton
brandonpayton / index.js
Created December 19, 2018 07:42
A hack to show the Set Featured Image modal when a meta box button is clicked.
wp.domReady( function () {
var button = document.getElementById( 'mbwr-meta-box-button' );
var container = document.getElementById( 'mbwr-meta-box-media-upload' );
// NOTE: I'm not sure what timing issue this is avoiding, but it's necessary to avoid error.
setTimeout( function () {
wp.element.render(
wp.element.createElement( wp.editor.PostFeaturedImage ),
container
);