1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

marshal buffer ok

This commit is contained in:
J.-F. Cap 2019-02-22 15:57:48 +01:00
parent d570aae817
commit db9ac6dba5
4 changed files with 71 additions and 54 deletions

View File

@ -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 { void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value) {
JanetMarshalState *st; MarshalState *st =(MarshalState *)(ctx->m_state);
int flags; pushbyte(st,value);
} JanetMarshalContext; };
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() \ #define MARK_SEEN() \
janet_table_put(&st->seen, x, janet_wrap_integer(st->nextid++)) 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) { static int marshal_one_abstract(MarshalState *st, Janet x, int flags) {
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x)); 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(); MARK_SEEN();
JanetMarshalContext context={st,flags}; JanetMarshalContext context={st,NULL,flags};
at->marshal(janet_unwrap_abstract(x),&context); pushbyte(st, LB_ABSTRACT);
pushint(st,info->tag);
/* objects has to be allocate by marshal function and null/terminated*/ info->marshal(janet_unwrap_abstract(x),&context);
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);
return 1; return 1;
} else {
return 0;
} }
return 0;
} }

View File

@ -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 { typedef struct {
@ -193,7 +197,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
TA_View_##type * tview=(TA_View_##type *) view; \ TA_View_##type * tview=(TA_View_##type *) view; \
size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \ size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \
if (buf==NULL) { \ 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); \ ta_buffer_init(buf,buf_size); \
} \ } \
if (buf->size<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; stride *= view->stride;
buffer = view->buffer; buffer = view->buffer;
} else { } 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)); 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); return janet_wrap_abstract(view->buffer);
} }
size_t size = (size_t)janet_getinteger(argv, 0); 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); ta_buffer_init(buf, size);
return janet_wrap_abstract(buf); 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]); TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
return janet_wrap_number(view->size); 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); 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)); 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) { static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
JanetAbstractTypeInfo * info; JanetAbstractTypeInfo * info;
@ -352,7 +357,7 @@ static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
} }
JanetKV *props = janet_struct_begin(2); JanetKV *props = janet_struct_begin(2);
janet_struct_put(props, janet_ckeywordv("tag"), janet_wrap_number(info->tag)); 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)); return janet_wrap_struct(janet_struct_end(props));
} }
@ -441,5 +446,5 @@ static const JanetReg ta_cfuns[] = {
/* Module entry point */ /* Module entry point */
void janet_lib_typed_array(JanetTable *env) { void janet_lib_typed_array(JanetTable *env) {
janet_core_cfuns(env, NULL, ta_cfuns); janet_core_cfuns(env, NULL, ta_cfuns);
janet_register_abstract_type(&ta_buffer_type,1111); janet_register_abstract_type(&ta_buffer_type);
} }

View File

@ -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}; static const JanetAbstractType type_info = {"core/type_info", NULL, NULL, NULL, NULL};
/*
void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag) { void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag) {
JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo)); JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo));
abstract->type=*atype; 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_wrap_number(tag), janet_wrap_abstract(abstract));
janet_table_put(janet_vm_registry,janet_ckeywordv(atype->name), 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) { JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag) {
Janet info=janet_table_get(janet_vm_registry,janet_wrap_number(tag)); Janet info=janet_table_get(janet_vm_registry,janet_wrap_number(tag));

View File

@ -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_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); JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
typedef struct { typedef struct {
JanetAbstractType type; void * m_state; /* void* to not expose MarshalState ?*/
uint32_t tag; 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; } 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 JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag);
/*JANET_API uint32_t janet_get_abstract_type_tag(const JanetAbstractType *atype);*/ /*JANET_API uint32_t janet_get_abstract_type_tag(const JanetAbstractType *atype);*/
JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name); JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name);
/***** END SECTION MAIN *****/ /***** END SECTION MAIN *****/
#ifdef __cplusplus #ifdef __cplusplus