2017-09-09 18:39:51 +00:00
|
|
|
/*
|
|
|
|
* 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-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
#ifdef DST_NANBOX
|
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
void *dst_nanbox_to_pointer(DstValue x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
/* We need to do this shift to keep the higher bits of the pointer
|
|
|
|
* the same as bit 47 as required by the x86 architecture. We may save
|
|
|
|
* an instruction if we do x.u64 & DST_NANBOX_POINTERBITS, but this 0s
|
|
|
|
* the high bits, and may make the pointer non-canocial on x86. */
|
|
|
|
x.i64 = (x.i64 << 16) >> 16;
|
|
|
|
return x.pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
DstValue dst_nanbox_from_pointer(void *p, uint64_t tagmask) {
|
|
|
|
DstValue ret;
|
|
|
|
ret.pointer = p;
|
|
|
|
ret.u64 &= DST_NANBOX_POINTERBITS;
|
|
|
|
ret.u64 |= tagmask;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
DstValue dst_nanbox_from_cpointer(const void *p, uint64_t tagmask) {
|
|
|
|
DstValue ret;
|
|
|
|
ret.cpointer = p;
|
|
|
|
ret.u64 &= DST_NANBOX_POINTERBITS;
|
|
|
|
ret.u64 |= tagmask;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
DstValue dst_nanbox_from_double(double d) {
|
|
|
|
DstValue ret;
|
|
|
|
ret.real = d;
|
|
|
|
/* Normalize NaNs */
|
|
|
|
if (d != d)
|
|
|
|
ret.u64 = dst_nanbox_tag(DST_REAL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DstValue dst_nanbox_from_bits(uint64_t bits) {
|
2017-11-29 20:17:56 +00:00
|
|
|
DstValue ret;
|
2017-11-28 23:27:55 +00:00
|
|
|
ret.u64 = bits;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
void *dst_nanbox_memalloc_empty(int32_t count) {
|
|
|
|
int32_t i;
|
|
|
|
void *mem = malloc(count * sizeof(DstValue));
|
|
|
|
DstValue *mmem = (DstValue *)mem;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
mmem[i] = dst_wrap_nil();
|
|
|
|
}
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dst_nanbox_memempty(DstValue *mem, int32_t count) {
|
|
|
|
int32_t i;
|
2017-11-28 23:27:55 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
mem[i] = dst_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Wrapper functions wrap a data type that is used from C into a
|
2017-11-01 21:53:43 +00:00
|
|
|
* dst value, which can then be used in dst internal functions. Use
|
|
|
|
* 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. */
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
DstValue dst_wrap_nil() {
|
2017-11-01 21:53:43 +00:00
|
|
|
DstValue y;
|
|
|
|
y.type = DST_NIL;
|
2017-11-29 20:17:56 +00:00
|
|
|
y.as.u64 = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
DstValue dst_wrap_true() {
|
|
|
|
DstValue y;
|
|
|
|
y.type = DST_TRUE;
|
2017-11-29 20:17:56 +00:00
|
|
|
y.as.u64 = 0;
|
2017-11-28 23:27:55 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
DstValue dst_wrap_false() {
|
|
|
|
DstValue y;
|
2017-11-29 20:17:56 +00:00
|
|
|
y.type = DST_FALSE;
|
|
|
|
y.as.u64 = 0;
|
2017-11-28 23:27:55 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
DstValue dst_wrap_boolean(int x) {
|
|
|
|
DstValue y;
|
|
|
|
y.type = x ? DST_TRUE : DST_FALSE;
|
2017-11-29 20:17:56 +00:00
|
|
|
y.as.u64 = 0;
|
2017-11-28 23:27:55 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#define DST_WRAP_DEFINE(NAME, TYPE, DTYPE, UM)\
|
|
|
|
DstValue dst_wrap_##NAME(TYPE x) {\
|
|
|
|
DstValue y;\
|
|
|
|
y.type = DTYPE;\
|
2017-11-29 20:33:27 +00:00
|
|
|
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;\
|
2017-09-09 18:39:51 +00:00
|
|
|
return y;\
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
DST_WRAP_DEFINE(real, double, DST_REAL, real)
|
2017-11-28 23:27:55 +00:00
|
|
|
DST_WRAP_DEFINE(integer, int32_t, DST_INTEGER, integer)
|
|
|
|
DST_WRAP_DEFINE(string, const uint8_t *, DST_STRING, cpointer)
|
|
|
|
DST_WRAP_DEFINE(symbol, const uint8_t *, DST_SYMBOL, cpointer)
|
|
|
|
DST_WRAP_DEFINE(array, DstArray *, DST_ARRAY, pointer)
|
|
|
|
DST_WRAP_DEFINE(tuple, const DstValue *, DST_TUPLE, cpointer)
|
|
|
|
DST_WRAP_DEFINE(struct, const DstValue *, DST_STRUCT, cpointer)
|
|
|
|
DST_WRAP_DEFINE(thread, DstFiber *, DST_FIBER, pointer)
|
|
|
|
DST_WRAP_DEFINE(buffer, DstBuffer *, DST_BUFFER, pointer)
|
|
|
|
DST_WRAP_DEFINE(function, DstFunction *, DST_FUNCTION, pointer)
|
|
|
|
DST_WRAP_DEFINE(cfunction, DstCFunction, DST_CFUNCTION, pointer)
|
|
|
|
DST_WRAP_DEFINE(table, DstTable *, DST_TABLE, pointer)
|
2017-11-01 21:53:43 +00:00
|
|
|
DST_WRAP_DEFINE(userdata, void *, DST_USERDATA, pointer)
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
#undef DST_WRAP_DEFINE
|
2017-11-28 23:27:55 +00:00
|
|
|
|
|
|
|
#endif
|