Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
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
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) {
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
@dherman
dherman / a.js
Created February 28, 2014 21:29
node cycle
var b = require('./b');
module.exports = {
a: function() {
console.log(b.b());
return "I am a.a";
}
};
<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 August 29, 2015 13:59
Iterable is the new newborn

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 August 29, 2015 14:00
how canceling a cancelation could arise organically
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 August 29, 2015 14:00
Iterators, generators, and loop exit

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 August 29, 2015 14:01
combinatorics of top-level execution

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.

<!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));