Skip to content

Instantly share code, notes, and snippets.

View olleharstedt's full-sized avatar

Olle Härstedt olleharstedt

View GitHub Profile
@olleharstedt
olleharstedt / tmp.php
Created September 25, 2023 19:54
Memory polymorph and polyglot SplDoublyLinkedList
//<?php echo "\x08\x08"; ob_start(); ?>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <glib.h>
#include <math.h>
#include <phollylib.c>
#if __PHP__//<?php
class GString { public $str; public function __construct($str) { $this->str = $str; } }
function g_string_new(string $str) { return new GString($str); }
@olleharstedt
olleharstedt / groupservice.php
Created July 12, 2023 10:26
Inject survey directly
<?php
namespace LimeSurvey\Models\Services;
use DI\DependencyException;
use LimeSurvey\DI;
use LSYii_Application;
use Survey;
use Permission;
use Question;
<?php
function getAttributesFromTheme(string $themeName)
{
$theme = $this->getTheme($themeName);
if (empty($theme)) {
return null;
}
$xml = $this->getXmlFromTheme($theme);
if (empty($xml)) {
function getAttributesFromTheme(string $themeName)
{
$theme = $this->getTheme($themeName);
if (empty($theme)) {
return null;
}
$xml = $this->getXmlFile($theme->path);
if (empty($xml)) {
return null;
}
@olleharstedt
olleharstedt / nbody.c
Created January 22, 2023 23:30
PHP+C polyglot nbody benchmark
//<?php echo "\x08\x08"; ob_start(); ?>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <glib.h>
#include <math.h>
#include <phollylib.c>
#if __PHP__//<?php
class GString { public $str; public function __construct($str) { $this->str = $str; } }
function g_string_new(string $str) { return new GString($str); }
<?php // @pholyglot
class Body
{
public float $x;
public float $y;
public float $z;
public float $vx;
public float $vy;
public float $vz;
@olleharstedt
olleharstedt / tmp6.php
Created March 2, 2022 23:41
One universal dry-run mock-spy AST evaluator to rule them all
<?php
namespace Tmp6;
use InvalidArgumentException;
interface Node
{
}
@olleharstedt
olleharstedt / edsl.php
Last active October 7, 2022 21:49
EDSL in PHP
<?php
/**
* EDSL = embedded domain-specific language
* The domain in this example is "side-effects", anything writing/reading to io, database, file, etc
*
* Using this EDSL, you neither need DI nor mocking to make the code testable. You can dry-run the particular
* parts needed of the "effectful" logic.
*
* Inspired by tagless-final pattern from FP. https://okmij.org/ftp/tagless-final/course/optimizations.html#primer

Pre-req: Functional core, imperative shell

Pre-req: Side-effects, purity, referential transparency

  • The functional core is more testable and more composable than the imperative shell or effectful code
  • Extending the functional core means making a bigger part of the software pure
  • Side-effects can in some cases easily be lifted out from a function, creating a more composable and testable unit
  • But sometimes, side-effects are tangled in business logic
  • Some side-effects can be delayed until end of request (e.g., updating user's name in database; exception at failure can still be thrown)
  • Example: Create dummy users
@olleharstedt
olleharstedt / benchmark.js
Last active December 30, 2020 22:38
Clone benchmark
class Query {
constructor() {
this.select = '';
this.from = '';
this.where = '';
}
}
for (let i = 0; i < 1000000; i++) {
const q = new Query();