From 44e75357842b819e6056f3289005c567c4c17b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Sun, 11 Oct 2020 04:34:57 +0200 Subject: [PATCH] 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. --- quickjs.c | 12 ++++++++++++ quickjs.h | 1 + 2 files changed, 13 insertions(+) diff --git a/quickjs.c b/quickjs.c index db4a8d01f..feb6b0633 100644 --- a/quickjs.c +++ b/quickjs.c @@ -9860,6 +9860,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 diff --git a/quickjs.h b/quickjs.h index 816f848d5..bfec838c7 100644 --- a/quickjs.h +++ b/quickjs.h @@ -809,6 +809,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,