Make stacktrace part of public API.

This commit is contained in:
Calvin Rose 2018-09-09 21:20:33 -04:00
parent 56c3b8aa94
commit 1ba3aeb3cd
3 changed files with 29 additions and 6 deletions

View File

@ -24,7 +24,7 @@
#include "state.h"
/* Error reporting */
static void print_error_report(JanetFiber *fiber, const char *errtype, Janet err) {
void janet_stacktrace(JanetFiber *fiber, const char *errtype, Janet err) {
const char *errstr = (const char *)janet_to_string(err);
printf("%s error: %s\n", errtype, errstr);
if (!fiber) return;
@ -34,20 +34,20 @@ static void print_error_report(JanetFiber *fiber, const char *errtype, Janet err
JanetFuncDef *def = NULL;
i = frame->prevframe;
printf(" at");
printf(" in");
if (frame->func) {
def = frame->func->def;
printf(" %s", def->name ? (const char *)def->name : "<anonymous>");
if (def->source) {
printf(" %s", (const char *)def->source);
printf(" [%s]", (const char *)def->source);
}
} else {
JanetCFunction cfun = (JanetCFunction)(frame->pc);
if (cfun) {
Janet name = janet_table_get(janet_vm_registry, janet_wrap_cfunction(cfun));
if (!janet_checktype(name, JANET_NIL))
printf(" [%s]", (const char *)janet_to_string(name));
printf(" %s", (const char *)janet_to_string(name));
}
}
if (frame->flags & JANET_STACKFRAME_TAILCALL)
@ -88,11 +88,11 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char
Janet ret = janet_wrap_nil();
JanetSignal status = janet_run(fiber, &ret);
if (status != JANET_SIGNAL_OK) {
print_error_report(fiber, "runtime", ret);
janet_stacktrace(fiber, "runtime", ret);
errflags |= 0x01;
}
} else {
print_error_report(cres.macrofiber, "compile",
janet_stacktrace(cres.macrofiber, "compile",
janet_wrap_string(cres.error));
errflags |= 0x02;
}

View File

@ -139,6 +139,18 @@ Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key) {
return janet_wrap_nil();
}
/* Iterate through a struct or dictionary generically */
const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv) {
const JanetKV *end = kvs + cap;
kv = (kv == NULL) ? kvs : kv + 1;
while (kv < end) {
if (!janet_checktype(kv->key, JANET_NIL))
return kv;
kv++;
}
return NULL;
}
/* Compare a janet string with a cstring. more efficient than loading
* c string as a janet string. */
int janet_cstrcmp(const uint8_t *str, const char *other) {

View File

@ -1002,6 +1002,7 @@ JANET_API int janet_indexed_view(Janet seq, const Janet **data, int32_t *len);
JANET_API int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len);
JANET_API int janet_dictionary_view(Janet tab, const JanetKV **data, int32_t *len, int32_t *cap);
JANET_API Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key);
JANET_API const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv);
/* Abstract */
#define janet_abstract_header(u) ((JanetAbstractHeader *)(u) - 1)
@ -1012,6 +1013,15 @@ JANET_API void *janet_abstract(const JanetAbstractType *type, size_t size);
/* Native */
JANET_API JanetCFunction janet_native(const char *name, const uint8_t **error);
/* Marshaling */
JANET_API int janet_marshal(JanetBuffer *buf, Janet x, int flags);
JANET_API int janet_unmarshal(
const uint8_t *bytes,
size_t len,
int flags,
Janet *out,
const uint8_t **next);
/* GC */
JANET_API void janet_mark(Janet x);
JANET_API void janet_sweep(void);
@ -1040,6 +1050,7 @@ JANET_API void janet_deinit(void);
JANET_API JanetSignal janet_continue(JanetFiber *fiber, Janet in, Janet *out);
#define janet_run(F,O) janet_continue(F, janet_wrap_nil(), O)
JANET_API JanetSignal janet_call(JanetFunction *fun, int32_t argn, const Janet *argv, Janet *out, JanetFiber **f);
JANET_API void janet_stacktrace(JanetFiber *fiber, const char *errtype, Janet err);
/* C Library helpers */
typedef enum {