summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-07-11 03:25:26 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-07-11 03:25:26 +0000
commit412c5a96be804d33b322ffe6f55283b85c94b67e (patch)
treeb036f4a05ec235272ef8c668a445104a53bf85c3
parent64ef091a64d61dc0cef4508b70e64f91e4db865d (diff)
* vm_eval.c (rb_eval_string_from_file,
rb_eval_string_from_file_protect): new functions to replace rb_compile_main_from_string() and ruby_eval_main(). * nacl/pepper_ruby.c: Follows the change in vm_eval.c git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36363 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--nacl/pepper_main.c14
-rw-r--r--vm_eval.c57
3 files changed, 68 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index e74fa69989..420395088b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Mon Jul 9 16:11:30 2012 Yuki Yugui Sonoda <yugui@google.com>
+
+ * vm_eval.c (rb_eval_string_from_file,
+ rb_eval_string_from_file_protect): new functions to replace
+ rb_compile_main_from_string() and ruby_eval_main().
+
+ * nacl/pepper_ruby.c: Follows the change in vm_eval.c
+
Mon Jul 9 14:05:42 2012 Yuki Yugui Sonoda <yugui@google.com>
Reverts a half of r36079. As we discussed on ruby-dev@ and IRC,
diff --git a/nacl/pepper_main.c b/nacl/pepper_main.c
index dc7b58ce69..25087e995a 100644
--- a/nacl/pepper_main.c
+++ b/nacl/pepper_main.c
@@ -374,7 +374,7 @@ init_libraries(void* data)
volatile VALUE err = rb_errinfo();
err = rb_obj_as_string(err);
} else {
- instance->async_call_args = "rubyReady";
+ instance->async_call_args = (void*)"rubyReady";
core_interface->CallOnMainThread(
0, PP_MakeCompletionCallback(pruby_post_cstr, instance), 0);
}
@@ -419,11 +419,10 @@ pruby_init(void)
static void*
pruby_eval(void* data)
{
+ extern VALUE ruby_eval_string_from_file_protect(const char* src, const char* path, int* state);
struct PepperInstance* const instance = (struct PepperInstance*)data;
- volatile VALUE path;
volatile VALUE src = (VALUE)instance->async_call_args;
volatile VALUE result = Qnil;
- ruby_opaque_t prog;
volatile int state;
RUBY_INIT_STACK;
@@ -432,13 +431,8 @@ pruby_eval(void* data)
perror("pepper-ruby:pthread_mutex_lock");
return 0;
}
-
- path = rb_usascii_str_new_cstr("(pepper-ruby)");
- prog = ruby_compile_main_from_string(path, src, (VALUE*)&result);
- if (prog) {
- state = ruby_eval_main(prog, (VALUE*)&result);
- }
-
+ result = ruby_eval_string_from_file_protect(
+ RSTRING_PTR(src), "(pepper-ruby)", &state);
pthread_mutex_unlock(&instance->mutex);
if (!state) {
diff --git a/vm_eval.c b/vm_eval.c
index bb9ac8d8f6..ea52273d52 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -1157,18 +1157,73 @@ rb_f_eval(int argc, VALUE *argv, VALUE self)
return eval_string(self, src, scope, file, line);
}
+/** @note This function name is not stable. */
+VALUE
+ruby_eval_string_from_file(const char *str, const char *filename) {
+ return eval_string(rb_vm_top_self(), rb_str_new2(str), Qnil, filename, 1);
+}
+
+struct eval_string_from_file_arg {
+ const char *str;
+ const char *filename;
+};
+static VALUE
+eval_string_from_file_helper(void *data) {
+ const struct eval_string_from_file_arg *const arg = (struct eval_string_from_file_arg*)data;
+ return eval_string(rb_vm_top_self(), rb_str_new2(arg->str), Qnil, arg->filename, 1);
+}
+
+VALUE
+ruby_eval_string_from_file_protect(const char *str, const char *filename, int *state) {
+ struct eval_string_from_file_arg arg = { str, filename };
+ return rb_protect((VALUE (*)(VALUE))eval_string_from_file_helper, (VALUE)&arg, state);
+}
+
+/**
+ * Evaluates the given string in an isolated binding.
+ *
+ * Here "isolated" means the binding does not inherit any other binding. This
+ * behaves same as the binding for required libraries.
+ *
+ * __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
+ *
+ * @param str Ruby code to evaluate.
+ * @return The evaluated result.
+ * @throw Exception Raises an exception on error.
+ */
VALUE
rb_eval_string(const char *str)
{
- return eval_string(rb_vm_top_self(), rb_str_new2(str), Qnil, "(eval)", 1);
+ return ruby_eval_string_from_file(str, "eval");
}
+/**
+ * Evaluates the given string in an isolated binding.
+ *
+ * __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
+ *
+ * @sa rb_eval_string
+ * @param str Ruby code to evaluate.
+ * @param state Being set to zero if succeeded. Nonzero if an error occurred.
+ * @return The evaluated result if succeeded, an undefined value if otherwise.
+ */
VALUE
rb_eval_string_protect(const char *str, int *state)
{
return rb_protect((VALUE (*)(VALUE))rb_eval_string, (VALUE)str, state);
}
+/**
+ * Evaluates the given string under a module binding in an isolated binding.
+ * This is same as the binding for required libraries on "require('foo', true)".
+ *
+ * __FILE__ will be "(eval)", and __LINE__ starts from 1 in the evaluation.
+ *
+ * @sa rb_eval_string
+ * @param str Ruby code to evaluate.
+ * @param state Being set to zero if succeeded. Nonzero if an error occurred.
+ * @return The evaluated result if succeeded, an undefined value if otherwise.
+ */
VALUE
rb_eval_string_wrap(const char *str, int *state)
{