add typeOf() and instanceOf()

This commit is contained in:
Rongsong Shen 2026-02-13 10:49:00 +08:00
parent dffbd936db
commit 6f6bb5b4e0
5 changed files with 69 additions and 10 deletions

View file

@ -2,6 +2,7 @@ pkg = import('pkgconfig')
librtti_c_srcs=[
'create.c',
'typeinfo.c',
'lock.c',
'iter.c',
'ref.c',

27
src/typeinfo.c Normal file
View file

@ -0,0 +1,27 @@
#include <errno.h>
#include <stdlib.h>
#include "rtti.h"
typeinfo_t *typeOf(var v) {
meta_obj_t *obj = NULL;
obj = RTTI_DATA_TO_OBJ(v);
if (RTTI_OBJ_INVALID(obj)) {
return NULL;
}
return obj->type;
}
int instanceOf(var v, typeinfo_t *t) {
if ((v == NULL) || (t == NULL)) {
return -EINVAL;
}
if (typeOf(v) == t) {
return 1;
}
return 0;
}