From 6ef718c47977978e228467a8124cf678fccc88c3 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 22 Dec 2014 18:34:44 -0800 Subject: [PATCH 1/3] Test timers are undefined --- test/test_execjs.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test_execjs.rb b/test/test_execjs.rb index becda22..d093d8f 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -230,6 +230,15 @@ class TestExecJS < Test assert ExecJS.eval("typeof console == 'undefined'") end + def test_timers_are_undefined + assert ExecJS.eval("typeof setTimeout == 'undefined'") + assert ExecJS.eval("typeof setInterval == 'undefined'") + assert ExecJS.eval("typeof clearTimeout == 'undefined'") + assert ExecJS.eval("typeof clearInterval == 'undefined'") + assert ExecJS.eval("typeof setImmediate == 'undefined'") + assert ExecJS.eval("typeof clearImmediate == 'undefined'") + end + def test_compile_large_scripts body = "var foo = 'bar';\n" * 100_000 assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true") From 1d885c1e36e3a4157312e49068e21b87549b3e38 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 22 Dec 2014 18:34:53 -0800 Subject: [PATCH 2/3] Clear timers in node.js --- lib/execjs/support/node_runner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/execjs/support/node_runner.js b/lib/execjs/support/node_runner.js index 86b91d1..c27e6c0 100644 --- a/lib/execjs/support/node_runner.js +++ b/lib/execjs/support/node_runner.js @@ -1,4 +1,4 @@ -(function(program, execJS) { execJS(program) })(function(module, exports, require, console) { #{source} +(function(program, execJS) { execJS(program) })(function(module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source} }, function(program) { var output, print = function(string) { process.stdout.write('' + string); From e1a13f62b51d7a5bdf1ac094b1299f60e42b166f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 22 Dec 2014 18:39:14 -0800 Subject: [PATCH 3/3] Add timer question to faq --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 92f812c..a84f397 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,11 @@ in. If you want to access the Node API, you should check another library like [commonjs.rb](https://github.com/cowboyd/commonjs.rb) designed to provide a consistent interface. +**Why can't I use `setTimeout`** + +For similar reasons as modules, not all runtimes guarantee a full JavaScript +event loop. So `setTimeout`, `setInterval` and other timers are not defined. + # License