]> git.siccegge.de Git - frida/frida.git/blob - src/bindings/Guile.cxx
Move Interpreter to its own class
[frida/frida.git] / src / bindings / Guile.cxx
1 #include "Guile.hxx"
2
3 namespace {
4 SCM handler (void*, SCM tag, SCM throw_args) {
5 scm_handle_by_message_noexit ((void*)"foo", tag, throw_args);
6 return SCM_BOOL_F;
7 }
8 }
9
10 GuileInterpreter::GuileInterpreter() {
11 scm_init_guile();
12
13 scm_internal_catch(SCM_BOOL_T,
14 (SCM (*)(void *))scm_c_eval_string,
15 (void*)"(use-modules (system repl server))",
16 handler, NULL);
17 scm_internal_catch(SCM_BOOL_T,
18 (SCM (*)(void *))scm_c_eval_string,
19 (void*)"(spawn-server)",
20 handler, NULL);
21
22 guile_output_port = scm_open_output_string();
23 guile_error_port = scm_open_output_string();
24 scm_set_current_output_port(guile_output_port);
25 scm_set_current_error_port(guile_error_port);
26 }
27
28 int GuileInterpreter::evaluate(const std::string& command,
29 std::ostream& stdout,
30 std::ostream& stderr,
31 std::string& result) {
32
33 SCM result_obj = scm_internal_catch(SCM_BOOL_T,
34 (SCM (*)(void *))scm_c_eval_string,
35 (void*)command.c_str(),
36 handler, NULL);
37
38 SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED);
39
40 SCM output = scm_get_output_string(guile_output_port);
41 stdout << scm_to_locale_string(output);
42
43 output = scm_get_output_string(guile_error_port);
44 stderr << scm_to_locale_string(output);
45
46 result = scm_to_locale_string(result_str);
47
48 scm_truncate_file(guile_output_port, scm_from_uint16(0));
49 scm_truncate_file(guile_error_port, scm_from_uint16(0));
50
51 return 0;
52 }