Skip to content

Instantly share code, notes, and snippets.

@egonelbre

egonelbre/dci.js Secret

Last active August 29, 2015 14:08
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 egonelbre/7dfe717af9416f77266a to your computer and use it in GitHub Desktop.
Save egonelbre/7dfe717af9416f77266a to your computer and use it in GitHub Desktop.
dci torture test
function ContextMacro(init, roles) {
// closure for scoping things
var sfn = "(function(){\n";
// scoped role players
sfn += " var " + Object.keys(roles).join(",") + ";\n";
// creates the function
var mkName = function(role, fn){ return role + "$" + fn; };
// redirects all role method calls
var fixMethod = function(fn){
fn = fn.toString();
for(var roleName in roles){
var role = roles[roleName];
for(var fnName in role){
// replaces Role.method(arg1,arg2,arg3) with
// with Role$method.call(Role, arg1, arg2, arg3)
var rx = new RegExp( "(?!\\.)" + roleName + "\\s*\\.\\s*" + fnName + "\\s*\\(", "g");
fn = fn.replace(rx, mkName(roleName, fnName) + ".call(" + roleName + ",");
}
}
// hack-fix Role$method.call(Arg,)
fn = fn.replace(/,\)/g, ")");
return fn;
};
// define role methods in to the context
var methods = [];
for(var roleName in roles){
var role = roles[roleName];
for(var fnName in role){
methods.push(" var " + mkName(roleName, fnName) + " = " + fixMethod(role[fnName]));
}
}
sfn += methods.join(";\n") + ";\n";
// call the init
sfn += " return (" + fixMethod(init) + ").apply(null, arguments);\n})";
return sfn;
};
function Context(init, roles, debug){
// scoped role players
var sfn = " var " + Object.keys(roles).join(",") + ";\n";
// creates the function
var mkName = function(role, fn){ return role + "$" + fn; };
// redirects all role method calls
var fixMethod = function(fn){
fn = fn.toString();
for(var roleName in roles){
var role = roles[roleName];
for(var fnName in role){
// replaces Role.method(arg1,arg2,arg3) with
// with Role$method.call(Role, arg1, arg2, arg3)
var rx = new RegExp( "(?!\\.)" + roleName + "\\s*\\.\\s*" + fnName + "\\s*\\(", "g");
fn = fn.replace(rx, mkName(roleName, fnName) + ".call(" + roleName + ",");
}
}
// hack-fix Role$method.call(Arg,)
fn = fn.replace(/,\)/g, ")");
return fn;
};
// define role methods in to the context
var methods = [];
for(var roleName in roles){
var role = roles[roleName];
for(var fnName in role){
methods.push(" var " + mkName(roleName, fnName) + " = " + fixMethod(role[fnName]));
}
}
sfn += methods.join(";\n") + ";\n";
// return the init
sfn += " return (" + fixMethod(init) + ").apply(null, arguments);";
if(debug){
console.log(sfn);
}
return (new Function(sfn));
}
function CompileDCI(source){
return ProcessMacros(source, {Context : ContextMacro});
};
<!doctype html>
<script src="dci.js"></script>
<script>
function doInterview(player) {
console.log("[Interviewer] Hello!");
player.say(); // should call the instance method
}
function callback(fn){ fn(); }
var Battle = Context(function(id, firstPlayer, secondPlayer) {
Id = id;
Bear = firstPlayer;
Lion = secondPlayer;
return {
start: function(){
console.log(Id + " battle commencing:");
Bear.fight();
},
interview: function(){
doInterview(Bear);
doInterview(Lion);
}
}
}, {
Id: "",
Bear : {
say: function() {
console.log(Id + " [" + this.name + "] " + "Grrrr.....");
},
fight: function(){
Bear.say();
Lion.fight();
}
},
Lion : {
say: function() {
console.log(Id + " [" + this.name + "] " + "Meow.....");
},
fight: function() {
// invoking role method via callback
callback(function(){ Lion.say(); });
}
}
});
function Tournament(player, cpu) {
var b1 = Battle("1", player, cpu);
var b2 = Battle("2", cpu, player);
var b3 = Battle("3", cpu, cpu);
b1.start();
b2.start();
b3.start();
b1.interview();
}
// example
var player = {
name: "Jack",
say: function(){
console.log("[" + this.name + "] says Hello!");
}
};
var cpu = {
name: "Cyborg",
say: function(){
console.log("[" + this.name + "] bleeps Hello!");
}
};
Tournament(player, cpu);
</script>
<!--
expected output:
1 battle commencing:
1 [Jack] Grrrr.....
1 [Cyborg] Meow.....
2 battle commencing:
2 [Cyborg] Grrrr.....
2 [Jack] Meow.....
3 battle commencing:
3 [Cyborg] Grrrr.....
3 [Cyborg] Meow.....
[Interviewer] Hello!
[Jack] says Hello!
[Interviewer] Hello!
[Cyborg] bleeps Hello!
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment