Skip to content

Instantly share code, notes, and snippets.

Avatar

Dave Herman dherman

View GitHub Profile
View hijack.js
Promise.prototype.resolve = function() {
let loader = new Reflect.Loader();
let self = this;
loader[Reflect.Loader.resolve] = () => "whatevs";
loader[Reflect.Loader.fetch] = function(whatevs) {
return self;
};
return function(v) {
loader.load("whatevs"); // causes the loader to block on this promise for @@fetch
loader.provide(v); // causes the loader to resolve this promise
View secret-promise.js
let secret = new WeakMap();
function SecretPromise(cb) {
return new Promise(function(resolve, reject) {
secret.set(this, { resolve, reject });;
return cb.call(this, resolve, reject);
})
}
function ResolveWithInternalSlot(p, value) {
View realm-oo.js
class Realm {
constructor: (target: object?, handler: object?) -> void,
// intended to be overridden by subclasses
indirectEval(...any) -> stringable, // default: no translation
directEval(self: any, ...any) -> stringable, // default: no translation
nonEval(self: any, callee: any, ...any) -> any, // default: call callee on args
init() -> void, // default: make global instanceof global.Object and define standard globals
// accessor methods
View a.js
var b = require('./b');
module.exports = {
a: function() {
console.log(b.b());
return "I am a.a";
}
};
View bang-slash.txt
<link href="/wps/portal/mtatasky/homeaction/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOJ9_A0MjLwsDLwMzEJcDDz9XJxCvPyDjQ0sDYAKIoEKDHAARwNC-sP1o_AqMTSCKsBjhZ9Hfm6qfkFuhEGWiaMiABY_PtQ!/dl4/d5/L2dJQSEvUUt3QS80SmtFL1o2X0xPMDAySjgwSjA2VEQwSU5EQlRKT1MzMDkw/" id="com.ibm.lotus.NavStateUrl" rel="alternate"/>
@dherman
dherman / newborn.md
Last active Aug 29, 2015
Iterable is the new newborn
View newborn.md

THIS IDEA IS BROKEN

THIS WHOLE ESSAY IS BROKEN. THE IDEA DOESN'T WORK BECAUSE BY MAKING GENERATOR FUNCTIONS START EXECUTING EAGERLY, THEIR FIRST YIELD SENDS A VALUE THAT THE CALLER CAN'T RECEIVE:

function* foo() {
  yield "first value"
}
let g = foo();
let it = g.next(); // where did "first value" go?
@dherman
dherman / cancel-cancel.js
Last active Aug 29, 2015
how canceling a cancelation could arise organically
View cancel-cancel.js
for (let x of foo()) {
...
// generator gets canceled at some point before it completes the iteration
break;
...
}
function* foo() {
...
try {
@dherman
dherman / iterators.md
Last active Aug 29, 2015
Iterators, generators, and loop exit
View iterators.md

Iterators, generators, and loop exit

Understanding the iterable/iterator distinction

It's important to understand what the purpose of the two abstractions are in order to understand the rest of this essay.

An iterable is a data source, often a mutable one, that can at arbitrary points be iterated over, especially by a for-of loop. An iterable object can have an arbitrary lifetime, and may be long-lived. But iterating over an iterable data structure should be thought of as a relatively short-lived operation, in the sense that the data source should be temporarily frozen during iteration---modifying the iterable data source in the middle of an iteration will generally result in unpredictable behavior.

An iterator produced by an iterable object is therefore a relatively short-lived object, which should not be used once the iteration operation is complete.

@dherman
dherman / 1.variables.md
Last active Aug 29, 2015
combinatorics of top-level execution
View 1.variables.md

Orthogonal Variables

Considering the full combinatorics of the space, there are four different independent variables for how to execute top-level code:

  • HTML vs JS: is the code loaded from an HTML tag or from a JS constructor?
  • script vs module: is the code executed as an ES(6) Script or Module?
  • worker vs sync: is the code loaded in a worker or synchronously?
  • URL vs source: is the code provided as a source string or a URL to an external file?

This leads to 2^4=16 combinations that have to be considered for the design. It doesn't mean authors actually have to deal with all this combinations, since (a) they don't all have to be supported, and (b) some can eventually become old-fashioned legacy.

View aa.html
<!doctype html>
<script>
function go() {
var tgt = document.getElementById("tgt");
var getSekrit = tgt.contentWindow.getSekrit;
var results1 = getSekrit();
tgt.addEventListener('load', function() {
var results2 = getSekrit();
alert(results1.concat(results2));