Skip to content

Commit 3c144fd

Browse files
oleavrsaghul
authored andcommitted
Add JS_GetAnyOpaque() to support polymorphism
To be able to check if the class ID is one of multiple known ones, where the data has a common structure.
1 parent ae17b85 commit 3c144fd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

quickjs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9582,6 +9582,18 @@ void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id)
95829582
return p;
95839583
}
95849584

9585+
void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id)
9586+
{
9587+
JSObject *p;
9588+
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
9589+
*class_id = 0;
9590+
return NULL;
9591+
}
9592+
p = JS_VALUE_GET_OBJ(obj);
9593+
*class_id = p->class_id;
9594+
return p->u.opaque;
9595+
}
9596+
95859597
#define HINT_STRING 0
95869598
#define HINT_NUMBER 1
95879599
#define HINT_NONE 2

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
761761
void JS_SetOpaque(JSValue obj, void *opaque);
762762
void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
763763
void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
764+
void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id);
764765

765766
/* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
766767
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,

0 commit comments

Comments
 (0)