first commit
CI / Build (clang, clang++, ubuntu-22.04, linux, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Build (clang, clang++, windows-2022, windows, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Release (push) Canceled after 0s

This commit is contained in:
2026-09-13 22:18:11 +01:00
commit 08c03ac533
6701 changed files with 1577314 additions and 0 deletions
@@ -0,0 +1,7 @@
MRuby::Gem::Specification.new('mruby-eval') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'standard Kernel#eval method'
add_dependency 'mruby-compiler', :core => 'mruby-compiler'
end
+188
View File
@@ -0,0 +1,188 @@
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/compile.h>
#include <mruby/irep.h>
#include <mruby/proc.h>
#include <mruby/opcode.h>
#include <mruby/error.h>
mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
void mrb_codedump_all(mrb_state*, struct RProc*);
static struct RProc*
create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
{
mrbc_context *cxt;
struct mrb_parser_state *p;
struct RProc *proc;
struct REnv *e;
mrb_callinfo *ci; /* callinfo of eval caller */
struct RClass *target_class = NULL;
int bidx;
if (!mrb_nil_p(binding)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
}
cxt = mrbc_context_new(mrb);
cxt->lineno = (uint16_t)line;
mrbc_filename(mrb, cxt, file ? file : "(eval)");
cxt->capture_errors = TRUE;
cxt->no_optimize = TRUE;
ci = (mrb->c->ci > mrb->c->cibase) ? mrb->c->ci - 1 : mrb->c->cibase;
cxt->upper = ci->proc && MRB_PROC_CFUNC_P(ci->proc) ? NULL : ci->proc;
p = mrb_parse_nstring(mrb, s, len, cxt);
/* only occur when memory ran out */
if (!p) {
mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state.");
}
if (0 < p->nerr) {
/* parse error */
mrb_value str;
if (file) {
str = mrb_format(mrb, "file %s line %d: %s",
file,
p->error_buffer[0].lineno,
p->error_buffer[0].message);
}
else {
str = mrb_format(mrb, "line %d: %s",
p->error_buffer[0].lineno,
p->error_buffer[0].message);
}
mrb_parser_free(p);
mrbc_context_free(mrb, cxt);
mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str));
}
proc = mrb_generate_code(mrb, p);
if (proc == NULL) {
/* codegen error */
mrb_parser_free(p);
mrbc_context_free(mrb, cxt);
mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
}
if (mrb->c->ci > mrb->c->cibase) {
ci = &mrb->c->ci[-1];
}
else {
ci = mrb->c->cibase;
}
if (ci->proc) {
target_class = MRB_PROC_TARGET_CLASS(ci->proc);
}
if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
if (ci->env) {
e = ci->env;
}
else {
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
(struct RClass*)target_class);
e->mid = ci->mid;
e->stack = ci[1].stackent;
e->cxt = mrb->c;
MRB_ENV_SET_LEN(e, ci->proc->body.irep->nlocals);
bidx = ci->argc;
if (ci->argc < 0) bidx = 2;
else bidx += 1;
MRB_ENV_SET_BIDX(e, bidx);
ci->env = e;
}
proc->e.env = e;
proc->flags |= MRB_PROC_ENVSET;
mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
}
proc->upper = ci->proc;
mrb->c->ci->target_class = target_class;
/* mrb_codedump_all(mrb, proc); */
mrb_parser_free(p);
mrbc_context_free(mrb, cxt);
return proc;
}
static mrb_value
exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
{
/* no argument passed from eval() */
mrb->c->ci->argc = 0;
if (mrb->c->ci->acc < 0) {
ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase;
mrb_value ret = mrb_top_run(mrb, proc, self, 0);
if (mrb->exc) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
}
mrb->c->ci = mrb->c->cibase + cioff;
return ret;
}
/* clear block */
mrb->c->stack[1] = mrb_nil_value();
return mrb_exec_irep(mrb, self, proc);
}
static mrb_value
f_eval(mrb_state *mrb, mrb_value self)
{
char *s;
mrb_int len;
mrb_value binding = mrb_nil_value();
char *file = NULL;
mrb_int line = 1;
struct RProc *proc;
mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);
proc = create_proc_from_string(mrb, s, len, binding, file, line);
mrb_assert(!MRB_PROC_CFUNC_P(proc));
return exec_irep(mrb, self, proc);
}
static mrb_value
f_instance_eval(mrb_state *mrb, mrb_value self)
{
mrb_value b;
mrb_int argc; mrb_value *argv;
mrb_get_args(mrb, "*!&", &argv, &argc, &b);
if (mrb_nil_p(b)) {
char *s;
mrb_int len;
char *file = NULL;
mrb_int line = 1;
mrb_value cv;
struct RProc *proc;
mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
cv = mrb_singleton_class(mrb, self);
proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
mrb_assert(!MRB_PROC_CFUNC_P(proc));
mrb->c->ci->target_class = mrb_class_ptr(cv);
return exec_irep(mrb, self, proc);
}
else {
mrb_get_args(mrb, "&", &b);
return mrb_obj_instance_eval(mrb, self);
}
}
void
mrb_mruby_eval_gem_init(mrb_state* mrb)
{
mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
}
void
mrb_mruby_eval_gem_final(mrb_state* mrb)
{
}
@@ -0,0 +1,153 @@
assert('Kernel.eval', '15.3.1.2.3') do
assert_equal(10) { Kernel.eval '1 * 10' }
assert_equal('aaa') { Kernel.eval "'a' * 3" }
assert_equal(10) {
a = 10
Kernel.eval "a"
}
assert_equal(20) {
a = 10
Kernel.eval "a = 20"
a
}
assert_equal(15) {
c = 5
lambda {
a = 10
Kernel.eval "c = a + c"
}.call
c
}
assert_equal(5) {
c = 5
lambda {
Kernel.eval 'lambda { c }.call'
}.call
}
assert_equal(15) {
c = 5
lambda {
a = 10
Kernel.eval 'lambda { c = a + c }.call'
}.call
c
}
assert_equal(2) {
a = 10
Kernel.eval 'def f(a); b=a+1; end'
f(1)
}
end
assert('Kernel#eval', '15.3.1.3.12') do
assert_equal(10) { eval '1 * 10' }
end
assert('rest arguments of eval') do
assert_raise(ArgumentError) { Kernel.eval('0', 0, 'test', 0) }
assert_equal ['test', 'test.rb', 10] do
Kernel.eval('[\'test\', __FILE__, __LINE__]', nil, 'test.rb', 10)
end
end
assert 'eval syntax error' do
assert_raise(SyntaxError) do
eval 'p "test'
end
end
assert('String instance_eval') do
obj = Object.new
obj.instance_eval{ @test = 'test' }
assert_raise(ArgumentError) { obj.instance_eval(0) { } }
assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') }
assert_equal(['test.rb', 10]) { obj.instance_eval('[__FILE__, __LINE__]', 'test.rb', 10)}
assert_equal('test') { obj.instance_eval('@test') }
assert_equal('test') { obj.instance_eval { @test } }
o = Object.new
assert_equal ['', o, o], o.instance_eval("[''].each { |s| break [s, o, self] }")
end
assert('Kernel.#eval(string) context') do
class TestEvalConstScope
EVAL_CONST_CLASS = 'class'
def const_string
eval 'EVAL_CONST_CLASS'
end
end
obj = TestEvalConstScope.new
assert_raise(NameError) { eval 'EVAL_CONST_CLASS' }
assert_equal('class') { obj.const_string }
end
assert('BasicObject#instance_eval with begin-rescue-ensure execution order') do
class HellRaiser
def raise_hell
order = [:enter_raise_hell]
begin
order.push :begin
self.instance_eval("raise 'error'")
rescue
order.push :rescue
ensure
order.push :ensure
end
order
end
end
hell_raiser = HellRaiser.new
assert_equal([:enter_raise_hell, :begin, :rescue, :ensure], hell_raiser.raise_hell)
end
assert('BasicObject#instance_eval to define singleton methods Issue #3141') do
foo_class = Class.new do
def bar(x)
instance_eval "def baz; #{x}; end"
end
end
f1 = foo_class.new
f2 = foo_class.new
f1.bar 1
f2.bar 2
assert_equal(1){f1.baz}
assert_equal(2){f2.baz}
end
assert('Kernel.#eval(string) Issue #4021') do
assert_equal('FOO') { (eval <<'EOS').call }
foo = "FOO"
Proc.new { foo }
EOS
assert_equal('FOO') {
def do_eval(code)
eval(code)
end
do_eval(<<'EOS').call
foo = "FOO"
Proc.new { foo }
EOS
}
end
assert('Calling the same method as the variable name') do
hoge = Object.new
def hoge.fuga
"Hit!"
end
assert_equal("Hit!") { fuga = "Miss!"; eval "hoge.fuga" }
assert_equal("Hit!") { fuga = "Miss!"; -> { eval "hoge.fuga" }.call }
assert_equal("Hit!") { -> { fuga = "Miss!"; eval "hoge.fuga" }.call }
assert_equal("Hit!") { fuga = "Miss!"; eval("-> { hoge.fuga }").call }
end
assert('Access numbered parameter from eval') do
hoge = Object.new
def hoge.fuga(a, &b)
b.call(a)
end
assert_equal(6) {
hoge.fuga(3) { _1 + eval("_1") }
}
end