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

Add JS_IsPlainObject function #959

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

harumazzz
Copy link

@harumazzz harumazzz commented Mar 9, 2025

Used to check if an object is actually a JS object, since JS_IsObject only checked if the tag of a JSValue is JS_TAG_OBJECT.

@saghul
Copy link
Contributor

saghul commented Mar 9, 2025

Perhaps this should be called IsPlainObject?

@harumazzz
Copy link
Author

I have changed the function name to JS_IsPlainObject now.

@harumazzz harumazzz changed the title Add JS_IsActualObject function Add JS_IsPlainObject function Mar 9, 2025
Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

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

LGTM. @bnoordhuis WDYT?

@bnoordhuis
Copy link
Contributor

@harumazzz what (or where) do you intend to use this? What you're doing here is very close to obj.constructor === Object but when is that useful?

@harumazzz
Copy link
Author

harumazzz commented Mar 9, 2025

In my project, I used this check for my own JSON parse/stringify and nested deep clone. There is not much case we would need this API, however since we already got JS_IsArray, I think we should also have JS_IsPlainObject?

@bnoordhuis
Copy link
Contributor

What I'm trying to understand is under what conditions specifically you'd branch on JS_IsPlainObject() rather than calling e.g. JS_ToObject() and see if the conversion succeeds.

The latter corresponds to the ToObject abstract operation from the ECMA spec and is the de facto and idiomatic way of dealing with "maybe object, maybe not" values.

@harumazzz
Copy link
Author

harumazzz commented Mar 9, 2025

I think calling JS_ToObject and checking for result is rather more difficult than explicitly call JS_ToPlainObject because the additional checking, and we have to call JS_FreeValue in order to prevent memory leak. In C++, this will be a lot helpful since metaprogramming will generate objects, and reflecting plain object/class to a C/C++ struct require additional checking first, and this function perfectly suited.

@bnoordhuis
Copy link
Contributor

That's not a very compelling argument. In the balance, I think it's best if we don't merge this.

On the one hand, this API is a minor addition that cannot do much harm. On the other hand, if, by your own admission, the primary use case is to skirt on writing a few extra lines of code that make for a more cohesive user experience, then it's definitely not a great API.

@chqrlie
Copy link
Collaborator

chqrlie commented Mar 9, 2025

@harumazzz what (or where) do you intend to use this? What you're doing here is very close to obj.constructor === Object but when is that useful?

Indeed it is a weaker predicate than obj.constructor === Object.

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.

4 participants