1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Fix issue with compilation with source name.

Also add tuple/sourcemap and tuple/setmap.
This commit is contained in:
Calvin Rose 2019-05-20 04:02:38 -04:00
parent 97ad4c4f89
commit 55d8e8b56b
4 changed files with 42 additions and 12 deletions

View File

@ -197,16 +197,15 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
case JANET_STRING: case JANET_STRING:
janet_escape_string_b(buffer, janet_unwrap_string(x)); janet_escape_string_b(buffer, janet_unwrap_string(x));
return; return;
case JANET_BUFFER: case JANET_BUFFER: {
{ JanetBuffer *b = janet_unwrap_buffer(x);
JanetBuffer *b = janet_unwrap_buffer(x); if (b == buffer) {
if (b == buffer) { /* Ensures buffer won't resize while escaping */
/* Ensures buffer won't resize while escaping */ janet_buffer_ensure(b, 5 * b->count + 3, 1);
janet_buffer_ensure(b, 5 * b->count + 3, 1);
}
janet_escape_buffer_b(buffer, b);
return;
} }
janet_escape_buffer_b(buffer, b);
return;
}
case JANET_ABSTRACT: { case JANET_ABSTRACT: {
void *p = janet_unwrap_abstract(x); void *p = janet_unwrap_abstract(x);
const JanetAbstractType *at = janet_abstract_type(p); const JanetAbstractType *at = janet_abstract_type(p);

View File

@ -174,7 +174,7 @@ static int destructure(JanetCompiler *c,
/* Create a source map for definitions. */ /* Create a source map for definitions. */
static const Janet *janetc_make_sourcemap(JanetCompiler *c) { static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
Janet *tup = janet_tuple_begin(3); 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[1] = janet_wrap_integer(c->current_mapping.start);
tup[2] = janet_wrap_integer(c->current_mapping.end); tup[2] = janet_wrap_integer(c->current_mapping.end);
return janet_tuple_end(tup); return janet_tuple_end(tup);

View File

@ -290,8 +290,8 @@ static Janet cfun_string_hassuffix(int32_t argc, Janet *argv) {
return str.len < suffix.len return str.len < suffix.len
? janet_wrap_false() ? janet_wrap_false()
: janet_wrap_boolean(memcmp(suffix.bytes, : janet_wrap_boolean(memcmp(suffix.bytes,
str.bytes + str.len - suffix.len, str.bytes + str.len - suffix.len,
suffix.len) == 0); suffix.len) == 0);
} }
static Janet cfun_string_findall(int32_t argc, Janet *argv) { static Janet cfun_string_findall(int32_t argc, Janet *argv) {

View File

@ -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[] = { static const JanetReg tuple_cfuns[] = {
{ {
"tuple/brackets", cfun_tuple_brackets, "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 time, but will print differently and be treated differently by "
"the compiler.") "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} {NULL, NULL, NULL}
}; };