2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2018-09-06 02:18:42 +00:00
|
|
|
* Define a number of functions that can be used internally on ANY Janet.
|
2017-11-01 21:53:43 +00:00
|
|
|
*/
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Check if two values are equal. This is strict equality with no conversion. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_equals(Janet x, Janet y) {
|
2017-02-09 23:50:47 +00:00
|
|
|
int result = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_type(x) != janet_type(y)) {
|
2017-02-09 20:02:59 +00:00
|
|
|
result = 0;
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
switch (janet_type(x)) {
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_NIL:
|
|
|
|
case JANET_TRUE:
|
|
|
|
case JANET_FALSE:
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
case JANET_NUMBER:
|
|
|
|
result = (janet_unwrap_number(x) == janet_unwrap_number(y));
|
|
|
|
break;
|
|
|
|
case JANET_STRING:
|
|
|
|
result = janet_string_equal(janet_unwrap_string(x), janet_unwrap_string(y));
|
|
|
|
break;
|
|
|
|
case JANET_TUPLE:
|
|
|
|
result = janet_tuple_equal(janet_unwrap_tuple(x), janet_unwrap_tuple(y));
|
|
|
|
break;
|
|
|
|
case JANET_STRUCT:
|
|
|
|
result = janet_struct_equal(janet_unwrap_struct(x), janet_unwrap_struct(y));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* compare pointers */
|
|
|
|
result = (janet_unwrap_pointer(x) == janet_unwrap_pointer(y));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes a hash value for a function */
|
|
|
|
int32_t janet_hash(Janet x) {
|
|
|
|
int32_t hash = 0;
|
|
|
|
switch (janet_type(x)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = 0;
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_FALSE:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = 1;
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_TRUE:
|
|
|
|
hash = 2;
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_KEYWORD:
|
|
|
|
hash = janet_string_hash(janet_unwrap_string(x));
|
2017-11-27 19:03:34 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TUPLE:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = janet_tuple_hash(janet_unwrap_tuple(x));
|
2018-01-04 02:36:10 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRUCT:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = janet_struct_hash(janet_unwrap_struct(x));
|
2017-11-27 19:03:34 +00:00
|
|
|
break;
|
2017-04-15 20:05:59 +00:00
|
|
|
default:
|
2019-02-20 01:51:34 +00:00
|
|
|
/* TODO - test performance with different hash functions */
|
|
|
|
if (sizeof(double) == sizeof(void *)) {
|
|
|
|
/* Assuming 8 byte pointer */
|
|
|
|
uint64_t i = janet_u64(x);
|
|
|
|
hash = (int32_t)(i & 0xFFFFFFFF);
|
|
|
|
/* Get a bit more entropy by shifting the low bits out */
|
|
|
|
hash >>= 3;
|
|
|
|
hash ^= (int32_t)(i >> 32);
|
|
|
|
} else {
|
|
|
|
/* Assuming 4 byte pointer (or smaller) */
|
|
|
|
hash = (int32_t)((char *)janet_unwrap_pointer(x) - (char *)0);
|
|
|
|
hash >>= 2;
|
|
|
|
}
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Compares x to y. If they are equal returns 0. If x is less, returns -1.
|
2017-02-09 20:02:59 +00:00
|
|
|
* If y is less, returns 1. All types are comparable
|
|
|
|
* and should have strict ordering. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_compare(Janet x, Janet y) {
|
|
|
|
if (janet_type(x) == janet_type(y)) {
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
case JANET_NIL:
|
|
|
|
case JANET_FALSE:
|
|
|
|
case JANET_TRUE:
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_NUMBER:
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Check for NaNs to ensure total order */
|
2018-12-27 18:05:29 +00:00
|
|
|
if (janet_unwrap_number(x) != janet_unwrap_number(x))
|
|
|
|
return janet_unwrap_number(y) != janet_unwrap_number(y)
|
2019-02-20 01:51:34 +00:00
|
|
|
? 0
|
|
|
|
: -1;
|
2018-12-27 18:05:29 +00:00
|
|
|
if (janet_unwrap_number(y) != janet_unwrap_number(y))
|
2017-11-06 03:05:47 +00:00
|
|
|
return 1;
|
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
if (janet_unwrap_number(x) == janet_unwrap_number(y)) {
|
2017-04-24 20:02:54 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2018-12-27 18:05:29 +00:00
|
|
|
return janet_unwrap_number(x) > janet_unwrap_number(y) ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_string_compare(janet_unwrap_string(x), janet_unwrap_string(y));
|
|
|
|
case JANET_TUPLE:
|
|
|
|
return janet_tuple_compare(janet_unwrap_tuple(x), janet_unwrap_tuple(y));
|
|
|
|
case JANET_STRUCT:
|
|
|
|
return janet_struct_compare(janet_unwrap_struct(x), janet_unwrap_struct(y));
|
2017-02-19 16:19:39 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_unwrap_string(x) == janet_unwrap_string(y)) {
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_unwrap_string(x) > janet_unwrap_string(y) ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
return (janet_type(x) < janet_type(y)) ? -1 : 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
/* Gets a value and returns. Can panic. */
|
|
|
|
Janet janet_get(Janet ds, Janet key) {
|
2018-12-27 18:05:29 +00:00
|
|
|
Janet value;
|
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-06 16:56:40 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
value = janet_wrap_nil();
|
|
|
|
break;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRUCT:
|
|
|
|
value = janet_struct_get(janet_unwrap_struct(ds), key);
|
|
|
|
break;
|
|
|
|
case JANET_TABLE:
|
|
|
|
value = janet_table_get(janet_unwrap_table(ds), key);
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ARRAY: {
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
|
|
|
int32_t index;
|
|
|
|
if (!janet_checkint(key))
|
|
|
|
janet_panic("expected integer key");
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index >= array->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = array->data[index];
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
const Janet *tuple = janet_unwrap_tuple(ds);
|
|
|
|
int32_t index;
|
|
|
|
if (!janet_checkint(key))
|
|
|
|
janet_panic("expected integer key");
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index >= janet_tuple_length(tuple)) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = tuple[index];
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
|
|
|
int32_t index;
|
|
|
|
if (!janet_checkint(key))
|
|
|
|
janet_panic("expected integer key");
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index >= buffer->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(buffer->data[index]);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_KEYWORD: {
|
|
|
|
const uint8_t *str = janet_unwrap_string(ds);
|
|
|
|
int32_t index;
|
|
|
|
if (!janet_checkint(key))
|
|
|
|
janet_panic("expected integer key");
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index >= janet_string_length(str)) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(str[index]);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->get) {
|
|
|
|
value = (type->get)(janet_unwrap_abstract(ds), key);
|
|
|
|
} else {
|
|
|
|
janet_panicf("no getter for %T ", JANET_TFLAG_LENGTHABLE, ds);
|
|
|
|
value = janet_wrap_nil();
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-01-05 05:33:20 +00:00
|
|
|
return value;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
Janet janet_getindex(Janet ds, int32_t index) {
|
2018-12-27 18:05:29 +00:00
|
|
|
Janet value;
|
2019-01-05 05:33:20 +00:00
|
|
|
if (index < 0) janet_panic("expected non-negative index");
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-06 16:56:40 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
value = janet_wrap_nil();
|
|
|
|
break;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2018-12-27 18:05:29 +00:00
|
|
|
if (index >= janet_string_length(janet_unwrap_string(ds))) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(janet_unwrap_string(ds)[index]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_ARRAY:
|
|
|
|
if (index >= janet_unwrap_array(ds)->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_unwrap_array(ds)->data[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_BUFFER:
|
|
|
|
if (index >= janet_unwrap_buffer(ds)->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(janet_unwrap_buffer(ds)->data[index]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_TUPLE:
|
|
|
|
if (index >= janet_tuple_length(janet_unwrap_tuple(ds))) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_unwrap_tuple(ds)[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_TABLE:
|
|
|
|
value = janet_table_get(janet_unwrap_table(ds), janet_wrap_integer(index));
|
|
|
|
break;
|
|
|
|
case JANET_STRUCT:
|
|
|
|
value = janet_struct_get(janet_unwrap_struct(ds), janet_wrap_integer(index));
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->get) {
|
|
|
|
value = (type->get)(janet_unwrap_abstract(ds), janet_wrap_integer(index));
|
|
|
|
} else {
|
|
|
|
janet_panicf("no getter for %T ", JANET_TFLAG_LENGTHABLE, ds);
|
|
|
|
value = janet_wrap_nil();
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-01-05 05:33:20 +00:00
|
|
|
return value;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
int32_t janet_length(Janet x) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(x)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, x);
|
|
|
|
return 0;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_string_length(janet_unwrap_string(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_ARRAY:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_array(x)->count;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_BUFFER:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_buffer(x)->count;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TUPLE:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_tuple_length(janet_unwrap_tuple(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRUCT:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_struct_length(janet_unwrap_struct(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TABLE:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_table(x)->count;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
void janet_putindex(Janet ds, int32_t index, Janet value) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v",
|
2019-02-20 01:51:34 +00:00
|
|
|
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ARRAY: {
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
|
|
|
if (index >= array->count) {
|
|
|
|
janet_array_ensure(array, index + 1, 2);
|
|
|
|
array->count = index + 1;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
array->data[index] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
|
|
|
if (!janet_checkint(value))
|
|
|
|
janet_panicf("can only put integers in buffers, got %v", value);
|
|
|
|
if (index >= buffer->count) {
|
|
|
|
janet_buffer_ensure(buffer, index + 1, 2);
|
|
|
|
buffer->count = index + 1;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
buffer->data[index] = janet_unwrap_integer(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_TABLE: {
|
|
|
|
JanetTable *table = janet_unwrap_table(ds);
|
|
|
|
janet_table_put(table, janet_wrap_integer(index), value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->put) {
|
|
|
|
(type->put)(janet_unwrap_abstract(ds), janet_wrap_integer(index), value);
|
|
|
|
} else {
|
|
|
|
janet_panicf("no setter for %T ", JANET_TFLAG_LENGTHABLE, ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
void janet_put(Janet ds, Janet key, Janet value) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v",
|
2019-02-20 01:51:34 +00:00
|
|
|
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ARRAY: {
|
|
|
|
int32_t index;
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
|
|
|
if (!janet_checkint(key)) janet_panicf("expected integer key, got %v", key);
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index == INT32_MAX) janet_panicf("bad integer key, got %v", key);
|
|
|
|
if (index >= array->count) {
|
|
|
|
janet_array_setcount(array, index + 1);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
array->data[index] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
int32_t index;
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
|
|
|
if (!janet_checkint(key)) janet_panicf("expected integer key, got %v", key);
|
|
|
|
index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0 || index == INT32_MAX) janet_panicf("bad integer key, got %v", key);
|
|
|
|
if (!janet_checkint(value))
|
|
|
|
janet_panicf("can only put integers in buffers, got %v", value);
|
|
|
|
if (index >= buffer->count) {
|
|
|
|
janet_buffer_setcount(buffer, index + 1);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
buffer->data[index] = (uint8_t)(janet_unwrap_integer(value) & 0xFF);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TABLE:
|
|
|
|
janet_table_put(janet_unwrap_table(ds), key, value);
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->put) {
|
|
|
|
(type->put)(janet_unwrap_abstract(ds), key, value);
|
|
|
|
} else {
|
|
|
|
janet_panicf("no setter for %T ", JANET_TFLAG_LENGTHABLE, ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|