]> git.siccegge.de Git - frida/frida.git/blobdiff - src/bindings/Guile.cxx
Move Interpreter to its own class
[frida/frida.git] / src / bindings / Guile.cxx
diff --git a/src/bindings/Guile.cxx b/src/bindings/Guile.cxx
new file mode 100644 (file)
index 0000000..2552e7d
--- /dev/null
@@ -0,0 +1,52 @@
+#include "Guile.hxx"
+
+namespace {
+       SCM handler (void*, SCM tag, SCM throw_args) {
+               scm_handle_by_message_noexit ((void*)"foo", tag, throw_args);
+               return SCM_BOOL_F;
+       }
+}
+
+GuileInterpreter::GuileInterpreter() {
+       scm_init_guile();
+
+       scm_internal_catch(SCM_BOOL_T,
+                          (SCM (*)(void *))scm_c_eval_string,
+                          (void*)"(use-modules (system repl server))",
+                          handler, NULL);
+       scm_internal_catch(SCM_BOOL_T,
+                          (SCM (*)(void *))scm_c_eval_string,
+                          (void*)"(spawn-server)",
+                          handler, NULL);
+
+       guile_output_port = scm_open_output_string();
+       guile_error_port = scm_open_output_string();
+       scm_set_current_output_port(guile_output_port);
+       scm_set_current_error_port(guile_error_port);
+}
+
+int GuileInterpreter::evaluate(const std::string& command,
+                               std::ostream& stdout,
+                               std::ostream& stderr,
+                               std::string& result) {
+
+       SCM result_obj = scm_internal_catch(SCM_BOOL_T,
+                                           (SCM (*)(void *))scm_c_eval_string,
+                                           (void*)command.c_str(),
+                                           handler, NULL);
+
+       SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED);
+
+       SCM output = scm_get_output_string(guile_output_port);
+       stdout << scm_to_locale_string(output);
+
+       output =  scm_get_output_string(guile_error_port);
+       stderr << scm_to_locale_string(output);
+
+       result = scm_to_locale_string(result_str);
+
+       scm_truncate_file(guile_output_port, scm_from_uint16(0));
+       scm_truncate_file(guile_error_port, scm_from_uint16(0));
+
+       return 0;
+}