Skip to content

Commit

Permalink
Merge pull request #840 from null-a/fix-compile
Browse files Browse the repository at this point in the history
Initialize store before running compiled code.
  • Loading branch information
stuhlmueller committed May 1, 2017
2 parents 45876ad + 3bb4b26 commit e54d87e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
60 changes: 35 additions & 25 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function prepare(codeAndAssets, k, options) {
errors.extendError(error, codeAndAssets, currentAddress);
// Trigger clean-up. When using the mongo store, the process won't
// exit until the connection is closed.
cleanup(_.identity);
runtimeTeardown(_.noop);
throw error;
};
var allErrorHandlers = [defaultHandler].concat(options.errorHandlers);
Expand All @@ -229,31 +229,9 @@ function prepare(codeAndAssets, k, options) {
var baseRunner = options.baseRunner || util.trampolineRunners[util.runningInBrowser() ? 'web' : 'cli']();
var runner = wrapRunner(baseRunner, allErrorHandlers);

// We store the trampoline runner so that header functions that call
// external asynchronous functions can resume execution in callbacks.
global.resumeTrampoline = runner;

// Before the program finishes, we tell the param store to finish up
// gracefully (e.g., shutting down a connection to a remote store).
var cleanup = params.stop;
var finish = function(s, x) {
return cleanup(function() {
return k(s, x);
});
};

var run = function() {
// We reset env since a previous call to run may have raised an
// exception and left an inference coroutine installed.
env.reset();
// We initialize the parameter store (e.g., connecting to a remote
// store, retrieving params).
params.init(function() {
var wpplFn = eval.call(global, codeAndAssets.code)(currentAddress)(runner);
var initialAddress = '';
return wpplFn(options.initialStore, finish, initialAddress);
});
util.resetWarnings();
var main = eval.call(global, codeAndAssets.code);
runEvaled(main, runner, currentAddress, options.initialStore, k, '');
};

return { run: run };
Expand All @@ -265,6 +243,37 @@ function run(code, k, options) {
util.timeif(options.verbose, 'run', prepare(codeAndAssets, k, options).run);
}

function runEvaled(main, runner, addressCell, s, k, a) {
function finish(s, x) {
runtimeTeardown(function() {
return k(s, x);
});
}
runtimeSetup(runner, function() {
main(addressCell)(runner)(s, finish, a);
});
}

function runtimeSetup(runner, cont) {
// We store the trampoline runner so that header functions that call
// external asynchronous functions can resume execution in
// callbacks.
global.resumeTrampoline = runner;
// We reset env since a previous call to run may have raised an
// exception and left an inference coroutine installed.
env.reset();
util.resetWarnings();
// We initialize the parameter store (e.g., connecting to a remote
// store, retrieving params).
params.init(cont);
}

function runtimeTeardown(cont) {
// Before the program finishes, we tell the param store to finish up
// gracefully (e.g., shutting down a connection to a remote store).
params.stop(cont);
}

// Make webppl eval available within webppl
// runner is one of 'cli','web'
global.webpplEval = function(s, k, a, code, runnerName) {
Expand All @@ -290,5 +299,6 @@ module.exports = {
parsePackageCode: parsePackageCode,
prepare: prepare,
run: run,
runEvaled: runEvaled,
compile: compile
};
2 changes: 1 addition & 1 deletion webppl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function compile(code, packages, verbose, debug, programFile, outputFile) {
'var __runner__ = util.trampolineRunners.cli();',
topK.toString() + ';',
'var main = ' + compiledBody + '\n',
"main({})(__runner__)({}, topK, '');"
"webppl.runEvaled(main, __runner__, {}, {}, topK, '');"
]);

// Write Javascript code to file
Expand Down

0 comments on commit e54d87e

Please sign in to comment.