You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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?
@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
@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?
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.
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
Let O be ? ToObject(this value).
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?
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?
Activity
RageKnify commentedon Sep 14, 2020
Hey @HalidOdat I found this interesting and am trying to tackle it.
To ensure I'm following your "vision", methods from builtins like
Arraywould stop receiving&Valueasthisand instead receive&Object/&mut Object, right?Ps: Also, functions like
Array::new_arraywould change to returnResult<GcObject>orResult<Object>rather thanResult<Value>, or no?jasonwilliams commentedon Sep 15, 2020
@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 workHalidOdat commentedon Sep 15, 2020
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_fieldand.get_fieldcause“hello world ”.trim()to break?jasonwilliams commentedon Sep 15, 2020
Ok sounds good.
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.
HalidOdat commentedon Sep 15, 2020
Changing
thisfrom&Valueto anything else wont work, since they would not match theNativeFunctionsignature, and we should not change theNativeFunctionto take object sincethiscan 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.pushIt calls
.to_objectonthiswhich converts it to a object (if it is not already), so we handle the case for whenthisis not an object but.set_fieldand.get_fielddoes not and just ignore it if it isn't an object, this is not spec compliant.There is also another problem that
to_objectreturns aResult<Value>instead ofResult<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_objectreturns aResult<GcObject>and another PR fixing a something else and so on.If it is a helper function then, yes it should.
RageKnify commentedon Sep 15, 2020
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_objinRealmbecome anObject/GcObjector would it remain aValue?And if it does change, which would be more aproppriate?
16 remaining items
Razican commentedon Feb 5, 2022
I don't think this will be ready for 0.14, so I'm moving it out.
Removing internal deprecated functions (#1854)
Removing internal deprecated functions (#1854)
Make iterator spec complaint (#1889)
HalidOdat commentedon Nov 17, 2022
Many refactors/changes have been done since this issue was created and they have been resolved. So closing this :)