1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-13 04:09:54 +00:00
janet/core/value.c

351 lines
11 KiB
C
Raw Normal View History

/*
* Copyright (c) 2017 Calvin Rose
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
2017-03-22 04:27:18 +00:00
#include <gst/gst.h>
#include <stdio.h>
2017-02-09 20:02:59 +00:00
/* Boolean truth definition */
int gst_truthy(GstValue v) {
return v.type != GST_NIL && !(v.type == GST_BOOLEAN && !v.data.boolean);
}
/* Temporary buffer size */
#define GST_BUFSIZE 36
static const uint8_t *number_to_string(Gst *vm, GstNumber x) {
uint8_t buf[GST_BUFSIZE];
/* TODO - not depend on stdio */
int count = snprintf((char *) buf, GST_BUFSIZE, "%.21g", x);
2017-04-15 20:05:59 +00:00
return gst_string_b(vm, buf, (uint32_t) count);
2017-02-09 20:02:59 +00:00
}
static const char *HEX_CHARACTERS = "0123456789abcdef";
2017-02-09 20:02:59 +00:00
#define HEX(i) (((uint8_t *) HEX_CHARACTERS)[(i)])
/* Returns a string description for a pointer. Max titlelen is GST_BUFSIZE
* - 5 - 2 * sizeof(void *). */
static const uint8_t *string_description(Gst *vm, const char *title, void *pointer) {
uint8_t buf[GST_BUFSIZE];
uint8_t *c = buf;
2017-02-09 20:02:59 +00:00
uint32_t i;
union {
uint8_t bytes[sizeof(void *)];
void *p;
} pbuf;
pbuf.p = pointer;
2017-02-09 20:02:59 +00:00
*c++ = '<';
for (i = 0; title[i]; ++i)
2017-02-09 20:02:59 +00:00
*c++ = ((uint8_t *)title) [i];
*c++ = ' ';
*c++ = '0';
*c++ = 'x';
for (i = sizeof(void *); i > 0; --i) {
uint8_t byte = pbuf.bytes[i - 1];
if (!byte) continue;
2017-02-09 20:02:59 +00:00
*c++ = HEX(byte >> 4);
*c++ = HEX(byte & 0xF);
}
*c++ = '>';
2017-04-15 20:05:59 +00:00
return gst_string_b(vm, buf, c - buf);
2017-02-09 20:02:59 +00:00
}
#undef GST_BUFSIZE
2017-02-09 20:02:59 +00:00
/* Returns a string pointer or NULL if could not allocate memory. */
const uint8_t *gst_to_string(Gst *vm, GstValue x) {
switch (x.type) {
2017-04-15 20:05:59 +00:00
case GST_NIL:
return gst_string_c(vm, "nil");
case GST_BOOLEAN:
if (x.data.boolean) {
return gst_string_c(vm, "true");
} else {
return gst_string_c(vm, "false");
}
case GST_NUMBER:
return number_to_string(vm, x.data.number);
case GST_ARRAY:
return string_description(vm, "array", x.data.pointer);
case GST_TUPLE:
return string_description(vm, "tuple", x.data.pointer);
case GST_STRUCT:
return string_description(vm, "struct", x.data.pointer);
case GST_OBJECT:
return string_description(vm, "object", x.data.pointer);
case GST_STRING:
return x.data.string;
case GST_BYTEBUFFER:
return string_description(vm, "buffer", x.data.pointer);
case GST_CFUNCTION:
return string_description(vm, "cfunction", x.data.pointer);
case GST_FUNCTION:
return string_description(vm, "function", x.data.pointer);
case GST_THREAD:
return string_description(vm, "thread", x.data.pointer);
case GST_USERDATA:
return string_description(vm, "userdata", x.data.pointer);
case GST_FUNCENV:
return string_description(vm, "funcenv", x.data.pointer);
case GST_FUNCDEF:
return string_description(vm, "funcdef", x.data.pointer);
2017-02-09 20:02:59 +00:00
}
return NULL;
}
/* Check if two values are equal. This is strict equality with no conversion. */
int gst_equals(GstValue x, GstValue y) {
int result = 0;
if (x.type != y.type) {
2017-02-09 20:02:59 +00:00
result = 0;
} else {
switch (x.type) {
2017-04-15 20:05:59 +00:00
case GST_NIL:
result = 1;
break;
case GST_BOOLEAN:
result = (x.data.boolean == y.data.boolean);
break;
case GST_NUMBER:
result = (x.data.number == y.data.number);
break;
default:
/* compare pointers */
result = (x.data.pointer == y.data.pointer);
break;
2017-02-09 20:02:59 +00:00
}
}
return result;
}
/* Computes a hash value for a function */
uint32_t gst_hash(GstValue x) {
uint32_t hash = 0;
switch (x.type) {
2017-04-15 20:05:59 +00:00
case GST_NIL:
hash = 0;
break;
case GST_BOOLEAN:
hash = x.data.boolean;
break;
case GST_NUMBER:
{
union {
uint32_t hash;
GstNumber number;
} u;
u.number = x.data.number;
hash = u.hash;
}
break;
/* String hashes */
case GST_STRING:
hash = gst_string_hash(x.data.string);
break;
case GST_TUPLE:
hash = gst_tuple_hash(x.data.tuple);
break;
case GST_STRUCT:
hash = gst_struct_hash(x.data.st);
break;
default:
/* Cast the pointer */
{
union {
void * pointer;
uint32_t hash;
} u;
u.pointer = x.data.pointer;
hash = u.hash;
}
break;
2017-02-09 20:02:59 +00:00
}
return hash;
}
/* Compares x to y. If they are equal retuns 0. If x is less, returns -1.
* If y is less, returns 1. All types are comparable
* and should have strict ordering. */
int gst_compare(GstValue x, GstValue y) {
if (x.type == y.type) {
switch (x.type) {
case GST_NIL:
2017-02-09 20:02:59 +00:00
return 0;
case GST_BOOLEAN:
if (x.data.boolean == y.data.boolean) {
2017-02-09 20:02:59 +00:00
return 0;
} else {
return x.data.boolean ? 1 : -1;
2017-02-09 20:02:59 +00:00
}
case GST_NUMBER:
2017-02-09 20:02:59 +00:00
/* TODO: define behavior for NaN and infinties. */
if (x.data.number == y.data.number) {
2017-02-09 20:02:59 +00:00
return 0;
} else {
return x.data.number > y.data.number ? 1 : -1;
2017-02-09 20:02:59 +00:00
}
case GST_STRING:
2017-03-22 04:27:18 +00:00
return gst_string_compare(x.data.string, y.data.string);
2017-03-07 20:29:40 +00:00
/* Lower indices are most significant */
case GST_TUPLE:
2017-03-10 05:26:28 +00:00
{
uint32_t i;
uint32_t xlen = gst_tuple_length(x.data.tuple);
uint32_t ylen = gst_tuple_length(y.data.tuple);
uint32_t count = xlen < ylen ? xlen : ylen;
for (i = 0; i < count; ++i) {
int comp = gst_compare(x.data.tuple[i], y.data.tuple[i]);
if (comp != 0) return comp;
}
if (xlen < ylen)
return -1;
else if (xlen > ylen)
return 1;
return 0;
}
2017-03-07 20:29:40 +00:00
break;
2017-02-19 16:19:39 +00:00
default:
if (x.data.string == y.data.string) {
2017-02-09 20:02:59 +00:00
return 0;
} else {
return x.data.string > y.data.string ? 1 : -1;
2017-02-09 20:02:59 +00:00
}
}
} else if (x.type < y.type) {
2017-02-09 20:02:59 +00:00
return -1;
}
return 1;
}
2017-02-12 20:53:52 +00:00
/* Convert a number into a byte. */
static uint8_t to_byte(GstNumber raw) {
2017-03-10 05:26:28 +00:00
if (raw > 255) return 255;
if (raw < 0) return 0;
return (uint8_t) raw;
}
/* Get a value out af an associated data structure.
* Returns possible c error message, and NULL for no error. The
* useful return value is written to out on success */
const char *gst_get(GstValue ds, GstValue key, GstValue *out) {
int32_t index;
GstValue ret;
2017-03-10 05:26:28 +00:00
switch (ds.type) {
case GST_ARRAY:
if (key.type != GST_NUMBER) return "expected numeric key";
index = gst_to_index(key.data.number, ds.data.array->count);
2017-03-10 05:26:28 +00:00
if (index == -1) return "invalid array access";
ret = ds.data.array->data[index];
break;
case GST_TUPLE:
if (key.type != GST_NUMBER) return "expected numeric key";
index = gst_to_index(key.data.number, gst_tuple_length(ds.data.tuple));
2017-03-10 05:26:28 +00:00
if (index < 0) return "invalid tuple access";
ret = ds.data.tuple[index];
break;
case GST_BYTEBUFFER:
if (key.type != GST_NUMBER) return "expected numeric key";
index = gst_to_index(key.data.number, ds.data.buffer->count);
2017-03-10 05:26:28 +00:00
if (index == -1) return "invalid buffer access";
ret.type = GST_NUMBER;
ret.data.number = ds.data.buffer->data[index];
break;
case GST_STRING:
if (key.type != GST_NUMBER) return "expected numeric key";
index = gst_to_index(key.data.number, gst_string_length(ds.data.string));
2017-03-10 05:26:28 +00:00
if (index == -1) return "invalid string access";
ret.type = GST_NUMBER;
ret.data.number = ds.data.string[index];
break;
2017-04-15 20:05:59 +00:00
case GST_STRUCT:
ret = gst_struct_get(ds.data.st, key);
break;
case GST_OBJECT:
2017-03-10 05:26:28 +00:00
ret = gst_object_get(ds.data.object, key);
break;
2017-02-12 20:53:52 +00:00
default:
return "cannot get";
2017-03-10 05:26:28 +00:00
}
*out = ret;
return NULL;
2017-02-12 20:53:52 +00:00
}
/* Set a value in an associative data structure. Returns possible
* error message, and NULL if no error. */
const char *gst_set(Gst *vm, GstValue ds, GstValue key, GstValue value) {
int32_t index;
2017-03-10 05:26:28 +00:00
switch (ds.type) {
case GST_ARRAY:
if (key.type != GST_NUMBER) return "expected numeric key";
index = gst_to_index(key.data.number, ds.data.array->count);
2017-03-10 05:26:28 +00:00
if (index == -1) return "invalid array access";
ds.data.array->data[index] = value;
break;
case GST_BYTEBUFFER:
if (key.type != GST_NUMBER) return "expected numeric key";
if (value.type != GST_NUMBER) return "expected numeric value";
index = gst_to_index(key.data.number, ds.data.buffer->count);
2017-03-10 05:26:28 +00:00
if (index == -1) return "invalid buffer access";
ds.data.buffer->data[index] = to_byte(value.data.number);
break;
case GST_OBJECT:
gst_object_put(vm, ds.data.object, key, value);
break;
default:
2017-03-10 05:26:28 +00:00
return "cannot set";
}
return NULL;
}
/* Get the length of an object. Returns errors for invalid types */
int gst_length(Gst *vm, GstValue x, GstValue *len) {
uint32_t length;
switch (x.type) {
default:
2017-04-15 20:05:59 +00:00
vm->ret = gst_string_cv(vm, "cannot get length");
return GST_RETURN_ERROR;
case GST_STRING:
length = gst_string_length(x.data.string);
break;
case GST_ARRAY:
length = x.data.array->count;
break;
case GST_BYTEBUFFER:
length = x.data.buffer->count;
break;
case GST_TUPLE:
length = gst_tuple_length(x.data.tuple);
break;
2017-04-15 20:05:59 +00:00
case GST_STRUCT:
length = gst_struct_length(x.data.st);
break;
case GST_OBJECT:
length = x.data.object->count;
break;
}
/* Normal numeric return */
len->type = GST_NUMBER;
len->data.number = (GstNumber) length;
return GST_RETURN_OK;
}