mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 09:47:17 +00:00
marshal buffer ok
This commit is contained in:
parent
d570aae817
commit
db9ac6dba5
@ -289,63 +289,43 @@ static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) {
|
||||
}
|
||||
|
||||
|
||||
void janet_marshal_int(JanetMarshalContext *ctx,int32_t value) {
|
||||
MarshalState *st =(MarshalState *)(ctx->m_state);
|
||||
pushint(st,value);
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
JanetMarshalState *st;
|
||||
int flags;
|
||||
} JanetMarshalContext;
|
||||
|
||||
void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value) {
|
||||
MarshalState *st =(MarshalState *)(ctx->m_state);
|
||||
pushbyte(st,value);
|
||||
};
|
||||
|
||||
void janet_marshal_bytes(JanetMarshalContext *ctx,const uint8_t *bytes, int32_t len) {
|
||||
MarshalState *st =(MarshalState *)(ctx->m_state);
|
||||
pushbytes(st,bytes,len);
|
||||
}
|
||||
|
||||
void janet_marshal_janet(JanetMarshalContext *ctx,Janet x) {
|
||||
MarshalState *st =(MarshalState *)(ctx->m_state);
|
||||
marshal_one(st,x,ctx->flags + 1);
|
||||
}
|
||||
|
||||
#define MARK_SEEN() \
|
||||
janet_table_put(&st->seen, x, janet_wrap_integer(st->nextid++))
|
||||
|
||||
|
||||
void janet_marshal_int(JanetMarshalContext *ctx,int32_t value) {
|
||||
pushint(ctx->st,value);
|
||||
};
|
||||
void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value) {
|
||||
pushbyte(ctx->st,value);
|
||||
};
|
||||
void janet_marshal_bytes(JanetMarshalContext *ctx,const uint8_t *bytes, int32_t len) {
|
||||
pushbytes(ctx->st,bytes,len);
|
||||
}
|
||||
void janet_marshal_janet(JanetMarshalContext *ctx,Janet x) {
|
||||
marshal_one(ctx->st,x,ctx->st->flags + 1);
|
||||
}
|
||||
|
||||
static int marshal_one_abstract(MarshalState *st, Janet x, int flags) {
|
||||
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x));
|
||||
if (at->marshal) {
|
||||
const JanetAbstractTypeInfo *info = janet_get_abstract_type_info_byname(at->name);
|
||||
if (! info) return 1 ; /* unregistered type skip marshalling*/
|
||||
if (info->marshal) {
|
||||
MARK_SEEN();
|
||||
JanetMarshalContext context={st,flags};
|
||||
at->marshal(janet_unwrap_abstract(x),&context);
|
||||
|
||||
/* objects has to be allocate by marshal function and null/terminated*/
|
||||
JanetMarshalObject * walk = objects;
|
||||
while (walk) {
|
||||
switch (walk->type) {
|
||||
case JANET_MO_TYPE_INTEGER :
|
||||
pushint(st,walk->value.integer);
|
||||
break;
|
||||
case JANET_MO_TYPE_BYTE :
|
||||
pushbyte(st,walk->value.byte);
|
||||
break;
|
||||
case JANET_MO_TYPE_BYTES :
|
||||
pushbyte(st,walk->value.bytes,walk->length);
|
||||
break;
|
||||
case JANET_MO_TYPE_JANET :
|
||||
marshal_one(st,walk->value.janet, flags + 1);
|
||||
break;
|
||||
}
|
||||
walk++;
|
||||
}
|
||||
if (objects) free(objects);
|
||||
JanetMarshalContext context={st,NULL,flags};
|
||||
pushbyte(st, LB_ABSTRACT);
|
||||
pushint(st,info->tag);
|
||||
info->marshal(janet_unwrap_abstract(x),&context);
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -128,7 +128,11 @@ static void ta_buffer_marshal(void *p, JanetMarshalContext *ctx) {
|
||||
}
|
||||
|
||||
|
||||
static const JanetAbstractType ta_buffer_type = {"ta/buffer", ta_buffer_gc, NULL, NULL, NULL};
|
||||
static const JanetAbstractTypeInfo ta_buffer_type={
|
||||
{"ta/buffer", ta_buffer_gc, NULL, NULL, NULL},
|
||||
1111,
|
||||
ta_buffer_marshal,
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
@ -193,7 +197,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
|
||||
TA_View_##type * tview=(TA_View_##type *) view; \
|
||||
size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \
|
||||
if (buf==NULL) { \
|
||||
buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer)); \
|
||||
buf=(TA_Buffer *)janet_abstract(&ta_buffer_type.at,sizeof(TA_Buffer)); \
|
||||
ta_buffer_init(buf,buf_size); \
|
||||
} \
|
||||
if (buf->size<buf_size) { \
|
||||
@ -279,7 +283,7 @@ static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
|
||||
stride *= view->stride;
|
||||
buffer = view->buffer;
|
||||
} else {
|
||||
buffer = (TA_Buffer *)janet_getabstract(argv, 4, &ta_buffer_type);
|
||||
buffer = (TA_Buffer *)janet_getabstract(argv, 4, &ta_buffer_type.at);
|
||||
}
|
||||
}
|
||||
TA_View *view = janet_abstract(&ta_array_types[type], sizeof(TA_View));
|
||||
@ -307,7 +311,7 @@ static Janet cfun_typed_array_buffer(int32_t argc, Janet *argv) {
|
||||
return janet_wrap_abstract(view->buffer);
|
||||
}
|
||||
size_t size = (size_t)janet_getinteger(argv, 0);
|
||||
TA_Buffer *buf = (TA_Buffer *)janet_abstract(&ta_buffer_type, sizeof(TA_Buffer));
|
||||
TA_Buffer *buf = (TA_Buffer *)janet_abstract(&ta_buffer_type.at, sizeof(TA_Buffer));
|
||||
ta_buffer_init(buf, size);
|
||||
return janet_wrap_abstract(buf);
|
||||
}
|
||||
@ -318,7 +322,7 @@ static Janet cfun_typed_array_size(int32_t argc, Janet *argv) {
|
||||
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
||||
return janet_wrap_number(view->size);
|
||||
}
|
||||
TA_Buffer *buf = (TA_Buffer *)janet_getabstract(argv, 0, &ta_buffer_type);
|
||||
TA_Buffer *buf = (TA_Buffer *)janet_getabstract(argv, 0, &ta_buffer_type.at);
|
||||
return janet_wrap_number(buf->size);
|
||||
}
|
||||
|
||||
@ -337,6 +341,7 @@ static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) {
|
||||
return janet_wrap_struct(janet_struct_end(props));
|
||||
}
|
||||
|
||||
/* TODO for test it's not the good place for this function */
|
||||
static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
|
||||
janet_fixarity(argc, 1);
|
||||
JanetAbstractTypeInfo * info;
|
||||
@ -352,7 +357,7 @@ static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
|
||||
}
|
||||
JanetKV *props = janet_struct_begin(2);
|
||||
janet_struct_put(props, janet_ckeywordv("tag"), janet_wrap_number(info->tag));
|
||||
janet_struct_put(props, janet_ckeywordv("name"), janet_ckeywordv(info->type.name));
|
||||
janet_struct_put(props, janet_ckeywordv("name"), janet_ckeywordv(info->at.name));
|
||||
return janet_wrap_struct(janet_struct_end(props));
|
||||
}
|
||||
|
||||
@ -441,5 +446,5 @@ static const JanetReg ta_cfuns[] = {
|
||||
/* Module entry point */
|
||||
void janet_lib_typed_array(JanetTable *env) {
|
||||
janet_core_cfuns(env, NULL, ta_cfuns);
|
||||
janet_register_abstract_type(&ta_buffer_type,1111);
|
||||
janet_register_abstract_type(&ta_buffer_type);
|
||||
}
|
||||
|
@ -286,6 +286,7 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
|
||||
|
||||
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;
|
||||
@ -297,6 +298,19 @@ void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag) {
|
||||
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));
|
||||
}
|
||||
*/
|
||||
|
||||
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))) {
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag) {
|
||||
Janet info=janet_table_get(janet_vm_registry,janet_wrap_number(tag));
|
||||
|
@ -1191,17 +1191,35 @@ JANET_API JanetRange janet_getslice(int32_t argc, const Janet *argv);
|
||||
JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which);
|
||||
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
JanetAbstractType type;
|
||||
uint32_t tag;
|
||||
void * m_state; /* void* to not expose MarshalState ?*/
|
||||
void * u_state;
|
||||
int flags;
|
||||
} JanetMarshalContext;
|
||||
|
||||
void janet_marshal_int(JanetMarshalContext *ctx,int32_t value);
|
||||
void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value);
|
||||
void janet_marshal_bytes(JanetMarshalContext *ctx,const uint8_t *bytes, int32_t len);
|
||||
void janet_marshal_janet(JanetMarshalContext *ctx,Janet x);
|
||||
|
||||
|
||||
typedef struct {
|
||||
const JanetAbstractType at;
|
||||
const uint32_t tag;
|
||||
void (* marshal)(void *p,JanetMarshalContext *ctx);
|
||||
} JanetAbstractTypeInfo;
|
||||
|
||||
JANET_API void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag);
|
||||
JANET_API void janet_register_abstract_type(const JanetAbstractTypeInfo * info);
|
||||
|
||||
JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag);
|
||||
/*JANET_API uint32_t janet_get_abstract_type_tag(const JanetAbstractType *atype);*/
|
||||
JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name);
|
||||
|
||||
|
||||
|
||||
|
||||
/***** END SECTION MAIN *****/
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
Loading…
Reference in New Issue
Block a user