Skip to content

Commit

Permalink
Begin testing harness: Add support for keeping heap between executions
Browse files Browse the repository at this point in the history
This will permit us to run the test262 prelude in a separate context
from the testcase itself.
This was a bug in the original JSRef, see issue #9:
resource-reasoning/jscert_dev#9
  • Loading branch information
IgnoredAmbience committed Oct 6, 2016
1 parent 6bc4e94 commit 7743528
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions esprima-to-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API$revision/707671

function esprimaToAST(prog, sourceText, filename) {
filename = filename === undefined ? "" : filename;

var returnSourceText = filename.startsWith("_eval_");

Expand Down
14 changes: 11 additions & 3 deletions generator/tests/jsref/JsInterpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4171,10 +4171,18 @@ and run_call s c l vthis args =
let arguments_ = (LibList.append boundArgs args) in run_call s c target boundThis arguments_
| Coq_call_prealloc b -> run_call_prealloc s c b vthis args

(** val run_javascript : prog -> result **)
(** val run_javascript_with_state : prog -> state -> result **)

and run_javascript p =
and run_javascript_from_state s p =
let c = execution_ctx_initial (prog_intro_strictness p) in
let%void s_2 =
execution_ctx_binding_inst state_initial c Coq_codetype_global None p [] in
execution_ctx_binding_inst s c Coq_codetype_global None p [] in
run_prog s_2 c p

and run_javascript_from_result w p =
if_success w (fun s _ -> run_javascript_from_state s p)

(** val run_javascript : prog -> result **)

and run_javascript p =
run_javascript_from_state state_initial p
32 changes: 32 additions & 0 deletions test/interpreter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

var esprima = require('esprima');
var esprimaToAST = require('../esprima-to-ast.js').esprimaToAST;
var JsInterpreter = require('../generator/tests/jsref/assembly.js');

// Stub logging functions
['ctx_empty', 'ctx_push', 'log_event'].forEach(f => global[f] = function(){})

var parse = function(source) {
return esprimaToAST(esprima.parse(source, {loc: true, range: true}), source);
}

var run = function(program) {
return JsInterpreter.run_javascript(program);
};

// Functions to run multiple programs in the same heap
// Useful for correctly loading the test262 prelude before the testcase
var run_multiple = function(programs) {
if(programs.length < 1) { throw "No programs" }
var firstResult = run(programs.shift());
return programs.reduce(JsInterpreter.run_javascript_from_result, firstResult);
};

var parse_and_run_multiple = function(sources) {
var programs = sources.map(parse);
return run_multiple(programs);
}

console.dir(parse_and_run_multiple(["var x = 10", "x+1"]), {depth: 6, colors: true});
console.dir(parse_and_run_multiple(["throw 10", "x+1"]), {depth: 6, colors: true});

0 comments on commit 7743528

Please sign in to comment.