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.
This commit is contained in:
parent
ae17b8522d
commit
3c144fd553
2 changed files with 13 additions and 0 deletions
12
quickjs.c
12
quickjs.c
|
@ -9582,6 +9582,18 @@ void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id)
|
|||
return p;
|
||||
}
|
||||
|
||||
void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id)
|
||||
{
|
||||
JSObject *p;
|
||||
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
|
||||
*class_id = 0;
|
||||
return NULL;
|
||||
}
|
||||
p = JS_VALUE_GET_OBJ(obj);
|
||||
*class_id = p->class_id;
|
||||
return p->u.opaque;
|
||||
}
|
||||
|
||||
#define HINT_STRING 0
|
||||
#define HINT_NUMBER 1
|
||||
#define HINT_NONE 2
|
||||
|
|
|
@ -761,6 +761,7 @@ int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
|
|||
void JS_SetOpaque(JSValue obj, void *opaque);
|
||||
void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
|
||||
void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
|
||||
void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id);
|
||||
|
||||
/* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
|
||||
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
|
||||
|
|
Loading…
Reference in a new issue