Skip to content

Instantly share code, notes, and snippets.

@tschneidereit
tschneidereit / ts-module-merger.js
Created September 3, 2015 16:01
TypeScript module merger
var esprima = require('esprima');
var codegen = require('escodegen');
var fs = require('fs');
function optimizeFile(path) {
console.error("Optimizing file", path);
fs.readFile(path, function (err, data) {
if (err) {
throw err;
}
@tschneidereit
tschneidereit / iterable-weak-key-map.js
Created November 3, 2013 21:15
Iterable WeakMap implemented using Weak References
function IterableWeakKeyMap() {
const keyMap = new WeakMap();
const refValueMap = new Map();
return Object.freeze({
get: function(key) {
const entry = keyMap.get(key);
return entry && entry.value;
},
set: function(key, value) {
const ref = makeWeakRef(key);
@tschneidereit
tschneidereit / Lazy forwarding proxy.js
Last active December 19, 2015 14:29
More principled implementation
LazySet_handlers = {
has: function(target, name) {
if (target.inHas) {
return true;
}
target.inHas = true;
var result = LazySet_installForwarder(target, name);
target.inHas = false;
target.hasCalled = true;
return result;

Self-Hosting in SpiderMonkey

Firefox has always contained large amounts of JavaScript in the application itself, and with each release, that amount usually grows. Some of the JS code that is contained in Firefox 21, released last Tuesday, is special, however: it forms part of SpiderMonkey itself, the virtual machine that runs all JS code in Firefox and on websites loaded into the browser.

Implementing parts or the whole of a runtime or compilation environment for a language in that very language itself is called self-hosting, and it isn't a new invention by any means. Examples of languages with (partly) self-hosted compilers include C/C++, Pascal and Rust. Partially self-hosted VMs exist, among others, for Lisps of all ages, Python, Ruby, Java, .Net, ActionScript3 - and JS. In fact, the V8 VM that executes JS in Chromium and Node.js has been partially self-hosted for a number of years now (maybe even from the outset, I don't know).

Why?

Various reasons. Code that is implemented in managed, memory-saf

@tschneidereit
tschneidereit / rl1-to-rl2-regex
Created February 27, 2012 11:36
Regular Expressions for porting Robotlegs 1 to Robotlegs 2
Converting Injector mappings:
mapValue\((\w+)\s*,\s*(.+?)(,\s*['"]\w+['"]|)\); -> map($1$3).toValue($2);
mapClass\((\w+)\s*,\s*(.+?)(,\s*['"]\w+['"]|)\); -> map($1$3).toType($2);
mapSingleton\((\w+)\s*(,\s*['"]\w+['"]|)\); -> map($1$2).asSingleton();
mapSingletonOf\((\w+)\s*,\s*(.+?)(,\s*['"]\w+['"]|)\); -> map($1$3).toSingleton($2);
// will be extended for Command- and MediatorMap
@tschneidereit
tschneidereit / CastingSpeed.as
Created September 28, 2010 15:06
Benchmark of several variants of type casting in AS3
package
{
import flash.display.*;
import flash.text.*;
import flash.utils.*;
public class CastingSpeed extends Sprite
{
public function CastingSpeed()
{
@tschneidereit
tschneidereit / stagelistener_benchmarks.as
Created April 19, 2010 16:31
Benchmarks for various forms of listeners for ADDED_TO_STAGE and REMOVED_FROM_STAGE
package
{
import com.gskinner.performance.TestSuite;
import com.gskinner.performance.ptest;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.getQualifiedClassName;
package avmplus
{
public class PublicDescribeType
{
public static function describeTypeJSON(target : Object, flags : uint) : Object
{
return DescribeType.describeTypeJSON(target, flags);
}
public function publicDescribeMetadata(xml : XML, metadata : Array) : void