mirror of
https://github.com/janet-lang/janet
synced 2025-10-29 06:37:41 +00:00
implement typed array marshal/unmarshal and
generic marshaling capabilities to abstract types.
This commit is contained in:
@@ -284,31 +284,30 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
|
||||
}
|
||||
}
|
||||
|
||||
/* Abstract type introspection */
|
||||
|
||||
static const JanetAbstractType type_info = {"core/type_info", NULL, NULL, NULL, NULL};
|
||||
|
||||
/*
|
||||
void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag) {
|
||||
JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo));
|
||||
abstract->type=*atype;
|
||||
abstract->tag=tag;
|
||||
if (!(janet_checktype(janet_table_get(janet_vm_registry,janet_wrap_number(tag)),JANET_NIL)) ||
|
||||
!(janet_checktype(janet_table_get(janet_vm_registry,janet_ckeywordv(atype->name)),JANET_NIL))) {
|
||||
janet_panic("Register abstract type fail, a type with same name or tag exist");
|
||||
}
|
||||
janet_table_put(janet_vm_registry,janet_wrap_number(tag), janet_wrap_abstract(abstract));
|
||||
janet_table_put(janet_vm_registry,janet_ckeywordv(atype->name), janet_wrap_abstract(abstract));
|
||||
static uint32_t janet_abstract_type_gentag(const char * name,uint32_t salt) {
|
||||
/* something smarter should propably done here ? */
|
||||
int32_t len = strlen(name);
|
||||
const char *end = name + len;
|
||||
uint32_t hash = 5381+salt;
|
||||
while (name < end)
|
||||
hash = (hash << 5) + hash + *name++;
|
||||
return (int32_t) hash;
|
||||
}
|
||||
*/
|
||||
|
||||
void janet_register_abstract_type(const JanetAbstractTypeInfo * info) {
|
||||
JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo));
|
||||
memcpy(abstract,info,sizeof(JanetAbstractTypeInfo));
|
||||
if (!(janet_checktype(janet_table_get(janet_vm_registry,janet_wrap_number(info->tag)),JANET_NIL)) ||
|
||||
!(janet_checktype(janet_table_get(janet_vm_registry,janet_ckeywordv(info->at->name)),JANET_NIL))) {
|
||||
memcpy(abstract,info,sizeof(JanetAbstractTypeInfo));
|
||||
abstract->tag=janet_abstract_type_gentag(info->at->name,info->salt);
|
||||
if (!(janet_checktype(janet_table_get(janet_vm_registry,janet_wrap_number(abstract->tag)),JANET_NIL)) ||
|
||||
!(janet_checktype(janet_table_get(janet_vm_registry,janet_ckeywordv(abstract->at->name)),JANET_NIL))) {
|
||||
janet_panic("Register abstract type fail, a type with same name or tag exist");
|
||||
}
|
||||
janet_table_put(janet_vm_registry,janet_wrap_number(info->tag), janet_wrap_abstract(abstract));
|
||||
janet_table_put(janet_vm_registry,janet_ckeywordv(info->at->name), janet_wrap_abstract(abstract));
|
||||
janet_table_put(janet_vm_registry,janet_wrap_number(abstract->tag), janet_wrap_abstract(abstract));
|
||||
janet_table_put(janet_vm_registry,janet_ckeywordv(abstract->at->name), janet_wrap_abstract(abstract));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user