@@ -0,0 +1,114 @@
1+// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+/*
4+ repl.c
5+ system startup, main(), and console interaction
6+*/
7+
8+#define JULIA_ENABLE_THREADING
9+
10+#include <assert.h>
11+#include <ctype.h>
12+#include <errno.h>
13+#include <inttypes.h>
14+#include <limits.h>
15+#include <math.h>
16+#include <setjmp.h>
17+#include <signal.h>
18+#include <stdarg.h>
19+#include <stdio.h>
20+#include <stdlib.h>
21+#include <string.h>
22+#include <sys/stat.h>
23+#include <sys/types.h>
24+#include <time.h>
25+
26+#include <julia.h>
27+
28+JULIA_DEFINE_FAST_TLS()
29+
30+void fancy_repl(void) {
31+ assert(jl_base_module && "oh no, we have no repl?");
32+
33+ jl_function_t *start_client =
34+ (jl_function_t *)jl_get_global(jl_base_module, jl_symbol("_start"));
35+
36+ JL_TRY {
37+ size_t last_age = jl_get_ptls_states()->world_age;
38+ jl_get_ptls_states()->world_age = jl_get_world_counter();
39+
40+ jl_apply(&start_client, 1);
41+
42+ jl_get_ptls_states()->world_age = last_age;
43+ }
44+ JL_CATCH { jl_no_exc_handler(jl_current_exception()); }
45+}
46+
47+static int exec_program(const char *program) {
48+ JL_TRY { jl_load(jl_main_module, program); }
49+ JL_CATCH {
50+ jl_value_t *errs = jl_stderr_obj();
51+ volatile int shown_err = 0;
52+ jl_printf(JL_STDERR, "error during bootstrap:\n");
53+ JL_TRY {
54+ if (errs) {
55+ jl_value_t *showf = jl_get_function(jl_base_module, "show");
56+ if (showf != NULL) {
57+ jl_call2(showf, errs, jl_current_exception());
58+ jl_printf(JL_STDERR, "\n");
59+ shown_err = 1;
60+ }
61+ }
62+ }
63+ JL_CATCH {}
64+ if (!shown_err) {
65+ jl_static_show(JL_STDERR, jl_current_exception());
66+ jl_printf(JL_STDERR, "\n");
67+ }
68+ jlbacktrace();
69+ jl_printf(JL_STDERR, "\n");
70+ return 1;
71+ }
72+ return 0;
73+}
74+
75+const char *get_banner(void) {
76+ jl_value_t *ret = jl_eval_string("io = IOBuffer(); Base.banner(IOContext(io, "
77+ ":color => true)); String(take!(io))");
78+
79+ assert(jl_typeis(ret, jl_string_type) && "banner should be a string");
80+
81+ return jl_string_ptr(ret);
82+}
83+
84+void setup() {
85+ jl_init();
86+ libsupport_init();
87+
88+ jl_options.banner = 0;
89+ jl_options.color = 1;
90+
91+ jl_eval_string("import REPL");
92+ jl_set_global(jl_base_module, jl_symbol("replit_prompt"),
93+ jl_cstr_to_string("julia> "));
94+ jl_eval_string(
95+ "atreplinit(function (r)"
96+ "r.interface = REPL.setup_interface(r, true, r.options.extra_keymap); "
97+ "r.interface.modes[1].prompt=Base.replit_prompt;"
98+ "r.interface.modes[2].prompt=\"shell> \";"
99+ "r.interface.modes[3].prompt=\"help?> \";"
100+ "end)");
101+}
102+
103+void cleanup() { jl_atexit_hook(0); }
104+
105+void eval(const char *str) { jl_eval_string(str); }
106+
107+void eval_file(const char *path) { exec_program(path); }
108+
109+void run_repl() { fancy_repl(); }
110+
111+void set_prompt(const char *prompt) {
112+ jl_set_global(jl_base_module, jl_symbol("replit_prompt"),
113+ jl_cstr_to_string(prompt));
114+}