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

Improve obj dup using spread syntax #7043

Merged
merged 2 commits into from
Sep 20, 2024
Merged

Conversation

cometkim
Copy link
Member

@cometkim cometkim commented Sep 19, 2024

This removes Caml_obj.obj_dup runtime primitive. The spread syntax can be a perfect alternative to obj_dup, as it doesn't change the key order.

However, this may change FFI's behavior since it no longer handles Arrays separately. it's mostly Arrays extended with additional fields, so upcasting to Object makes sense IMO.

It's not an ideal implementation, but I'd like to merge first to finish #6984. It should be integrated into Caml_block, not Object.

Currently, the output looks like:

-let newrecord = Caml_obj.obj_dup(x);
+let newrecord = {...x};
newrecord.x0 = 1;
return newrecord;

But later:

return {
  ...x,
  x0: 1
};

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks great.
Not sure I understand the comments about FFI.
Changes in current tests seem harmless, but perhaps I'm not fully getting what the concern is.

@cometkim
Copy link
Member Author

Not sure I understand the comments about FFI.
Changes in current tests seem harmless, but perhaps I'm not fully getting what the concern is.

When using %identity of array values, previously the result of obj_dup(array) keep Array class, but now { ...array } cast it to a plain object. The behavior on the ReScript side is fine, but on the JS side the result of Array.isArray will be changed.

@cristianoc
Copy link
Collaborator

Not sure I understand the comments about FFI.
Changes in current tests seem harmless, but perhaps I'm not fully getting what the concern is.

When using %identity of array values, previously the result of obj_dup(array) keep Array class, but now { ...array } cast it to a plain object. The behavior on the ReScript side is fine, but on the JS side the result of Array.isArray will be changed.

And was that a desired behaviour in FFI or a programming mistake of mixing arrays and objects, that now will have different semantics?

@cometkim
Copy link
Member Author

Well, the existing obj_dup implementation was clearly handling it intentionally.

One case I found during experiments was Caml_int64.discard_sign,
https://github.com/rescript-lang/rescript-compiler/blob/1bc9e03/jscomp/runtime/caml_int64.res#L524
which has an internal representation of [hi, lo], but is treated as a record in ReScript code. Therefore, the int64 literal and the cloned value will have different prototypes.

In a real-world case, a pattern in JS like Object.assign([ ... ], { extra_fields }) (e.g. RegExpMatchArray), the existing obj_dup is problematic too because it discards extra_fields.

So yes, this PR will change the semantics. We have to choose whether to preserve the prototype of the array or preserve the data.

@cristianoc
Copy link
Collaborator

Well, the existing obj_dup implementation was clearly handling it intentionally.

One case I found during experiments was Caml_int64.discard_sign,

https://github.com/rescript-lang/rescript-compiler/blob/1bc9e03/jscomp/runtime/caml_int64.res#L524

which has an internal representation of [hi, lo], but is treated as a record in ReScript code. Therefore, the int64 literal and the cloned value will have different prototypes.

In a real-world case, a pattern in JS like Object.assign([ ... ], { extra_fields }) (e.g. RegExpMatchArray), the existing obj_dup is problematic too because it discards extra_fields.

So yes, this PR will change the semantics. We have to choose whether to preserve the prototype of the array or preserve the data.

Ok so it's a semantic change that removes a hack.
That sounds like a good semantic change.

@cometkim
Copy link
Member Author

I'm merging this to continue work at #6984 , but I have to revisit this to remove newrecord bindings. I need to dive deeper into the compiler backend for that.

@cometkim
Copy link
Member Author

@cknitt @cristianoc Should we note this as a breaking change?

@cknitt
Copy link
Member

cknitt commented Sep 20, 2024

@cknitt @cristianoc Should we note this as a breaking change?

It is only breaking in the unlikely case that people misused this using %identity on arrays, right?
So from my point of view it is fine in the CHANGELOG as it is.

Thanks a lot for your work!

@cometkim
Copy link
Member Author

It is only breaking in the unlikely case that people misused this using %identity on arrays, right?
So from my point of view it is fine in the CHANGELOG as it is.

Right. IMHO it's very rare that this change is a problem, in fact it often fixes (another) problem, like copying such RegExpMatchArray stuff.

@cometkim cometkim merged commit 7a722a1 into rescript-lang:master Sep 20, 2024
20 checks passed
@cometkim cometkim deleted the obj-dup branch September 20, 2024 17:43
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.

3 participants