140 Commits

Author SHA1 Message Date
dearblue cf20687ee7 mruby-eval: unify f_instance_eval and f_class_eval
Their bodies were nearly identical: same argument parsing,
same proc creation, same target-class plumbing. The only
differences are which method to delegate to in the block-given
case (mrb_obj_instance_eval vs. mrb_mod_module_eval) and
which class to use as the target (singleton vs. self-as-class).

Extract the shared logic into object_eval(self, class_eval).
The two top-level dispatchers become one-line wrappers.

Closes #6579, picked from PR by dearblue.

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-08 11:51:25 +09:00
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 16:50:58 +09:00
dearblue 5bc08befae Use the specialized MRB_ENSURE() instead of mrb_protect_error() 2026-01-24 11:34:02 +09:00
Yukihiro "Matz" Matsumoto 3d0b5c848a mruby-eval: rename exec_irep to eval_irep
Avoid static function name collision with src/vm.c for future
amalgamation support.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-23 08:34:52 +09:00
Yukihiro "Matz" Matsumoto 3872587ec2 mruby-eval: combine variable declaration with initialization 2025-10-27 07:45:40 +09:00
John Bampton e09893daa9 mruby-eval fix license link in README 2025-07-29 01:27:49 +10:00
Yukihiro "Matz" Matsumoto d9d95ef57d mruby-eval: add comprehensive call-seq documentation for all methods
Added complete call-seq documentation for all 5 public methods in the
eval gem, improving documentation coverage from 0% to 100%.

Documentation added:
- eval: Evaluate Ruby expressions with optional binding and file context
- Object#instance_eval: Evaluate code in the context of an object instance
- Module#class_eval/module_eval: Evaluate code in the context of a class/module
- Binding#eval: Evaluate code within a specific binding context

Each method now includes:
- Clear method signatures with parameter and return types
- Detailed descriptions of evaluation context and scope behavior
- Comprehensive examples showing practical usage patterns
- Notes about binding objects, file/line reporting, and block alternatives
- Explanations of self context changes and variable access
- Security and error handling considerations

Key documentation features:
- String vs block evaluation differences explained
- Binding context usage with practical examples
- Instance variable and private method access patterns
- Class/module modification examples
- Error reporting with filename and line number context

Co-authored-by: Atlassian Rovo Dev
2025-07-16 13:38:50 +09:00
Yukihiro "Matz" Matsumoto 0a45941337 mruby-eval: add README.md
The document is written by Google Jules.
2025-06-11 18:31:52 +09:00
Yukihiro "Matz" Matsumoto 1fa9eaa2b5 mruby-eval (binding_eval_error_check): re-raise Exception in parsing 2025-05-21 10:25:35 +09:00
Yukihiro "Matz" Matsumoto dbc4768758 mruby/internal.h: rename visibility separation macros; ref #6512 2025-05-08 07:32:36 +09:00
Yukihiro "Matz" Matsumoto 41e91a2ca8 vm.c (mrb_exec_irep): remove recently added separate_module argument
The argument was added in #6512
2025-05-08 07:27:23 +09:00
Yukihiro "Matz" Matsumoto ef0cbc2981 mruby-eval (exec_irep): cache mrb->c->ci to a local variable 2025-05-07 23:30:41 +09:00
dearblue 3fd5e1c250 Fixed visibility at method definition
There was a problem with visibility state from proc that straddles a fiber or is independent.

Therefore, it has been changed to give priority to env objects, if any.
Also, added "separate module" flag to block traversal to a higher level env object.

Note that the "separate module" flag is now set when calling blocks with the `mrb_yield_with_class()` function.

fixed https://github.com/mruby/mruby/issues/6494
2025-04-11 21:36:26 +09:00
Yukihiro "Matz" Matsumoto 93e133548f mruby-eval: make #eval private 2025-03-07 17:17:44 +09:00
Yukihiro "Matz" Matsumoto d022a3ee68 mruby-eval/test: use #__send__ to call private #eval 2025-03-07 17:17:41 +09:00
Yukihiro "Matz" Matsumoto 6535f6d159 mruby-eval: use presym for initialization 2024-06-14 00:26:22 +09:00
dearblue 75b0d06c41 Fixed Binding#eval that failed to assign to the same variable
In `Binding#eval`, two times parsing is executed.
The problem found in this case was caused by not passing an upper block to hold variables of the binding object during the first parsing.

