2018-10-17 03:08:26 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2018-10-17 03:08:26 +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-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-10-17 03:08:26 +00:00
|
|
|
#include <emscripten.h>
|
|
|
|
|
2018-12-06 19:30:11 +00:00
|
|
|
extern const unsigned char *janet_gen_webinit;
|
2018-12-08 22:20:24 +00:00
|
|
|
extern int32_t janet_gen_webinit_size;
|
2018-12-06 19:30:11 +00:00
|
|
|
|
2018-10-17 03:08:26 +00:00
|
|
|
static JanetFiber *repl_fiber = NULL;
|
|
|
|
static JanetBuffer *line_buffer = NULL;
|
|
|
|
static const uint8_t *line_prompt = NULL;
|
|
|
|
|
|
|
|
/* Yield to JS event loop from janet. Takes a repl prompt
|
|
|
|
* and a buffer to fill with input data. */
|
2019-01-06 01:45:24 +00:00
|
|
|
static Janet repl_yield(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
line_prompt = janet_getstring(argv, 0);
|
|
|
|
line_buffer = janet_getbuffer(argv, 1);
|
|
|
|
return janet_wrap_nil();
|
2018-10-17 03:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-enter the loop */
|
|
|
|
static int enter_loop(void) {
|
|
|
|
Janet ret;
|
|
|
|
JanetSignal status = janet_continue(repl_fiber, janet_wrap_nil(), &ret);
|
|
|
|
if (status == JANET_SIGNAL_ERROR) {
|
2019-01-31 18:02:09 +00:00
|
|
|
janet_stacktrace(repl_fiber, ret);
|
2018-10-17 03:08:26 +00:00
|
|
|
janet_deinit();
|
|
|
|
repl_fiber = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Allow JS interoperation from within janet */
|
2019-01-06 01:45:24 +00:00
|
|
|
static Janet cfun_js(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetByteView bytes = janet_getbytes(argv, 0);
|
|
|
|
emscripten_run_script((const char *)bytes.bytes);
|
|
|
|
return janet_wrap_nil();
|
2018-10-17 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Initialize the repl */
|
2018-10-17 03:08:26 +00:00
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
void repl_init(void) {
|
|
|
|
int status;
|
|
|
|
JanetTable *env;
|
|
|
|
|
|
|
|
/* Set up VM */
|
|
|
|
janet_init();
|
2018-11-26 14:02:07 +00:00
|
|
|
janet_register("repl-yield", repl_yield);
|
|
|
|
janet_register("js", cfun_js);
|
2019-03-11 03:06:10 +00:00
|
|
|
env = janet_core_env(NULL);
|
2018-10-17 03:08:26 +00:00
|
|
|
|
2018-11-15 20:45:41 +00:00
|
|
|
janet_def(env, "repl-yield", janet_wrap_cfunction(repl_yield), NULL);
|
|
|
|
janet_def(env, "js", janet_wrap_cfunction(cfun_js), NULL);
|
2018-10-17 19:19:54 +00:00
|
|
|
|
2018-10-17 03:08:26 +00:00
|
|
|
/* Run startup script */
|
|
|
|
Janet ret;
|
2018-12-06 19:30:11 +00:00
|
|
|
status = janet_dobytes(env, janet_gen_webinit, janet_gen_webinit_size, "webinit.janet", &ret);
|
2018-10-17 03:08:26 +00:00
|
|
|
if (status == JANET_SIGNAL_ERROR) {
|
|
|
|
printf("start up error.\n");
|
|
|
|
janet_deinit();
|
|
|
|
repl_fiber = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
janet_gcroot(ret);
|
|
|
|
repl_fiber = janet_unwrap_fiber(ret);
|
|
|
|
|
|
|
|
/* Start repl */
|
|
|
|
if (enter_loop()) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deinitialize the repl */
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
void repl_deinit(void) {
|
|
|
|
if (!repl_fiber) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
repl_fiber = NULL;
|
|
|
|
line_buffer = NULL;
|
|
|
|
janet_deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the prompt to show in the repl */
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
const char *repl_prompt(void) {
|
|
|
|
return line_prompt ? ((const char *)line_prompt) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart the repl calling from JS. Pass in the input for the next line. */
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
void repl_input(char *input) {
|
|
|
|
|
|
|
|
/* Create the repl if we haven't yet */
|
|
|
|
if (!repl_fiber) {
|
|
|
|
printf("initialize the repl first");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now fill the pending line_buffer and resume the repl loop */
|
|
|
|
if (line_buffer) {
|
|
|
|
janet_buffer_push_cstring(line_buffer, input);
|
|
|
|
line_buffer = NULL;
|
|
|
|
enter_loop();
|
|
|
|
}
|
|
|
|
}
|