Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes become inconsistent if they're first used through a property. #13

Open
TerranceN opened this issue Oct 19, 2024 · 1 comment
Open

Comments

@TerranceN
Copy link

Using the main branch, and v8 runtime, if I have a script like:

export default class Test extends Node3D {
  _ready(): void {
    console.log("new Vector3() instanceof Vector3", new Vector3() instanceof Vector3);
    console.log("new Vector3().constructor === Vector3", new Vector3().constructor === Vector3);
    console.log("this.position instanceof Vector3", this.position instanceof Vector3);
  }
}

When loaded, correctly prints:

[JS] new Vector3() instanceof Vector3 true
[JS] new Vector3().constructor === Vector3 true
[JS] this.position instanceof Vector3 true

But if I first do anything with this.position:

export default class Test extends Node3D {
  _ready(): void {
    this.position; // Do nothing with it, just reference it
    console.log("new Vector3() instanceof Vector3", new Vector3() instanceof Vector3);
    console.log("new Vector3().constructor === Vector3", new Vector3().constructor === Vector3);
    console.log("this.position instanceof Vector3", this.position instanceof Vector3);
  }
}

I incorrectly get:

[JS] new Vector3() instanceof Vector3 false
[JS] new Vector3().constructor === Vector3 false
[JS] this.position instanceof Vector3 false

And if I just instantiate a Vector3 before both of these:

export default class Test extends Node3D {
  _ready(): void {
    new Vector3(); // Do nothing with it, just instantiate it
    this.position; // Do nothing with it, just reference it
    console.log("new Vector3() instanceof Vector3", new Vector3() instanceof Vector3);
    console.log("new Vector3().constructor === Vector3", new Vector3().constructor === Vector3);
    console.log("this.position instanceof Vector3", this.position instanceof Vector3);
  }
}

I once again correctly get:

[JS] new Vector3() instanceof Vector3 true
[JS] new Vector3().constructor === Vector3 true
[JS] this.position instanceof Vector3 true

My guess from this behaviour:

I wonder if accessing this.position first is internally causing the creation of a Vector3 type that's somehow different than the Vector3 that's imported? And maybe that's being cached so that subsequent new Vector3() calls return that cached class rather than the Vector3 from the import? And if we do new Vector3() first, then that's what gets saved to the cache (and so this.position matches the imported Vector3)?

@ialex32x
Copy link
Collaborator

It may caused by the same problem in #12. The prototype.constructor is unexpectedly changed after ObjectTemplate.GetFunction, I'm tracing it but haven't got any meaningful clues.

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

No branches or pull requests

2 participants