Skip to content

Instantly share code, notes, and snippets.

View jorendorff's full-sized avatar

Jason Orendorff jorendorff

View GitHub Profile
<!doctype html>
<html>
<head>
<title>js speed test</title>
</head>
<body>
<script>
{
let a = [];

jorendorff potetm: OK, a quick thought on the value of a single test, from a mathematical perspective.

Any program has a "configuration space", to use a term from physics. That's the set of all possible states the program can be in. if you're testing a single stateless function, then the set of all inputs. I tend to think of it as a 3D volume - and if it just so happens that the function has three numeric arguments, or the program has 3 variables, that's what it is :)

A test, as you pointed out earlier, probes a single point in this space. That does not seem very valuable! And indeed if the function you're testing is implemented as a giant (cond) with one case for each possible input, it would be useless. Each test could only test a single code path. Testing them all would be impractical.

The mathematical reason testing can be more effective than it seems at first is that lambdas are quite small, so bugs tend to affect many possible combinations of inputs -- huge, often contiguous, swaths of the 3D s

Analysis for JSAPI error handling

Some C++ functions, including most JSAPI functions, are fallible. You pass them a context argument, usually of type JSContext *, and they can either succeed or fail. If they fail, there might be a pending exception on that context which needs to be handled.

In any case the caller can't ignore the return value.

(function () {
var XHREventTargetPrototype = Object.getPrototypeOf(XMLHttpRequest.prototype);
var desc = Object.getOwnPropertyDescriptor(XHREventTargetPrototype, "onload");
var real_set_onload = desc.set;
Object.defineProperty(XHREventTargetPrototype, "onload", {
set: function onload(real_callback) {
real_set_onload.call(this, function fake_callback() {
console.log("loaded:", arguments);
return real_callback.apply(this, arguments);
});
(function () {
var real_fetch = window.fetch;
window.fetch = function () {
console.log("calling fetch:", arguments)
return real_fetch.apply(this, arguments).then(function (value) {
console.log("fetch succeeded:", value);
return value;
});
};
})();
@jorendorff
jorendorff / main.rs
Created April 2, 2016 17:49
randspeed/src/main.rs
#![feature(test)]
/*
* main.rs - Clueless random number generation benchmark
*/
extern crate rand;
extern crate test;
use rand::{random, thread_rng, XorShiftRng, Isaac64Rng, StdRng, Rng};

Functional programming and games

Games are impossible in functional languages

The first thing you have to understand, before anything else, is that it is impossible to write games in functional languages.

And when I say games, I don't mean everything. Here's a game Joshua Bush wrote in Elm, and it's smooth and great.

// Template-based argument conversion in jsapi.h?
// First, to show what we're shooting for here...
namespace JS {
// The main entry point for getting object properties. Everything goes through here.
extern JS_PUBLIC_API(bool)
GetProperty(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp);
// The helper template for automatically converting/rooting arguments of random types
// how I would implement https://github.com/kripken/lua.vm.js/blob/master/src/lua.js#L6-L27
var cached_constructors = [];
// applying arguments to new isn't easy with js.....
function new_(a, b, c, d, e, f, g, h, i) {
switch (arguments.length) {
case 0: return new this();
case 1: return new this(a);
case 2: return new this(a, b);
import Control.Monad.ST
import Data.STRef
------------------------------------------------------------------------
data Type s = TInt
| TString
| TFun (Type s) (Type s)
| TyVar (STRef s (Maybe (Type s)));