Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created October 16, 2011 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save back2dos/1291347 to your computer and use it in GitHub Desktop.
Save back2dos/1291347 to your computer and use it in GitHub Desktop.
(function($ctx) {
var $empty = function () { return function () {} };
var $estr = function () { return $ctx.Std.string(this) };
var $array = function (args, from, to) { return Array.prototype.slice.call(args, from, to); };
var $copy = function (from, to, overwrite) { for (var id in from) if (overwrite || (to[id] == null)) to[id] = from[id]; return to; };
var $c = function (s, c) { return { c: c, toString : function () { return s; }}};
var $init = [];
var $enum = function (path, constructs) {
this.__ename__ = path;
var c = this.__constructs__ = [];
var self = this;
var finalize = function (v) {
v.toString = $estr;
v.__enum__ = self;
return v;
}
var mkConstr = function (ids, c) {
return function () { return finalize(ids.concat($array(arguments, 0, c))) };
}
for (var i = 0; i < constructs.length; i++) {
var id = constructs[i];
var ids = [String(id), c.push(String(id)) - 1];
this[id] = id.c ? mkConstr(ids, id.c) : finalize(ids)
}
};
var $package = function (path, parent, target) {
this.path = path;
this.parent = parent;
this.target = target || this;
};
$package.prototype = {
makeEnum : function (id) {
if (this.target[id] == null)
this.target[id] = new $enum(this.path.concat(id), $array(arguments, 1));
return this;
},
child : function (id) {
return this.target[id] || (this.target[id] = new $package(this.path.concat(id), this));
},
close : function () {
return this.parent;
},
makeClass : function (id, constr, instance, statics, superClass) {
if (this.target[id] == null)
this.target[id] = this.initClass(this.path.concat(id), constr, instance, statics);
return this;
},
initClass : function (path, constr, instance, statics, superClass) {
if (constr == null) constr = (superClass ? superClass : $empty());
constr.prototype = instance || {}
constr.prototype.__class__ = constr;
constr.__name__ = path;
if (superClass) {
constr.__super__ = superClass;
$copy(superClass.prototype, constr.prototype);
}
constr.empty = $empty();
constr.empty.prototype = constr.prototype;//this one should be used for Type.createEmptyInstance instead of Boot.skipConstructor
$copy(statics, constr);
if (constr.__init) $init.push(constr);
return constr;
}
};
//OUTPUT:
$cca = String.charCodeAt;
$sub = String.substr;
(new $package([], null, $ctx))
.makeEnum("ValueType", "TNull", "TInt", "TFloat", "TBool", "TObject", "TFunction", $c("TClass", 1), $c("TEnum", 1), "TUnknown")
.makeClass("Array", Array, $copy({ copy: Array.prototype.slice /*iterator goes here*/ }, Array.prototype))
.makeClass("String", String, $copy({ /*override charCodeAt and substr*/ }, String.prototype, true))
.makeClass("List", function () {}, {
h: null,
q: null,
length: 0,
add: function(item) {
var x = [item];
if(this.h == null) this.h = x; else this.q[1] = x;
this.q = x;
this.length++;
}
/* ... */
})
.child('haxe')
.makeClass('Public')
.child('rtti')
.makeClass('Generic')
.close()
.child('unit')
.makeClass('TestCase' /* ... */)
.makeClass('TestResult' /* ... */)
.makeClass('TestRunner', function (p) {
if (p === $skip) return;
this.result = new $ctx.haxe.unit.TestResult();
this.cases = new $ctx.List();
}, { /* ... */ })
.close()
.close()
/* ... */
.close();
for (var i = 0; i < $init.length; i++)
$init[i].__init();
return ctx;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment