1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Fix cfunctions not printing with whole name im some cases.

This commit is contained in:
Calvin Rose 2018-10-22 01:28:39 -04:00
parent ddc23182e9
commit ecb9196e7b
5 changed files with 42 additions and 20 deletions

View File

@ -1139,8 +1139,9 @@ value, one key will be ignored."
(defn chunks [buf _] (defn chunks [buf _]
(def ret state) (def ret state)
(:= state nil) (:= state nil)
(if ret (when ret
(buffer.push-string buf ret))) (buffer.push-string buf ret)
(buffer.push-string buf "\n")))
(var returnval nil) (var returnval nil)
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval") (run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval")
returnval) returnval)

View File

@ -29,6 +29,7 @@
typedef struct { typedef struct {
jmp_buf err; jmp_buf err;
Janet current;
JanetBuffer *buf; JanetBuffer *buf;
JanetTable seen; JanetTable seen;
JanetTable *rreg; JanetTable *rreg;
@ -278,11 +279,13 @@ static void marshal_one_fiber(MarshalState *st, JanetFiber *fiber, int flags) {
/* The main body of the marshaling function. Is the main /* The main body of the marshaling function. Is the main
* entry point for the mutually recursive functions. */ * entry point for the mutually recursive functions. */
static void marshal_one(MarshalState *st, Janet x, int flags) { static void marshal_one(MarshalState *st, Janet x, int flags) {
Janet parent = st->current;
JanetType type = janet_type(x); JanetType type = janet_type(x);
st->current = x;
if ((flags & 0xFFFF) > JANET_RECURSION_GUARD) if ((flags & 0xFFFF) > JANET_RECURSION_GUARD)
longjmp(st->err, MR_STACKOVERFLOW); longjmp(st->err, MR_STACKOVERFLOW);
/* Check simple primitvies (non reference types, no benefit from memoization) */ /* Check simple primitives (non reference types, no benefit from memoization) */
switch (type) { switch (type) {
default: default:
break; break;
@ -290,10 +293,10 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
case JANET_FALSE: case JANET_FALSE:
case JANET_TRUE: case JANET_TRUE:
pushbyte(st, 200 + type); pushbyte(st, 200 + type);
return; goto done;
case JANET_INTEGER: case JANET_INTEGER:
pushint(st, janet_unwrap_integer(x)); pushint(st, janet_unwrap_integer(x));
return; goto done;
} }
#define MARK_SEEN() \ #define MARK_SEEN() \
@ -305,7 +308,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
if (janet_checktype(check, JANET_INTEGER)) { if (janet_checktype(check, JANET_INTEGER)) {
pushbyte(st, LB_REFERENCE); pushbyte(st, LB_REFERENCE);
pushint(st, janet_unwrap_integer(check)); pushint(st, janet_unwrap_integer(check));
return; goto done;
} }
if (st->rreg) { if (st->rreg) {
check = janet_table_get(st->rreg, x); check = janet_table_get(st->rreg, x);
@ -315,7 +318,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushbyte(st, LB_REGISTRY); pushbyte(st, LB_REGISTRY);
pushint(st, janet_string_length(regname)); pushint(st, janet_string_length(regname));
pushbytes(st, regname, janet_string_length(regname)); pushbytes(st, regname, janet_string_length(regname));
return; goto done;
} }
} }
} }
@ -341,7 +344,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushbytes(st, u.bytes, 8); pushbytes(st, u.bytes, 8);
MARK_SEEN(); MARK_SEEN();
} }
return; goto done;
case JANET_STRING: case JANET_STRING:
case JANET_SYMBOL: case JANET_SYMBOL:
{ {
@ -354,7 +357,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushint(st, length); pushint(st, length);
pushbytes(st, str, length); pushbytes(st, str, length);
} }
return; goto done;
case JANET_BUFFER: case JANET_BUFFER:
{ {
JanetBuffer *buffer = janet_unwrap_buffer(x); JanetBuffer *buffer = janet_unwrap_buffer(x);
@ -364,7 +367,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
pushint(st, buffer->count); pushint(st, buffer->count);
pushbytes(st, buffer->data, buffer->count); pushbytes(st, buffer->data, buffer->count);
} }
return; goto done;
case JANET_ARRAY: case JANET_ARRAY:
{ {
int32_t i; int32_t i;
@ -375,7 +378,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
for (i = 0; i < a->count; i++) for (i = 0; i < a->count; i++)
marshal_one(st, a->data[i], flags + 1); marshal_one(st, a->data[i], flags + 1);
} }
return; goto done;
case JANET_TUPLE: case JANET_TUPLE:
{ {
int32_t i, count; int32_t i, count;
@ -388,7 +391,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
/* Mark as seen AFTER marshaling */ /* Mark as seen AFTER marshaling */
MARK_SEEN(); MARK_SEEN();
} }
return; goto done;
case JANET_TABLE: case JANET_TABLE:
{ {
const JanetKV *kv = NULL; const JanetKV *kv = NULL;
@ -403,7 +406,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
marshal_one(st, kv->value, flags + 1); marshal_one(st, kv->value, flags + 1);
} }
} }
return; goto done;
case JANET_STRUCT: case JANET_STRUCT:
{ {
int32_t count; int32_t count;
@ -419,7 +422,7 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
/* Mark as seen AFTER marshaling */ /* Mark as seen AFTER marshaling */
MARK_SEEN(); MARK_SEEN();
} }
return; goto done;
case JANET_ABSTRACT: case JANET_ABSTRACT:
case JANET_CFUNCTION: case JANET_CFUNCTION:
goto noregval; goto noregval;
@ -433,19 +436,23 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
for (int32_t i = 0; i < func->def->environments_length; i++) for (int32_t i = 0; i < func->def->environments_length; i++)
marshal_one_env(st, func->envs[i], flags + 1); marshal_one_env(st, func->envs[i], flags + 1);
} }
return; goto done;
case JANET_FIBER: case JANET_FIBER:
{ {
pushbyte(st, LB_FIBER); pushbyte(st, LB_FIBER);
marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1); marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1);
} }
return; goto done;
default: default:
goto nyi; goto nyi;
} }
#undef MARK_SEEN #undef MARK_SEEN
done:
st->current = parent;
return;
/* Errors */ /* Errors */
nyi: nyi:
@ -455,7 +462,12 @@ noregval:
longjmp(st->err, MR_NRV); longjmp(st->err, MR_NRV);
} }
int janet_marshal(JanetBuffer *buf, Janet x, JanetTable *rreg, int flags) { int janet_marshal(
JanetBuffer *buf,
Janet x,
Janet *errval,
JanetTable *rreg,
int flags) {
int status; int status;
MarshalState st; MarshalState st;
st.buf = buf; st.buf = buf;
@ -463,9 +475,12 @@ int janet_marshal(JanetBuffer *buf, Janet x, JanetTable *rreg, int flags) {
st.seen_defs = NULL; st.seen_defs = NULL;
st.seen_envs = NULL; st.seen_envs = NULL;
st.rreg = rreg; st.rreg = rreg;
st.current = x;
janet_table_init(&st.seen, 0); janet_table_init(&st.seen, 0);
if (!(status = setjmp(st.err))) if (!(status = setjmp(st.err)))
marshal_one(&st, x, flags); marshal_one(&st, x, flags);
if (status && errval)
*errval = st.current;
janet_table_deinit(&st.seen); janet_table_deinit(&st.seen);
janet_v_free(st.seen_envs); janet_v_free(st.seen_envs);
janet_v_free(st.seen_defs); janet_v_free(st.seen_defs);
@ -1081,6 +1096,7 @@ static int cfun_env_lookup(JanetArgs args) {
static int cfun_marshal(JanetArgs args) { static int cfun_marshal(JanetArgs args) {
JanetBuffer *buffer; JanetBuffer *buffer;
JanetTable *rreg; JanetTable *rreg;
Janet err_param = janet_wrap_nil();
int status; int status;
JANET_MINARITY(args, 1); JANET_MINARITY(args, 1);
JANET_MAXARITY(args, 3); JANET_MAXARITY(args, 3);
@ -1096,9 +1112,13 @@ static int cfun_marshal(JanetArgs args) {
} else { } else {
buffer = janet_buffer(10); buffer = janet_buffer(10);
} }
status = janet_marshal(buffer, args.v[0], rreg, 0); status = janet_marshal(buffer, args.v[0], &err_param, rreg, 0);
if (status) { if (status) {
JANET_THROW(args, mr_strings[status]); const uint8_t *errstr = janet_formatc(
"%s for %V",
mr_strings[status],
err_param);
JANET_THROWV(args, janet_wrap_string(errstr));
} }
JANET_RETURN_BUFFER(args, buffer); JANET_RETURN_BUFFER(args, buffer);
} }

View File

@ -350,7 +350,7 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
case JANET_CFUNCTION: case JANET_CFUNCTION:
{ {
Janet check = janet_table_get(janet_vm_registry, x); Janet check = janet_table_get(janet_vm_registry, x);
if (janet_checktype(x, JANET_SYMBOL)) { if (janet_checktype(check, JANET_SYMBOL)) {
janet_buffer_push_cstring(buffer, "<cfunction "); janet_buffer_push_cstring(buffer, "<cfunction ");
janet_buffer_push_bytes(buffer, janet_buffer_push_bytes(buffer,
janet_unwrap_symbol(check), janet_unwrap_symbol(check),

View File

@ -1031,6 +1031,7 @@ JANET_API JanetCFunction janet_native(const char *name, const uint8_t **error);
JANET_API int janet_marshal( JANET_API int janet_marshal(
JanetBuffer *buf, JanetBuffer *buf,
Janet x, Janet x,
Janet *errval,
JanetTable *rreg, JanetTable *rreg,
int flags); int flags);
JANET_API int janet_unmarshal( JANET_API int janet_unmarshal(

0
test.lisp Normal file
View File