Skip to content

Commit

Permalink
Merge pull request #289 from longouyang/pausy-trampoline
Browse files Browse the repository at this point in the history
Factor out runTrampoline; use "pausy" version in browser
  • Loading branch information
stuhlmueller committed Jan 19, 2016
2 parents e87212c + eff3eeb commit 4b8c735
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ global.webppl = {
compile: compile,
cps: webpplCPS,
naming: webpplNaming,
analyze: analyze
analyze: analyze,
runTrampoline: require('./transforms/trampoline').runner
};

console.log('webppl loaded.');
9 changes: 8 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,18 @@ global.webpplEval = function(s, k, a, code) {
return eval.call(global, compiledCode)(s, k, a);
};

function runTrampoline(t) {
while (t) {
t = t();
}
}

module.exports = {
requireHeader: requireHeader,
requireHeaderWrapper: requireHeaderWrapper,
parsePackageCode: parsePackageCode,
run: run,
compile: compile,
analyze: analyze
analyze: analyze,
runTrampoline: runTrampoline
};
46 changes: 34 additions & 12 deletions src/transforms/trampoline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var parse = require('esprima').parse;
var fail = require('../syntax').fail;
var inProgram = require('../syntax').inProgram;
var isPrimitive = require('../syntax').isPrimitive;
var util = require('../util');


function thunkify(node) {
Expand Down Expand Up @@ -47,26 +48,47 @@ function trampoline(node) {
}
}

var driver = parse(
['(function(p) {',
' return function(s, k, a) {',
' var trampoline = p(s, k, a);',
' while (trampoline) {',
' trampoline = trampoline();',
' }',
' }',
'})'].join('\n')
).body[0].expression;
var cliTrampoline = function(t) {
while (t) {
t = t()
}
};

var webTrampoline = function f(t) {
var lastPauseTime = Date.now();
while (t) {
var currTime = Date.now();
if (currTime - lastPauseTime > 100) {
return setTimeout(function() { f(t) }, 0);
} else {
t = t();
}
}
};

var runner = util.runningInBrowser() ? webTrampoline : cliTrampoline;

var driver = parse(['(function (p) {',
' var runTrampoline = ' + runner.toString(),
' return function(s, k, a) {',
' var t = p(s, k, a);',
' runTrampoline(t);',
' }',
'})'].join('\n')
).body[0].expression;

function trampolineMain(node) {
return inProgram(function(node) {
var r = inProgram(function(node) {
return build.callExpression(driver, [replace(node, {
enter: skip,
leave: trampoline
})]);
})(node, fail('trampoline', node));

return r;
}

module.exports = {
trampoline: trampolineMain
trampoline: trampolineMain,
runner: runner
};
1 change: 1 addition & 0 deletions webppl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function compile(code, packages, verbose, outputFile) {
var compileOptions = { extra: webppl.parsePackageCode(packages, verbose), verbose: verbose };

compiledCode += (
webppl.runTrampoline.toString() + '\n' +
printWebPPLValue.toString() + '\n' +
'var topK = function(s, x){ \n' +
" console.log('\\n* Program return value:\\n'); \n" +
Expand Down

0 comments on commit 4b8c735

Please sign in to comment.