The problem was uncovered by <https://github.com/mruby/mruby/discussions/6274>.
2024-05-25 21:55:02 +09:00
dearblue 82312467d0 Refer to mrb->module_class directly in mrb_mruby_eval_gem_init() 2024-01-20 22:20:20 +09:00
Yukihiro "Matz" Matsumoto 72689b0b9c mruby-eval: replace mrbc_ prefix by mrb_ccontext 2023-12-06 15:49:21 +09:00
dearblue 0b25c9d224 Fixed acquisition of wrong target class in Kernel#binding
fixed #6089
2023-10-28 18:24:41 +09:00
dearblue 0e00d8e224 Integrate mruby-binding-eval into mruby-eval
Currently mruby-eval depends on mruby-binding (formerly mruby-binding-core).
Also, `Kernel#eval` can accept `binding` objects.
With this in mind, it would be better if `Binding#eval` could also be handled by `mrbgems/mruby-eval`.
2023-04-16 21:44:00 +09:00
Yukihiro "Matz" Matsumoto 2de602b869 mruby-eval: add dependency to mruby-binding 2023-04-01 10:37:56 +09:00
Yukihiro "Matz" Matsumoto 0bbfec3e8b mruby-eval: use functions from mruby-binding to simplify the code 2023-03-25 19:12:46 +09:00
Yukihiro "Matz" Matsumoto 11af5db1c3 mruby-binding: renamed from mruby-binding-core
The dependency is now:

mruby-binding-eval:

  - mruby-binding
  - mruby-eval

mruby-proc-binding:

  - mruby-binding

mruby-eval:

  - mruby-binding [new]
2023-03-24 12:25:57 +09:00
Yukihiro "Matz" Matsumoto 3ef41672ab mruby-biding-eval: move essential part of the gem to mruby-eval
This change makes mruby-binding-eval gem nearly empty, but needed to
make `eval(str, binding)` works as well as `binding.eval(str)`.
2023-03-24 07:33:13 +09:00
Yukihiro "Matz" Matsumoto 35e2779575 Small cosmetic changes (adjust indent, and remove extra space) 2023-03-21 16:10:15 +09:00
Yukihiro "Matz" Matsumoto 0c8404a3e2 mruby-binding: use C implementation of Kernel#eval directly 2023-03-21 16:10:15 +09:00
dearblue 36f822d9af Fixed Kernel#.eval crash when given a custom defined Binding instance
```console
% bin/mruby -e 'class Binding; end; eval "1", Binding.new'
zsh: segmentation fault (core dumped)  bin/mruby -e 'class Binding; end; eval "1", Binding.new'
```
2023-01-20 21:26:21 +09:00
Yukihiro "Matz" Matsumoto 3a7e138c05 mruby/internal.h: add mrb_env_new to the internal function header 2023-01-18 10:37:48 +09:00
Yukihiro "Matz" Matsumoto 55eea005c8 mruby/internal.h: add codedump functions to the internal function header
ref #5906
2023-01-18 10:37:48 +09:00
Yukihiro "Matz" Matsumoto ddefcea151 mruby-eval/eval.c (f_instance_eval): skip mrb_value to get singleton
Use mrb_singleton_class_ptr() instead of mrb_instance_class(), because
the former returns NULL, when the latter raises error for immediate
objects.

```
1.instance_eval <<END
  def foo
    :foo
  end
END
```
2022-12-13 10:51:44 +09:00
Yukihiro "Matz" Matsumoto 4107cc95a2 internal.c: move some functions to <mruby/internal.h>.
Ref #5776
2022-08-15 18:13:59 +09:00
Yukihiro "Matz" Matsumoto dccd66f9ef Support Ruby3.0 keyword arguments.
The Difference

Since Ruby1.9, the keyword arguments were emulated by Ruby using the hash
object at the bottom of the arguments. But we have gradually moved toward
keyword arguments separated from normal (positinal) arguments.

At the same time, we value compatibility, so that Ruby3.0 keyword
arguments are somewhat compromise. Basically, keyword arguments are
separated from positional arguments, except when the method does not
take any formal keyword arguments, given keyword arguments (packed
in the hash object) are considered as the last argument.

