Skip to content

Issues with new SetOpaque API #695

@bptato

Description

@bptato

Attempting to upgrade to the new version (?), I've noticed some oddities with SetOpaque:

  • Since Prevent JS_SetOpaque from overriding internal class state #658 it can fail. But it returns JS_FALSE (0) on success and JS_TRUE (1) on failure, which I find somewhat counter-intuitive. Maybe it's not too late to change it?
  • Previously, I could set an opaque on the global object, but now I can't because it's a 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:

diff --git a/quickjs.c b/quickjs.c
index 9cac6de..e268ce3 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -10102,7 +10102,8 @@ JS_BOOL JS_SetOpaque(JSValue obj, void *opaque)
     if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
         p = JS_VALUE_GET_OBJ(obj);
         // User code can't set the opaque of internal objects.
-        if (p->class_id >= JS_CLASS_INIT_COUNT) {
+        if (p->class_id >= JS_CLASS_INIT_COUNT ||
+            p->class_id == JS_CLASS_OBJECT) {
             p->u.opaque = opaque;
             return 0;
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions