1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-01 11:20:27 +00:00

fix C source format

This commit is contained in:
J.-F. Cap 2019-02-23 17:13:43 +01:00
parent 0cc6c6ff33
commit 2becebce92
5 changed files with 238 additions and 240 deletions

View File

@ -28,7 +28,7 @@
/* Create new userdata */
void *janet_abstract(const JanetAbstractType *atype, size_t size) {
JanetAbstractHead *header = janet_gcalloc(JANET_MEMORY_ABSTRACT,
sizeof(JanetAbstractHead) + size);
sizeof(JanetAbstractHead) + size);
header->size = size;
header->type = atype;
return (void *) & (header->data);

View File

@ -289,24 +289,24 @@ 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);
void janet_marshal_int(JanetMarshalContext *ctx, int32_t value) {
MarshalState *st = (MarshalState *)(ctx->m_state);
pushint(st, value);
};
void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value) {
MarshalState *st =(MarshalState *)(ctx->m_state);
pushbyte(st,value);
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_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);
void janet_marshal_janet(JanetMarshalContext *ctx, Janet x) {
MarshalState *st = (MarshalState *)(ctx->m_state);
marshal_one(st, x, ctx->flags + 1);
}
#define MARK_SEEN() \
@ -314,18 +314,18 @@ void janet_marshal_janet(JanetMarshalContext *ctx,Janet x) {
static int marshal_one_abstract(MarshalState *st, Janet x, int flags) {
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x));
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,NULL,flags,NULL};
pushbyte(st, LB_ABSTRACT);
pushint(st,info->tag);
info->marshal(janet_unwrap_abstract(x),&context);
return 1;
}
return 0;
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x));
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, NULL, flags, NULL};
pushbyte(st, LB_ABSTRACT);
pushint(st, info->tag);
info->marshal(janet_unwrap_abstract(x), &context);
return 1;
}
return 0;
}
@ -488,12 +488,12 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
}
goto done;
case JANET_ABSTRACT: {
if (marshal_one_abstract(st,x,flags)) {
goto done;
} else {
goto noregval;
}
}
if (marshal_one_abstract(st, x, flags)) {
goto done;
} else {
goto noregval;
}
}
case JANET_CFUNCTION:
goto noregval;
case JANET_FUNCTION: {
@ -957,50 +957,50 @@ error:
}
void janet_unmarshal_int(JanetMarshalContext *ctx,int32_t* i) {
UnmarshalState *st =(UnmarshalState *)(ctx->u_state);
*i=readint(st,&(ctx->data));
void janet_unmarshal_int(JanetMarshalContext *ctx, int32_t *i) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
*i = readint(st, &(ctx->data));
};
void janet_unmarshal_uint(JanetMarshalContext *ctx,uint32_t* i) {
UnmarshalState *st =(UnmarshalState *)(ctx->u_state);
*i=(uint32_t)readint(st,&(ctx->data));
void janet_unmarshal_uint(JanetMarshalContext *ctx, uint32_t *i) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
*i = (uint32_t)readint(st, &(ctx->data));
};
void janet_unmarshal_size(JanetMarshalContext *ctx,size_t* i) {
UnmarshalState *st =(UnmarshalState *)(ctx->u_state);
*i=(size_t)readint(st,&(ctx->data));
void janet_unmarshal_size(JanetMarshalContext *ctx, size_t *i) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
*i = (size_t)readint(st, &(ctx->data));
};
void janet_unmarshal_byte(JanetMarshalContext *ctx,uint8_t* b) {
*b=*(ctx->data++);
void janet_unmarshal_byte(JanetMarshalContext *ctx, uint8_t *b) {
*b = *(ctx->data++);
};
void janet_unmarshal_bytes(JanetMarshalContext *ctx,uint8_t *dest, int32_t len) {
memcpy(dest,ctx->data,len);
ctx->data+=len;
void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, int32_t len) {
memcpy(dest, ctx->data, len);
ctx->data += len;
}
void janet_unmarshal_janet(JanetMarshalContext *ctx,Janet *out) {
UnmarshalState *st =(UnmarshalState *)(ctx->u_state);
ctx->data=unmarshal_one(st,ctx->data,out,ctx->flags);
void janet_unmarshal_janet(JanetMarshalContext *ctx, Janet *out) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
ctx->data = unmarshal_one(st, ctx->data, out, ctx->flags);
}
static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t *data, Janet *out, int flags) {
uint32_t tag=readint(st,&data);
const JanetAbstractTypeInfo *info = janet_get_abstract_type_info(tag);
if (info==NULL) goto error;
if (info->unmarshal) {
void *p = janet_abstract(info->at,info->size);
JanetMarshalContext context={NULL,st,flags,data};
info->unmarshal(p,&context);
*out=janet_wrap_abstract(p);
return data;
}
return 0;
error:
uint32_t tag = readint(st, &data);
const JanetAbstractTypeInfo *info = janet_get_abstract_type_info(tag);
if (info == NULL) goto error;
if (info->unmarshal) {
void *p = janet_abstract(info->at, info->size);
JanetMarshalContext context = {NULL, st, flags, data};
info->unmarshal(p, &context);
*out = janet_wrap_abstract(p);
return data;
}
return 0;
error:
longjmp(st->err, UMR_INVALID_ABSTRACT);
return NULL;
}
@ -1123,9 +1123,9 @@ static const uint8_t *unmarshal_one(
return data;
}
case LB_ABSTRACT: {
data++;
return unmarshal_one_abstract(st,data,out,flags);
}
data++;
return unmarshal_one_abstract(st, data, out, flags);
}
case LB_REFERENCE:
case LB_ARRAY:
case LB_TUPLE:

