-
Notifications
You must be signed in to change notification settings - Fork 171
Issues with new SetOpaque API #695
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
Comments
Hey there! Many APIs return 0 for success and -1 for error. I guess we could align it to that. JS_BOOL is indeed probably not the right return type here. I took a quick look and JS_CLASS_OBJECT does not seem to use the union. That said, what is it that you are using the opaque for? If it's for hiding stuff perhaps a Symbol would do? Or the context / runtime opaque? Since there is only one global... |
Sounds good :)
It's for object conversion: I could check if |
Returns < 0 on failure. Also document it in the header file. Fixes: #695
I opened #696 to better align the API. I gave it some thought and I think I'd like to keep it as is, that is, only supporting custom classes. Arrays, Maps, Sets, are objects too but not quite JS_CLASS_OBJECT, so it wouldn't be obvious why it doesn't work there. Making it work with plain objects too would make things a bit more confusing IMHO. Right now it's easy to explain when it works and when it doesn't.
Well, instead we would have to do the special casing ;-) @bnoordhuis WDYT? |
The flip side is we'd have to make JSObject bigger to support JS_SetOpaque on arbitrary objects. Then everyone is worse off. |
Indeed. The corner case here is that plain objects don't use the union so the opaque is technically available, but I think it's best to not support it and have the simple rule of "just custom classes". |
Well, now you have to explain that there is a breaking change lurking in there ;) I can imagine code that also uses it for regular objects other than the global one, and that would also break. But maybe I'm the only one.
Fair enough.
To clarify: SetOpaque had always worked with JS_CLASS_OBJECT, without the need for an extra pointer. QJS only uses the opaque for its own object classes. |
When used with other types of objects it would cause memory corruption because it overwrote the memory as opaque is part of the union. That was the reasoning for the initial change. |
I know, I've read the linked PR & issue. By regular objects I mean objects with class_id |
It doesn't, but that is by chance really. Let's wait a bit and see. The change only got released yesterday and we have a single data point so far :-) Understanding how this impacts different users would go a long way here. From my side, txiki.js is not impacted because I actually didn't know we could set it on any object to begin with 😅 |
Well, my code works because I had checked the source before using the API. No chance involved :p
OK. To note, my use case would also be satisfied if we had an API to change the class of the global object instead. I think I even implemented that at some point, but it didn't bring the benefits I'd expected (exotics) so I discarded the patch :/ |
Returns < 0 on failure. Also document it in the header file. Fixes: quickjs-ng/quickjs#695
Attempting to upgrade to the new version (?), I've noticed some oddities with SetOpaque:
JS_FALSE
(0) on success andJS_TRUE
(1) on failure, which I find somewhat counter-intuitive. Maybe it's not too late to change it?JS_CLASS_OBJECT
which is <JS_CLASS_INIT_COUNT
.The latter one is a bit problematic, because my type conversion functions expect the global object to have an opaque. I would change its class ID if I could, but at least when I was writing my wrapper for qjs I couldn't, and I don't see any changes in -ng that would enable this.
I don't know what the preferred fix is for this one; this patch works for me:
The text was updated successfully, but these errors were encountered: