| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2009 The Native Client Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can |
| 3 * be found in the LICENSE file. */ |
| 4 |
| 5 #include <stdio.h> |
| 6 #include <string.h> |
| 7 #include <nacl/nacl_srpc.h> |
| 8 #include "ruby-1.8.6-p368/ruby.h" |
| 9 |
| 10 int initialized = 0; |
| 11 |
| 12 char* INIT_RUBY_SCRIPT = |
| 13 "$stdout_default = $stdout \n" |
| 14 "class Logger \n" |
| 15 " def initialize() \n" |
| 16 " @strings = [] \n" |
| 17 " end \n" |
| 18 "\n" |
| 19 " def write(s) \n" |
| 20 " @strings.push(s) \n" |
| 21 " $stdout_default.write s \n" |
| 22 " end \n" |
| 23 "\n" |
| 24 " def get_string() \n" |
| 25 " current = @strings \n" |
| 26 " @strings = [] \n" |
| 27 " return current.join("") \n" |
| 28 " end \n" |
| 29 "end \n" |
| 30 "\n" |
| 31 "$stdout_logger = Logger.new \n" |
| 32 "$stdout = $stdout_logger \n"; |
| 33 |
| 34 /* |
| 35 * Evals Ruby expression and returns stdout updates. |
| 36 */ |
| 37 NaClSrpcError RubyEval(NaClSrpcChannel *channel, |
| 38 NaClSrpcArg **in_args, NaClSrpcArg **out_args) { |
| 39 |
| 40 /* TODO(krasin): MAKE IT THREAD-SAFE */ |
| 41 if (!initialized) { |
| 42 ruby_init(); |
| 43 rb_eval_string(INIT_RUBY_SCRIPT); |
| 44 initialized = 1; |
| 45 } |
| 46 rb_eval_string(in_args[0]->u.sval); |
| 47 VALUE result = rb_eval_string("$stdout_logger.get_string()"); |
| 48 |
| 49 /* |
| 50 * Strdup must be used because the SRPC layer frees the string passed to it. |
| 51 */ |
| 52 out_args[0]->u.sval = strdup(StringValuePtr(result)); |
| 53 |
| 54 return NACL_SRPC_RESULT_OK; |
| 55 } |
| 56 |
| 57 /* |
| 58 * Export the method as taking no arguments and returning one integer. |
| 59 */ |
| 60 NACL_SRPC_METHOD("rubyEval:s:s", RubyEval); |
| OLD | NEW |