View File

@ -120,31 +120,31 @@ static int ta_buffer_gc(void *p, size_t s) {
}
static void ta_buffer_marshal(void *p, JanetMarshalContext *ctx) {
TA_Buffer *buf = (TA_Buffer *)p;
janet_marshal_int(ctx,buf->size);
janet_marshal_int(ctx,buf->flags);
janet_marshal_bytes(ctx,buf->data,buf->size);
TA_Buffer *buf = (TA_Buffer *)p;
janet_marshal_int(ctx, buf->size);
janet_marshal_int(ctx, buf->flags);
janet_marshal_bytes(ctx, buf->data, buf->size);
}
static void ta_buffer_unmarshal(void *p, JanetMarshalContext *ctx) {
TA_Buffer *buf = (TA_Buffer *)p;
uint32_t size;
janet_unmarshal_uint(ctx,&size);
ta_buffer_init(buf,size); // warning if indianess <> platform ??
janet_unmarshal_uint(ctx,&(buf->flags));
janet_unmarshal_bytes(ctx,buf->data,buf->size);
TA_Buffer *buf = (TA_Buffer *)p;
uint32_t size;
janet_unmarshal_uint(ctx, &size);
ta_buffer_init(buf, size); // warning if indianess <> platform ??
janet_unmarshal_uint(ctx, &(buf->flags));
janet_unmarshal_bytes(ctx, buf->data, buf->size);
}
static const JanetAbstractType ta_buffer_type={"ta/buffer", ta_buffer_gc, NULL, NULL, NULL};
static const JanetAbstractType ta_buffer_type = {"ta/buffer", ta_buffer_gc, NULL, NULL, NULL};
static const JanetAbstractTypeInfo ta_buffer_typeinfo={
&ta_buffer_type,
sizeof(TA_Buffer),
1000,
0,
ta_buffer_marshal,
ta_buffer_unmarshal,
static const JanetAbstractTypeInfo ta_buffer_typeinfo = {
&ta_buffer_type,
sizeof(TA_Buffer),
1000,
0,
ta_buffer_marshal,
ta_buffer_unmarshal,
};
@ -165,27 +165,27 @@ static int ta_mark(void *p, size_t s) {
}
static void ta_view_marshal(void *p, JanetMarshalContext *ctx) {
TA_View *view = (TA_View *)p;
size_t offset = (view->buffer->data - (uint8_t *)(view->data));
janet_marshal_int(ctx,view->size);
janet_marshal_int(ctx,view->stride);
janet_marshal_int(ctx,view->type);
janet_marshal_int(ctx,offset);
janet_marshal_janet(ctx,janet_wrap_abstract(view->buffer));
TA_View *view = (TA_View *)p;
size_t offset = (view->buffer->data - (uint8_t *)(view->data));
janet_marshal_int(ctx, view->size);
janet_marshal_int(ctx, view->stride);
janet_marshal_int(ctx, view->type);
janet_marshal_int(ctx, offset);
janet_marshal_janet(ctx, janet_wrap_abstract(view->buffer));
}
static void ta_view_unmarshal(void *p, JanetMarshalContext *ctx) {
TA_View *view = (TA_View *)p;
size_t offset;
Janet buffer;
janet_unmarshal_size(ctx,&(view->size));
janet_unmarshal_size(ctx,&(view->stride));
janet_unmarshal_uint(ctx,&(view->type));
janet_unmarshal_size(ctx,&offset);
janet_unmarshal_janet(ctx,&buffer);
view->buffer=(TA_Buffer *)janet_unwrap_abstract(buffer);
view->data=view->buffer->data+offset;
TA_View *view = (TA_View *)p;
size_t offset;
Janet buffer;
janet_unmarshal_size(ctx, &(view->size));
janet_unmarshal_size(ctx, &(view->stride));
janet_unmarshal_uint(ctx, &(view->type));
janet_unmarshal_size(ctx, &offset);
janet_unmarshal_janet(ctx, &buffer);
view->buffer = (TA_Buffer *)janet_unwrap_abstract(buffer);
view->data = view->buffer->data + offset;
}
@ -254,13 +254,13 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
#define DEFINE_VIEW_ABSTRACT_TYPE(type) static const JanetAbstractType ta_view_##type##_t = {"ta/"#type,NULL,ta_mark,ta_get_##type,ta_put_##type};
#define BUILD_TYPE(type) \
DEFINE_VIEW_TYPE(type) \
DEFINE_VIEW_GETTER(type) \
DEFINE_VIEW_SETTER(type) \
DEFINE_VIEW_INITIALIZER(type) \
DEFINE_VIEW_ABSTRACT_TYPE(type)
DEFINE_VIEW_ABSTRACT_TYPE(type)
BUILD_TYPE(uint8)
BUILD_TYPE(int8)
@ -282,16 +282,16 @@ BUILD_TYPE(float64)
#define VIEW_ABSTRACT_INFO_INIT(type,salt) {&ta_view_##type##_t,sizeof(TA_View),salt,0,ta_view_marshal,ta_view_unmarshal}
static const JanetAbstractTypeInfo ta_array_types[] = {
VIEW_ABSTRACT_INFO_INIT(uint8,1001),
VIEW_ABSTRACT_INFO_INIT(int8,1002),
VIEW_ABSTRACT_INFO_INIT(uint16,1003),
VIEW_ABSTRACT_INFO_INIT(int16,1004),
VIEW_ABSTRACT_INFO_INIT(uint32,1005),
VIEW_ABSTRACT_INFO_INIT(int32,1006),
VIEW_ABSTRACT_INFO_INIT(uint64,1007),
VIEW_ABSTRACT_INFO_INIT(int64,1008),
VIEW_ABSTRACT_INFO_INIT(float32,1009),
VIEW_ABSTRACT_INFO_INIT(float64,1010),
VIEW_ABSTRACT_INFO_INIT(uint8, 1001),
VIEW_ABSTRACT_INFO_INIT(int8, 1002),
VIEW_ABSTRACT_INFO_INIT(uint16, 1003),
VIEW_ABSTRACT_INFO_INIT(int16, 1004),
VIEW_ABSTRACT_INFO_INIT(uint32, 1005),
VIEW_ABSTRACT_INFO_INIT(int32, 1006),
VIEW_ABSTRACT_INFO_INIT(uint64, 1007),
VIEW_ABSTRACT_INFO_INIT(int64, 1008),
VIEW_ABSTRACT_INFO_INIT(float32, 1009),
VIEW_ABSTRACT_INFO_INIT(float64, 1010),
};
static int is_ta_type(Janet x) {
@ -328,7 +328,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);
}
}
TA_View *view = janet_abstract(ta_array_types[type].at, sizeof(TA_View));
@ -367,7 +367,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);
return janet_wrap_number(buf->size);
}
@ -389,23 +389,23 @@ static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) {
/* 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;
if (janet_checktype(argv[0],JANET_KEYWORD)) {
const uint8_t *keyw = janet_unwrap_keyword(argv[0]);
info=janet_get_abstract_type_info_byname((const char*)keyw);
JanetAbstractTypeInfo *info;
if (janet_checktype(argv[0], JANET_KEYWORD)) {
const uint8_t *keyw = janet_unwrap_keyword(argv[0]);
info = janet_get_abstract_type_info_byname((const char *)keyw);
} else {
uint32_t tag = (uint32_t)janet_getinteger(argv, 0);
info=janet_get_abstract_type_info(tag);
uint32_t tag = (uint32_t)janet_getinteger(argv, 0);
info = janet_get_abstract_type_info(tag);
}
if (info==NULL) {
return janet_wrap_nil();
if (info == NULL) {
return janet_wrap_nil();
}
JanetKV *props = janet_struct_begin(5);
janet_struct_put(props, janet_ckeywordv("tag"), janet_wrap_number(info->tag));
janet_struct_put(props, janet_ckeywordv("salt"), janet_wrap_number(info->salt));
janet_struct_put(props, janet_ckeywordv("name"), janet_ckeywordv(info->at->name));
janet_struct_put(props, janet_ckeywordv("size"), janet_wrap_number(info->size));
janet_struct_put(props, janet_ckeywordv("marshal"), janet_wrap_boolean((info->marshal !=NULL) && (info->unmarshal!=NULL)));
janet_struct_put(props, janet_ckeywordv("marshal"), janet_wrap_boolean((info->marshal != NULL) && (info->unmarshal != NULL)));
return janet_wrap_struct(janet_struct_end(props));
}
@ -413,30 +413,29 @@ static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5);
if (is_ta_type(argv[0]) && is_ta_type(argv[2])) {
TA_View *src = (TA_View *)janet_unwrap_abstract(argv[0]);
size_t index_src=(size_t)janet_getinteger(argv, 1);
TA_View *dst = (TA_View *)janet_unwrap_abstract(argv[2]);
size_t index_dst=(size_t)janet_getinteger(argv, 3);
size_t count=(argc == 5)? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size=ta_type_sizes[src->type];
size_t dst_atom_size=ta_type_sizes[dst->type];
size_t step_src=src->stride*src_atom_size;
size_t step_dst=dst->stride*dst_atom_size;
size_t pos_src=((uint8_t *)(src->data) - src->buffer->data)+(index_src*step_src);
size_t pos_dst=((uint8_t *)(dst->data) - dst->buffer->data)+(index_dst*step_dst);
uint8_t * ps=src->buffer->data+pos_src,* pd=dst->buffer->data+pos_dst;
if ((pos_dst+(count-1)*step_dst+src_atom_size <= dst->buffer->size) &&
(pos_src+(count-1)*step_src+src_atom_size <= src->buffer->size)) {
for (size_t i=0;i<count;i++) {
memmove(pd,ps,src_atom_size);
pd+=step_dst;
ps+=step_src;
}
}
else
janet_panic("typed array copy out of bound");
TA_View *src = (TA_View *)janet_unwrap_abstract(argv[0]);
size_t index_src = (size_t)janet_getinteger(argv, 1);
TA_View *dst = (TA_View *)janet_unwrap_abstract(argv[2]);
size_t index_dst = (size_t)janet_getinteger(argv, 3);
size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size = ta_type_sizes[src->type];
size_t dst_atom_size = ta_type_sizes[dst->type];
size_t step_src = src->stride * src_atom_size;
size_t step_dst = dst->stride * dst_atom_size;
size_t pos_src = ((uint8_t *)(src->data) - src->buffer->data) + (index_src * step_src);
size_t pos_dst = ((uint8_t *)(dst->data) - dst->buffer->data) + (index_dst * step_dst);
uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst;
if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) &&
(pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) {
for (size_t i = 0; i < count; i++) {
memmove(pd, ps, src_atom_size);
pd += step_dst;
ps += step_src;
}
} else
janet_panic("typed array copy out of bound");
} else {
janet_panic("expected typed array");
janet_panic("expected typed array");
}
return janet_wrap_nil();
}
@ -444,33 +443,32 @@ static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) {
static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5);
if (is_ta_type(argv[0]) && is_ta_type(argv[2])) {
TA_View *src = (TA_View *)janet_unwrap_abstract(argv[0]);
size_t index_src=(size_t)janet_getinteger(argv, 1);
TA_View *dst = (TA_View *)janet_unwrap_abstract(argv[2]);
size_t index_dst=(size_t)janet_getinteger(argv, 3);
size_t count=(argc == 5)? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size=ta_type_sizes[src->type];
size_t dst_atom_size=ta_type_sizes[dst->type];
size_t step_src=src->stride*src_atom_size;
size_t step_dst=dst->stride*dst_atom_size;
size_t pos_src=((uint8_t *)(src->data) - src->buffer->data)+(index_src*step_src);
size_t pos_dst=((uint8_t *)(dst->data) - dst->buffer->data)+(index_dst*step_dst);
uint8_t * ps=src->buffer->data+pos_src,* pd=dst->buffer->data+pos_dst;
uint8_t temp[TA_ATOM_MAXSIZE];
if ((pos_dst+(count-1)*step_dst+src_atom_size <= dst->buffer->size) &&
(pos_src+(count-1)*step_src+src_atom_size <= src->buffer->size)) {
for (size_t i=0;i<count;i++) {
memcpy(temp,ps,src_atom_size);
memcpy(ps,pd,src_atom_size);
memcpy(pd,temp,src_atom_size);
pd+=step_dst;
ps+=step_src;
}
}
else
janet_panic("typed array swap out of bound");
TA_View *src = (TA_View *)janet_unwrap_abstract(argv[0]);
size_t index_src = (size_t)janet_getinteger(argv, 1);
TA_View *dst = (TA_View *)janet_unwrap_abstract(argv[2]);
size_t index_dst = (size_t)janet_getinteger(argv, 3);
size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size = ta_type_sizes[src->type];
size_t dst_atom_size = ta_type_sizes[dst->type];
size_t step_src = src->stride * src_atom_size;
size_t step_dst = dst->stride * dst_atom_size;
size_t pos_src = ((uint8_t *)(src->data) - src->buffer->data) + (index_src * step_src);
size_t pos_dst = ((uint8_t *)(dst->data) - dst->buffer->data) + (index_dst * step_dst);
uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst;
uint8_t temp[TA_ATOM_MAXSIZE];
if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) &&
(pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) {
for (size_t i = 0; i < count; i++) {
memcpy(temp, ps, src_atom_size);
memcpy(ps, pd, src_atom_size);
memcpy(pd, temp, src_atom_size);
pd += step_dst;
ps += step_src;
}
} else
janet_panic("typed array swap out of bound");
} else {
janet_panic("expected typed array");
janet_panic("expected typed array");
}
return janet_wrap_nil();
}
@ -503,16 +501,16 @@ static const JanetReg ta_cfuns[] = {
JDOC("(tarray/copy-bytes src sindex dst dindex [count=1])\n\n"
"copy count elements of src array from index sindex \n"
"to dst array at position dindex \n"
"memory can overlap"
)
"memory can overlap"
)
},
{
"tarray/swap-bytes", cfun_typed_array_swap_bytes,
JDOC("(tarray/swap-bytes src sindex dst dindex [count=1])\n\n"
"swap count elements between src array from index sindex \n"
"and dst array at position dindex \n"
"memory can overlap"
)
"memory can overlap"
)
},
{
"abstract/properties", cfun_abstract_properties,
@ -530,7 +528,7 @@ static const JanetReg ta_cfuns[] = {
void janet_lib_typed_array(JanetTable *env) {
janet_core_cfuns(env, NULL, ta_cfuns);
janet_register_abstract_type(&ta_buffer_typeinfo);
for (size_t i=0;i<TA_COUNT_TYPES;i++) {
janet_register_abstract_type(ta_array_types + i);
for (size_t i = 0; i < TA_COUNT_TYPES; i++) {
janet_register_abstract_type(ta_array_types + i);
}
}

View File

@ -288,54 +288,54 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
static const JanetAbstractType type_info = {"core/type_info", NULL, NULL, NULL, NULL};
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;
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));
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(abstract->tag), janet_wrap_abstract(abstract));
janet_table_put(janet_vm_registry,janet_ckeywordv(abstract->at->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));
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(abstract->tag), janet_wrap_abstract(abstract));
janet_table_put(janet_vm_registry, janet_ckeywordv(abstract->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));
if (janet_checktype(info, JANET_NIL)) {
return NULL;
}
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
janet_panic("expected type_info");
}
JanetAbstractTypeInfo * type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
return type_info;
JanetAbstractTypeInfo *janet_get_abstract_type_info(uint32_t tag) {
Janet info = janet_table_get(janet_vm_registry, janet_wrap_number(tag));
if (janet_checktype(info, JANET_NIL)) {
return NULL;
}
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
janet_panic("expected type_info");
}
JanetAbstractTypeInfo *type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
return type_info;
}
JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name) {
Janet info=janet_table_get(janet_vm_registry,janet_ckeywordv(name));
if (janet_checktype(info, JANET_NIL)) {
return NULL;
}
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
janet_panic("expected type_info");
}
JanetAbstractTypeInfo * type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
return type_info;
JanetAbstractTypeInfo *janet_get_abstract_type_info_byname(const char *name) {
Janet info = janet_table_get(janet_vm_registry, janet_ckeywordv(name));
if (janet_checktype(info, JANET_NIL)) {
return NULL;
}
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
janet_panic("expected type_info");
}
JanetAbstractTypeInfo *type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
return type_info;
}
#ifndef JANET_BOOTSTRAP

View File

@ -1244,41 +1244,41 @@ JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t lengt
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
typedef struct {
void * m_state; /* void* to not expose MarshalState ?*/
void * u_state;
int flags;
const uint8_t * data;
void *m_state; /* void* to not expose MarshalState ?*/
void *u_state;
int flags;
const uint8_t *data;
} JanetMarshalContext;
JANET_API void janet_marshal_int(JanetMarshalContext *ctx,int32_t value);
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx,uint8_t value);
JANET_API void janet_marshal_bytes(JanetMarshalContext *ctx,const uint8_t *bytes, int32_t len);
JANET_API void janet_marshal_janet(JanetMarshalContext *ctx,Janet x);
JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value);
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value);
JANET_API void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, int32_t len);
JANET_API void janet_marshal_janet(JanetMarshalContext *ctx, Janet x);
JANET_API void janet_unmarshal_int(JanetMarshalContext *ctx,int32_t* i);
JANET_API void janet_unmarshal_uint(JanetMarshalContext *ctx,uint32_t* i);
JANET_API void janet_unmarshal_size(JanetMarshalContext *ctx,size_t * i);
JANET_API void janet_unmarshal_byte(JanetMarshalContext *ctx,uint8_t* b);
JANET_API void janet_unmarshal_bytes(JanetMarshalContext *ctx,uint8_t *dest, int32_t len);
JANET_API void janet_unmarshal_janet(JanetMarshalContext *ctx,Janet *out);
JANET_API void janet_unmarshal_int(JanetMarshalContext *ctx, int32_t *i);
JANET_API void janet_unmarshal_uint(JanetMarshalContext *ctx, uint32_t *i);
JANET_API void janet_unmarshal_size(JanetMarshalContext *ctx, size_t *i);
JANET_API void janet_unmarshal_byte(JanetMarshalContext *ctx, uint8_t *b);
JANET_API void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, int32_t len);
JANET_API void janet_unmarshal_janet(JanetMarshalContext *ctx, Janet *out);
typedef struct {
const JanetAbstractType *at;
size_t size; /* abstract type size */
const uint32_t salt; /* salt */
uint32_t tag; /* unique tag computed by janet (hash of name and salt) */
void (* marshal)(void *p,JanetMarshalContext *ctx);
void (* unmarshal)(void *p,JanetMarshalContext *ctx);
const JanetAbstractType *at;
size_t size; /* abstract type size */
const uint32_t salt; /* salt */
uint32_t tag; /* unique tag computed by janet (hash of name and salt) */
void (* marshal)(void *p, JanetMarshalContext *ctx);
void (* unmarshal)(void *p, JanetMarshalContext *ctx);
} JanetAbstractTypeInfo;
JANET_API void janet_register_abstract_type(const JanetAbstractTypeInfo * info);
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_byname(const char *name);
JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag);
JANET_API JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name);
/***** END SECTION MAIN *****/
#ifdef __cplusplus