Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Can't overwrite global setTimeout and setInterval in jxcore #86

Open
chapko opened this issue Dec 18, 2016 · 2 comments
Open

Can't overwrite global setTimeout and setInterval in jxcore #86

chapko opened this issue Dec 18, 2016 · 2 comments
Assignees

Comments

@chapko
Copy link

chapko commented Dec 18, 2016

Test case:

// test.js
'use strict';
var assert = require('assert');

global.setTimeout = function () {};
assert.equal(global.setTimeout, setTimeout);

console.log('OK');

Output:

$ node test.js
v7.2.1
OK

$ n use 0.10.40 test.js
v0.10.40
OK

$ jx test.js
v0.10.40

assert.js:105
  throw new assert.AssertionError({
        ^
AssertionError: "function () {}" == "function () {\n      var t = NativeModule.require('timers');\n      return t.setTimeout.apply(this, arguments);\n    }"
    at Object.<anonymous> (/home/tmp/Thali_CordovaPlugin/test/www/jxcore/test1.js:6:8)
    at Module._compile (module.js:619:26)
    at Object.Module._extensions..js (module.js:649:10)
    at Module.load (module.js:440:36)
    at Function.Module._load (module.js:405:12)
    at Function.Module.runMain (module.js:878:12)
    at startup (node.js:492:18)
    at node.js:1611:3

I've discovered it when trying to use sinon's fake timers. They simply doesn't work in jxcore. It affects at least setTimeout and setInterval functions. Other global variables (like Date, Number, setImmediate) are fine.

I'm not sure about priority of this bug. I had to changesetTimeout to global.setTimeout everywhere in my code in order to make everything work. So for now I can use this workaround.

@chapko
Copy link
Author

chapko commented Dec 18, 2016

The issue comes from here and here.

  // TODO(obastemur) check ION again and remove the below hack or fix it.
  // -- I personally suspect ION skips it mostly because of multiple references
  //    to globals?
  //
  // experimental hack
  // ionSpew may skip ION compilation when there is a global member used.
  // sending the popular references: setTimeout, setInterval, and process
  var args = [self.exports, require, self, filename, dirname,
              global.setTimeout, global.setInterval, global.process];
  return compiledWrapper.apply(self.exports, args);
  NativeModule.wrapper = [
    '(function (exports, require, module, __filename, __dirname, ' +
        'setTimeout, setInterval, process) { ',
    '\n});'];

In jxcore setTimeout, setInterval, and process are local module variables, but in node they are global.

@chapko
Copy link
Author

chapko commented Mar 20, 2017

One more difference between node and jxcore:

var timers = require('timers');
console.log(global.setTimeout === timers.setTimeout);

jxcore prints false while node prints true

opetkevich added a commit to thaliproject/Thali_CordovaPlugin that referenced this issue Mar 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants