1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-21 12:43:15 +00:00
janet/src/core/wrap.c

170 lines
4.6 KiB
C
Raw Normal View History

/*
* Copyright (c) 2019 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.
*/
#ifndef JANET_AMALG
2018-09-06 02:18:42 +00:00
#include <janet/janet.h>
#endif
void *janet_memalloc_empty(int32_t count) {
int32_t i;
void *mem = malloc(count * sizeof(JanetKV));
JanetKV *mmem = (JanetKV *)mem;
for (i = 0; i < count; i++) {
JanetKV *kv = mmem + i;
kv->key = janet_wrap_nil();
kv->value = janet_wrap_nil();
}
return mem;
}
void janet_memempty(JanetKV *mem, int32_t count) {
int32_t i;
for (i = 0; i < count; i++) {
mem[i].key = janet_wrap_nil();
mem[i].value = janet_wrap_nil();
}
}
2018-11-18 21:43:43 +00:00
#ifdef JANET_NANBOX_64
2018-09-06 02:18:42 +00:00
void *janet_nanbox_to_pointer(Janet x) {
2018-11-18 21:43:43 +00:00
x.i64 &= JANET_NANBOX_PAYLOADBITS;
2018-11-18 19:17:50 +00:00
return x.pointer;
}
2018-09-06 02:18:42 +00:00
Janet janet_nanbox_from_pointer(void *p, uint64_t tagmask) {
Janet ret;
2018-11-18 19:26:52 +00:00
ret.pointer = p;
ret.u64 |= tagmask;
return ret;
}
2018-09-06 02:18:42 +00:00
Janet janet_nanbox_from_cpointer(const void *p, uint64_t tagmask) {
Janet ret;
2018-11-18 19:26:52 +00:00
ret.pointer = (void *)p;
2017-11-29 20:17:56 +00:00
ret.u64 |= tagmask;
return ret;
}
2018-09-06 02:18:42 +00:00
Janet janet_nanbox_from_double(double d) {
Janet ret;
ret.number = d;
return ret;
}
2018-09-06 02:18:42 +00:00
Janet janet_nanbox_from_bits(uint64_t bits) {
Janet ret;
ret.u64 = bits;
return ret;
}
2018-11-18 21:43:43 +00:00
#elif defined(JANET_NANBOX_32)
Janet janet_wrap_number(double x) {
2018-11-18 21:43:43 +00:00
Janet ret;
ret.number = x;
2018-11-18 21:43:43 +00:00
ret.tagged.type += JANET_DOUBLE_OFFSET;
return ret;
}
Janet janet_nanbox32_from_tagi(uint32_t tag, int32_t integer) {
Janet ret;
ret.tagged.type = tag;
ret.tagged.payload.integer = integer;
return ret;
}
Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer) {
Janet ret;
ret.tagged.type = tag;
ret.tagged.payload.pointer = pointer;
return ret;
}
double janet_unwrap_number(Janet x) {
2018-11-18 21:43:43 +00:00
x.tagged.type -= JANET_DOUBLE_OFFSET;
return x.number;
2018-11-18 21:43:43 +00:00
}
#else
/* Wrapper functions wrap a data type that is used from C into a
2018-09-06 02:18:42 +00:00
* janet value, which can then be used in janet internal functions. Use
2017-11-01 21:53:43 +00:00
* these functions sparingly, as these function will let the programmer
* leak memory, where as the stack based API ensures that all values can
* be collected by the garbage collector. */
2018-09-06 02:18:42 +00:00
Janet janet_wrap_nil() {
Janet y;
y.type = JANET_NIL;
2017-11-29 20:17:56 +00:00
y.as.u64 = 0;
return y;
}
2018-09-06 02:18:42 +00:00
Janet janet_wrap_true(void) {
Janet y;
y.type = JANET_TRUE;
2017-11-29 20:17:56 +00:00
y.as.u64 = 0;
return y;
}
2018-09-06 02:18:42 +00:00
Janet janet_wrap_false(void) {
Janet y;
y.type = JANET_FALSE;
2017-11-29 20:17:56 +00:00
y.as.u64 = 0;
return y;
}
2018-09-06 02:18:42 +00:00
Janet janet_wrap_boolean(int x) {
Janet y;
y.type = x ? JANET_TRUE : JANET_FALSE;
2017-11-29 20:17:56 +00:00
y.as.u64 = 0;
return y;
}
2018-09-06 02:18:42 +00:00
#define JANET_WRAP_DEFINE(NAME, TYPE, DTYPE, UM)\
Janet janet_wrap_##NAME(TYPE x) {\
Janet y;\
y.type = DTYPE;\
y.as.u64 = 0; /* zero other bits in case of 32 bit integer */ \
2017-11-01 21:53:43 +00:00
y.as.UM = x;\
return y;\
}
JANET_WRAP_DEFINE(number, double, JANET_NUMBER, number)
2018-09-06 02:18:42 +00:00
JANET_WRAP_DEFINE(string, const uint8_t *, JANET_STRING, cpointer)
JANET_WRAP_DEFINE(symbol, const uint8_t *, JANET_SYMBOL, cpointer)
2019-01-03 03:08:51 +00:00
JANET_WRAP_DEFINE(keyword, const uint8_t *, JANET_KEYWORD, cpointer)
2018-09-06 02:18:42 +00:00
JANET_WRAP_DEFINE(array, JanetArray *, JANET_ARRAY, pointer)
JANET_WRAP_DEFINE(tuple, const Janet *, JANET_TUPLE, cpointer)
JANET_WRAP_DEFINE(struct, const JanetKV *, JANET_STRUCT, cpointer)
JANET_WRAP_DEFINE(fiber, JanetFiber *, JANET_FIBER, pointer)
JANET_WRAP_DEFINE(buffer, JanetBuffer *, JANET_BUFFER, pointer)
JANET_WRAP_DEFINE(function, JanetFunction *, JANET_FUNCTION, pointer)
JANET_WRAP_DEFINE(cfunction, JanetCFunction, JANET_CFUNCTION, pointer)
JANET_WRAP_DEFINE(table, JanetTable *, JANET_TABLE, pointer)
JANET_WRAP_DEFINE(abstract, void *, JANET_ABSTRACT, pointer)
#undef JANET_WRAP_DEFINE
#endif