Skip to content

Instantly share code, notes, and snippets.

View mikesamuel's full-sized avatar

Mike Samuel mikesamuel

View GitHub Profile
@mikesamuel
mikesamuel / corner-cases.md
Last active June 21, 2018 19:30
GH Markdown auto-identifier corner cases

Ambiguous

Ambiguous

Ambiguous

NFC Å - Å

NFKC Äffin - Äffin

@mikesamuel
mikesamuel / node.diff
Created July 27, 2018 00:55
Node CJS module resolver hooks
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 33d8907e4f..6df7c32efd 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -46,6 +46,9 @@ const preserveSymlinks = !!process.binding('config').preserveSymlinks;
const preserveSymlinksMain = !!process.binding('config').preserveSymlinksMain;
const experimentalModules = !!process.binding('config').experimentalModules;
+const { defineProperties, hasOwnProperty } = Object;
+const { apply } = Reflect;
@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.
@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 / 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 / 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 / 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 / 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 / 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 / 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;