Skip to content

Decouple Object from Value #577

Closed
Closed

Description

@HalidOdat

What is the problem with the current implementation?
The problem is that we treat Value as an Object which is not always the case. for example Value has a .set_field() which sets an object field, this does not make sense for a couple of reasons:

  • what if Value is not an object should calling .set_field panic?, should it ignore it? should it return an option or result? or something else? (we ignore non object values which can become a really hard bug to debug), as we can see it does not make sense for Value to have Object behaviour
  • This also forces us to do checks if the value is an object.

If we need object behaviour we can call as_object method.

Changes:

  • Move all object behaivour from Value to Object
  • Places were we need (or places were the type is only an object for example: the global object) should be GcObjects
  • function like .to_object should return a GcObject not Value done

Doing these thing should remove unnecessary checks and make the API more intuitive

Any suggestions on how to improve this?

Activity

  1. added
    Help Wanted
    A-Technical Debt
    R-Discussion
    A-Execution
    on Jul 19, 2020
  2. added this to the v0.10.0 milestone on Jul 19, 2020
  3. self-assigned this
    on Jul 19, 2020
  4. added
    A-Performance
    E-Hard
    on Jul 19, 2020
  5. RageKnify commented on Sep 14, 2020

    @RageKnify
    Contributor

    Hey @HalidOdat I found this interesting and am trying to tackle it.
    To ensure I'm following your "vision", methods from builtins like Array would stop receiving &Value as this and instead receive &Object/&mut Object, right?

    Ps: Also, functions like Array::new_array would change to return Result<GcObject> or Result<Object> rather than Result<Value>, or no?

  6. jasonwilliams commented on Sep 15, 2020

    @jasonwilliams
    Member

    @HalidOdat how are you handling boxing, unboxing of primitive values? I think I left set_field and get_field on value so things like “hello world ”.trim() would still work

  7. HalidOdat commented on Sep 15, 2020

    @HalidOdat
    MemberAuthor

    @HalidOdat how are you handling boxing, unboxing of primitive values? I think I left set_field and get_field on value so things like “hello world ”.trim() would still work

    We store some primitives on the stack (number, boolean, undefined, null) and string, bigint, symbol on the heap with Rcs (since they can't have cycles, less work for the gc) and object with Gc. we discussed this in #465 .

    Why would removing .set_field and .get_field cause “hello world ”.trim() to break?

  8. jasonwilliams commented on Sep 15, 2020

    @jasonwilliams
    Member

    Ok sounds good.

    Why would removing .set_field and .get_field cause “hello world ”.trim() to break?

    I think originally the unboxing happened inside of get_field() where the value was checked and upgraded to an object of it was a primitive. .trim() would have called get_field(“trim”)

    I think we refactored quite a few times since this though so you can ignore this.

  9. HalidOdat commented on Sep 15, 2020

    @HalidOdat
    MemberAuthor

    Hey @HalidOdat I found this interesting and am trying to tackle it.
    To ensure I'm following your "vision", methods from builtins like Array would stop receiving &Value as this and instead receive &Object/&mut Object, right?

    Changing this from &Value to anything else wont work, since they would not match the NativeFunction signature, and we should not change the NativeFunction to take object since this can be some value that is not an object.

    We need to the check as described in the spec for when this is not a object for example: Array.prototype.push

    1. Let O be ? ToObject(this value).
    2. Let len be ? LengthOfArrayLike(O).

    It calls .to_object on this which converts it to a object (if it is not already), so we handle the case for when this is not an object but .set_field and .get_field does not and just ignore it if it isn't an object, this is not spec compliant.
    There is also another problem that to_object returns a Result<Value> instead of Result<GcObject>, there are many places where this happens and that's why is why it's hard to remove them directly (generates ~1000 errors). Many PRs I did were to make this transition easier. So the way I would suggest on doing this, would be in small incremental changes, doing it all at once is very hard. For example making a PR so .to_object returns a Result<GcObject> and another PR fixing a something else and so on.

    Ps: Also, functions like Array::new_array would change to return Result<GcObject> or Result<Object> rather than Result<Value>, or no?

    If it is a helper function then, yes it should.

  10. RageKnify commented on Sep 15, 2020

    @RageKnify
    Contributor

    Yeah, I started at .to_object, I'll try to make it in small stages like you said.

    One thing I was wondering, would the global_obj in Realm become an Object/GcObject or would it remain a Value?
    And if it does change, which would be more aproppriate?

  11. 16 remaining items

  12. added this to the v0.13.0 milestone on May 22, 2021
  13. modified the milestones: v0.13.0, v0.14.0 on Sep 25, 2021
  14. Razican commented on Feb 5, 2022

    @Razican
    Member

    I don't think this will be ready for 0.14, so I'm moving it out.

  15. modified the milestones: v0.14.0, v0.15.0 on Feb 5, 2022
  16. modified the milestones: v0.15.0, v0.16.0 on Jun 1, 2022
  17. modified the milestones: v0.16.0, v0.17.0 on Sep 19, 2022
  18. HalidOdat commented on Nov 17, 2022

    @HalidOdat
    MemberAuthor

    Many refactors/changes have been done since this issue was created and they have been resolved. So closing this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-ExecutionA-PerformanceA-Technical DebtE-HardHelp WantedR-Discussion

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions