diff --git a/src/core/pp.c b/src/core/pp.c index 8aff7e44..e36986ac 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -197,16 +197,15 @@ void janet_description_b(JanetBuffer *buffer, Janet x) { case JANET_STRING: janet_escape_string_b(buffer, janet_unwrap_string(x)); return; - case JANET_BUFFER: - { - JanetBuffer *b = janet_unwrap_buffer(x); - if (b == buffer) { - /* Ensures buffer won't resize while escaping */ - janet_buffer_ensure(b, 5 * b->count + 3, 1); - } - janet_escape_buffer_b(buffer, b); - return; + case JANET_BUFFER: { + JanetBuffer *b = janet_unwrap_buffer(x); + if (b == buffer) { + /* Ensures buffer won't resize while escaping */ + janet_buffer_ensure(b, 5 * b->count + 3, 1); } + janet_escape_buffer_b(buffer, b); + return; + } case JANET_ABSTRACT: { void *p = janet_unwrap_abstract(x); const JanetAbstractType *at = janet_abstract_type(p); diff --git a/src/core/specials.c b/src/core/specials.c index 252a1407..addd63c6 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -174,7 +174,7 @@ static int destructure(JanetCompiler *c, /* Create a source map for definitions. */ static const Janet *janetc_make_sourcemap(JanetCompiler *c) { Janet *tup = janet_tuple_begin(3); - tup[0] = janet_wrap_string(c->source); + tup[0] = c->source ? janet_wrap_string(c->source) : janet_wrap_nil(); tup[1] = janet_wrap_integer(c->current_mapping.start); tup[2] = janet_wrap_integer(c->current_mapping.end); return janet_tuple_end(tup); diff --git a/src/core/string.c b/src/core/string.c index 3ac59775..dc0d67f2 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -290,8 +290,8 @@ static Janet cfun_string_hassuffix(int32_t argc, Janet *argv) { return str.len < suffix.len ? janet_wrap_false() : janet_wrap_boolean(memcmp(suffix.bytes, - str.bytes + str.len - suffix.len, - suffix.len) == 0); + str.bytes + str.len - suffix.len, + suffix.len) == 0); } static Janet cfun_string_findall(int32_t argc, Janet *argv) { diff --git a/src/core/tuple.c b/src/core/tuple.c index 64a058ba..9d367155 100644 --- a/src/core/tuple.c +++ b/src/core/tuple.c @@ -115,6 +115,23 @@ static Janet cfun_tuple_type(int32_t argc, Janet *argv) { } } +static Janet cfun_tuple_sourcemap(int32_t argc, Janet *argv) { + janet_fixarity(argc, 1); + const Janet *tup = janet_gettuple(argv, 0); + Janet contents[2]; + contents[0] = janet_wrap_integer(janet_tuple_head(tup)->sm_start); + contents[1] = janet_wrap_integer(janet_tuple_head(tup)->sm_end); + return janet_wrap_tuple(janet_tuple_n(contents, 2)); +} + +static Janet cfun_tuple_setmap(int32_t argc, Janet *argv) { + janet_fixarity(argc, 3); + const Janet *tup = janet_gettuple(argv, 0); + janet_tuple_head(tup)->sm_start = janet_getinteger(argv, 1); + janet_tuple_head(tup)->sm_end = janet_getinteger(argv, 2); + return argv[0]; +} + static const JanetReg tuple_cfuns[] = { { "tuple/brackets", cfun_tuple_brackets, @@ -138,6 +155,20 @@ static const JanetReg tuple_cfuns[] = { "the time, but will print differently and be treated differently by " "the compiler.") }, + { + "tuple/sourcemap", cfun_tuple_sourcemap, + JDOC("(tuple/sourcemap tup)\n\n" + "Returns the sourcemap metadata attached to a tuple. " + "The mapping is represented by a pair of byte offsets into the " + "the source code representing the start and end byte indices where " + "the tuple is. ") + }, + { + "tuple/setmap", cfun_tuple_setmap, + JDOC("(tuple/setmap tup start end)\n\n" + "Set the sourcemap metadata on a tuple. start and end should " + "be integers representing byte offsets into the file. Returns tup.") + }, {NULL, NULL, NULL} };