2018-02-07 05:44:51 +00:00
|
|
|
/*
|
2022-03-21 22:47:37 +00:00
|
|
|
* Copyright (c) 2022 Calvin Rose
|
2018-02-07 05:44:51 +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>
|
2022-03-22 00:41:06 +00:00
|
|
|
#include "state.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
/* Run a string */
|
2018-10-17 03:08:26 +00:00
|
|
|
int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetParser parser;
|
2019-02-27 18:09:19 +00:00
|
|
|
int errflags = 0, done = 0;
|
2018-02-07 05:44:51 +00:00
|
|
|
int32_t index = 0;
|
2018-10-17 03:08:26 +00:00
|
|
|
Janet ret = janet_wrap_nil();
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *where = sourcePath ? janet_cstring(sourcePath) : NULL;
|
2019-02-27 18:09:19 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
if (where) janet_gcroot(janet_wrap_string(where));
|
2019-01-31 04:07:30 +00:00
|
|
|
if (NULL == sourcePath) sourcePath = "<unknown>";
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_init(&parser);
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2019-02-27 18:09:19 +00:00
|
|
|
/* While we haven't seen an error */
|
|
|
|
while (!done) {
|
2019-01-04 01:44:58 +00:00
|
|
|
|
|
|
|
/* Evaluate parsed values */
|
|
|
|
while (janet_parser_has_more(&parser)) {
|
|
|
|
Janet form = janet_parser_produce(&parser);
|
|
|
|
JanetCompileResult cres = janet_compile(form, env, where);
|
|
|
|
if (cres.status == JANET_COMPILE_OK) {
|
|
|
|
JanetFunction *f = janet_thunk(cres.funcdef);
|
2019-01-12 22:31:15 +00:00
|
|
|
JanetFiber *fiber = janet_fiber(f, 64, 0, NULL);
|
2019-04-16 19:41:45 +00:00
|
|
|
fiber->env = env;
|
2019-01-04 01:44:58 +00:00
|
|
|
JanetSignal status = janet_continue(fiber, janet_wrap_nil(), &ret);
|
2020-04-17 21:27:02 +00:00
|
|
|
if (status != JANET_SIGNAL_OK && status != JANET_SIGNAL_EVENT) {
|
2021-12-06 14:51:40 +00:00
|
|
|
janet_stacktrace_ext(fiber, ret, "");
|
2019-01-04 01:44:58 +00:00
|
|
|
errflags |= 0x01;
|
2019-02-27 18:09:19 +00:00
|
|
|
done = 1;
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
2019-01-04 01:44:58 +00:00
|
|
|
} else {
|
2020-06-22 13:31:07 +00:00
|
|
|
ret = janet_wrap_string(cres.error);
|
2019-11-08 23:35:27 +00:00
|
|
|
if (cres.macrofiber) {
|
|
|
|
janet_eprintf("compile error in %s: ", sourcePath);
|
2021-12-06 14:51:40 +00:00
|
|
|
janet_stacktrace_ext(cres.macrofiber, ret, "");
|
2019-11-08 23:35:27 +00:00
|
|
|
} else {
|
|
|
|
janet_eprintf("compile error in %s: %s\n", sourcePath,
|
|
|
|
(const char *)cres.error);
|
|
|
|
}
|
2019-01-04 01:44:58 +00:00
|
|
|
errflags |= 0x02;
|
2019-02-27 18:09:19 +00:00
|
|
|
done = 1;
|
2019-01-04 01:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:31:07 +00:00
|
|
|
if (done) break;
|
|
|
|
|
2019-01-04 01:44:58 +00:00
|
|
|
/* Dispatch based on parse state */
|
|
|
|
switch (janet_parser_status(&parser)) {
|
2019-02-27 18:09:19 +00:00
|
|
|
case JANET_PARSE_DEAD:
|
|
|
|
done = 1;
|
|
|
|
break;
|
2020-06-22 13:31:07 +00:00
|
|
|
case JANET_PARSE_ERROR: {
|
|
|
|
const char *e = janet_parser_error(&parser);
|
2018-02-07 05:44:51 +00:00
|
|
|
errflags |= 0x04;
|
2020-06-22 13:31:07 +00:00
|
|
|
ret = janet_cstringv(e);
|
2022-09-03 19:03:51 +00:00
|
|
|
int32_t line = (int32_t) parser.line;
|
|
|
|
int32_t col = (int32_t) parser.column;
|
|
|
|
janet_eprintf("%s:%d:%d: parse error: %s\n", sourcePath, line, col, e);
|
2019-02-27 18:09:19 +00:00
|
|
|
done = 1;
|
2018-02-07 05:44:51 +00:00
|
|
|
break;
|
2020-06-22 13:31:07 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_PARSE_ROOT:
|
2020-06-22 13:31:07 +00:00
|
|
|
case JANET_PARSE_PENDING:
|
2018-05-16 02:03:45 +00:00
|
|
|
if (index >= len) {
|
2019-02-27 18:09:19 +00:00
|
|
|
janet_parser_eof(&parser);
|
2018-05-16 02:03:45 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_consume(&parser, bytes[index++]);
|
2018-05-16 02:03:45 +00:00
|
|
|
}
|
2018-02-07 05:44:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-04 01:44:58 +00:00
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
2019-02-27 18:09:19 +00:00
|
|
|
|
|
|
|
/* Clean up and return errors */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_deinit(&parser);
|
|
|
|
if (where) janet_gcunroot(janet_wrap_string(where));
|
2022-03-21 22:47:37 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
/* Enter the event loop if we are not already in it */
|
2022-03-22 00:41:06 +00:00
|
|
|
if (janet_vm.stackn == 0) {
|
2022-03-21 22:47:37 +00:00
|
|
|
janet_gcroot(ret);
|
|
|
|
janet_loop();
|
|
|
|
janet_gcunroot(ret);
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-17 03:08:26 +00:00
|
|
|
if (out) *out = ret;
|
2018-05-16 02:03:45 +00:00
|
|
|
return errflags;
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:08:26 +00:00
|
|
|
int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out) {
|
2018-02-07 05:44:51 +00:00
|
|
|
int32_t len = 0;
|
|
|
|
while (str[len]) ++len;
|
2018-10-17 03:08:26 +00:00
|
|
|
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 14:39:37 +00:00
|
|
|
/* Run a fiber to completion (use event loop if enabled). Return the status. */
|
|
|
|
int janet_loop_fiber(JanetFiber *fiber) {
|
|
|
|
int status;
|
|
|
|
#ifdef JANET_EV
|
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_loop();
|
|
|
|
status = janet_fiber_status(fiber);
|
|
|
|
#else
|
|
|
|
Janet out;
|
|
|
|
status = janet_continue(fiber, janet_wrap_nil(), &out);
|
|
|
|
if (status != JANET_SIGNAL_OK && status != JANET_SIGNAL_EVENT) {
|
2021-12-06 14:51:40 +00:00
|
|
|
janet_stacktrace_ext(fiber, out, "");
|
2021-07-18 14:39:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|