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:
Ole André Vadla Ravnås 2020-10-11 04:34:57 +02:00 committed by Saúl Ibarra Corretgé
parent ae17b8522d
commit 3c144fd553
2 changed files with 13 additions and 0 deletions

View file

@ -9582,6 +9582,18 @@ void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id)
return p; 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_STRING 0
#define HINT_NUMBER 1 #define HINT_NUMBER 1
#define HINT_NONE 2 #define HINT_NONE 2

View file

@ -761,6 +761,7 @@ int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
void JS_SetOpaque(JSValue obj, void *opaque); void JS_SetOpaque(JSValue obj, void *opaque);
void *JS_GetOpaque(JSValueConst obj, JSClassID class_id); void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
void *JS_GetOpaque2(JSContext *ctx, 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'. */ /* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,