And we also allow non symbol keys in the keyword arguments. In that
case, those keys are just passed in the `**` hash (or raise
`ArgumentError` for unknown keys).

The Instruction Changes

We have changed `OP_SEND` instruction. `OP_SEND` instruction used to
take 3 operands, the register, the symbol, the number of (positional)
arguments. The meaning of the third operand has been changed. It is now
considered as `n|(nk<<4)`, where `n` is the number of positional
arguments, and `nk` is the number of keyword arguments, both occupies
4 bits in the operand.

The number `15` in both `n` and `nk` means variable sized arguments are
packed in the object. Positional arguments will be packed in the array,
and keyword arguments will be packed in the hash object. That means
arguments more than 14 values are always packed in the object.

Arguments information for other instructions (`OP_SENDB` and `OP_SUPER`)
are also changed. It works as the third operand of `OP_SEND`. the
difference between `OP_SEND` and `OP_SENDB` is just trivial. It assigns
`nil` to the block hidden arguments (right after arguments).

The instruction `OP_SENDV` and `OP_SENDVB` are removed. Those
instructions are replaced by `OP_SEND` and `OP_SENDB` respectively with
the `15` (variable sized) argument information.

Calling Convention

When calling a method, the stack elements shall be in the order of the
receiver of the method, positional arguments, keyword arguments and the
block argument. If the number of positional or keyword arugument (`n` or
`nk`) is zero, corresponding arguments will be empty. So when `n=0` and
`nk=0` the stack layout (from bottom to top) will be:

+-----------------------+
| recv | block (or nil) |
+-----------------------+

The last elements `block` should be explicitly filled before `OP_SEND`
or assigned to `nil` by `OP_SENDB` internally. In other words, the
following have exactly same behavior:

OP_SENDB clears `block` implicitly:

```
OP_SENDB reg sym 0
```

OP_SEND clears `block` implicitly:

```
OP_LOADNIL  R2
OP_SEND     R2 sym 0
```

When calling a method with only positional arguments (n=0..14) without
keyword arguments, the stack layout will be like following:

+--------------------------------------------+
| recv | arg1 | ... | arg_n | block (or nil) |
+--------------------------------------------+

When calling a method with arguments packed in the array (n=15) which
means argument splat (*) is used in the actual arguments, or more than
14 arguments are passed the stack layout will be like following:

+-------------------------------+
| recv | array | block (or nil) |
+-------------------------------+

The number of the actual arguments is determined by the length of the
argument array.

When keyword arguments are given (nk>0), keyword arguments are passed
between positional arguments and the block argument. For example, when
we pass one positional argument `1` and one keyword argument `a: 2`,
the stack layout will be like:

+------------------------------------+
| recv | 1 | :a | 2 | block (or nil) |
+------------------------------------+

Note that keyword arguments consume `2*nk` elements in the stack when
`nk=0..14` (unpacked).

When calling a method with keyword arguments packed in the hash object
(nk=15) which means keyword argument splat (**) is used or more than
14 keyword arguments in the actual arguments, the stack layout will
be like:

+------------------------------+
| recv | hash | block (or nil) |
+------------------------------+

Note for mruby/c

When mruby/c authors try to support new keyword arguments, they need
to handle the new meaning of the argument information operand. If they
choose not to support keyword arguments in mruby/c, it just raise
error when `nk` (taken by `(c>>4)&0xf`) is not zero. And combine
`OP_SENDV` behavior with `OP_SEND` when `n` is `15`.

If they want to support keyword arguments seriously, contact me at
<matz@ruby.or.jp> or `@yukihiro_matz`. I can help you.
2021-10-12 20:16:36 +09:00
Yukihiro "Matz" Matsumoto 674d841ead eval.c: if the parser raises an exception without an error message
`eval` would have crashed in formatting.
2021-09-07 07:37:19 +09:00
Yukihiro "Matz" Matsumoto 711f06a562 eval.c: check length of the file name.
It should be lexx than `UINT16_MAX`. If you don't check here, the parser
would raise an exception.
2021-09-07 07:35:35 +09:00
Yukihiro "Matz" Matsumoto 013365498d test/eval.c: add test for #5528 2021-08-12 11:57:56 +09:00
Yukihiro "Matz" Matsumoto 43f6f765b0 Remove unused prototypes for mrb_proc_merge_lvar(); ref #5511 2021-07-19 07:42:49 +09:00
Yukihiro "Matz" Matsumoto d63c0df6bc Merge pull request #5493 from dearblue/binding.2
Fixed finding variables from `proc` in `binding.eval` failed
2021-06-28 09:13:54 +09:00
dearblue c182903ea0 Fixed finding variables from proc in binding.eval failed
Previously the following code did not produce the expected results:
```ruby
bx = binding
block = bx.eval("a = 1; proc { a }")
bx.eval("a = 2")
p block.call  # Expect 2 but return 1 due to a bug
```

