Skip to content

Instantly share code, notes, and snippets.

@dariusf
Last active August 29, 2015 14:01
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 dariusf/11492467685ec6cd933f to your computer and use it in GitHub Desktop.
Save dariusf/11492467685ec6cd933f to your computer and use it in GitHub Desktop.
Example of compiled output
/*
This is the compiled result of the following:
chooseAction := method(
# Written by user
moveRight
)
player chooseAction := getSlot("chooseAction")
player chooseAction
*/
//////////////////////////////////////////
// Insert contents of ./src/lib.js here.
// Can be prepended or require()'d.
//////////////////////////////////////////
// Boilerplate wrapping user code
function execute () {
var obj = this;
var localsProxy = new IoProxy(IoRootObject, function (message) {
if (obj.hasOwnProperty(message)) {
this.stopPrototypePropagation();
var args = Array.prototype.slice.call(arguments, 1);
return obj[message].apply(obj, args);
}
});
var playerProxy = new IoProxy(IoRootObject, function (message) {
if (message === 'chooseAction') {
this.stopPrototypePropagation();
var args = Array.prototype.slice.call(arguments, 1);
var slot = this.findSlot(message);
slot.activate.locals = localsProxy;
// unwrap arguments
args = args.map(function (arg) {
if (arg.type === 'Block') {
// IoMethod
return function () {
return arg.activate.apply(arg, arguments);
};
} else {
// IoStringWrapper / IoNumberWrapper / IoBooleanWrapper
return arg.slots.value;
}
});
return slot.activate.apply(slot, [IoRootObject].concat(args));
}
});
Lobby.slots['player'] = playerProxy;
// User code
Lobby.send('Lobby').send('setSlot', IoStringWrapper('chooseAction'), Lobby.send('method', 'n', IoThunk(function (locals) {
return locals.send('moveRight', locals.send('n'));
})));
Lobby.send('player').send('setSlot', IoStringWrapper('chooseAction'), Lobby.send('getSlot', IoStringWrapper('chooseAction')));
Lobby.send('player').send('chooseAction', IoStringWrapper('bob'));
// End user code
}
// End boilerplate
// External call:
var player = {
moveRight: function (name) {
console.log(name, 'moving right');
}
};
execute.call(player);
// After lib.js is made available, this file may be run standalone in node or a browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment