Skip to content

Uncaught TypeError when calling method on global object #1987

Closed

Description

@lupd

Calling a method of the global object (using this/ globalThis) results in a TypeError.

To Reproduce
This JavaScript code reproduces the issue:

this.hasOwnProperty("length")

Expected behavior
true is printed

Actual behavior
Uncaught "TypeError": "not a callable function" is printed.

Additional context
At least one test in the test suite requires the expected behavior above.

Activity

  1. added
    A-Bug
    on Mar 29, 2022
  2. added this to the v0.16.0 milestone on Aug 22, 2022
  3. Razican commented on Aug 22, 2022

    @Razican
    Member

    It turns out that we have two bugs related to the global object:

    • When we initialize builtins, they are added to the Global environment record as bindings, not as properties of the global object, which means that this and globalThis are empty objects ({}).
    • The global object has no prototype. This is required by the spec, and as far as I can tell, it should probably be the Object prototype, since hasOwnProperty() and others are part of the Object prototype.

    The first issue is relatively easy to fix: we can just insert properties in the global object when we initialize builtins. In theory, we should not create bindings for these, and the global environment should check if a variable is part of the global object or one of the global bindings, but removing the insertion into the global bindings breaks around 50k tests.

    The second issue is trickier, since when the global object gets constructed, the Object built-in doesn't exist, so we cannot easily assign its prototype to it. I will publish a PR that solves the first issue, but the second one will need some extra thought.

  4. added
    A-Execution
    on Aug 22, 2022
  5. moved this to In Progress in Boa pre-v1on Aug 22, 2022
  6. raskad commented on Aug 23, 2022

    @raskad
    Member
    • When we initialize builtins, they are added to the Global environment record as bindings, not as properties of the global object, which means that this and globalThis are empty objects ({}).
    • The global object has no prototype. This is required by the spec, and as far as I can tell, it should probably be the Object prototype, since hasOwnProperty() and others are part of the Object prototype.

    The way the global object currently works is, that it uses special internal object methods defiend in GLOBAL_INTERNAL_METHODS in boa_engine/src/object/internal_methods/global.rs. That way it can use the global environment bindings as properties. The implementation currently completeley ignores the objects prototype. The best example is global_has_property where only the global property map is checked while the spec suggests that the object's prototype should be checked if no property is found on the object itself.

    I think the best way to fix this issue is to add a global_prototype field on the Realm (same principle as global_property_map and global_extensible). Then the internal methods for the global object must be adjusted to use this global prototype in the same way that ordinary internal methods use the objects prototype. Hopefully this can be implemented without impacting the performance of those methods.
    Additionally the internal methods __get_prototype_of__, __set_prototype_of__ and __own_property_keys__ must be implemented for global object as they are currently taken from the ordinary methods.

    While the spec currently does not define that the prototype of the global object should be Object.prototype, there is an open issue that attempts to make sure that it is somewhere in the prototype chain tc39/ecma262#1967.

  7. self-assigned this
    on Sep 12, 2022
  8. Repository owner moved this from In Progress to Done in Boa pre-v1on Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-BugA-Execution

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions