2018-06-12 18:24:45 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2018-06-12 18:24:45 +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:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-08-20 00:21:27 +00:00
|
|
|
#include "state.h"
|
|
|
|
#include "vector.h"
|
|
|
|
#include "gc.h"
|
2018-11-26 23:01:50 +00:00
|
|
|
#include "fiber.h"
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-08-20 00:21:27 +00:00
|
|
|
|
2018-06-12 18:24:45 +00:00
|
|
|
typedef struct {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buf;
|
|
|
|
JanetTable seen;
|
2018-10-21 05:35:07 +00:00
|
|
|
JanetTable *rreg;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv **seen_envs;
|
|
|
|
JanetFuncDef **seen_defs;
|
2018-06-12 18:24:45 +00:00
|
|
|
int32_t nextid;
|
|
|
|
} MarshalState;
|
|
|
|
|
|
|
|
/* Lead bytes in marshaling protocol */
|
|
|
|
enum {
|
2018-12-30 17:37:50 +00:00
|
|
|
LB_REAL = 200,
|
2020-04-05 13:01:18 +00:00
|
|
|
LB_NIL, /* 201 */
|
|
|
|
LB_FALSE, /* 202 */
|
|
|
|
LB_TRUE, /* 203 */
|
|
|
|
LB_FIBER, /* 204 */
|
|
|
|
LB_INTEGER, /* 205 */
|
|
|
|
LB_STRING, /* 206 */
|
|
|
|
LB_SYMBOL, /* 207 */
|
|
|
|
LB_KEYWORD, /* 208 */
|
|
|
|
LB_ARRAY, /* 209 */
|
|
|
|
LB_TUPLE, /* 210 */
|
|
|
|
LB_TABLE, /* 211 */
|
|
|
|
LB_TABLE_PROTO, /* 212 */
|
|
|
|
LB_STRUCT, /* 213 */
|
|
|
|
LB_BUFFER, /* 214 */
|
|
|
|
LB_FUNCTION, /* 215 */
|
|
|
|
LB_REGISTRY, /* 216 */
|
|
|
|
LB_ABSTRACT, /* 217 */
|
|
|
|
LB_REFERENCE, /* 218 */
|
|
|
|
LB_FUNCENV_REF, /* 219 */
|
2020-04-19 15:55:00 +00:00
|
|
|
LB_FUNCDEF_REF, /* 220 */
|
|
|
|
LB_UNSAFE_CFUNCTION, /* 221 */
|
|
|
|
LB_UNSAFE_POINTER /* 222 */
|
2018-06-12 18:24:45 +00:00
|
|
|
} LeadBytes;
|
|
|
|
|
2018-10-21 05:35:07 +00:00
|
|
|
/* Helper to look inside an entry in an environment */
|
|
|
|
static Janet entry_getval(Janet env_entry) {
|
|
|
|
if (janet_checktype(env_entry, JANET_TABLE)) {
|
|
|
|
JanetTable *entry = janet_unwrap_table(env_entry);
|
2019-01-03 00:41:07 +00:00
|
|
|
Janet checkval = janet_table_get(entry, janet_ckeywordv("value"));
|
2018-10-21 05:35:07 +00:00
|
|
|
if (janet_checktype(checkval, JANET_NIL)) {
|
2019-01-03 00:41:07 +00:00
|
|
|
checkval = janet_table_get(entry, janet_ckeywordv("ref"));
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
|
|
|
return checkval;
|
|
|
|
} else if (janet_checktype(env_entry, JANET_STRUCT)) {
|
|
|
|
const JanetKV *entry = janet_unwrap_struct(env_entry);
|
2019-01-03 00:41:07 +00:00
|
|
|
Janet checkval = janet_struct_get(entry, janet_ckeywordv("value"));
|
2018-10-21 05:35:07 +00:00
|
|
|
if (janet_checktype(checkval, JANET_NIL)) {
|
2019-01-03 00:41:07 +00:00
|
|
|
checkval = janet_struct_get(entry, janet_ckeywordv("ref"));
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
|
|
|
return checkval;
|
|
|
|
} else {
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 18:25:14 +00:00
|
|
|
/* Merge values from an environment into an existing lookup table. */
|
|
|
|
void janet_env_lookup_into(JanetTable *renv, JanetTable *env, const char *prefix, int recurse) {
|
2018-10-21 05:35:07 +00:00
|
|
|
while (env) {
|
|
|
|
for (int32_t i = 0; i < env->capacity; i++) {
|
|
|
|
if (janet_checktype(env->data[i].key, JANET_SYMBOL)) {
|
2019-07-28 18:25:14 +00:00
|
|
|
if (prefix) {
|
2019-07-28 23:44:00 +00:00
|
|
|
int32_t prelen = (int32_t) strlen(prefix);
|
2019-07-28 18:25:14 +00:00
|
|
|
const uint8_t *oldsym = janet_unwrap_symbol(env->data[i].key);
|
|
|
|
int32_t oldlen = janet_string_length(oldsym);
|
|
|
|
uint8_t *symbuf = janet_smalloc(prelen + oldlen);
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(symbuf, prefix, prelen);
|
|
|
|
safe_memcpy(symbuf + prelen, oldsym, oldlen);
|
2019-07-28 18:25:14 +00:00
|
|
|
Janet s = janet_symbolv(symbuf, prelen + oldlen);
|
|
|
|
janet_sfree(symbuf);
|
|
|
|
janet_table_put(renv, s, entry_getval(env->data[i].value));
|
|
|
|
} else {
|
|
|
|
janet_table_put(renv,
|
|
|
|
env->data[i].key,
|
|
|
|
entry_getval(env->data[i].value));
|
|
|
|
}
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-28 18:25:14 +00:00
|
|
|
env = recurse ? env->proto : NULL;
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
2019-07-28 18:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a forward lookup table from an environment (for unmarshaling) */
|
|
|
|
JanetTable *janet_env_lookup(JanetTable *env) {
|
|
|
|
JanetTable *renv = janet_table(env->count);
|
|
|
|
janet_env_lookup_into(renv, env, NULL, 1);
|
2018-10-21 05:35:07 +00:00
|
|
|
return renv;
|
|
|
|
}
|
|
|
|
|
2018-06-12 18:24:45 +00:00
|
|
|
/* Marshal an integer onto the buffer */
|
2018-08-20 00:21:27 +00:00
|
|
|
static void pushint(MarshalState *st, int32_t x) {
|
2019-02-08 15:12:25 +00:00
|
|
|
if (x >= 0 && x < 128) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_u8(st->buf, x);
|
2019-02-08 15:12:25 +00:00
|
|
|
} else if (x <= 8191 && x >= -8192) {
|
|
|
|
uint8_t intbuf[2];
|
|
|
|
intbuf[0] = ((x >> 8) & 0x3F) | 0x80;
|
|
|
|
intbuf[1] = x & 0xFF;
|
|
|
|
janet_buffer_push_bytes(st->buf, intbuf, 2);
|
2018-06-12 18:24:45 +00:00
|
|
|
} else {
|
|
|
|
uint8_t intbuf[5];
|
|
|
|
intbuf[0] = LB_INTEGER;
|
2019-02-08 15:12:25 +00:00
|
|
|
intbuf[1] = (x >> 24) & 0xFF;
|
|
|
|
intbuf[2] = (x >> 16) & 0xFF;
|
|
|
|
intbuf[3] = (x >> 8) & 0xFF;
|
|
|
|
intbuf[4] = x & 0xFF;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_bytes(st->buf, intbuf, 5);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
static void pushbyte(MarshalState *st, uint8_t b) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_u8(st->buf, b);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
static void pushbytes(MarshalState *st, const uint8_t *bytes, int32_t len) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_buffer_push_bytes(st->buf, bytes, len);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2019-03-08 03:06:49 +00:00
|
|
|
/* Marshal a size_t onto the buffer */
|
2019-03-19 01:12:38 +00:00
|
|
|
static void push64(MarshalState *st, uint64_t x) {
|
2019-03-08 03:06:49 +00:00
|
|
|
if (x <= 0xF0) {
|
|
|
|
/* Single byte */
|
|
|
|
pushbyte(st, (uint8_t) x);
|
|
|
|
} else {
|
2019-03-08 05:41:26 +00:00
|
|
|
/* Multibyte, little endian */
|
2019-03-08 03:06:49 +00:00
|
|
|
uint8_t bytes[9];
|
|
|
|
int nbytes = 0;
|
|
|
|
while (x) {
|
|
|
|
bytes[++nbytes] = x & 0xFF;
|
|
|
|
x >>= 8;
|
|
|
|
}
|
|
|
|
bytes[0] = 0xF0 + nbytes;
|
|
|
|
pushbytes(st, bytes, nbytes + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Forward declaration to enable mutual recursion. */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void marshal_one(MarshalState *st, Janet x, int flags);
|
|
|
|
static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags);
|
|
|
|
static void marshal_one_def(MarshalState *st, JanetFuncDef *def, int flags);
|
|
|
|
static void marshal_one_env(MarshalState *st, JanetFuncEnv *env, int flags);
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
/* Prevent stack overflows */
|
|
|
|
#define MARSH_STACKCHECK if ((flags & 0xFFFF) > JANET_RECURSION_GUARD) janet_panic("stack overflow")
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Marshal a function env */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void marshal_one_env(MarshalState *st, JanetFuncEnv *env, int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_STACKCHECK;
|
2018-09-06 02:18:42 +00:00
|
|
|
for (int32_t i = 0; i < janet_v_count(st->seen_envs); i++) {
|
2018-08-20 00:21:27 +00:00
|
|
|
if (st->seen_envs[i] == env) {
|
|
|
|
pushbyte(st, LB_FUNCENV_REF);
|
|
|
|
pushint(st, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 15:58:47 +00:00
|
|
|
janet_env_valid(env);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_push(st->seen_envs, env);
|
2020-04-06 15:58:47 +00:00
|
|
|
if (env->offset > 0 && (JANET_STATUS_ALIVE == janet_fiber_status(env->as.fiber))) {
|
2020-03-18 01:53:11 +00:00
|
|
|
pushint(st, 0);
|
|
|
|
pushint(st, env->length);
|
|
|
|
Janet *values = env->as.fiber->data + env->offset;
|
2020-03-18 14:30:10 +00:00
|
|
|
uint32_t *bitset = janet_stack_frame(values)->func->def->closure_bitset;
|
|
|
|
for (int32_t i = 0; i < env->length; i++) {
|
|
|
|
if (1 & (bitset[i >> 5] >> (i & 0x1F))) {
|
|
|
|
marshal_one(st, values[i], flags + 1);
|
|
|
|
} else {
|
|
|
|
pushbyte(st, LB_NIL);
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 01:53:11 +00:00
|
|
|
} else {
|
|
|
|
janet_env_maybe_detach(env);
|
|
|
|
pushint(st, env->offset);
|
|
|
|
pushint(st, env->length);
|
2020-04-06 15:58:47 +00:00
|
|
|
if (env->offset > 0) {
|
2020-03-18 01:53:11 +00:00
|
|
|
/* On stack variant */
|
|
|
|
marshal_one(st, janet_wrap_fiber(env->as.fiber), flags + 1);
|
|
|
|
} else {
|
|
|
|
/* Off stack variant */
|
|
|
|
for (int32_t i = 0; i < env->length; i++)
|
|
|
|
marshal_one(st, env->as.values[i], flags + 1);
|
|
|
|
}
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2020-03-18 14:30:10 +00:00
|
|
|
/* Marshal a sequence of u32s */
|
|
|
|
static void janet_marshal_u32s(MarshalState *st, const uint32_t *u32s, int32_t n) {
|
|
|
|
for (int32_t i = 0; i < n; i++) {
|
|
|
|
pushbyte(st, u32s[i] & 0xFF);
|
|
|
|
pushbyte(st, (u32s[i] >> 8) & 0xFF);
|
|
|
|
pushbyte(st, (u32s[i] >> 16) & 0xFF);
|
|
|
|
pushbyte(st, (u32s[i] >> 24) & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Marshal a function def */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void marshal_one_def(MarshalState *st, JanetFuncDef *def, int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_STACKCHECK;
|
2018-09-06 02:18:42 +00:00
|
|
|
for (int32_t i = 0; i < janet_v_count(st->seen_defs); i++) {
|
2018-08-20 00:21:27 +00:00
|
|
|
if (st->seen_defs[i] == def) {
|
|
|
|
pushbyte(st, LB_FUNCDEF_REF);
|
|
|
|
pushint(st, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 18:16:55 +00:00
|
|
|
/* Add to lookup */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_push(st->seen_defs, def);
|
2018-08-20 00:21:27 +00:00
|
|
|
pushint(st, def->flags);
|
|
|
|
pushint(st, def->slotcount);
|
|
|
|
pushint(st, def->arity);
|
2019-03-12 04:23:14 +00:00
|
|
|
pushint(st, def->min_arity);
|
|
|
|
pushint(st, def->max_arity);
|
2018-08-20 00:21:27 +00:00
|
|
|
pushint(st, def->constants_length);
|
|
|
|
pushint(st, def->bytecode_length);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASENVS)
|
2018-08-20 00:21:27 +00:00
|
|
|
pushint(st, def->environments_length);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASDEFS)
|
2018-08-20 00:21:27 +00:00
|
|
|
pushint(st, def->defs_length);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASNAME)
|
|
|
|
marshal_one(st, janet_wrap_string(def->name), flags);
|
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCE)
|
|
|
|
marshal_one(st, janet_wrap_string(def->source), flags);
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* marshal constants */
|
|
|
|
for (int32_t i = 0; i < def->constants_length; i++)
|
|
|
|
marshal_one(st, def->constants[i], flags);
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* marshal the bytecode */
|
2020-03-18 14:30:10 +00:00
|
|
|
janet_marshal_u32s(st, def->bytecode, def->bytecode_length);
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* marshal the environments if needed */
|
|
|
|
for (int32_t i = 0; i < def->environments_length; i++)
|
2019-02-09 17:21:11 +00:00
|
|
|
pushint(st, def->environments[i]);
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* marshal the sub funcdefs if needed */
|
|
|
|
for (int32_t i = 0; i < def->defs_length; i++)
|
|
|
|
marshal_one_def(st, def->defs[i], flags);
|
|
|
|
|
|
|
|
/* marshal source maps if needed */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
2019-02-08 05:44:30 +00:00
|
|
|
int32_t current = 0;
|
2018-08-20 00:21:27 +00:00
|
|
|
for (int32_t i = 0; i < def->bytecode_length; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSourceMapping map = def->sourcemap[i];
|
2019-09-22 22:18:28 +00:00
|
|
|
pushint(st, map.line - current);
|
|
|
|
pushint(st, map.column);
|
|
|
|
current = map.line;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 14:30:10 +00:00
|
|
|
|
|
|
|
/* Marshal closure bitset, if needed */
|
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASCLOBITSET) {
|
|
|
|
janet_marshal_u32s(st, def->closure_bitset, ((def->slotcount + 31) >> 5));
|
|
|
|
}
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FIBER_FLAG_HASCHILD (1 << 29)
|
2020-07-20 00:41:12 +00:00
|
|
|
#define JANET_FIBER_FLAG_HASENV (1 << 30)
|
2020-12-06 20:33:08 +00:00
|
|
|
#define JANET_STACKFRAME_HASENV (INT32_MIN)
|
2018-08-23 01:41:25 +00:00
|
|
|
|
|
|
|
/* Marshal a fiber */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_STACKCHECK;
|
2018-11-26 14:02:07 +00:00
|
|
|
int32_t fflags = fiber->flags;
|
|
|
|
if (fiber->child) fflags |= JANET_FIBER_FLAG_HASCHILD;
|
2019-04-16 19:41:45 +00:00
|
|
|
if (fiber->env) fflags |= JANET_FIBER_FLAG_HASENV;
|
2018-11-26 14:02:07 +00:00
|
|
|
if (janet_fiber_status(fiber) == JANET_STATUS_ALIVE)
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panic("cannot marshal alive fiber");
|
2018-11-26 14:02:07 +00:00
|
|
|
pushint(st, fflags);
|
2018-08-23 01:41:25 +00:00
|
|
|
pushint(st, fiber->frame);
|
|
|
|
pushint(st, fiber->stackstart);
|
|
|
|
pushint(st, fiber->stacktop);
|
|
|
|
pushint(st, fiber->maxstack);
|
|
|
|
/* Do frames */
|
|
|
|
int32_t i = fiber->frame;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t j = fiber->stackstart - JANET_FRAME_SIZE;
|
2018-08-23 01:41:25 +00:00
|
|
|
while (i > 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetStackFrame *frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
|
|
|
if (frame->env) frame->flags |= JANET_STACKFRAME_HASENV;
|
2019-02-22 15:10:41 +00:00
|
|
|
if (!frame->func) janet_panic("cannot marshal fiber with c stackframe");
|
2018-08-23 01:41:25 +00:00
|
|
|
pushint(st, frame->flags);
|
|
|
|
pushint(st, frame->prevframe);
|
2018-12-08 22:40:05 +00:00
|
|
|
int32_t pcdiff = (int32_t)(frame->pc - frame->func->def->bytecode);
|
2018-08-23 01:41:25 +00:00
|
|
|
pushint(st, pcdiff);
|
2018-09-06 02:18:42 +00:00
|
|
|
marshal_one(st, janet_wrap_function(frame->func), flags + 1);
|
2018-08-23 01:41:25 +00:00
|
|
|
if (frame->env) marshal_one_env(st, frame->env, flags + 1);
|
|
|
|
/* Marshal all values in the stack frame */
|
|
|
|
for (int32_t k = i; k < j; k++)
|
|
|
|
marshal_one(st, fiber->data[k], flags + 1);
|
2018-09-06 02:18:42 +00:00
|
|
|
j = i - JANET_FRAME_SIZE;
|
2018-08-23 01:41:25 +00:00
|
|
|
i = frame->prevframe;
|
|
|
|
}
|
2019-04-16 19:41:45 +00:00
|
|
|
if (fiber->env) {
|
|
|
|
marshal_one(st, janet_wrap_table(fiber->env), flags + 1);
|
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
if (fiber->child)
|
2018-11-26 14:02:07 +00:00
|
|
|
marshal_one(st, janet_wrap_fiber(fiber->child), flags + 1);
|
2018-08-23 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
void janet_marshal_size(JanetMarshalContext *ctx, size_t value) {
|
|
|
|
janet_marshal_int64(ctx, (int64_t) value);
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
void janet_marshal_int64(JanetMarshalContext *ctx, int64_t value) {
|
2019-03-08 03:06:49 +00:00
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
2019-03-19 01:12:38 +00:00
|
|
|
push64(st, (uint64_t) value);
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
2019-03-08 03:06:49 +00:00
|
|
|
|
2019-02-23 16:13:43 +00:00
|
|
|
void janet_marshal_int(JanetMarshalContext *ctx, int32_t value) {
|
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
|
|
|
pushint(st, value);
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-02-23 16:13:43 +00:00
|
|
|
void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value) {
|
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
|
|
|
pushbyte(st, value);
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-03-08 03:19:34 +00:00
|
|
|
void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, size_t len) {
|
2019-02-23 16:13:43 +00:00
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
2019-03-08 03:19:34 +00:00
|
|
|
if (len > INT32_MAX) janet_panic("size_t too large to fit in buffer");
|
|
|
|
pushbytes(st, bytes, (int32_t) len);
|
2019-02-21 19:52:39 +00:00
|
|
|
}
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-02-23 16:13:43 +00:00
|
|
|
void janet_marshal_janet(JanetMarshalContext *ctx, Janet x) {
|
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
|
|
|
marshal_one(st, x, ctx->flags + 1);
|
2019-02-21 19:52:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 04:12:18 +00:00
|
|
|
void janet_marshal_abstract(JanetMarshalContext *ctx, void *abstract) {
|
|
|
|
MarshalState *st = (MarshalState *)(ctx->m_state);
|
|
|
|
janet_table_put(&st->seen,
|
|
|
|
janet_wrap_abstract(abstract),
|
|
|
|
janet_wrap_integer(st->nextid++));
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:57:48 +00:00
|
|
|
#define MARK_SEEN() \
|
|
|
|
janet_table_put(&st->seen, x, janet_wrap_integer(st->nextid++))
|
|
|
|
|
2019-02-23 16:54:09 +00:00
|
|
|
static void marshal_one_abstract(MarshalState *st, Janet x, int flags) {
|
2019-02-24 01:02:54 +00:00
|
|
|
void *abstract = janet_unwrap_abstract(x);
|
|
|
|
const JanetAbstractType *at = janet_abstract_type(abstract);
|
|
|
|
if (at->marshal) {
|
2019-02-23 16:13:43 +00:00
|
|
|
pushbyte(st, LB_ABSTRACT);
|
2019-03-08 05:41:26 +00:00
|
|
|
marshal_one(st, janet_csymbolv(at->name), flags + 1);
|
2019-12-07 04:12:18 +00:00
|
|
|
JanetMarshalContext context = {st, NULL, flags, NULL, at};
|
2019-02-24 01:02:54 +00:00
|
|
|
at->marshal(abstract, &context);
|
2019-02-23 16:54:09 +00:00
|
|
|
} else {
|
2019-02-24 01:02:54 +00:00
|
|
|
janet_panicf("try to marshal unregistered abstract type, cannot marshal %p", x);
|
2019-02-23 16:13:43 +00:00
|
|
|
}
|
2019-02-21 19:52:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 18:24:45 +00:00
|
|
|
/* The main body of the marshaling function. Is the main
|
|
|
|
* entry point for the mutually recursive functions. */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void marshal_one(MarshalState *st, Janet x, int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_STACKCHECK;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetType type = janet_type(x);
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2018-10-22 05:28:39 +00:00
|
|
|
/* Check simple primitives (non reference types, no benefit from memoization) */
|
2018-06-12 18:24:45 +00:00
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2019-03-13 18:50:25 +00:00
|
|
|
pushbyte(st, LB_NIL);
|
|
|
|
return;
|
|
|
|
case JANET_BOOLEAN:
|
|
|
|
pushbyte(st, janet_unwrap_boolean(x) ? LB_TRUE : LB_FALSE);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_NUMBER: {
|
|
|
|
double xval = janet_unwrap_number(x);
|
|
|
|
if (janet_checkintrange(xval)) {
|
|
|
|
pushint(st, (int32_t) xval);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 05:35:07 +00:00
|
|
|
/* Check reference and registry value */
|
2018-06-12 18:24:45 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet check = janet_table_get(&st->seen, x);
|
2018-12-27 18:05:29 +00:00
|
|
|
if (janet_checkint(check)) {
|
2018-08-20 00:21:27 +00:00
|
|
|
pushbyte(st, LB_REFERENCE);
|
2018-09-06 02:18:42 +00:00
|
|
|
pushint(st, janet_unwrap_integer(check));
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
2018-10-21 05:35:07 +00:00
|
|
|
if (st->rreg) {
|
|
|
|
check = janet_table_get(st->rreg, x);
|
|
|
|
if (janet_checktype(check, JANET_SYMBOL)) {
|
|
|
|
MARK_SEEN();
|
|
|
|
const uint8_t *regname = janet_unwrap_symbol(check);
|
|
|
|
pushbyte(st, LB_REGISTRY);
|
|
|
|
pushint(st, janet_string_length(regname));
|
|
|
|
pushbytes(st, regname, janet_string_length(regname));
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reference types */
|
|
|
|
switch (type) {
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_NUMBER: {
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
uint8_t bytes[8];
|
|
|
|
} u;
|
|
|
|
u.d = janet_unwrap_number(x);
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_BIG_ENDIAN
|
2019-02-20 01:51:34 +00:00
|
|
|
/* Swap byte order */
|
|
|
|
uint8_t temp;
|
|
|
|
temp = u.bytes[7];
|
|
|
|
u.bytes[7] = u.bytes[0];
|
|
|
|
u.bytes[0] = temp;
|
|
|
|
temp = u.bytes[6];
|
|
|
|
u.bytes[6] = u.bytes[1];
|
|
|
|
u.bytes[1] = temp;
|
|
|
|
temp = u.bytes[5];
|
|
|
|
u.bytes[5] = u.bytes[2];
|
|
|
|
u.bytes[2] = temp;
|
|
|
|
temp = u.bytes[4];
|
|
|
|
u.bytes[4] = u.bytes[3];
|
|
|
|
u.bytes[3] = temp;
|
2018-06-12 18:24:45 +00:00
|
|
|
#endif
|
2019-02-20 01:51:34 +00:00
|
|
|
pushbyte(st, LB_REAL);
|
|
|
|
pushbytes(st, u.bytes, 8);
|
|
|
|
MARK_SEEN();
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +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(x);
|
|
|
|
int32_t length = janet_string_length(str);
|
|
|
|
/* Record reference */
|
|
|
|
MARK_SEEN();
|
|
|
|
uint8_t lb = (type == JANET_STRING) ? LB_STRING :
|
|
|
|
(type == JANET_SYMBOL) ? LB_SYMBOL :
|
|
|
|
LB_KEYWORD;
|
|
|
|
pushbyte(st, lb);
|
|
|
|
pushint(st, length);
|
|
|
|
pushbytes(st, str, length);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(x);
|
|
|
|
/* Record reference */
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, LB_BUFFER);
|
|
|
|
pushint(st, buffer->count);
|
|
|
|
pushbytes(st, buffer->data, buffer->count);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_ARRAY: {
|
|
|
|
int32_t i;
|
|
|
|
JanetArray *a = janet_unwrap_array(x);
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, LB_ARRAY);
|
|
|
|
pushint(st, a->count);
|
|
|
|
for (i = 0; i < a->count; i++)
|
|
|
|
marshal_one(st, a->data[i], flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
int32_t i, count, flag;
|
|
|
|
const Janet *tup = janet_unwrap_tuple(x);
|
|
|
|
count = janet_tuple_length(tup);
|
2019-02-21 16:22:29 +00:00
|
|
|
flag = janet_tuple_flag(tup) >> 16;
|
2019-02-20 01:51:34 +00:00
|
|
|
pushbyte(st, LB_TUPLE);
|
|
|
|
pushint(st, count);
|
|
|
|
pushint(st, flag);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
marshal_one(st, tup[i], flags + 1);
|
|
|
|
/* Mark as seen AFTER marshaling */
|
|
|
|
MARK_SEEN();
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_TABLE: {
|
|
|
|
JanetTable *t = janet_unwrap_table(x);
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, t->proto ? LB_TABLE_PROTO : LB_TABLE);
|
|
|
|
pushint(st, t->count);
|
|
|
|
if (t->proto)
|
|
|
|
marshal_one(st, janet_wrap_table(t->proto), flags + 1);
|
|
|
|
for (int32_t i = 0; i < t->capacity; i++) {
|
|
|
|
if (janet_checktype(t->data[i].key, JANET_NIL))
|
|
|
|
continue;
|
|
|
|
marshal_one(st, t->data[i].key, flags + 1);
|
|
|
|
marshal_one(st, t->data[i].value, flags + 1);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_STRUCT: {
|
|
|
|
int32_t count;
|
|
|
|
const JanetKV *struct_ = janet_unwrap_struct(x);
|
|
|
|
count = janet_struct_length(struct_);
|
|
|
|
pushbyte(st, LB_STRUCT);
|
|
|
|
pushint(st, count);
|
|
|
|
for (int32_t i = 0; i < janet_struct_capacity(struct_); i++) {
|
|
|
|
if (janet_checktype(struct_[i].key, JANET_NIL))
|
|
|
|
continue;
|
|
|
|
marshal_one(st, struct_[i].key, flags + 1);
|
|
|
|
marshal_one(st, struct_[i].value, flags + 1);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
/* Mark as seen AFTER marshaling */
|
|
|
|
MARK_SEEN();
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
2019-02-21 19:52:39 +00:00
|
|
|
case JANET_ABSTRACT: {
|
2019-02-24 01:02:54 +00:00
|
|
|
marshal_one_abstract(st, x, flags);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_FUNCTION: {
|
|
|
|
pushbyte(st, LB_FUNCTION);
|
|
|
|
JanetFunction *func = janet_unwrap_function(x);
|
2020-12-06 22:32:23 +00:00
|
|
|
/* Mark seen before reading def */
|
2019-02-20 01:51:34 +00:00
|
|
|
MARK_SEEN();
|
2020-12-06 22:32:23 +00:00
|
|
|
pushint(st, func->def->environments_length);
|
|
|
|
marshal_one_def(st, func->def, flags);
|
2019-02-20 01:51:34 +00:00
|
|
|
for (int32_t i = 0; i < func->def->environments_length; i++)
|
|
|
|
marshal_one_env(st, func->envs[i], flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
case JANET_FIBER: {
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, LB_FIBER);
|
|
|
|
marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-19 15:55:00 +00:00
|
|
|
case JANET_CFUNCTION: {
|
|
|
|
if (!(flags & JANET_MARSHAL_UNSAFE)) goto no_registry;
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, LB_UNSAFE_CFUNCTION);
|
|
|
|
JanetCFunction cfn = janet_unwrap_cfunction(x);
|
|
|
|
pushbytes(st, (uint8_t *) &cfn, sizeof(JanetCFunction));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case JANET_POINTER: {
|
|
|
|
if (!(flags & JANET_MARSHAL_UNSAFE)) goto no_registry;
|
|
|
|
MARK_SEEN();
|
|
|
|
pushbyte(st, LB_UNSAFE_POINTER);
|
|
|
|
void *ptr = janet_unwrap_pointer(x);
|
|
|
|
pushbytes(st, (uint8_t *) &ptr, sizeof(void *));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
no_registry:
|
2019-02-22 15:10:41 +00:00
|
|
|
default: {
|
|
|
|
janet_panicf("no registry value and cannot marshal %p", x);
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
#undef MARK_SEEN
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
void janet_marshal(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetBuffer *buf,
|
|
|
|
Janet x,
|
|
|
|
JanetTable *rreg,
|
|
|
|
int flags) {
|
2018-11-16 21:24:10 +00:00
|
|
|
MarshalState st;
|
2018-06-12 18:24:45 +00:00
|
|
|
st.buf = buf;
|
|
|
|
st.nextid = 0;
|
2018-08-20 00:21:27 +00:00
|
|
|
st.seen_defs = NULL;
|
|
|
|
st.seen_envs = NULL;
|
2018-10-21 05:35:07 +00:00
|
|
|
st.rreg = rreg;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_init(&st.seen, 0);
|
2019-02-22 15:10:41 +00:00
|
|
|
marshal_one(&st, x, flags);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_deinit(&st.seen);
|
2018-10-04 21:33:44 +00:00
|
|
|
janet_v_free(st.seen_envs);
|
|
|
|
janet_v_free(st.seen_defs);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
jmp_buf err;
|
2019-06-05 20:19:51 +00:00
|
|
|
Janet *lookup;
|
2018-10-21 05:35:07 +00:00
|
|
|
JanetTable *reg;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv **lookup_envs;
|
|
|
|
JanetFuncDef **lookup_defs;
|
2019-02-22 15:10:41 +00:00
|
|
|
const uint8_t *start;
|
2018-06-12 18:24:45 +00:00
|
|
|
const uint8_t *end;
|
|
|
|
} UnmarshalState;
|
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
#define MARSH_EOS(st, data) do { \
|
|
|
|
if ((data) >= (st)->end) janet_panic("unexpected end of source");\
|
|
|
|
} while (0)
|
2018-06-12 18:24:45 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Helper to read a 32 bit integer from an unmarshal state */
|
|
|
|
static int32_t readint(UnmarshalState *st, const uint8_t **atdata) {
|
|
|
|
const uint8_t *data = *atdata;
|
|
|
|
int32_t ret;
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data);
|
2019-02-08 15:12:25 +00:00
|
|
|
if (*data < 128) {
|
2018-08-20 00:21:27 +00:00
|
|
|
ret = *data++;
|
2019-02-08 15:12:25 +00:00
|
|
|
} else if (*data < 192) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data + 1);
|
2019-03-04 16:17:34 +00:00
|
|
|
uint32_t uret = ((data[0] & 0x3F) << 8) + data[1];
|
|
|
|
/* Sign extend 18 MSBs */
|
|
|
|
uret |= (uret >> 13) ? 0xFFFFC000 : 0;
|
|
|
|
ret = (int32_t)uret;
|
2019-02-08 15:12:25 +00:00
|
|
|
data += 2;
|
2018-08-20 00:21:27 +00:00
|
|
|
} else if (*data == LB_INTEGER) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data + 4);
|
2019-03-04 16:17:34 +00:00
|
|
|
uint32_t ui = ((uint32_t)(data[1]) << 24) |
|
|
|
|
((uint32_t)(data[2]) << 16) |
|
|
|
|
((uint32_t)(data[3]) << 8) |
|
|
|
|
(uint32_t)(data[4]);
|
|
|
|
ret = (int32_t)ui;
|
2018-08-20 00:21:27 +00:00
|
|
|
data += 5;
|
|
|
|
} else {
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panicf("expected integer, got byte %x at index %d",
|
|
|
|
*data,
|
|
|
|
data - st->start);
|
|
|
|
ret = 0;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
*atdata = data;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-05 13:01:18 +00:00
|
|
|
/* Helper to read a natural number (int >= 0). */
|
|
|
|
static int32_t readnat(UnmarshalState *st, const uint8_t **atdata) {
|
|
|
|
int32_t ret = readint(st, atdata);
|
|
|
|
if (ret < 0) {
|
|
|
|
janet_panicf("expected integer >= 0, got %d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:06:49 +00:00
|
|
|
/* Helper to read a size_t (up to 8 bytes unsigned). */
|
2019-03-19 01:12:38 +00:00
|
|
|
static uint64_t read64(UnmarshalState *st, const uint8_t **atdata) {
|
|
|
|
uint64_t ret;
|
2019-03-08 03:06:49 +00:00
|
|
|
const uint8_t *data = *atdata;
|
|
|
|
MARSH_EOS(st, data);
|
|
|
|
if (*data <= 0xF0) {
|
|
|
|
/* Single byte */
|
2019-03-08 05:41:26 +00:00
|
|
|
ret = *data;
|
|
|
|
*atdata = data + 1;
|
2019-03-08 03:06:49 +00:00
|
|
|
} else {
|
2019-03-08 05:41:26 +00:00
|
|
|
/* Multibyte, little endian */
|
|
|
|
int nbytes = *data - 0xF0;
|
|
|
|
ret = 0;
|
2019-03-19 01:12:38 +00:00
|
|
|
if (nbytes > 8) janet_panic("invalid 64 bit integer");
|
2019-03-08 05:41:26 +00:00
|
|
|
MARSH_EOS(st, data + nbytes);
|
|
|
|
for (int i = nbytes; i > 0; i--)
|
|
|
|
ret = (ret << 8) + data[i];
|
|
|
|
*atdata = data + nbytes + 1;
|
2019-03-08 03:06:49 +00:00
|
|
|
}
|
2019-03-08 05:41:26 +00:00
|
|
|
return ret;
|
2019-03-08 03:06:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
/* Assert a janet type */
|
|
|
|
static void janet_asserttype(Janet x, JanetType t) {
|
|
|
|
if (!janet_checktype(x, t)) {
|
|
|
|
janet_panicf("expected type %T, got %v", 1 << t, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:35:08 +00:00
|
|
|
/* Forward declarations for mutual recursion */
|
2018-08-20 00:21:27 +00:00
|
|
|
static const uint8_t *unmarshal_one(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
Janet *out,
|
|
|
|
int flags);
|
2018-08-24 15:35:08 +00:00
|
|
|
static const uint8_t *unmarshal_one_env(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFuncEnv **out,
|
|
|
|
int flags);
|
2018-08-24 15:35:08 +00:00
|
|
|
static const uint8_t *unmarshal_one_def(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFuncDef **out,
|
|
|
|
int flags);
|
2018-08-24 15:35:08 +00:00
|
|
|
static const uint8_t *unmarshal_one_fiber(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFiber **out,
|
|
|
|
int flags);
|
2018-08-20 00:21:27 +00:00
|
|
|
|
|
|
|
/* Unmarshal a funcenv */
|
|
|
|
static const uint8_t *unmarshal_one_env(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFuncEnv **out,
|
|
|
|
int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data);
|
2018-08-20 00:21:27 +00:00
|
|
|
if (*data == LB_FUNCENV_REF) {
|
2018-08-21 17:09:01 +00:00
|
|
|
data++;
|
2018-08-20 00:21:27 +00:00
|
|
|
int32_t index = readint(st, &data);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (index < 0 || index >= janet_v_count(st->lookup_envs))
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panicf("invalid funcenv reference %d", index);
|
2018-08-20 00:21:27 +00:00
|
|
|
*out = st->lookup_envs[index];
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncEnv *env = janet_gcalloc(JANET_MEMORY_FUNCENV, sizeof(JanetFuncEnv));
|
2018-08-24 12:22:43 +00:00
|
|
|
env->length = 0;
|
|
|
|
env->offset = 0;
|
2020-04-06 00:18:59 +00:00
|
|
|
env->as.values = NULL;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_push(st->lookup_envs, env);
|
2020-04-06 00:18:59 +00:00
|
|
|
int32_t offset = readnat(st, &data);
|
2020-04-05 13:01:18 +00:00
|
|
|
int32_t length = readnat(st, &data);
|
|
|
|
if (offset > 0) {
|
2018-11-26 14:02:07 +00:00
|
|
|
Janet fiberv;
|
2018-08-20 00:21:27 +00:00
|
|
|
/* On stack variant */
|
2018-11-26 14:02:07 +00:00
|
|
|
data = unmarshal_one(st, data, &fiberv, flags);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(fiberv, JANET_FIBER);
|
2018-11-26 14:02:07 +00:00
|
|
|
env->as.fiber = janet_unwrap_fiber(fiberv);
|
2020-04-06 15:58:47 +00:00
|
|
|
/* Negative offset indicates untrusted input */
|
|
|
|
env->offset = -offset;
|
2018-08-20 00:21:27 +00:00
|
|
|
} else {
|
|
|
|
/* Off stack variant */
|
2020-04-06 00:18:59 +00:00
|
|
|
if (length == 0) {
|
|
|
|
janet_panic("invalid funcenv length");
|
|
|
|
}
|
2020-01-03 04:02:57 +00:00
|
|
|
env->as.values = malloc(sizeof(Janet) * (size_t) length);
|
2018-08-20 00:21:27 +00:00
|
|
|
if (!env->as.values) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2020-04-06 15:58:47 +00:00
|
|
|
env->offset = 0;
|
2018-08-24 15:35:08 +00:00
|
|
|
for (int32_t i = 0; i < length; i++)
|
2018-08-20 00:21:27 +00:00
|
|
|
data = unmarshal_one(st, data, env->as.values + i, flags);
|
|
|
|
}
|
2018-08-24 12:22:43 +00:00
|
|
|
env->length = length;
|
2018-08-21 17:09:01 +00:00
|
|
|
*out = env;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:30:10 +00:00
|
|
|
/* Unmarshal a series of u32s */
|
|
|
|
static const uint8_t *janet_unmarshal_u32s(UnmarshalState *st, const uint8_t *data, uint32_t *into, int32_t n) {
|
|
|
|
for (int32_t i = 0; i < n; i++) {
|
|
|
|
MARSH_EOS(st, data + 3);
|
|
|
|
into[i] =
|
|
|
|
(uint32_t)(data[0]) |
|
|
|
|
((uint32_t)(data[1]) << 8) |
|
|
|
|
((uint32_t)(data[2]) << 16) |
|
|
|
|
((uint32_t)(data[3]) << 24);
|
|
|
|
data += 4;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal a funcdef */
|
|
|
|
static const uint8_t *unmarshal_one_def(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFuncDef **out,
|
|
|
|
int flags) {
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data);
|
2018-08-20 00:21:27 +00:00
|
|
|
if (*data == LB_FUNCDEF_REF) {
|
2018-08-21 17:09:01 +00:00
|
|
|
data++;
|
2018-08-20 00:21:27 +00:00
|
|
|
int32_t index = readint(st, &data);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (index < 0 || index >= janet_v_count(st->lookup_defs))
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panicf("invalid funcdef reference %d", index);
|
2018-08-20 00:21:27 +00:00
|
|
|
*out = st->lookup_defs[index];
|
|
|
|
} else {
|
2018-08-23 01:41:25 +00:00
|
|
|
/* Initialize with values that will not break garbage collection
|
2019-01-06 08:23:03 +00:00
|
|
|
* if unmarshalling fails. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef *def = janet_gcalloc(JANET_MEMORY_FUNCDEF, sizeof(JanetFuncDef));
|
2018-08-21 18:16:55 +00:00
|
|
|
def->environments_length = 0;
|
|
|
|
def->defs_length = 0;
|
2018-08-23 01:41:25 +00:00
|
|
|
def->constants_length = 0;
|
|
|
|
def->bytecode_length = 0;
|
2018-08-21 18:16:55 +00:00
|
|
|
def->name = NULL;
|
|
|
|
def->source = NULL;
|
2020-03-18 14:30:10 +00:00
|
|
|
def->closure_bitset = NULL;
|
2020-04-05 13:01:18 +00:00
|
|
|
def->defs = NULL;
|
|
|
|
def->environments = NULL;
|
|
|
|
def->constants = NULL;
|
|
|
|
def->bytecode = NULL;
|
|
|
|
def->sourcemap = NULL;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_push(st->lookup_defs, def);
|
2018-08-23 01:41:25 +00:00
|
|
|
|
|
|
|
/* Set default lengths to zero */
|
|
|
|
int32_t bytecode_length = 0;
|
|
|
|
int32_t constants_length = 0;
|
|
|
|
int32_t environments_length = 0;
|
|
|
|
int32_t defs_length = 0;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Read flags and other fixed values */
|
2018-08-21 18:16:55 +00:00
|
|
|
def->flags = readint(st, &data);
|
2020-04-05 13:01:18 +00:00
|
|
|
def->slotcount = readnat(st, &data);
|
|
|
|
def->arity = readnat(st, &data);
|
|
|
|
def->min_arity = readnat(st, &data);
|
|
|
|
def->max_arity = readnat(st, &data);
|
2018-08-23 01:41:25 +00:00
|
|
|
|
|
|
|
/* Read some lengths */
|
2020-04-05 13:01:18 +00:00
|
|
|
constants_length = readnat(st, &data);
|
|
|
|
bytecode_length = readnat(st, &data);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASENVS)
|
2020-04-05 13:01:18 +00:00
|
|
|
environments_length = readnat(st, &data);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASDEFS)
|
2020-04-05 13:01:18 +00:00
|
|
|
defs_length = readnat(st, &data);
|
2018-08-23 01:41:25 +00:00
|
|
|
|
|
|
|
/* Check name and source (optional) */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASNAME) {
|
|
|
|
Janet x;
|
2018-08-20 00:21:27 +00:00
|
|
|
data = unmarshal_one(st, data, &x, flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(x, JANET_STRING);
|
2018-09-06 02:18:42 +00:00
|
|
|
def->name = janet_unwrap_string(x);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCE) {
|
|
|
|
Janet x;
|
2018-08-20 00:21:27 +00:00
|
|
|
data = unmarshal_one(st, data, &x, flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(x, JANET_STRING);
|
2018-09-06 02:18:42 +00:00
|
|
|
def->source = janet_unwrap_string(x);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal constants */
|
2018-08-23 01:41:25 +00:00
|
|
|
if (constants_length) {
|
2018-09-06 02:18:42 +00:00
|
|
|
def->constants = malloc(sizeof(Janet) * constants_length);
|
2018-08-21 18:16:55 +00:00
|
|
|
if (!def->constants) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
for (int32_t i = 0; i < constants_length; i++)
|
2018-08-21 18:16:55 +00:00
|
|
|
data = unmarshal_one(st, data, def->constants + i, flags + 1);
|
2018-08-20 00:21:27 +00:00
|
|
|
} else {
|
2018-08-21 18:16:55 +00:00
|
|
|
def->constants = NULL;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
def->constants_length = constants_length;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal bytecode */
|
2018-08-23 01:41:25 +00:00
|
|
|
def->bytecode = malloc(sizeof(uint32_t) * bytecode_length);
|
2018-08-21 18:16:55 +00:00
|
|
|
if (!def->bytecode) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2020-03-18 14:30:10 +00:00
|
|
|
data = janet_unmarshal_u32s(st, data, def->bytecode, bytecode_length);
|
2018-08-23 01:41:25 +00:00
|
|
|
def->bytecode_length = bytecode_length;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal environments */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASENVS) {
|
2020-01-03 04:02:57 +00:00
|
|
|
def->environments = calloc(1, sizeof(int32_t) * (size_t) environments_length);
|
2018-08-21 18:16:55 +00:00
|
|
|
if (!def->environments) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
for (int32_t i = 0; i < environments_length; i++) {
|
2018-08-21 18:16:55 +00:00
|
|
|
def->environments[i] = readint(st, &data);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-21 18:16:55 +00:00
|
|
|
def->environments = NULL;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
def->environments_length = environments_length;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal sub funcdefs */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASDEFS) {
|
2020-01-03 04:02:57 +00:00
|
|
|
def->defs = calloc(1, sizeof(JanetFuncDef *) * (size_t) defs_length);
|
2018-08-21 18:16:55 +00:00
|
|
|
if (!def->defs) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
for (int32_t i = 0; i < defs_length; i++) {
|
2018-11-16 21:24:10 +00:00
|
|
|
data = unmarshal_one_def(st, data, def->defs + i, flags + 1);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-21 18:16:55 +00:00
|
|
|
def->defs = NULL;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
def->defs_length = defs_length;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Unmarshal source maps if needed */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASSOURCEMAP) {
|
2019-02-08 05:44:30 +00:00
|
|
|
int32_t current = 0;
|
2020-01-03 04:02:57 +00:00
|
|
|
def->sourcemap = malloc(sizeof(JanetSourceMapping) * (size_t) bytecode_length);
|
2018-08-21 18:16:55 +00:00
|
|
|
if (!def->sourcemap) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2018-08-23 01:41:25 +00:00
|
|
|
for (int32_t i = 0; i < bytecode_length; i++) {
|
2019-02-08 05:44:30 +00:00
|
|
|
current += readint(st, &data);
|
2019-09-22 22:18:28 +00:00
|
|
|
def->sourcemap[i].line = current;
|
2020-07-04 00:54:58 +00:00
|
|
|
def->sourcemap[i].column = readint(st, &data);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-21 18:16:55 +00:00
|
|
|
def->sourcemap = NULL;
|
2018-11-16 21:24:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:30:10 +00:00
|
|
|
/* Unmarshal closure bitset if needed */
|
|
|
|
if (def->flags & JANET_FUNCDEF_FLAG_HASCLOBITSET) {
|
2020-07-04 01:13:49 +00:00
|
|
|
int32_t n = (def->slotcount + 31) >> 5;
|
|
|
|
def->closure_bitset = malloc(sizeof(uint32_t) * (size_t) n);
|
2020-03-18 14:30:10 +00:00
|
|
|
if (NULL == def->closure_bitset) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2020-07-04 00:54:58 +00:00
|
|
|
data = janet_unmarshal_u32s(st, data, def->closure_bitset, n);
|
2020-03-18 14:30:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
/* Validate */
|
2019-02-22 15:10:41 +00:00
|
|
|
if (janet_verify(def))
|
|
|
|
janet_panic("funcdef has invalid bytecode");
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2018-08-21 17:09:01 +00:00
|
|
|
/* Set def */
|
2018-08-21 18:16:55 +00:00
|
|
|
*out = def;
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-08-24 12:22:43 +00:00
|
|
|
/* Unmarshal a fiber */
|
2018-08-23 01:41:25 +00:00
|
|
|
static const uint8_t *unmarshal_one_fiber(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
JanetFiber **out,
|
|
|
|
int flags) {
|
2018-08-24 12:22:43 +00:00
|
|
|
|
2020-04-06 02:39:39 +00:00
|
|
|
/* Initialize a new fiber with gc friendly defaults */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFiber *fiber = janet_gcalloc(JANET_MEMORY_FIBER, sizeof(JanetFiber));
|
2018-08-24 12:22:43 +00:00
|
|
|
fiber->flags = 0;
|
|
|
|
fiber->frame = 0;
|
|
|
|
fiber->stackstart = 0;
|
|
|
|
fiber->stacktop = 0;
|
|
|
|
fiber->capacity = 0;
|
2018-11-26 23:01:50 +00:00
|
|
|
fiber->maxstack = 0;
|
|
|
|
fiber->data = NULL;
|
2018-08-24 12:22:43 +00:00
|
|
|
fiber->child = NULL;
|
2019-04-16 19:41:45 +00:00
|
|
|
fiber->env = NULL;
|
2020-10-11 14:07:11 +00:00
|
|
|
#ifdef JANET_EV
|
2020-07-20 00:41:12 +00:00
|
|
|
fiber->waiting = NULL;
|
2020-10-11 14:07:11 +00:00
|
|
|
fiber->sched_id = 0;
|
2021-01-22 18:52:45 +00:00
|
|
|
fiber->supervisor_channel = NULL;
|
2020-10-11 14:07:11 +00:00
|
|
|
#endif
|
2018-08-24 12:22:43 +00:00
|
|
|
|
2019-02-22 00:11:28 +00:00
|
|
|
/* Push fiber to seen stack */
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, janet_wrap_fiber(fiber));
|
2019-02-22 00:11:28 +00:00
|
|
|
|
2018-08-24 12:22:43 +00:00
|
|
|
/* Read ints */
|
2020-04-06 02:39:39 +00:00
|
|
|
int32_t fiber_flags = readint(st, &data);
|
2020-04-06 05:11:22 +00:00
|
|
|
int32_t frame = readnat(st, &data);
|
2020-04-06 02:39:39 +00:00
|
|
|
int32_t fiber_stackstart = readnat(st, &data);
|
|
|
|
int32_t fiber_stacktop = readnat(st, &data);
|
|
|
|
int32_t fiber_maxstack = readnat(st, &data);
|
2020-04-06 05:11:22 +00:00
|
|
|
JanetTable *fiber_env = NULL;
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Check for bad flags and ints */
|
2020-04-06 02:39:39 +00:00
|
|
|
if ((int32_t)(frame + JANET_FRAME_SIZE) > fiber_stackstart ||
|
|
|
|
fiber_stackstart > fiber_stacktop ||
|
|
|
|
fiber_stacktop > fiber_maxstack) {
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panic("fiber has incorrect stack setup");
|
2018-08-24 15:35:08 +00:00
|
|
|
}
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Allocate stack memory */
|
2020-04-06 05:11:22 +00:00
|
|
|
fiber->capacity = fiber_stacktop + 10;
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->data = malloc(sizeof(Janet) * fiber->capacity);
|
2018-08-24 12:22:43 +00:00
|
|
|
if (!fiber->data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-08-24 12:22:43 +00:00
|
|
|
}
|
2020-04-06 05:11:22 +00:00
|
|
|
for (int32_t i = 0; i < fiber->capacity; i++) {
|
|
|
|
fiber->data[i] = janet_wrap_nil();
|
|
|
|
}
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* get frames */
|
2020-04-06 05:11:22 +00:00
|
|
|
int32_t stack = frame;
|
|
|
|
int32_t stacktop = fiber_stackstart - JANET_FRAME_SIZE;
|
2018-08-24 12:22:43 +00:00
|
|
|
while (stack > 0) {
|
2018-11-26 23:01:50 +00:00
|
|
|
JanetFunction *func = NULL;
|
|
|
|
JanetFuncDef *def = NULL;
|
2018-11-26 14:02:07 +00:00
|
|
|
JanetFuncEnv *env = NULL;
|
2018-08-24 12:22:43 +00:00
|
|
|
int32_t frameflags = readint(st, &data);
|
2020-04-05 13:01:18 +00:00
|
|
|
int32_t prevframe = readnat(st, &data);
|
|
|
|
int32_t pcdiff = readnat(st, &data);
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Get frame items */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *framestack = fiber->data + stack;
|
2018-11-26 23:01:50 +00:00
|
|
|
JanetStackFrame *framep = janet_stack_frame(framestack);
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Get function */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet funcv;
|
2018-08-24 12:22:43 +00:00
|
|
|
data = unmarshal_one(st, data, &funcv, flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(funcv, JANET_FUNCTION);
|
2018-09-06 02:18:42 +00:00
|
|
|
func = janet_unwrap_function(funcv);
|
2018-08-24 12:22:43 +00:00
|
|
|
def = func->def;
|
|
|
|
|
|
|
|
/* Check env */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (frameflags & JANET_STACKFRAME_HASENV) {
|
|
|
|
frameflags &= ~JANET_STACKFRAME_HASENV;
|
2018-08-24 12:22:43 +00:00
|
|
|
data = unmarshal_one_env(st, data, &env, flags + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error checking */
|
2018-08-24 15:35:08 +00:00
|
|
|
int32_t expected_framesize = def->slotcount;
|
2019-02-22 15:10:41 +00:00
|
|
|
if (expected_framesize != stacktop - stack) {
|
|
|
|
janet_panic("fiber stackframe size mismatch");
|
|
|
|
}
|
2020-04-06 17:41:56 +00:00
|
|
|
if (pcdiff >= def->bytecode_length) {
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panic("fiber stackframe has invalid pc");
|
|
|
|
}
|
|
|
|
if ((int32_t)(prevframe + JANET_FRAME_SIZE) > stack) {
|
2020-04-05 13:01:18 +00:00
|
|
|
janet_panic("fiber stackframe does not align with previous frame");
|
2019-02-22 15:10:41 +00:00
|
|
|
}
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Get stack items */
|
2018-08-24 15:35:08 +00:00
|
|
|
for (int32_t i = stack; i < stacktop; i++)
|
|
|
|
data = unmarshal_one(st, data, fiber->data + i, flags + 1);
|
2018-08-24 12:22:43 +00:00
|
|
|
|
|
|
|
/* Set frame */
|
|
|
|
framep->env = env;
|
|
|
|
framep->pc = def->bytecode + pcdiff;
|
|
|
|
framep->prevframe = prevframe;
|
|
|
|
framep->flags = frameflags;
|
|
|
|
framep->func = func;
|
|
|
|
|
|
|
|
/* Goto previous frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
stacktop = stack - JANET_FRAME_SIZE;
|
2018-08-24 12:22:43 +00:00
|
|
|
stack = prevframe;
|
|
|
|
}
|
2019-02-22 15:10:41 +00:00
|
|
|
if (stack < 0) {
|
|
|
|
janet_panic("fiber has too many stackframes");
|
|
|
|
}
|
2018-08-24 12:22:43 +00:00
|
|
|
|
2019-04-16 19:41:45 +00:00
|
|
|
/* Check for fiber env */
|
2020-04-06 02:39:39 +00:00
|
|
|
if (fiber_flags & JANET_FIBER_FLAG_HASENV) {
|
2019-04-16 19:41:45 +00:00
|
|
|
Janet envv;
|
2020-04-06 02:39:39 +00:00
|
|
|
fiber_flags &= ~JANET_FIBER_FLAG_HASENV;
|
2019-04-16 19:41:45 +00:00
|
|
|
data = unmarshal_one(st, data, &envv, flags + 1);
|
|
|
|
janet_asserttype(envv, JANET_TABLE);
|
2020-04-06 05:11:22 +00:00
|
|
|
fiber_env = janet_unwrap_table(envv);
|
2019-04-16 19:41:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:22:43 +00:00
|
|
|
/* Check for child fiber */
|
2020-04-06 02:39:39 +00:00
|
|
|
if (fiber_flags & JANET_FIBER_FLAG_HASCHILD) {
|
2018-11-26 14:02:07 +00:00
|
|
|
Janet fiberv;
|
2020-04-06 02:39:39 +00:00
|
|
|
fiber_flags &= ~JANET_FIBER_FLAG_HASCHILD;
|
2018-11-26 14:02:07 +00:00
|
|
|
data = unmarshal_one(st, data, &fiberv, flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(fiberv, JANET_FIBER);
|
2018-11-26 14:02:07 +00:00
|
|
|
fiber->child = janet_unwrap_fiber(fiberv);
|
2018-08-24 12:22:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 02:39:39 +00:00
|
|
|
/* We have valid fiber, finally construct remaining fields. */
|
2018-08-24 12:22:43 +00:00
|
|
|
fiber->frame = frame;
|
2020-04-06 02:39:39 +00:00
|
|
|
fiber->flags = fiber_flags;
|
|
|
|
fiber->stackstart = fiber_stackstart;
|
|
|
|
fiber->stacktop = fiber_stacktop;
|
|
|
|
fiber->maxstack = fiber_maxstack;
|
2020-04-06 05:11:22 +00:00
|
|
|
fiber->env = fiber_env;
|
2020-04-06 02:39:39 +00:00
|
|
|
|
2020-07-20 00:41:12 +00:00
|
|
|
int status = janet_fiber_status(fiber);
|
|
|
|
if (status < 0 || status > JANET_STATUS_ALIVE) {
|
|
|
|
janet_panic("invalid fiber status");
|
|
|
|
}
|
|
|
|
|
2020-04-06 02:39:39 +00:00
|
|
|
/* Return data */
|
2018-08-24 15:35:08 +00:00
|
|
|
*out = fiber;
|
2018-08-24 12:22:43 +00:00
|
|
|
return data;
|
2018-08-23 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 04:12:18 +00:00
|
|
|
void janet_unmarshal_ensure(JanetMarshalContext *ctx, size_t size) {
|
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
|
|
|
MARSH_EOS(st, ctx->data + size);
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
int32_t janet_unmarshal_int(JanetMarshalContext *ctx) {
|
2019-02-23 16:13:43 +00:00
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
2019-03-19 01:12:38 +00:00
|
|
|
return readint(st, &(ctx->data));
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t janet_unmarshal_size(JanetMarshalContext *ctx) {
|
|
|
|
return (size_t) janet_unmarshal_int64(ctx);
|
|
|
|
}
|
2019-02-23 15:58:47 +00:00
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
int64_t janet_unmarshal_int64(JanetMarshalContext *ctx) {
|
2019-02-23 16:13:43 +00:00
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
2019-03-19 01:12:38 +00:00
|
|
|
return read64(st, &(ctx->data));
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
2019-02-23 15:58:47 +00:00
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
uint8_t janet_unmarshal_byte(JanetMarshalContext *ctx) {
|
2019-02-24 17:45:14 +00:00
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
|
|
|
MARSH_EOS(st, ctx->data);
|
2019-03-19 01:12:38 +00:00
|
|
|
return *(ctx->data++);
|
2019-04-27 19:45:28 +00:00
|
|
|
}
|
2019-02-23 15:58:47 +00:00
|
|
|
|
2019-03-08 03:19:34 +00:00
|
|
|
void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, size_t len) {
|
2019-02-24 17:45:14 +00:00
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
2019-02-24 21:36:35 +00:00
|
|
|
MARSH_EOS(st, ctx->data + len - 1);
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(dest, ctx->data, len);
|
2019-02-23 16:13:43 +00:00
|
|
|
ctx->data += len;
|
2019-02-23 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
Janet janet_unmarshal_janet(JanetMarshalContext *ctx) {
|
|
|
|
Janet ret;
|
2019-02-23 16:13:43 +00:00
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
2019-03-19 01:12:38 +00:00
|
|
|
ctx->data = unmarshal_one(st, ctx->data, &ret, ctx->flags);
|
|
|
|
return ret;
|
2019-02-23 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 04:12:18 +00:00
|
|
|
void *janet_unmarshal_abstract(JanetMarshalContext *ctx, size_t size) {
|
|
|
|
UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
|
|
|
|
if (ctx->at == NULL) {
|
|
|
|
janet_panicf("janet_unmarshal_abstract called more than once");
|
|
|
|
}
|
|
|
|
void *p = janet_abstract(ctx->at, size);
|
|
|
|
janet_v_push(st->lookup, janet_wrap_abstract(p));
|
|
|
|
ctx->at = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:58:47 +00:00
|
|
|
static const uint8_t *unmarshal_one_abstract(UnmarshalState *st, const uint8_t *data, Janet *out, int flags) {
|
2019-02-24 01:51:34 +00:00
|
|
|
Janet key;
|
|
|
|
data = unmarshal_one(st, data, &key, flags + 1);
|
|
|
|
const JanetAbstractType *at = janet_get_abstract_type(key);
|
2020-04-05 22:38:14 +00:00
|
|
|
if (at == NULL) goto oops;
|
2019-02-24 01:02:54 +00:00
|
|
|
if (at->unmarshal) {
|
2019-12-07 04:12:18 +00:00
|
|
|
JanetMarshalContext context = {NULL, st, flags, data, at};
|
|
|
|
*out = janet_wrap_abstract(at->unmarshal(&context));
|
|
|
|
if (context.at != NULL) {
|
|
|
|
janet_panicf("janet_unmarshal_abstract not called");
|
|
|
|
}
|
2019-06-20 03:23:27 +00:00
|
|
|
return context.data;
|
2019-02-23 16:13:43 +00:00
|
|
|
}
|
2020-04-05 22:38:14 +00:00
|
|
|
oops:
|
|
|
|
janet_panic("invalid abstract type");
|
2019-02-23 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 00:21:27 +00:00
|
|
|
static const uint8_t *unmarshal_one(
|
2019-02-20 01:51:34 +00:00
|
|
|
UnmarshalState *st,
|
|
|
|
const uint8_t *data,
|
|
|
|
Janet *out,
|
|
|
|
int flags) {
|
2018-06-12 18:24:45 +00:00
|
|
|
uint8_t lead;
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_STACKCHECK;
|
|
|
|
MARSH_EOS(st, data);
|
2018-06-12 18:24:45 +00:00
|
|
|
lead = data[0];
|
2020-04-05 13:01:18 +00:00
|
|
|
if (lead < LB_REAL) {
|
2019-02-08 15:12:25 +00:00
|
|
|
*out = janet_wrap_integer(readint(st, &data));
|
|
|
|
return data;
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
switch (lead) {
|
|
|
|
case LB_NIL:
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_nil();
|
2018-06-12 18:24:45 +00:00
|
|
|
return data + 1;
|
|
|
|
case LB_FALSE:
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_false();
|
2018-06-12 18:24:45 +00:00
|
|
|
return data + 1;
|
|
|
|
case LB_TRUE:
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_true();
|
2018-06-12 18:24:45 +00:00
|
|
|
return data + 1;
|
|
|
|
case LB_INTEGER:
|
|
|
|
/* Long integer */
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data + 4);
|
2019-03-04 16:17:34 +00:00
|
|
|
uint32_t ui = ((uint32_t)(data[4])) |
|
|
|
|
((uint32_t)(data[3]) << 8) |
|
|
|
|
((uint32_t)(data[2]) << 16) |
|
|
|
|
((uint32_t)(data[1]) << 24);
|
|
|
|
int32_t si = (int32_t)ui;
|
|
|
|
*out = janet_wrap_integer(si);
|
2018-06-12 18:24:45 +00:00
|
|
|
return data + 5;
|
|
|
|
case LB_REAL:
|
|
|
|
/* Real */
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
uint8_t bytes[8];
|
|
|
|
} u;
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data + 8);
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_BIG_ENDIAN
|
2019-02-20 01:51:34 +00:00
|
|
|
u.bytes[0] = data[8];
|
|
|
|
u.bytes[1] = data[7];
|
|
|
|
u.bytes[2] = data[6];
|
2020-03-30 17:14:00 +00:00
|
|
|
u.bytes[3] = data[5];
|
2019-02-20 01:51:34 +00:00
|
|
|
u.bytes[4] = data[4];
|
|
|
|
u.bytes[5] = data[3];
|
|
|
|
u.bytes[6] = data[2];
|
|
|
|
u.bytes[7] = data[1];
|
2018-06-12 18:24:45 +00:00
|
|
|
#else
|
2019-02-20 01:51:34 +00:00
|
|
|
memcpy(&u.bytes, data + 1, sizeof(double));
|
2018-06-12 18:24:45 +00:00
|
|
|
#endif
|
2019-07-12 12:19:29 +00:00
|
|
|
*out = janet_wrap_number_safe(u.d);
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
return data + 9;
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
case LB_STRING:
|
|
|
|
case LB_SYMBOL:
|
|
|
|
case LB_BUFFER:
|
2019-01-03 00:41:07 +00:00
|
|
|
case LB_KEYWORD:
|
2019-02-20 01:51:34 +00:00
|
|
|
case LB_REGISTRY: {
|
|
|
|
data++;
|
2020-04-05 13:01:18 +00:00
|
|
|
int32_t len = readnat(st, &data);
|
2019-02-22 15:10:41 +00:00
|
|
|
MARSH_EOS(st, data - 1 + len);
|
2019-02-20 01:51:34 +00:00
|
|
|
if (lead == LB_STRING) {
|
|
|
|
const uint8_t *str = janet_string(data, len);
|
|
|
|
*out = janet_wrap_string(str);
|
|
|
|
} else if (lead == LB_SYMBOL) {
|
|
|
|
const uint8_t *str = janet_symbol(data, len);
|
|
|
|
*out = janet_wrap_symbol(str);
|
|
|
|
} else if (lead == LB_KEYWORD) {
|
|
|
|
const uint8_t *str = janet_keyword(data, len);
|
|
|
|
*out = janet_wrap_keyword(str);
|
|
|
|
} else if (lead == LB_REGISTRY) {
|
|
|
|
if (st->reg) {
|
|
|
|
Janet regkey = janet_symbolv(data, len);
|
|
|
|
*out = janet_table_get(st->reg, regkey);
|
|
|
|
} else {
|
|
|
|
*out = janet_wrap_nil();
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
} else { /* (lead == LB_BUFFER) */
|
|
|
|
JanetBuffer *buffer = janet_buffer(len);
|
|
|
|
buffer->count = len;
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(buffer->data, data, len);
|
2019-02-20 01:51:34 +00:00
|
|
|
*out = janet_wrap_buffer(buffer);
|
2018-08-23 01:41:25 +00:00
|
|
|
}
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
return data + len;
|
|
|
|
}
|
|
|
|
case LB_FIBER: {
|
|
|
|
JanetFiber *fiber;
|
|
|
|
data = unmarshal_one_fiber(st, data + 1, &fiber, flags);
|
|
|
|
*out = janet_wrap_fiber(fiber);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
case LB_FUNCTION: {
|
|
|
|
JanetFunction *func;
|
|
|
|
JanetFuncDef *def;
|
2020-12-06 22:32:23 +00:00
|
|
|
data++;
|
|
|
|
int32_t len = readnat(st, &data);
|
|
|
|
if (len > 255) {
|
|
|
|
janet_panicf("invalid function");
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
func = janet_gcalloc(JANET_MEMORY_FUNCTION, sizeof(JanetFunction) +
|
2020-12-06 22:32:23 +00:00
|
|
|
len * sizeof(JanetFuncEnv));
|
2019-02-20 01:51:34 +00:00
|
|
|
*out = janet_wrap_function(func);
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2020-12-06 22:32:23 +00:00
|
|
|
data = unmarshal_one_def(st, data, &def, flags + 1);
|
|
|
|
if (def->environments_length != len) {
|
|
|
|
janet_panicf("invalid function");
|
|
|
|
}
|
|
|
|
func->def = def;
|
2019-02-20 01:51:34 +00:00
|
|
|
for (int32_t i = 0; i < def->environments_length; i++) {
|
|
|
|
data = unmarshal_one_env(st, data, &(func->envs[i]), flags + 1);
|
2018-08-20 00:21:27 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
return data;
|
|
|
|
}
|
2019-02-23 15:58:47 +00:00
|
|
|
case LB_ABSTRACT: {
|
2019-02-23 16:13:43 +00:00
|
|
|
data++;
|
|
|
|
return unmarshal_one_abstract(st, data, out, flags);
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
case LB_REFERENCE:
|
|
|
|
case LB_ARRAY:
|
|
|
|
case LB_TUPLE:
|
|
|
|
case LB_STRUCT:
|
|
|
|
case LB_TABLE:
|
|
|
|
case LB_TABLE_PROTO:
|
|
|
|
/* Things that open with integers */
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
data++;
|
2020-04-05 13:01:18 +00:00
|
|
|
int32_t len = readnat(st, &data);
|
2020-04-05 13:16:40 +00:00
|
|
|
/* DOS check */
|
|
|
|
if (lead != LB_REFERENCE) {
|
|
|
|
MARSH_EOS(st, data - 1 + len);
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
if (lead == LB_ARRAY) {
|
|
|
|
/* Array */
|
|
|
|
JanetArray *array = janet_array(len);
|
|
|
|
array->count = len;
|
|
|
|
*out = janet_wrap_array(array);
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
data = unmarshal_one(st, data, array->data + i, flags + 1);
|
|
|
|
}
|
|
|
|
} else if (lead == LB_TUPLE) {
|
|
|
|
/* Tuple */
|
|
|
|
Janet *tup = janet_tuple_begin(len);
|
|
|
|
int32_t flag = readint(st, &data);
|
2019-02-21 16:22:29 +00:00
|
|
|
janet_tuple_flag(tup) |= flag << 16;
|
2019-02-20 01:51:34 +00:00
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
data = unmarshal_one(st, data, tup + i, flags + 1);
|
|
|
|
}
|
|
|
|
*out = janet_wrap_tuple(janet_tuple_end(tup));
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
} else if (lead == LB_STRUCT) {
|
|
|
|
/* Struct */
|
|
|
|
JanetKV *struct_ = janet_struct_begin(len);
|
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
Janet key, value;
|
|
|
|
data = unmarshal_one(st, data, &key, flags + 1);
|
|
|
|
data = unmarshal_one(st, data, &value, flags + 1);
|
|
|
|
janet_struct_put(struct_, key, value);
|
|
|
|
}
|
|
|
|
*out = janet_wrap_struct(janet_struct_end(struct_));
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
} else if (lead == LB_REFERENCE) {
|
2020-04-05 13:16:40 +00:00
|
|
|
if (len >= janet_v_count(st->lookup))
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_panicf("invalid reference %d", len);
|
2019-06-05 20:19:51 +00:00
|
|
|
*out = st->lookup[len];
|
2019-02-20 01:51:34 +00:00
|
|
|
} else {
|
|
|
|
/* Table */
|
|
|
|
JanetTable *t = janet_table(len);
|
|
|
|
*out = janet_wrap_table(t);
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_push(st->lookup, *out);
|
2019-02-20 01:51:34 +00:00
|
|
|
if (lead == LB_TABLE_PROTO) {
|
|
|
|
Janet proto;
|
|
|
|
data = unmarshal_one(st, data, &proto, flags + 1);
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_asserttype(proto, JANET_TABLE);
|
2019-02-20 01:51:34 +00:00
|
|
|
t->proto = janet_unwrap_table(proto);
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
Janet key, value;
|
|
|
|
data = unmarshal_one(st, data, &key, flags + 1);
|
|
|
|
data = unmarshal_one(st, data, &value, flags + 1);
|
|
|
|
janet_table_put(t, key, value);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
return data;
|
|
|
|
}
|
2020-04-19 15:55:00 +00:00
|
|
|
case LB_UNSAFE_POINTER: {
|
|
|
|
MARSH_EOS(st, data + sizeof(void *));
|
|
|
|
data++;
|
|
|
|
if (!(flags & JANET_MARSHAL_UNSAFE)) {
|
|
|
|
janet_panicf("unsafe flag not given, "
|
|
|
|
"will not unmarshal raw pointer at index %d",
|
|
|
|
(int)(data - st->start));
|
|
|
|
}
|
|
|
|
union {
|
|
|
|
void *ptr;
|
|
|
|
uint8_t bytes[sizeof(void *)];
|
|
|
|
} u;
|
|
|
|
memcpy(u.bytes, data, sizeof(void *));
|
|
|
|
data += sizeof(void *);
|
|
|
|
*out = janet_wrap_pointer(u.ptr);
|
|
|
|
janet_v_push(st->lookup, *out);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
case LB_UNSAFE_CFUNCTION: {
|
|
|
|
MARSH_EOS(st, data + sizeof(JanetCFunction));
|
|
|
|
data++;
|
|
|
|
if (!(flags & JANET_MARSHAL_UNSAFE)) {
|
|
|
|
janet_panicf("unsafe flag not given, "
|
|
|
|
"will not unmarshal function pointer at index %d",
|
|
|
|
(int)(data - st->start));
|
|
|
|
}
|
|
|
|
union {
|
|
|
|
JanetCFunction ptr;
|
|
|
|
uint8_t bytes[sizeof(JanetCFunction)];
|
|
|
|
} u;
|
|
|
|
memcpy(u.bytes, data, sizeof(JanetCFunction));
|
|
|
|
data += sizeof(JanetCFunction);
|
|
|
|
*out = janet_wrap_cfunction(u.ptr);
|
|
|
|
janet_v_push(st->lookup, *out);
|
|
|
|
return data;
|
|
|
|
}
|
2019-02-22 15:10:41 +00:00
|
|
|
default: {
|
|
|
|
janet_panicf("unknown byte %x at index %d",
|
|
|
|
*data,
|
|
|
|
(int)(data - st->start));
|
2018-06-12 18:24:45 +00:00
|
|
|
return NULL;
|
2019-02-22 15:10:41 +00:00
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
#undef EXTRA
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
Janet janet_unmarshal(
|
2019-02-20 01:51:34 +00:00
|
|
|
const uint8_t *bytes,
|
|
|
|
size_t len,
|
|
|
|
int flags,
|
|
|
|
JanetTable *reg,
|
|
|
|
const uint8_t **next) {
|
2018-11-16 21:24:10 +00:00
|
|
|
UnmarshalState st;
|
2019-02-22 15:10:41 +00:00
|
|
|
st.start = bytes;
|
2018-06-12 18:24:45 +00:00
|
|
|
st.end = bytes + len;
|
2018-08-20 00:21:27 +00:00
|
|
|
st.lookup_defs = NULL;
|
|
|
|
st.lookup_envs = NULL;
|
2019-06-05 20:19:51 +00:00
|
|
|
st.lookup = NULL;
|
2018-10-21 05:35:07 +00:00
|
|
|
st.reg = reg;
|
2019-02-22 15:10:41 +00:00
|
|
|
Janet out;
|
|
|
|
const uint8_t *nextbytes = unmarshal_one(&st, bytes, &out, flags);
|
|
|
|
if (next) *next = nextbytes;
|
2018-10-04 21:33:44 +00:00
|
|
|
janet_v_free(st.lookup_defs);
|
|
|
|
janet_v_free(st.lookup_envs);
|
2019-06-05 20:19:51 +00:00
|
|
|
janet_v_free(st.lookup);
|
2019-02-22 15:10:41 +00:00
|
|
|
return out;
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* C functions */
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet cfun_env_lookup(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetTable *env = janet_gettable(argv, 0);
|
|
|
|
return janet_wrap_table(janet_env_lookup(env));
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet cfun_marshal(int32_t argc, Janet *argv) {
|
2020-02-15 16:04:44 +00:00
|
|
|
janet_arity(argc, 1, 3);
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buffer;
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetTable *rreg = NULL;
|
|
|
|
if (argc > 1) {
|
|
|
|
rreg = janet_gettable(argv, 1);
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc > 2) {
|
|
|
|
buffer = janet_getbuffer(argv, 2);
|
2018-06-12 18:24:45 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
buffer = janet_buffer(10);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
2019-02-22 15:10:41 +00:00
|
|
|
janet_marshal(buffer, argv[0], rreg, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_buffer(buffer);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet cfun_unmarshal(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
JanetByteView view = janet_getbytes(argv, 0);
|
|
|
|
JanetTable *reg = NULL;
|
|
|
|
if (argc > 1) {
|
|
|
|
reg = janet_gettable(argv, 1);
|
2018-10-21 05:35:07 +00:00
|
|
|
}
|
2019-02-22 15:10:41 +00:00
|
|
|
return janet_unmarshal(view.bytes, (size_t) view.len, 0, reg, NULL);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg marsh_cfuns[] = {
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"marshal", cfun_marshal,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(marshal x &opt reverse-lookup buffer)\n\n"
|
2020-05-10 21:44:16 +00:00
|
|
|
"Marshal a value into a buffer and return the buffer. The buffer "
|
2019-02-20 01:51:34 +00:00
|
|
|
"can the later be unmarshalled to reconstruct the initial value. "
|
|
|
|
"Optionally, one can pass in a reverse lookup table to not marshal "
|
|
|
|
"aliased values that are found in the table. Then a forward"
|
2020-05-10 21:44:16 +00:00
|
|
|
"lookup table can be used to recover the original value when "
|
2019-02-20 01:51:34 +00:00
|
|
|
"unmarshalling.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"unmarshal", cfun_unmarshal,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(unmarshal buffer &opt lookup)\n\n"
|
2020-05-10 21:44:16 +00:00
|
|
|
"Unmarshal a value from a buffer. An optional lookup table "
|
2019-02-20 01:51:34 +00:00
|
|
|
"can be provided to allow for aliases to be resolved. Returns the value "
|
|
|
|
"unmarshalled from the buffer.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"env-lookup", cfun_env_lookup,
|
|
|
|
JDOC("(env-lookup env)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a forward lookup table for unmarshalling from an environment. "
|
|
|
|
"To create a reverse lookup table, use the invert function to swap keys "
|
|
|
|
"and values in the returned table.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-06-12 18:24:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_marsh(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, marsh_cfuns);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|