The previous implementation of `Binding#eval` evaluated the code and then merged the top layer variables.
This patch will parse and expand the variable space before making a call to `eval`.
This means that the call to `Binding#eval` will do the parsing twice.

In addition, the following changes will be made:
- Make `mrb_parser_foreach_top_variable()`, `mrb_binding_extract_proc()` and `mrb_binding_extract_env()` functions private global functions.
- Remove the `posthook` argument from `mrb_exec_irep()`.
  The `posthook` argument was introduced to implement the `binding` method.
  This patch is unnecessary because it uses a different implementation method.

ref #5362
fixed #5491
2021-06-26 13:43:36 +09:00
dearblue cf38fc2f9e Fix memory leak in Kernel#eval
The `mrbc_context` remained unreleased when the `mrb_parse_nstring()` function returned `NULL`.
2021-06-26 10:59:39 +09:00
dearblue c40802865a Added MRB_API function to get block arguments info.
- ` mrb_block_given_p()` -- The name comes from CRuby's `rb_block_given_p ()`

At the same time, it applies to `f_instance_eval()` and `f_class_eval()` of `mruby-eval`.
2021-06-19 21:15:02 +09:00
Yukihiro "Matz" Matsumoto 651c234f9c eval.c: implement class_eval with string; close #5478 2021-06-16 09:12:03 +09:00
dearblue dd34ac647f Make mrb_exec_irep() allow non-VM to enter.
Change the old `mrb_exec_irep()` as-is to static `mrb_exec_irep_vm()`.
Extract the VM entry part from the old `exec_irep()` in `mruby-eval/src/eval.c` and make it the core of the new `mrb_exec_irep()`.
2021-04-03 21:17:48 +09:00
Yukihiro "Matz" Matsumoto 5d37160455 Use MRB_SYM() more extensively. 2021-02-26 15:05:55 +09:00
dearblue 927615e1f0 Added other methods for Binding
- Added to `mruby-binding-core`
  - `Binding#local_variable_defined?`
  - `Binding#local_variable_get`
  - `Binding#local_variable_set`
  - `Binding#local_variables`
  - `Binding#receiver`
  - `Binding#source_location`
  - `Binding#inspect`
- Added to `mruby-proc-binding`
  - `Proc#binding`

The reason for separating `Proc#binding` is that core-mrbgems has a method that returns a closure object to minimize possible problems with being able to manipulate internal variables.
By separating it as different mrbgem, each user can judge this problem and incorporate it arbitrarily.
2021-02-22 23:32:43 +09:00
dearblue 792f6ac670 Adjustment of the current HEAD and bindings, and separation
Make changes to make `Binding` work.
At the same time, it separates `Binding#eval`, which depends on `mruby-eval`, from `mruby-binding-core`.
2021-02-22 23:32:18 +09:00
dearblue 297c5ac598 Merge branch 'binding' of https://github.com/ksss/mruby into HEAD 2021-02-22 22:14:46 +09:00
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00
Yukihiro "Matz" Matsumoto dc51d89ac2 Minimize the changes in #5277
Instead of including `mruby/presym.h` everywhere, we provided the
fallback `mruby/presym.inc` under `include/mruby` directory, and specify
`-I<build-dir>/include` before `-I<top-dir>/include` in `presym.rake`.
So even when someone drops `-I<build-dir>/include` in compiler options,
it just compiles without failure.
2021-01-22 18:38:53 +09:00
Yukihiro "Matz" Matsumoto 6c5ccd5abe Merge branch 'avoid-including-presym.inc-in-existing-header-files' of https://github.com/shuujii/mruby into shuujii-avoid-including-presym.inc-in-existing-header-files 2021-01-21 14:53:18 +09:00