Skip to content

Instantly share code, notes, and snippets.

View mikesamuel's full-sized avatar

Mike Samuel mikesamuel

View GitHub Profile
@mikesamuel
mikesamuel / output.txt
Last active February 16, 2024 20:52
Exploring how regexes work differently on engines with different code-unit sizes / assumed encodings
Matching code-units that have all the same size OK.
applying /a/ to '"aό𝄞"'
. py UTF-8
. . 00000000 61 |a|
. . 00000001
. js UTF-16
. . 00000000 61 |a|
. . 00000001
. py UTF-32
. . 00000000 61 |a|
@mikesamuel
mikesamuel / subjectivity.html
Created November 16, 2023 21:59
Subjective method dispatch explanation
<!doctype html><meta charset=utf-8>
<!-- this file is subjectivity.html -->
<script>
// Just calls a method on the string it's given.
// Below each iframe locally overrides that method.
window.makeItBold = (str) => str.bold();
// We're loading the same HTML in three frames so fork.
switch (window.location.hash) {
@mikesamuel
mikesamuel / cmp.js
Created November 14, 2023 20:56
JS cmp function sorting NaN high and distinguishing zeroes
function cmpNums(a, b) {
if (Object.is(a, b)) { return 0; }
let delta = a - b;
if (delta) {
return Math.sign(delta);
} else if (delta === 0) {
// The only values that do not match Object.is above
// and which do not lead to a NaN delta are -0, +0
// and vice versa.
return Object.is(a, -0) ? -1 : 1;
@mikesamuel
mikesamuel / examples.md
Last active October 16, 2023 22:50
Lexically nested types

In JavaScript a nested type can read and assign variables from outer scopes.

function counter(start = 0) {
  let n = start;
  class Counter {
    peek() { return n; }
    increment() { ++n; }
  }
 return new Counter();
@mikesamuel
mikesamuel / Foo.java
Created June 3, 2021 13:43
Java boxing is violable
import java.lang.reflect.*;
import java.util.*;
public class Foo {
public static void main(String[] argv) throws Exception {
Class<?> integerCacheClass = Arrays.stream(Integer.class.getDeclaredClasses())
.filter(c -> c.getSimpleName().equals("IntegerCache"))
.findFirst()
.get();
Field cacheField = Arrays.stream(integerCacheClass.getDeclaredFields())
@mikesamuel
mikesamuel / test.js
Last active April 3, 2019 19:20
Informal tests for augmented HostEnsureCanCompileStrings
// Run in the context of a HostEnsureCanCompileStrings callout that
// throws an EvalError (from the callee realm) for string values, and
// arrays.
const PASS = {}, FAIL = {};
function tryCompileAsFunctionBody(description, source, expectedStatus) {
let status = FAIL;
try {
new Function(source);
@mikesamuel
mikesamuel / unundoable.js
Created March 14, 2019 04:53
Things you can't undo
// On the server
response.write(x);
// On the client
document.body.innerHTML = x;
@mikesamuel
mikesamuel / weird0.html
Created February 24, 2019 15:51
Yet another example of why HTML is weird.
<script>/*&#42;/alert('svg');//*/alert('html');
//<![CDATA[</script><!--]]>--></script>
<svg>
<script>/*&#42;/alert('svg');//*/alert('html');
//<![CDATA[</script><!--]]>--></script>
</svg>
@mikesamuel
mikesamuel / type-checking-code-loaders.js
Last active January 20, 2019 15:16
Checking arguments to Function/eval
class TrustedScript { // Just a stub of github.com/wicg/trusted-types
constructor(x) {
this.content = String(x);
}
toString() {
return this.content;
}
static createUnsafely(x) {
@mikesamuel
mikesamuel / monkey-with-JSON.js
Last active August 6, 2018 16:00
Monkeypatching JSON to distinguish parsed objects from user-code created objects
/**
* A symbol that should only be added to objects whose
* properties come from an external string, or which include
* uncherry-picked properties from such an object.
*/
const PARSER_OUTPUT_SYMBOL = Symbol('parserOutput');
function markingReviver(_, value) {
"use strict";
if (value && typeof value === 'object') {
// HACK: This might fail if optReviver freezes values.