mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	More code to better integrate with size_t
Typed arrays use proper size_t support in more places now.
This commit is contained in:
		| @@ -122,6 +122,14 @@ int64_t janet_getinteger64(const Janet *argv, int32_t n) { | |||||||
|     return (int64_t) janet_unwrap_number(x); |     return (int64_t) janet_unwrap_number(x); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | size_t janet_getsize(const Janet *argv, int32_t n) { | ||||||
|  |     Janet x = argv[n]; | ||||||
|  |     if (!janet_checksize(x)) { | ||||||
|  |         janet_panicf("bad slot #%d, expected size, got %v", n, x); | ||||||
|  |     } | ||||||
|  |     return (size_t) janet_unwrap_number(x); | ||||||
|  | } | ||||||
|  |  | ||||||
| int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which) { | int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which) { | ||||||
|     int32_t raw = janet_getinteger(argv, n); |     int32_t raw = janet_getinteger(argv, n); | ||||||
|     if (raw < 0) raw += length + 1; |     if (raw < 0) raw += length + 1; | ||||||
|   | |||||||
| @@ -134,7 +134,7 @@ static void pushsize(MarshalState *st, size_t x) { | |||||||
|         /* Single byte */ |         /* Single byte */ | ||||||
|         pushbyte(st, (uint8_t) x); |         pushbyte(st, (uint8_t) x); | ||||||
|     } else { |     } else { | ||||||
|         /* Multibyte */ |         /* Multibyte, little endian */ | ||||||
|         uint8_t bytes[9]; |         uint8_t bytes[9]; | ||||||
|         int nbytes = 0; |         int nbytes = 0; | ||||||
|         while (x) { |         while (x) { | ||||||
| @@ -320,7 +320,7 @@ static void marshal_one_abstract(MarshalState *st, Janet x, int flags) { | |||||||
|         MARK_SEEN(); |         MARK_SEEN(); | ||||||
|         JanetMarshalContext context = {st, NULL, flags, NULL}; |         JanetMarshalContext context = {st, NULL, flags, NULL}; | ||||||
|         pushbyte(st, LB_ABSTRACT); |         pushbyte(st, LB_ABSTRACT); | ||||||
|         marshal_one(st, janet_ckeywordv(at->name), flags + 1); |         marshal_one(st, janet_csymbolv(at->name), flags + 1); | ||||||
|         pushsize(st, janet_abstract_size(abstract)); |         pushsize(st, janet_abstract_size(abstract)); | ||||||
|         at->marshal(abstract, &context); |         at->marshal(abstract, &context); | ||||||
|     } else { |     } else { | ||||||
| @@ -577,25 +577,24 @@ static int32_t readint(UnmarshalState *st, const uint8_t **atdata) { | |||||||
|  |  | ||||||
| /* Helper to read a size_t (up to 8 bytes unsigned). */ | /* Helper to read a size_t (up to 8 bytes unsigned). */ | ||||||
| static size_t readsize(UnmarshalState *st, const uint8_t **atdata) { | static size_t readsize(UnmarshalState *st, const uint8_t **atdata) { | ||||||
|  |     size_t ret; | ||||||
|     const uint8_t *data = *atdata; |     const uint8_t *data = *atdata; | ||||||
|     MARSH_EOS(st, data); |     MARSH_EOS(st, data); | ||||||
|     if (*data <= 0xF0) { |     if (*data <= 0xF0) { | ||||||
|         /* Single byte */ |         /* Single byte */ | ||||||
|         size_t ret = *data++; |         ret = *data; | ||||||
|         *atdata = data; |         *atdata = data + 1; | ||||||
|         return ret; |  | ||||||
|     } else { |     } else { | ||||||
|         /* Multibyte */ |         /* Multibyte, little endian */ | ||||||
|         int nbytes = *data++ - 0xF0; |         int nbytes = *data - 0xF0; | ||||||
|         size_t value = 0; |         ret = 0; | ||||||
|         if (nbytes < 1 || nbytes > 8) |         if (nbytes > 8) janet_panic("invalid size_t"); | ||||||
|             janet_panic("invalid size_t"); |         MARSH_EOS(st, data + nbytes); | ||||||
|         MARSH_EOS(st, data + nbytes - 1); |         for (int i = nbytes; i > 0; i--) | ||||||
|         for (int i = 0; i < nbytes; i++) |             ret = (ret << 8) + data[i]; | ||||||
|             value = (value << 8) + *data++; |         *atdata = data + nbytes + 1; | ||||||
|         *atdata = data; |  | ||||||
|         return value; |  | ||||||
|     } |     } | ||||||
|  |     return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| /* Assert a janet type */ | /* Assert a janet type */ | ||||||
|   | |||||||
| @@ -29,16 +29,14 @@ | |||||||
| #include "util.h" | #include "util.h" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| typedef uint8_t   ta_uint8_t; | typedef uint8_t ta_uint8_t; | ||||||
| typedef int8_t    ta_int8_t; | typedef int8_t ta_int8_t; | ||||||
| typedef uint16_t  ta_uint16_t; | typedef uint16_t ta_uint16_t; | ||||||
| typedef int16_t   ta_int16_t; | typedef int16_t ta_int16_t; | ||||||
| typedef uint32_t  ta_uint32_t; | typedef uint32_t ta_uint32_t; | ||||||
| typedef int32_t   ta_int32_t; | typedef int32_t ta_int32_t; | ||||||
| typedef uint64_t  ta_uint64_t; | typedef float ta_float32_t; | ||||||
| typedef int64_t   ta_int64_t; | typedef double ta_float64_t; | ||||||
| typedef float     ta_float32_t; |  | ||||||
| typedef double    ta_float64_t; |  | ||||||
|  |  | ||||||
| static char *ta_type_names[] = { | static char *ta_type_names[] = { | ||||||
|     "uint8", |     "uint8", | ||||||
| @@ -47,8 +45,6 @@ static char *ta_type_names[] = { | |||||||
|     "int16", |     "int16", | ||||||
|     "uint32", |     "uint32", | ||||||
|     "int32", |     "int32", | ||||||
|     "uint64", |  | ||||||
|     "int64", |  | ||||||
|     "float32", |     "float32", | ||||||
|     "float64", |     "float64", | ||||||
|     "any" |     "any" | ||||||
| @@ -61,11 +57,9 @@ static size_t ta_type_sizes[] = { | |||||||
|     sizeof(ta_int16_t), |     sizeof(ta_int16_t), | ||||||
|     sizeof(ta_uint32_t), |     sizeof(ta_uint32_t), | ||||||
|     sizeof(ta_int32_t), |     sizeof(ta_int32_t), | ||||||
|     sizeof(ta_uint64_t), |  | ||||||
|     sizeof(ta_int64_t), |  | ||||||
|     sizeof(ta_float32_t), |     sizeof(ta_float32_t), | ||||||
|     sizeof(ta_float64_t), |     sizeof(ta_float64_t), | ||||||
|     0, |     0 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #define TA_COUNT_TYPES (JANET_TARRAY_TYPE_float64 + 1) | #define TA_COUNT_TYPES (JANET_TARRAY_TYPE_float64 + 1) | ||||||
| @@ -73,11 +67,11 @@ static size_t ta_type_sizes[] = { | |||||||
| #define TA_FLAG_BIG_ENDIAN 1 | #define TA_FLAG_BIG_ENDIAN 1 | ||||||
|  |  | ||||||
| static JanetTArrayType get_ta_type_by_name(const uint8_t *name) { | static JanetTArrayType get_ta_type_by_name(const uint8_t *name) { | ||||||
|     size_t nt = sizeof(ta_type_names) / sizeof(char *); |     for (int i = 0; i < TA_COUNT_TYPES; i++) { | ||||||
|     for (size_t i = 0; i < nt; i++) { |  | ||||||
|         if (!janet_cstrcmp(name, ta_type_names[i])) |         if (!janet_cstrcmp(name, ta_type_names[i])) | ||||||
|             return i; |             return i; | ||||||
|     } |     } | ||||||
|  |     janet_panicf("invalid typed array type %S", name); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -118,7 +112,7 @@ static void ta_buffer_unmarshal(void *p, JanetMarshalContext *ctx) { | |||||||
|     janet_unmarshal_size(ctx, &size); |     janet_unmarshal_size(ctx, &size); | ||||||
|     ta_buffer_init(buf, size); |     ta_buffer_init(buf, size); | ||||||
|     janet_unmarshal_int(ctx, &(buf->flags)); |     janet_unmarshal_int(ctx, &(buf->flags)); | ||||||
|     janet_unmarshal_bytes(ctx, buf->data, buf->size); |     janet_unmarshal_bytes(ctx, buf->data, size); | ||||||
| } | } | ||||||
|  |  | ||||||
| static const JanetAbstractType ta_buffer_type = { | static const JanetAbstractType ta_buffer_type = { | ||||||
| @@ -161,6 +155,10 @@ static void ta_view_unmarshal(void *p, JanetMarshalContext *ctx) { | |||||||
|     view->type = atype; |     view->type = atype; | ||||||
|     janet_unmarshal_size(ctx, &offset); |     janet_unmarshal_size(ctx, &offset); | ||||||
|     janet_unmarshal_janet(ctx, &buffer); |     janet_unmarshal_janet(ctx, &buffer); | ||||||
|  |     if (!janet_checktype(buffer, JANET_ABSTRACT) || | ||||||
|  |             (janet_abstract_type(janet_unwrap_abstract(buffer)) != &ta_buffer_type)) { | ||||||
|  |         janet_panicf("expected typed array buffer"); | ||||||
|  |     } | ||||||
|     view->buffer = (JanetTArrayBuffer *)janet_unwrap_abstract(buffer); |     view->buffer = (JanetTArrayBuffer *)janet_unwrap_abstract(buffer); | ||||||
|     size_t buf_need_size = offset + (janet_tarray_type_size(view->type)) * ((view->size - 1) * view->stride + 1); |     size_t buf_need_size = offset + (janet_tarray_type_size(view->type)) * ((view->size - 1) * view->stride + 1); | ||||||
|     if (view->buffer->size < buf_need_size) |     if (view->buffer->size < buf_need_size) | ||||||
| @@ -181,10 +179,10 @@ static void ta_view_unmarshal(void *p, JanetMarshalContext *ctx) { | |||||||
| static Janet ta_get_##type(void *p, Janet key) { \ | static Janet ta_get_##type(void *p, Janet key) { \ | ||||||
|   Janet value;  \ |   Janet value;  \ | ||||||
|   size_t index; \ |   size_t index; \ | ||||||
|   if (!janet_checkint(key))      \ |   if (!janet_checksize(key))      \ | ||||||
|     janet_panic("expected integer key");     \ |     janet_panic("expected size as key");     \ | ||||||
|   index = (size_t)janet_unwrap_integer(key);\ |   index = (size_t)janet_unwrap_number(key);\ | ||||||
|   TA_View_##type * array=(TA_View_##type *)p; \ |   TA_View_##type *array=(TA_View_##type *)p; \ | ||||||
|   if (index >= array->size) { \ |   if (index >= array->size) { \ | ||||||
|     value = janet_wrap_nil(); \ |     value = janet_wrap_nil(); \ | ||||||
|   } else { \ |   } else { \ | ||||||
| @@ -196,14 +194,14 @@ static Janet ta_get_##type(void *p, Janet key) { \ | |||||||
| #define DEFINE_VIEW_SETTER(type) \ | #define DEFINE_VIEW_SETTER(type) \ | ||||||
| void ta_put_##type(void *p, Janet key,Janet value) { \ | void ta_put_##type(void *p, Janet key,Janet value) { \ | ||||||
|   size_t index;\ |   size_t index;\ | ||||||
|   if (!janet_checkint(key))\ |   if (!janet_checksize(key))\ | ||||||
|     janet_panic("expected integer key"); \ |     janet_panic("expected size as key"); \ | ||||||
|   if (!janet_checktype(value,JANET_NUMBER)) \ |   if (!janet_checktype(value,JANET_NUMBER)) \ | ||||||
|     janet_panic("expected number value"); \ |     janet_panic("expected number value"); \ | ||||||
|   index = (size_t)janet_unwrap_integer(key); \ |   index = (size_t)janet_unwrap_number(key); \ | ||||||
|   TA_View_##type * array=(TA_View_##type *)p; \ |   TA_View_##type *array=(TA_View_##type *)p; \ | ||||||
|   if (index >= array->size) { \ |   if (index >= array->size) { \ | ||||||
|     janet_panic("typed array out of bounds"); \ |     janet_panic("index out of bounds"); \ | ||||||
|   } \ |   } \ | ||||||
|   array->data[index*array->stride]=(ta_##type##_t)janet_unwrap_number(value); \ |   array->data[index*array->stride]=(ta_##type##_t)janet_unwrap_number(value); \ | ||||||
| } | } | ||||||
| @@ -222,7 +220,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \ | |||||||
|     ta_buffer_init(buf,buf_size); \ |     ta_buffer_init(buf,buf_size); \ | ||||||
|   } \ |   } \ | ||||||
|   if (buf->size<buf_size) { \ |   if (buf->size<buf_size) { \ | ||||||
|     janet_panicf("bad buffer size : %i bytes allocated < %i required",buf->size,buf_size); \ |     janet_panicf("bad buffer size, %i bytes allocated < %i required",buf->size,buf_size); \ | ||||||
|   } \ |   } \ | ||||||
|   tview->buffer=buf; \ |   tview->buffer=buf; \ | ||||||
|   tview->stride=stride; \ |   tview->stride=stride; \ | ||||||
| @@ -244,8 +242,6 @@ BUILD_TYPE(uint16) | |||||||
| BUILD_TYPE(int16) | BUILD_TYPE(int16) | ||||||
| BUILD_TYPE(uint32) | BUILD_TYPE(uint32) | ||||||
| BUILD_TYPE(int32) | BUILD_TYPE(int32) | ||||||
| BUILD_TYPE(uint64) |  | ||||||
| BUILD_TYPE(int64) |  | ||||||
| BUILD_TYPE(float32) | BUILD_TYPE(float32) | ||||||
| BUILD_TYPE(float64) | BUILD_TYPE(float64) | ||||||
|  |  | ||||||
| @@ -272,8 +268,6 @@ static const JanetAbstractType ta_array_types[] = { | |||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(int16), |     DEFINE_VIEW_ABSTRACT_TYPE(int16), | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(uint32), |     DEFINE_VIEW_ABSTRACT_TYPE(uint32), | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(int32), |     DEFINE_VIEW_ABSTRACT_TYPE(int32), | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(uint64), |  | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(int64), |  | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(float32), |     DEFINE_VIEW_ABSTRACT_TYPE(float32), | ||||||
|     DEFINE_VIEW_ABSTRACT_TYPE(float64) |     DEFINE_VIEW_ABSTRACT_TYPE(float64) | ||||||
| }; | }; | ||||||
| @@ -296,7 +290,8 @@ static int is_ta_type(Janet x, JanetTArrayType type) { | |||||||
|            (janet_abstract_type(janet_unwrap_abstract(x)) == &ta_array_types[type]); |            (janet_abstract_type(janet_unwrap_abstract(x)) == &ta_array_types[type]); | ||||||
| } | } | ||||||
|  |  | ||||||
| #define CASE_TYPE_INITIALIZE(type)  case  JANET_TARRAY_TYPE_##type :  ta_init_##type(view,buffer,size,offset,stride); break | #define CASE_TYPE_INITIALIZE(type) case JANET_TARRAY_TYPE_##type: \ | ||||||
|  |     ta_init_##type(view,buffer,size,offset,stride); break | ||||||
|  |  | ||||||
| JanetTArrayBuffer *janet_tarray_buffer(size_t size) { | JanetTArrayBuffer *janet_tarray_buffer(size_t size) { | ||||||
|     JanetTArrayBuffer *buf = (JanetTArrayBuffer *)janet_abstract(&ta_buffer_type, sizeof(JanetTArrayBuffer)); |     JanetTArrayBuffer *buf = (JanetTArrayBuffer *)janet_abstract(&ta_buffer_type, sizeof(JanetTArrayBuffer)); | ||||||
| @@ -313,8 +308,6 @@ JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t str | |||||||
|             CASE_TYPE_INITIALIZE(int16); |             CASE_TYPE_INITIALIZE(int16); | ||||||
|             CASE_TYPE_INITIALIZE(uint32); |             CASE_TYPE_INITIALIZE(uint32); | ||||||
|             CASE_TYPE_INITIALIZE(int32); |             CASE_TYPE_INITIALIZE(int32); | ||||||
|             CASE_TYPE_INITIALIZE(uint64); |  | ||||||
|             CASE_TYPE_INITIALIZE(int64); |  | ||||||
|             CASE_TYPE_INITIALIZE(float32); |             CASE_TYPE_INITIALIZE(float32); | ||||||
|             CASE_TYPE_INITIALIZE(float64); |             CASE_TYPE_INITIALIZE(float64); | ||||||
|         default : |         default : | ||||||
| @@ -333,8 +326,8 @@ int janet_is_tarray_view(Janet x, JanetTArrayType type) { | |||||||
|     return (type == JANET_TARRAY_TYPE_any) ? is_ta_anytype(x) : is_ta_type(x, type); |     return (type == JANET_TARRAY_TYPE_any) ? is_ta_anytype(x) : is_ta_type(x, type); | ||||||
| } | } | ||||||
|  |  | ||||||
| int janet_tarray_type_size(JanetTArrayType type) { | size_t janet_tarray_type_size(JanetTArrayType type) { | ||||||
|     return (type < TA_COUNT_TYPES) ? ta_type_sizes[type] : 0 ; |     return (type < TA_COUNT_TYPES) ? ta_type_sizes[type] : 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type) { | JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type) { | ||||||
| @@ -354,11 +347,11 @@ static Janet cfun_typed_array_new(int32_t argc, Janet *argv) { | |||||||
|     JanetTArrayBuffer *buffer = NULL; |     JanetTArrayBuffer *buffer = NULL; | ||||||
|     const uint8_t *keyw = janet_getkeyword(argv, 0); |     const uint8_t *keyw = janet_getkeyword(argv, 0); | ||||||
|     JanetTArrayType type = get_ta_type_by_name(keyw); |     JanetTArrayType type = get_ta_type_by_name(keyw); | ||||||
|     size_t size = (size_t)janet_getinteger(argv, 1); |     size_t size = janet_getsize(argv, 1); | ||||||
|     if (argc > 2) |     if (argc > 2) | ||||||
|         stride = (size_t)janet_getinteger(argv, 2); |         stride = janet_getsize(argv, 2); | ||||||
|     if (argc > 3) |     if (argc > 3) | ||||||
|         offset = (size_t)janet_getinteger(argv, 3); |         offset = janet_getsize(argv, 3); | ||||||
|     if (argc > 4) { |     if (argc > 4) { | ||||||
|         if (is_ta_anytype(argv[4])) { |         if (is_ta_anytype(argv[4])) { | ||||||
|             JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[4]); |             JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[4]); | ||||||
| @@ -379,7 +372,7 @@ static Janet cfun_typed_array_buffer(int32_t argc, Janet *argv) { | |||||||
|         JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[0]); |         JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[0]); | ||||||
|         return janet_wrap_abstract(view->buffer); |         return janet_wrap_abstract(view->buffer); | ||||||
|     } |     } | ||||||
|     size_t size = (size_t)janet_getinteger(argv, 0); |     size_t size = janet_getsize(argv, 0); | ||||||
|     JanetTArrayBuffer *buf = janet_tarray_buffer(size); |     JanetTArrayBuffer *buf = janet_tarray_buffer(size); | ||||||
|     return janet_wrap_abstract(buf); |     return janet_wrap_abstract(buf); | ||||||
| } | } | ||||||
| @@ -399,46 +392,31 @@ static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) { | |||||||
|     if (is_ta_anytype(argv[0])) { |     if (is_ta_anytype(argv[0])) { | ||||||
|         JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[0]); |         JanetTArrayView *view = (JanetTArrayView *)janet_unwrap_abstract(argv[0]); | ||||||
|         JanetKV *props = janet_struct_begin(6); |         JanetKV *props = janet_struct_begin(6); | ||||||
|  |         ptrdiff_t boffset = (uint8_t *)(view->data) - view->buffer->data; | ||||||
|         janet_struct_put(props, janet_ckeywordv("size"), |         janet_struct_put(props, janet_ckeywordv("size"), | ||||||
|                 janet_wrap_number((double) view->size)); |                          janet_wrap_number((double) view->size)); | ||||||
|         janet_struct_put(props, janet_ckeywordv("byte-offset"), |         janet_struct_put(props, janet_ckeywordv("byte-offset"), | ||||||
|                 janet_wrap_number((uint8_t *)(view->data) - view->buffer->data)); |                          janet_wrap_number((double) boffset)); | ||||||
|         janet_struct_put(props, janet_ckeywordv("stride"), |         janet_struct_put(props, janet_ckeywordv("stride"), | ||||||
|                 janet_wrap_number((double) view->stride)); |                          janet_wrap_number((double) view->stride)); | ||||||
|         janet_struct_put(props, janet_ckeywordv("type"), |         janet_struct_put(props, janet_ckeywordv("type"), | ||||||
|                 janet_ckeywordv(ta_type_names[view->type])); |                          janet_ckeywordv(ta_type_names[view->type])); | ||||||
|         janet_struct_put(props, janet_ckeywordv("type-size"), |         janet_struct_put(props, janet_ckeywordv("type-size"), | ||||||
|                 janet_wrap_number((double) ta_type_sizes[view->type])); |                          janet_wrap_number((double) ta_type_sizes[view->type])); | ||||||
|         janet_struct_put(props, janet_ckeywordv("buffer"), |         janet_struct_put(props, janet_ckeywordv("buffer"), | ||||||
|                 janet_wrap_abstract(view->buffer)); |                          janet_wrap_abstract(view->buffer)); | ||||||
|         return janet_wrap_struct(janet_struct_end(props)); |         return janet_wrap_struct(janet_struct_end(props)); | ||||||
|     } else { |     } else { | ||||||
|         JanetTArrayBuffer *buffer = janet_gettarray_buffer(argv, 0); |         JanetTArrayBuffer *buffer = janet_gettarray_buffer(argv, 0); | ||||||
|         JanetKV *props = janet_struct_begin(2); |         JanetKV *props = janet_struct_begin(2); | ||||||
|         janet_struct_put(props, janet_ckeywordv("size"), |         janet_struct_put(props, janet_ckeywordv("size"), | ||||||
|                 janet_wrap_number((double) buffer->size)); |                          janet_wrap_number((double) buffer->size)); | ||||||
|         janet_struct_put(props, janet_ckeywordv("big-endian"), |         janet_struct_put(props, janet_ckeywordv("big-endian"), | ||||||
|                 janet_wrap_boolean(buffer->flags & TA_FLAG_BIG_ENDIAN)); |                          janet_wrap_boolean(buffer->flags & TA_FLAG_BIG_ENDIAN)); | ||||||
|         return janet_wrap_struct(janet_struct_end(props)); |         return janet_wrap_struct(janet_struct_end(props)); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| /* TODO move it, it's not the good place for this function */ |  | ||||||
| static Janet cfun_abstract_properties(int32_t argc, Janet *argv) { |  | ||||||
|     janet_fixarity(argc, 1); |  | ||||||
|     const uint8_t *key = janet_getkeyword(argv, 0); |  | ||||||
|     const JanetAbstractType *at = janet_get_abstract_type(janet_wrap_keyword(key)); |  | ||||||
|     if (at == NULL) { |  | ||||||
|         return janet_wrap_nil(); |  | ||||||
|     } |  | ||||||
|     JanetKV *props = janet_struct_begin(2); |  | ||||||
|     janet_struct_put(props, janet_ckeywordv("name"), |  | ||||||
|             janet_ckeywordv(at->name)); |  | ||||||
|     janet_struct_put(props, janet_ckeywordv("marshal"), |  | ||||||
|             janet_wrap_boolean((at->marshal != NULL) && (at->unmarshal != NULL))); |  | ||||||
|     return janet_wrap_struct(janet_struct_end(props)); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static Janet cfun_typed_array_slice(int32_t argc, Janet *argv) { | static Janet cfun_typed_array_slice(int32_t argc, Janet *argv) { | ||||||
|     janet_arity(argc, 1, 3); |     janet_arity(argc, 1, 3); | ||||||
|     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); |     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); | ||||||
| @@ -470,10 +448,10 @@ static Janet cfun_typed_array_slice(int32_t argc, Janet *argv) { | |||||||
| static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) { | static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) { | ||||||
|     janet_arity(argc, 4, 5); |     janet_arity(argc, 4, 5); | ||||||
|     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); |     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); | ||||||
|     size_t index_src = (size_t)janet_getinteger(argv, 1); |     size_t index_src = janet_getsize(argv, 1); | ||||||
|     JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any); |     JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any); | ||||||
|     size_t index_dst = (size_t)janet_getinteger(argv, 3); |     size_t index_dst = janet_getsize(argv, 3); | ||||||
|     size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1; |     size_t count = (argc == 5) ? janet_getsize(argv, 4) : 1; | ||||||
|     size_t src_atom_size = ta_type_sizes[src->type]; |     size_t src_atom_size = ta_type_sizes[src->type]; | ||||||
|     size_t dst_atom_size = ta_type_sizes[dst->type]; |     size_t dst_atom_size = ta_type_sizes[dst->type]; | ||||||
|     size_t step_src = src->stride * src_atom_size; |     size_t step_src = src->stride * src_atom_size; | ||||||
| @@ -497,10 +475,10 @@ static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) { | |||||||
| static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) { | static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) { | ||||||
|     janet_arity(argc, 4, 5); |     janet_arity(argc, 4, 5); | ||||||
|     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); |     JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any); | ||||||
|     size_t index_src = (size_t)janet_getinteger(argv, 1); |     size_t index_src = janet_getsize(argv, 1); | ||||||
|     JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any); |     JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any); | ||||||
|     size_t index_dst = (size_t)janet_getinteger(argv, 3); |     size_t index_dst = janet_getsize(argv, 3); | ||||||
|     size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1; |     size_t count = (argc == 5) ? janet_getsize(argv, 4) : 1; | ||||||
|     size_t src_atom_size = ta_type_sizes[src->type]; |     size_t src_atom_size = ta_type_sizes[src->type]; | ||||||
|     size_t dst_atom_size = ta_type_sizes[dst->type]; |     size_t dst_atom_size = ta_type_sizes[dst->type]; | ||||||
|     size_t step_src = src->stride * src_atom_size; |     size_t step_src = src->stride * src_atom_size; | ||||||
| @@ -567,11 +545,6 @@ static const JanetReg ta_cfuns[] = { | |||||||
|              "from the end of the end of the typed array. By default, start is 0 and end is " |              "from the end of the end of the typed array. By default, start is 0 and end is " | ||||||
|              "the size of the typed array. Returns a new janet array.") |              "the size of the typed array. Returns a new janet array.") | ||||||
|     }, |     }, | ||||||
|     { |  | ||||||
|         "abstract/properties", cfun_abstract_properties, |  | ||||||
|         JDOC("(abstract/properties tag)\n\n" |  | ||||||
|              "Returns abstract type properties as a struct.") |  | ||||||
|     }, |  | ||||||
|     {NULL, NULL, NULL} |     {NULL, NULL, NULL} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -579,7 +552,7 @@ static const JanetReg ta_cfuns[] = { | |||||||
| 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); |     janet_register_abstract_type(&ta_buffer_type); | ||||||
|     for (size_t i = 0; i < TA_COUNT_TYPES; i++) { |     for (int i = 0; i < TA_COUNT_TYPES; i++) { | ||||||
|         janet_register_abstract_type(ta_array_types + i); |         janet_register_abstract_type(ta_array_types + i); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -286,36 +286,45 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) | |||||||
|  |  | ||||||
| /* Abstract type introspection */ | /* Abstract type introspection */ | ||||||
|  |  | ||||||
| static const JanetAbstractType type_wrap = {"core/type_info", NULL, NULL, NULL, NULL, NULL, NULL}; | static const JanetAbstractType type_wrap = { | ||||||
|  |     "core/type-info", | ||||||
|  |     NULL, | ||||||
|  |     NULL, | ||||||
|  |     NULL, | ||||||
|  |     NULL, | ||||||
|  |     NULL, | ||||||
|  |     NULL | ||||||
|  | }; | ||||||
|  |  | ||||||
| typedef struct { | typedef struct { | ||||||
|     const JanetAbstractType *at; |     const JanetAbstractType *at; | ||||||
| } JanetAbstractTypeWrap; | } JanetAbstractTypeWrap; | ||||||
|  |  | ||||||
|  |  | ||||||
| void janet_register_abstract_type(const JanetAbstractType *at) { | void janet_register_abstract_type(const JanetAbstractType *at) { | ||||||
|     JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *)janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap)); |     JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *) | ||||||
|  |                                       janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap)); | ||||||
|     abstract->at = at; |     abstract->at = at; | ||||||
|     if (!(janet_checktype(janet_table_get(janet_vm_registry, janet_ckeywordv(at->name)), JANET_NIL))) { |     Janet sym = janet_csymbolv(at->name); | ||||||
|         janet_panic("Register abstract type fail, a type with same name exists"); |     if (!(janet_checktype(janet_table_get(janet_vm_registry, sym), JANET_NIL))) { | ||||||
|  |         janet_panicf("cannot register abstract type %s, " | ||||||
|  |                      "a type with the same name exists", at->name); | ||||||
|     } |     } | ||||||
|     janet_table_put(janet_vm_registry, janet_ckeywordv(at->name), janet_wrap_abstract(abstract)); |     janet_table_put(janet_vm_registry, sym, janet_wrap_abstract(abstract)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| const JanetAbstractType *janet_get_abstract_type(Janet key) { | const JanetAbstractType *janet_get_abstract_type(Janet key) { | ||||||
|     Janet twrap = janet_table_get(janet_vm_registry, key); |     Janet twrap = janet_table_get(janet_vm_registry, key); | ||||||
|     if (janet_checktype(twrap, JANET_NIL)) { |     if (janet_checktype(twrap, JANET_NIL)) { | ||||||
|         return NULL; |         return NULL; | ||||||
|     } |     } | ||||||
|     if (!janet_checktype(twrap, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(twrap)) != &type_wrap)) { |     if (!janet_checktype(twrap, JANET_ABSTRACT) || | ||||||
|  |             (janet_abstract_type(janet_unwrap_abstract(twrap)) != &type_wrap)) { | ||||||
|         janet_panic("expected abstract type"); |         janet_panic("expected abstract type"); | ||||||
|     } |     } | ||||||
|     JanetAbstractTypeWrap *w = (JanetAbstractTypeWrap *)janet_unwrap_abstract(twrap); |     JanetAbstractTypeWrap *w = (JanetAbstractTypeWrap *)janet_unwrap_abstract(twrap); | ||||||
|     return w->at; |     return w->at; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| #ifndef JANET_BOOTSTRAP | #ifndef JANET_BOOTSTRAP | ||||||
| void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p) { | void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p) { | ||||||
|     (void) p; |     (void) p; | ||||||
| @@ -420,42 +429,10 @@ int janet_checkint64(Janet x) { | |||||||
|     return janet_checkint64range(dval); |     return janet_checkint64range(dval); | ||||||
| } | } | ||||||
|  |  | ||||||
| /* Useful for inspecting values while debugging */ | int janet_checksize(Janet x) { | ||||||
| void janet_inspect(Janet x) { |     if (!janet_checktype(x, JANET_NUMBER)) | ||||||
|     printf("<type=%s, ", janet_type_names[janet_type(x)]); |         return 0; | ||||||
|  |     double dval = janet_unwrap_number(x); | ||||||
| #ifdef JANET_BIG_ENDIAN |     return dval == (double)((size_t) dval) && | ||||||
|     printf("be "); |            dval <= SIZE_MAX; | ||||||
| #else |  | ||||||
|     printf("le "); |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #ifdef JANET_NANBOX_64 |  | ||||||
|     printf("nanbox64 raw=0x%.16" PRIx64 ", ", x.u64); |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #ifdef JANET_NANBOX_32 |  | ||||||
|     printf("nanbox32 type=0x%.8" PRIx32 ", ", x.tagged.type); |  | ||||||
|     printf("payload=%" PRId32 ", ", x.tagged.payload.integer); |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
|     switch (janet_type(x)) { |  | ||||||
|         case JANET_NIL: |  | ||||||
|             printf("value=nil"); |  | ||||||
|             break; |  | ||||||
|         case JANET_NUMBER: |  | ||||||
|             printf("number=%.17g", janet_unwrap_number(x)); |  | ||||||
|             break; |  | ||||||
|         case JANET_TRUE: |  | ||||||
|             printf("value=true"); |  | ||||||
|             break; |  | ||||||
|         case JANET_FALSE: |  | ||||||
|             printf("value=false"); |  | ||||||
|             break; |  | ||||||
|         default: |  | ||||||
|             printf("pointer=%p", janet_unwrap_pointer(x)); |  | ||||||
|             break; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     printf(">\n"); |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -585,6 +585,7 @@ JANET_API Janet janet_wrap_abstract(void *x); | |||||||
|  |  | ||||||
| JANET_API int janet_checkint(Janet x); | JANET_API int janet_checkint(Janet x); | ||||||
| JANET_API int janet_checkint64(Janet x); | JANET_API int janet_checkint64(Janet x); | ||||||
|  | JANET_API int janet_checksize(Janet x); | ||||||
| #define janet_checkintrange(x) ((x) == (int32_t)(x)) | #define janet_checkintrange(x) ((x) == (int32_t)(x)) | ||||||
| #define janet_checkint64range(x) ((x) == (int64_t)(x)) | #define janet_checkint64range(x) ((x) == (int64_t)(x)) | ||||||
| #define janet_unwrap_integer(x) ((int32_t) janet_unwrap_number(x)) | #define janet_unwrap_integer(x) ((int32_t) janet_unwrap_number(x)) | ||||||
| @@ -1245,6 +1246,7 @@ JANET_API int janet_getboolean(const Janet *argv, int32_t n); | |||||||
|  |  | ||||||
| JANET_API int32_t janet_getinteger(const Janet *argv, int32_t n); | JANET_API int32_t janet_getinteger(const Janet *argv, int32_t n); | ||||||
| JANET_API int64_t janet_getinteger64(const Janet *argv, int32_t n); | JANET_API int64_t janet_getinteger64(const Janet *argv, int32_t n); | ||||||
|  | JANET_API size_t janet_getsize(const Janet *argv, int32_t n); | ||||||
| JANET_API JanetView janet_getindexed(const Janet *argv, int32_t n); | JANET_API JanetView janet_getindexed(const Janet *argv, int32_t n); | ||||||
| JANET_API JanetByteView janet_getbytes(const Janet *argv, int32_t n); | JANET_API JanetByteView janet_getbytes(const Janet *argv, int32_t n); | ||||||
| JANET_API JanetDictView janet_getdictionary(const Janet *argv, int32_t n); | JANET_API JanetDictView janet_getdictionary(const Janet *argv, int32_t n); | ||||||
| @@ -1280,8 +1282,6 @@ typedef enum { | |||||||
|     JANET_TARRAY_TYPE_int16, |     JANET_TARRAY_TYPE_int16, | ||||||
|     JANET_TARRAY_TYPE_uint32, |     JANET_TARRAY_TYPE_uint32, | ||||||
|     JANET_TARRAY_TYPE_int32, |     JANET_TARRAY_TYPE_int32, | ||||||
|     JANET_TARRAY_TYPE_uint64, |  | ||||||
|     JANET_TARRAY_TYPE_int64, |  | ||||||
|     JANET_TARRAY_TYPE_float32, |     JANET_TARRAY_TYPE_float32, | ||||||
|     JANET_TARRAY_TYPE_float64, |     JANET_TARRAY_TYPE_float64, | ||||||
|     JANET_TARRAY_TYPE_any, |     JANET_TARRAY_TYPE_any, | ||||||
| @@ -1304,7 +1304,7 @@ typedef struct { | |||||||
| JANET_API JanetTArrayBuffer *janet_tarray_buffer(size_t size); | JANET_API JanetTArrayBuffer *janet_tarray_buffer(size_t size); | ||||||
| JANET_API JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t stride, size_t offset, JanetTArrayBuffer *buffer); | JANET_API JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t stride, size_t offset, JanetTArrayBuffer *buffer); | ||||||
| JANET_API int janet_is_tarray_view(Janet x, JanetTArrayType type); | JANET_API int janet_is_tarray_view(Janet x, JanetTArrayType type); | ||||||
| JANET_API int janet_tarray_type_size(JanetTArrayType type); | JANET_API size_t janet_tarray_type_size(JanetTArrayType type); | ||||||
| JANET_API JanetTArrayBuffer *janet_gettarray_buffer(const Janet *argv, int32_t n); | JANET_API JanetTArrayBuffer *janet_gettarray_buffer(const Janet *argv, int32_t n); | ||||||
| JANET_API JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type); | JANET_API JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -20,17 +20,24 @@ | |||||||
|  |  | ||||||
| (import test/helper :prefix "" :exit true) | (import test/helper :prefix "" :exit true) | ||||||
| (start-suite 5) | (start-suite 5) | ||||||
|  |  | ||||||
| # some tests typed array | # some tests typed array | ||||||
|  |  | ||||||
|  | (defn inspect-tarray | ||||||
|  |   [x] | ||||||
|  |   (def a @[]) | ||||||
|  |   (for i 0 (tarray/length x) (array/push a (x i))) | ||||||
|  |   (pp a)) | ||||||
|  |  | ||||||
| (assert-no-error | (assert-no-error | ||||||
|  "create some typed array" |  "create some typed arrays" | ||||||
|  (do |  (do | ||||||
|    (def a (tarray/new :float64 10)) |    (def a (tarray/new :float64 10)) | ||||||
|    (def b (tarray/new :float64 5 2 0 a)) |    (def b (tarray/new :float64 5 2 0 a)) | ||||||
|    (def c (tarray/new :uint32 20)))) |    (def c (tarray/new :uint32 20)))) | ||||||
|  |  | ||||||
| (assert-no-error | (assert-no-error | ||||||
|  "create some typed array from buffer" |  "create some typed arrays from a buffer" | ||||||
|  (do |  (do | ||||||
|    (def buf (tarray/buffer (+ 64 (* (+ 1 (* (- 10 1) 2)) 8)))) |    (def buf (tarray/buffer (+ 64 (* (+ 1 (* (- 10 1) 2)) 8)))) | ||||||
|    (def b (tarray/new :float64 10 2 64 buf)))) |    (def b (tarray/new :float64 10 2 64 buf)))) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose