| 170 | #endif |
| 171 | |
| 172 | [[nodiscard]] |
| 173 | static bool gjs_console_readline(std::string* bufp, const char* prompt, |
| 174 | const char* repl_history_path |
| 175 | [[maybe_unused]]) { |
| 176 | #ifdef HAVE_READLINE_READLINE_H |
| 177 | g_assert(rl_async_done && "should not attempt two parallel readline calls"); |
| 178 | |
| 179 | rl_callback_handler_install(prompt, on_readline_complete); |
| 180 | rl_async_done = false; |
| 181 | |
| 182 | Gjs::AutoUnref<GInputStream> stdin_stream{ |
| 183 | g_unix_input_stream_new(fileno(rl_instream), /* close_fd = */ false)}; |
| 184 | Gjs::AutoUnref<GSource> stdin_source{g_pollable_input_stream_create_source( |
| 185 | G_POLLABLE_INPUT_STREAM(stdin_stream.get()), nullptr)}; |
| 186 | g_source_set_callback(stdin_source, G_SOURCE_FUNC(on_stdin_ready), nullptr, |
| 187 | nullptr); |
| 188 | |
| 189 | Gjs::AutoPointer<GMainContext, GMainContext, g_main_context_unref> |
| 190 | main_context{g_main_context_ref_thread_default()}; |
| 191 | unsigned tag = g_source_attach(stdin_source, main_context); |
| 192 | stdin_source.release(); |
| 193 | |
| 194 | while (!rl_async_done) { |
| 195 | // Yield to other things while waiting for input |
| 196 | while (g_main_context_pending(main_context)) |
| 197 | g_main_context_iteration(main_context, /* may_block = */ false); |
| 198 | } |
| 199 | |
| 200 | g_source_remove(tag); |
| 201 | |
| 202 | if (!rl_async_line) |
| 203 | return false; |
| 204 | |
| 205 | *bufp = rl_async_line.extract(); |
| 206 | |
| 207 | if ((*bufp)[0] != '\0') { |
| 208 | add_history(bufp->c_str()); |
| 209 | gjs_console_write_repl_history(repl_history_path); |
| 210 | } |
| 211 | #else // !HAVE_READLINE_READLINE_H |
| 212 | char line[256]; |
| 213 | fprintf(stdout, "%s", prompt); |
| 214 | fflush(stdout); |
| 215 | if (!fgets(line, sizeof line, stdin)) |
| 216 | return false; |
| 217 | *bufp = line; |
| 218 | #endif // !HAVE_READLINE_READLINE_H |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | std::string print_string_value(JSContext* cx, JS::HandleValue v_string) { |
| 223 | if (!v_string.isString()) |
no test coverage detected