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

Object.assign is broken after 1.7.15 with custom ScriptableObjects #1558

Open
pavelhoral opened this issue Aug 7, 2024 · 3 comments
Open
Labels
docs Issues containing stuff that ought to be documented embedding Rhino Issues related to enbedding Rhino Regression

Comments

@pavelhoral
Copy link

pavelhoral commented Aug 7, 2024

The issue

This is related to #780, which changed how Object.assign works. That change makes sense as it aligns the implementation with the ECMA specification. However it is a breaking change for all custom ScriptableObject based classes (not only inside RhinoJS itself, but in depending projects as well).

Symptoms

Classes extending ScriptableObject quite often only override basic get / put / has / getIds methods. Trying to use Object.assign ends with error due to missing attribute slot:

js> var customNative = new Packages.org.example.javascript.CustomNative()
js> Object.assign({}, customNative)
js: Property hello not found.
        at (unknown):18

Quick solution to the issue

The first solution that comes to my mind is to make sure that missing attribute slot does not end with exception... at least not in case when called from Object.assign as the specification clearly states the following:

image

So undefined property attributes is a well defined state... the property should be skipped.

Proper solution

Previous quick solution still means 1.7.15+ brought breaking changes for custom ScriptableObject implementations. I would appreciate if there would be clear example how custom NativeObject / IdScriptableObject / ScriptableObject should be implemented... in the case of IdScriptableObject implementation I guess one needs to override findInstanceIdInfo method?

Footnote

Not sure if this is a partial duplicate of #1549. That issue's description is a bit cryptic for me.

@pavelhoral pavelhoral changed the title Object.assign is broken after 1.7.15 on NativeObjects Object.assign is broken after 1.7.15 with custom ScriptableObjects Aug 7, 2024
@p-bakker p-bakker added embedding Rhino Issues related to enbedding Rhino docs Issues containing stuff that ought to be documented labels Aug 15, 2024
@p-bakker
Copy link
Collaborator

p-bakker commented Aug 16, 2024

Added the docs tag as well, as in the end, we'll need to update the docs for implementing custom host objects as well: currently all examples are about implementing the Scriptable interface, but while that works, it doesn't result in host objects that extend Object, which is a best practice nowadays

@pavelhoral
Copy link
Author

I was playing with this a bit in a project that is using RhinoJS and I am not able to fix it on my end as NativeObject's assign method calls package private method putImpl that I am not able to override:

AbstractEcmaObjectOperations.put(

if (((ScriptableObject) base).putImpl(p, 0, o, v, isThrow)) return;

boolean putImpl(Object key, int index, Scriptable start, Object value, boolean isThrow) {

The ScriptableObject#putImpl method is tightly coupled with the private slotMap property. This prevents subclasses to intercept such call (e.g. when the object represents custom java.util.Map wrappers).

@p-bakker p-bakker added this to the Release 1.7.16 milestone Oct 3, 2024
@gbrail
Copy link
Collaborator

gbrail commented Dec 18, 2024

I would like to fix this before the releease. My current idea is:

  1. Add try-catch in the "isEnumerable" method like @pavelhoral suggests
  2. Somehow force Rhino into "strict mode" in Object.assign so that Object.assign behaves properly when not dealing with ScriptableObject subclasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Issues containing stuff that ought to be documented embedding Rhino Issues related to enbedding Rhino Regression
Projects
None yet
Development

No branches or pull requests

3 participants