-
-
Save damienbutt/9e7e65ef0b305e9c5b67bb77cff387c7 to your computer and use it in GitHub Desktop.
Revisions
-
josmardias revised this gist
Aug 15, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ // https://gist.github.com/josmardias/20493bd205e24e31c0a406472330515a // at least one timeout needs to be set, larger then your code bootstrap // or Nashorn will run forever // preferably, put a timeout 0 after your code bootstrap (function(context) { 'use strict'; -
josmardias revised this gist
Aug 15, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ // https://gist.github.com/josmardias/20493bd205e24e31c0a406472330515a // at least one timeout needs to be set, larger then your code bootstrap // or Nashorn will run forever (function(context) { 'use strict'; -
josmardias revised this gist
Aug 12, 2016 . 2 changed files with 35 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ // https://gist.github.com/josmardias/20493bd205e24e31c0a406472330515a (function(context) { 'use strict'; @@ -11,7 +9,11 @@ var phaser = new Phaser(); var timeoutStack = 0; function pushTimeout() { timeoutStack++; } function popTimeout() { timeoutStack--; if (timeoutStack > 0) { return; } @@ -21,7 +23,6 @@ var onTaskFinished = function() { phaser.arriveAndDeregister(); }; context.setTimeout = function(fn, millis /* [, args...] */) { @@ -40,15 +41,16 @@ print(e); } finally { onTaskFinished(); popTimeout(); } }, millis); pushTimeout(); return function() { onTaskFinished(); canceled = true; popTimeout(); }; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ // (TEST) /usr/java/default/bin/jjs setTimeout-nashorn.js test2.js console.log('start') var i = 0; t1 = setInterval(function () { console.log('i-' + ++i); }, 10) t2 = setTimeout(function () { clearInterval(t1) console.log('end') }, 100) // OUTPUT: // start // i-1 // i-2 // i-3 // i-4 // i-5 // i-6 // i-7 // i-8 // end -
josmardias revised this gist
Aug 12, 2016 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewing
File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ // (TEST) /usr/java/default/bin/jjs setTimeout-nashorn.js test.js console.log(1) -
josmardias revised this gist
Aug 12, 2016 . No changes.There are no files selected for viewing
-
josmardias revised this gist
Aug 12, 2016 . 1 changed file with 20 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ // (TEST) /usr/java/default/bin/jjs setTimeout.js test.js console.log(1) t1 = setTimeout(function () { console.log(4); }, 300) console.log(2) t2 = setTimeout(function () { console.log(3); }, 200) clearTimeout(t1) // OUTPUT: // 1 // 2 // 3 -
josmardias renamed this gist
Aug 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ // Adopted from here: https://gist.github.com/bripkens/8597903 // Makes ES7 Promises polyfill work on Nashorn https://github.com/jakearchibald/es6-promise (function(context) { 'use strict'; -
josmardias revised this gist
Aug 12, 2016 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,10 +10,6 @@ var timer = new Timer('jsEventLoop', false); var phaser = new Phaser(); var timeoutStack = 0; function tryShutdown() { if (timeoutStack > 0) { @@ -23,11 +19,14 @@ phaser.forceTermination(); } var onTaskFinished = function() { phaser.arriveAndDeregister(); timeoutStack--; }; context.setTimeout = function(fn, millis /* [, args...] */) { var args = [].slice.call(arguments, 2, arguments.length); var phase = phaser.register(); var canceled = false; timer.schedule(function() { @@ -37,7 +36,6 @@ try { fn.apply(context, args); } catch (e) { print(e); } finally { @@ -46,6 +44,8 @@ } }, millis); timeoutStack++; return function() { onTaskFinished(); canceled = true; -
josmardias revised this gist
Aug 12, 2016 . 1 changed file with 26 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,64 +3,77 @@ // (Haven't verified how correct it is, use with care) (function(context) { 'use strict'; var Timer = Java.type('java.util.Timer'); var Phaser = Java.type('java.util.concurrent.Phaser'); var timer = new Timer('jsEventLoop', false); var phaser = new Phaser(); var onTaskFinished = function() { phaser.arriveAndDeregister(); }; var timeoutStack = 0; function tryShutdown() { if (timeoutStack > 0) { return; } timer.cancel(); phaser.forceTermination(); } context.setTimeout = function(fn, millis /* [, args...] */) { var args = [].slice.call(arguments, 2, arguments.length); timeoutStack++; var phase = phaser.register(); var canceled = false; timer.schedule(function() { if (canceled) { return; } try { fn.apply(context, args); timeoutStack--; } catch (e) { print(e); } finally { onTaskFinished(); tryShutdown(); } }, millis); return function() { onTaskFinished(); canceled = true; }; }; context.clearTimeout = function(cancel) { cancel(); }; context.setInterval = function(fn, delay /* [, args...] */) { var args = [].slice.call(arguments, 2, arguments.length); var cancel = null; var loop = function() { cancel = context.setTimeout(loop, delay); fn.apply(context, args); }; cancel = context.setTimeout(loop, delay); return function() { cancel(); }; }; context.clearInterval = function(cancel) { cancel(); }; })(this); -
salomvary created this gist
Nov 27, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ // Adopted from here: https://gist.github.com/bripkens/8597903 // Makes ES7 Promises polyfill work on Nashorn https://github.com/jakearchibald/es6-promise // (Haven't verified how correct it is, use with care) (function(context) { 'use strict'; var Timer = Java.type('java.util.Timer'); var Phaser = Java.type('java.util.concurrent.Phaser'); var timer = new Timer('jsEventLoop', false); var phaser = new Phaser(); var onTaskFinished = function() { phaser.arriveAndDeregister(); }; context.setTimeout = function(fn, millis /* [, args...] */) { var args = [].slice.call(arguments, 2, arguments.length); var phase = phaser.register(); var canceled = false; timer.schedule(function() { if (canceled) { return; } try { fn.apply(context, args); } catch (e) { print(e); } finally { onTaskFinished(); } }, millis); return function() { onTaskFinished(); canceled = true; }; }; context.clearTimeout = function(cancel) { cancel(); }; context.setInterval = function(fn, delay /* [, args...] */) { var args = [].slice.call(arguments, 2, arguments.length); var cancel = null; var loop = function() { cancel = context.setTimeout(loop, delay); fn.apply(context, args); }; cancel = context.setTimeout(loop, delay); return function() { cancel(); }; }; context.clearInterval = function(cancel) { cancel(); }; })(this);