Skip to content

Instantly share code, notes, and snippets.

@damienbutt
Forked from josmardias/setTimeout-nashorn.js
Created December 10, 2024 21:37
Show Gist options
  • Select an option

  • Save damienbutt/9e7e65ef0b305e9c5b67bb77cff387c7 to your computer and use it in GitHub Desktop.

Select an option

Save damienbutt/9e7e65ef0b305e9c5b67bb77cff387c7 to your computer and use it in GitHub Desktop.

Revisions

  1. @josmardias josmardias revised this gist Aug 15, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions setTimeout-nashorn.js
    Original 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';
  2. @josmardias josmardias revised this gist Aug 15, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions setTimeout-nashorn.js
    Original 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';

  3. @josmardias josmardias revised this gist Aug 12, 2016. 2 changed files with 35 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions setTimeout-nashorn.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    // Adopted from here: https://gist.github.com/bripkens/8597903
    // Makes ES7 Promises polyfill work on Nashorn https://github.com/jakearchibald/es6-promise

    // https://gist.github.com/josmardias/20493bd205e24e31c0a406472330515a
    (function(context) {
    'use strict';

    @@ -11,7 +9,11 @@
    var phaser = new Phaser();

    var timeoutStack = 0;
    function tryShutdown() {
    function pushTimeout() {
    timeoutStack++;
    }
    function popTimeout() {
    timeoutStack--;
    if (timeoutStack > 0) {
    return;
    }
    @@ -21,7 +23,6 @@

    var onTaskFinished = function() {
    phaser.arriveAndDeregister();
    timeoutStack--;
    };

    context.setTimeout = function(fn, millis /* [, args...] */) {
    @@ -40,15 +41,16 @@
    print(e);
    } finally {
    onTaskFinished();
    tryShutdown();
    popTimeout();
    }
    }, millis);

    timeoutStack++;
    pushTimeout();

    return function() {
    onTaskFinished();
    canceled = true;
    popTimeout();
    };
    };

    26 changes: 26 additions & 0 deletions test2.js
    Original 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
  4. @josmardias josmardias revised this gist Aug 12, 2016. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion test.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // (TEST) /usr/java/default/bin/jjs setTimeout.js test.js
    // (TEST) /usr/java/default/bin/jjs setTimeout-nashorn.js test.js

    console.log(1)

  5. @josmardias josmardias revised this gist Aug 12, 2016. No changes.
  6. @josmardias josmardias revised this gist Aug 12, 2016. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions test.js
    Original 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
  7. @josmardias josmardias renamed this gist Aug 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion settimeout-nashorn.js → setTimeout.js
    Original 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
    // (Haven't verified how correct it is, use with care)

    (function(context) {
    'use strict';

  8. @josmardias josmardias revised this gist Aug 12, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions settimeout-nashorn.js
    Original file line number Diff line number Diff line change
    @@ -10,10 +10,6 @@
    var timer = new Timer('jsEventLoop', false);
    var phaser = new Phaser();

    var onTaskFinished = function() {
    phaser.arriveAndDeregister();
    };

    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);

    timeoutStack++;

    var phase = phaser.register();
    var canceled = false;
    timer.schedule(function() {
    @@ -37,7 +36,6 @@

    try {
    fn.apply(context, args);
    timeoutStack--;
    } catch (e) {
    print(e);
    } finally {
    @@ -46,6 +44,8 @@
    }
    }, millis);

    timeoutStack++;

    return function() {
    onTaskFinished();
    canceled = true;
  9. @josmardias josmardias revised this gist Aug 12, 2016. 1 changed file with 26 additions and 13 deletions.
    39 changes: 26 additions & 13 deletions settimeout-nashorn.js
    Original 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);
  10. @salomvary salomvary created this gist Nov 27, 2014.
    66 changes: 66 additions & 0 deletions settimeout-nashorn.js
    Original 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);