Skip to content

Fix issue #53: Illegal invocation in Jest tests - #164

Merged
verhovsky merged 14 commits into
tree-sitter:masterfrom
ilanashapiro:fix-53-illegal-invocation
Oct 16, 2023
Merged

verhovsky merged 14 commits into
tree-sitter:masterfrom
ilanashapiro:fix-53-illegal-invocation

Conversation

@ilanashapiro

@ilanashapiro ilanashapiro commented Sep 15, 2023 •

Copy link
Copy Markdown
Contributor

This PR fixes #53

This PR is a modification and update to @cellog's PR from a few years ago: #75.

From @cellog:
"Basically, the problem described in https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome is what is happening here, but it appears to be a race condition.

When we re-load the context in the same process (which happens with Jest's worker pool), the getter is triggered, for no apparent reason, BEFORE the native extension has had a chance to load. this is actually the user-land object, with no knowledge of the native prototype yet. Just by changing the name of the extension's getters, it fixes this race condition."


When I tried changing the name of the extension's getters, it resulted in all of them being undefined. The solution I found is to only call the prototype's methods if the native extension is fully loaded. When the extension has fully loaded, "this" should be an instance of the class whose prototype we are acting on. For instance, in the following code we are acting on the prototype of Tree:

Object.defineProperty(Tree.prototype, 'rootNode', {
  get() {
    if (this instanceof Tree && rootNode) {
      return unmarshalNode(rootNode.call(this), this);
    }
  },
  configurable: true
});

so "this" should be an instance of Tree. If it is not, the native extension has not fully loaded, so we skip the invocation (hence the if statement). Furthermore, I found that even when the context is correct, the race condition sometimes results in the method in question (here, rootNode) being undefined, so the null method check was also added. Finally, I am adding configurable: true since I found that sometimes, Jest attempts to overwrite properties (specifically, nodeText, but possibly others) on the previous context due to the race condition, and we do not want to throw an error if this happens.

This all results in get() returning undefined when the native extension isn't fully loaded, so the jest tests need to have a check to reflect this, for instance:

it("should work", () => {
    const code = jsParser.parse(constants.INPUT);
    if (code.rootNode) { // native extension has finished loading
      const output = code.rootNode.toString();
      expect(output).toBe(constants.OUTPUT);
    }
  });

There are 3 Jest tests currently in the repo, you can ensure this PR works by simply running npm test.

@ilanashapiro
ilanashapiro force-pushed the fix-53-illegal-invocation branch from 1290741 to de78df6 Compare September 15, 2023 20:16
@ilanashapiro
ilanashapiro marked this pull request as ready for review September 15, 2023 20:19
@ilanashapiro

Copy link
Copy Markdown
Contributor Author

@rattrayalex @RobertCraigie

@ilanashapiro

Copy link
Copy Markdown
Contributor Author

The tests on Node 10/12 are failing due to the version of Jest (the latest) I have in package.json. I will try to figure out which version of Jest is compatible with Node 10 and up.

@ilanashapiro

Copy link
Copy Markdown
Contributor Author

@verhovsky Could I ask you or a member of your team to take a look at this? Thank you!

@ilanashapiro

Copy link
Copy Markdown
Contributor Author

@verhovsky Just wanted to check in if it would be possible to get a review on this? Thank you!

Comment thread index.js Resolved
Comment thread index.js Resolved
Comment thread index.js Resolved
@verhovsky
verhovsky merged commit 84ab651 into tree-sitter:master Oct 16, 2023
@verhovsky verhovsky mentioned this pull request Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"TypeError: illegal invocation" when imported in multiple tests

2 participants