Support REPL for Linux
JerryScript-DCO-1.0-Signed-off-by: Hanjoung Lee hanjoung.lee@samsung.com
This commit is contained in:
committed by
László Langó
parent
91a0514fab
commit
0e88e819eb
+75
-4
@@ -219,6 +219,8 @@ main (int argc,
|
|||||||
bool is_dump_snapshot_mode_for_global_or_eval = false;
|
bool is_dump_snapshot_mode_for_global_or_eval = false;
|
||||||
const char *dump_snapshot_file_name_p = NULL;
|
const char *dump_snapshot_file_name_p = NULL;
|
||||||
|
|
||||||
|
bool is_repl_mode = false;
|
||||||
|
|
||||||
#ifdef JERRY_ENABLE_LOG
|
#ifdef JERRY_ENABLE_LOG
|
||||||
const char *log_file_name = NULL;
|
const char *log_file_name = NULL;
|
||||||
#endif /* JERRY_ENABLE_LOG */
|
#endif /* JERRY_ENABLE_LOG */
|
||||||
@@ -341,10 +343,9 @@ main (int argc,
|
|||||||
if (files_counter == 0
|
if (files_counter == 0
|
||||||
&& exec_snapshots_count == 0)
|
&& exec_snapshots_count == 0)
|
||||||
{
|
{
|
||||||
return JERRY_STANDALONE_EXIT_CODE_OK;
|
is_repl_mode = true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
#ifdef JERRY_ENABLE_LOG
|
#ifdef JERRY_ENABLE_LOG
|
||||||
if (log_file_name)
|
if (log_file_name)
|
||||||
{
|
{
|
||||||
@@ -462,6 +463,77 @@ main (int argc,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_repl_mode)
|
||||||
|
{
|
||||||
|
const char *prompt = "jerry> ";
|
||||||
|
bool is_done = false;
|
||||||
|
|
||||||
|
jerry_api_object_t *global_obj_p = jerry_api_get_global ();
|
||||||
|
jerry_api_value_t print_function;
|
||||||
|
|
||||||
|
if (!jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "print", &print_function))
|
||||||
|
{
|
||||||
|
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jerry_api_is_function (print_function.v_object))
|
||||||
|
{
|
||||||
|
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!is_done)
|
||||||
|
{
|
||||||
|
uint8_t *source_buffer_tail = buffer;
|
||||||
|
size_t len = 0;
|
||||||
|
|
||||||
|
printf ("%s", prompt);
|
||||||
|
|
||||||
|
// input a line
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (fread (source_buffer_tail, 1, 1, stdin) != 1 && len == 0)
|
||||||
|
{
|
||||||
|
is_done = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (*source_buffer_tail == '\n')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
source_buffer_tail ++;
|
||||||
|
len ++;
|
||||||
|
}
|
||||||
|
*source_buffer_tail = 0;
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
// evaluate the line
|
||||||
|
jerry_api_value_t ret_val;
|
||||||
|
ret_code = jerry_api_eval (buffer, len, false, false, &ret_val);
|
||||||
|
|
||||||
|
if (ret_code == JERRY_COMPLETION_CODE_OK)
|
||||||
|
{
|
||||||
|
// print return value
|
||||||
|
const jerry_api_value_t args[] = {ret_val};
|
||||||
|
jerry_api_value_t ret_val_print;
|
||||||
|
if (jerry_api_call_function (print_function.v_object, NULL, &ret_val_print, args, 1))
|
||||||
|
{
|
||||||
|
jerry_api_release_value (&ret_val_print);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf ("JERRY_COMPLETION_CODE_UNHANDLED_EXCEPTION\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
jerry_api_release_value (&ret_val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jerry_api_release_object (global_obj_p);
|
||||||
|
jerry_api_release_value (&print_function);
|
||||||
|
}
|
||||||
|
|
||||||
jerry_cleanup ();
|
jerry_cleanup ();
|
||||||
|
|
||||||
#ifdef JERRY_ENABLE_LOG
|
#ifdef JERRY_ENABLE_LOG
|
||||||
@@ -480,5 +552,4 @@ main (int argc,
|
|||||||
{
|
{
|
||||||
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
return JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} /* main */
|
} /* main */
|
||||||
|
|||||||
Reference in New Issue
Block a user