diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-11 03:25:26 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-11 03:25:26 +0000 |
commit | 412c5a96be804d33b322ffe6f55283b85c94b67e (patch) | |
tree | b036f4a05ec235272ef8c668a445104a53bf85c3 /vm_eval.c | |
parent | 64ef091a64d61dc0cef4508b70e64f91e4db865d (diff) | |
download | ruby-412c5a96be804d33b322ffe6f55283b85c94b67e.tar.gz |
* 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
Diffstat (limited to 'vm_eval.c')
-rw-r--r-- | vm_eval.c | 57 |
1 files changed, 56 insertions, 1 deletions
@@ -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) { |