1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Allow getting typed arrays from byte sequences.

Fix native importing for .so files in current directory.
This commit is contained in:
Calvin Rose 2020-07-24 07:01:34 -05:00
parent 8f82d19fd1
commit 43438d3824
5 changed files with 51 additions and 19 deletions

View File

@ -1,6 +1,11 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## ??? - Unreleased
- Fix jpm and git with multiple git installs on Windows
- Fix importing a .so file in the current directory
- Allow passing byte sequence types directly to typed-array constructors.
## 1.11.0 - 2020-07-18 ## 1.11.0 - 2020-07-18
- Add `forever` macro. - Add `forever` macro.
- Add `any?` predicate to core. - Add `any?` predicate to core.

View File

@ -20,7 +20,7 @@
project('janet', 'c', project('janet', 'c',
default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'], default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'],
version : '1.11.0') version : '1.11.1')
# Global settings # Global settings
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet') janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')

View File

@ -28,9 +28,9 @@
#define JANET_VERSION_MAJOR 1 #define JANET_VERSION_MAJOR 1
#define JANET_VERSION_MINOR 11 #define JANET_VERSION_MINOR 11
#define JANET_VERSION_PATCH 0 #define JANET_VERSION_PATCH 1
#define JANET_VERSION_EXTRA "" #define JANET_VERSION_EXTRA "-dev"
#define JANET_VERSION "1.11.0" #define JANET_VERSION "1.11.1-dev"
/* #define JANET_BUILD "local" */ /* #define JANET_BUILD "local" */

View File

@ -63,10 +63,29 @@ typedef void *Clib;
#define error_clib() dlerror() #define error_clib() dlerror()
#endif #endif
static char *get_processed_name(const char *name) {
if (name[0] == '.') return (char *) name;
const char *c;
for (c = name; *c; c++) {
if (*c == '/') return (char *) name;
}
size_t l = (size_t)(c - name);
char *ret = malloc(l + 3);
if (NULL == ret) {
JANET_OUT_OF_MEMORY;
}
ret[0] = '.';
ret[1] = '/';
memcpy(ret + 2, name, l + 3);
return ret;
}
JanetModule janet_native(const char *name, const uint8_t **error) { JanetModule janet_native(const char *name, const uint8_t **error) {
Clib lib = load_clib(name); char *processed_name = get_processed_name(name);
Clib lib = load_clib(processed_name);
JanetModule init; JanetModule init;
JanetModconf getter; JanetModconf getter;
if (name != processed_name) free(processed_name);
if (!lib) { if (!lib) {
*error = janet_cstring(error_clib()); *error = janet_cstring(error_clib());
return NULL; return NULL;

View File

@ -376,21 +376,29 @@ static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
if (argc > 3) if (argc > 3)
offset = janet_getsize(argv, 3); offset = janet_getsize(argv, 3);
if (argc > 4) { if (argc > 4) {
if (!janet_checktype(argv[4], JANET_ABSTRACT)) { int32_t blen;
janet_panicf("bad slot #%d, expected ta/view|ta/buffer, got %v", const uint8_t *bytes;
4, argv[4]); if (janet_bytes_view(argv[4], &bytes, &blen)) {
} buffer = janet_abstract(&janet_ta_buffer_type, sizeof(JanetTArrayBuffer));
void *p = janet_unwrap_abstract(argv[4]); ta_buffer_init(buffer, (size_t) blen);
if (janet_abstract_type(p) == &janet_ta_view_type) { memcpy(buffer->data, bytes, blen);
JanetTArrayView *view = (JanetTArrayView *)p;
offset = (view->buffer->data - view->as.u8) + offset * ta_type_sizes[view->type];
stride *= view->stride;
buffer = view->buffer;
} else if (janet_abstract_type(p) == &janet_ta_buffer_type) {
buffer = p;
} else { } else {
janet_panicf("bad slot #%d, expected ta/view|ta/buffer, got %v", if (!janet_checktype(argv[4], JANET_ABSTRACT)) {
4, argv[4]); janet_panicf("bad slot #%d, expected ta/view|ta/buffer, got %v",
4, argv[4]);
}
void *p = janet_unwrap_abstract(argv[4]);
if (janet_abstract_type(p) == &janet_ta_view_type) {
JanetTArrayView *view = (JanetTArrayView *)p;
offset = (view->buffer->data - view->as.u8) + offset * ta_type_sizes[view->type];
stride *= view->stride;
buffer = view->buffer;
} else if (janet_abstract_type(p) == &janet_ta_buffer_type) {
buffer = p;
} else {
janet_panicf("bad slot #%d, expected ta/view|ta/buffer, got %v",
4, argv[4]);
}
} }
} }
JanetTArrayView *view = janet_tarray_view(type, size, stride, offset, buffer); JanetTArrayView *view = janet_tarray_view(type, size, stride, offset, buffer);