Skip to content

Instantly share code, notes, and snippets.

View jorendorff's full-sized avatar

Jason Orendorff jorendorff

View GitHub Profile
@jorendorff
jorendorff / Script-getExecutionCounts.js
Created July 14, 2011 20:15
getExecutionCounts test code
// Basic getExecutionCounts test.
var g = newGlobal('new-compartment');
var dbg = Debugger(g);
var s;
dbg.onNewScript = function (script) { s = script; };
var f = g.Function("i",
"line0 = Error().lineNumber;\n" +
"var s = 0;\n" + // line0 + 1
"for (var a = 0; a < i; a++)\n" + // line0 + 2
@jorendorff
jorendorff / gist:1434249
Created December 5, 2011 16:44
Directed graph of maximally overlapping permutations of '123'
123
↙ ↖
231 → 312
132
↙ ↖
321 → 213
@jorendorff
jorendorff / gist:1468256
Created December 12, 2011 17:23
std::lower_bound
/**
* @brief Finds the first position in which @a val could be inserted
* without changing the ordering.
* @param first An iterator.
* @param last Another iterator.
* @param val The search term.
* @return An iterator pointing to the first element "not less than" @a val,
* or end() if every element is less than @a val.
* @ingroup binarysearch
*/
@jorendorff
jorendorff / test.js
Created January 6, 2012 21:17
test case
function f() {
var a = [1, 0.5, 1];
var b;
for (var j = 0; j < a.length; j++) {
var z = -a[j];
if (b === undefined || z < b)
b = z;
}
}
for (let V = INIT; TEST; UPD) STMT
===>
{
let %tmp = INIT, %first = true;
while (true) {
// A fresh binding for V gets either the initializer-expression value(s)
// or the value(s) from the end of the previous loop iteration.
let V = %tmp;
@jorendorff
jorendorff / gist:1790659
Created February 10, 2012 16:32
C++ APIs for Iteration
// *** design #1
if (!forOf(cx, obj, [cx] (const Value &v) {
...
})) {
return false;
}
// *** design #2
/*
* This is for writing C++ code that does the equivalent of JS try/finally.
* Normal infallible C++ cleanup code should just go in a C++ destructor.
* This is for cleanup that should be *skipped* in the unusual case that
* the try block exits with an uncatchable error (OOM or timeout).
*
* Use it like this:
*
* bool ok;
* ... YOUR TRY-BLOCK CODE HERE ...
// *** Design #1: helper class
if (AutoFinallyBlock afb(cx, ok)) {
bool closedOK = CLOSE THE ITERATOR;
ok = afb.leave(closedOK);
}
// *** Design #2: no helper class
if (ok || cx->isExceptionPending()) {
# This program is valid Python 2.7 and it's also valid Python 3.2.
# To run it, type: python hashfiles.py
import os, hashlib, collections
# What is a defaultdict? Some sort of data structure, apparently. Hmm.
file_groups = collections.defaultdict(list)
# This loop is going to execute once for each directory in the tree.
for dirpath, dirnames, filenames in os.walk('.'):
# The inner loop will run once for each file in the tree.
// Run this
// in Scratchpad
// in the *target* Firefox
// using Environment -> Browser
// before opening the script debugger in the other Firefox.
Components.utils.import("resource://gre/modules/devtools/dbg-server.jsm");
function allowDebuggersToConnect() {
const DEBUGGER_PORT = 60000;