Skip to content

Commit

Permalink
Add JS_GetAnyOpaque() to support polymorphism
Browse files Browse the repository at this point in the history
To be able to check if the class ID is one of multiple known ones, where
the data has a common structure.
  • Loading branch information
oleavr committed May 27, 2022
1 parent 1e14b01 commit 44e7535
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 44e7535

Please sign in to comment.