Only go to exception handling if `mrb->exc` is non-null.
This may cause some compatibility problems, but I doubt that it is necessary to maintain that compatibility.
Here is how I see the incompatibility with the change at this time:
- If `mrb->exc` is non-null and `mrb_vm_exec()` is called, an exception will be thrown immediately.
- If `MRB_THROW()` is used while `mrb->exc` is `NULL`, it will not go to exception handling.
Previous SWAR version assumes valid UTF-8 to count number of code points
in the string, but we need to handle invalid sequence as well. We now
use `search_nonascii` to skip counting single byte characters for
performance. The new version is even faster than SWAR version (probably
because `search_nonascii` uses SSE2 on Intel compatible CPU (which I use).
The patch assumes that `struct REnv::cxt` only performs checks with the `OP_BREAK` and `OP_RETURN_BLK` instructions, and does not reference the entity.
Therefore, by changing to a weak reference, it is possible to collect fibers that are no longer directly referenced while in the suspended state.
However, we need to detach the living env objects that remain in the call stack of the fiber.
So, in effect, it involves a revert of following commits.
- commit a3365d8b3f
- commit 57ffa1c150
Examples of the effects of change are shown below.
Note that it was built with `rake MRUBY_CONFIG=host-debug`.
```ruby
f = Fiber.new { (x, y, z) = "X", "Y", "Z"; Fiber.yield -> { [x, y, z] } }
g = f.resume
GC.start
p ObjectSpace.memsize_of_all
# => 59532
g.call
# => ["X", "Y", "Z"]
f = nil
GC.start
ObjectSpace.memsize_of_all
# BEFORE => 59532
# AFTER => 58044
g.call
# => ["X", "Y", "Z"]
```
`mrb_env_unshare()` calls `mrb_realloc_simple()` and follows `mrb_full_gc()` to avoid an infinite loop where `mrb_env_unshare()` is called again.
This does not occur at this time, but may occur in subsequent patches.
`mrb_vm_run()` is,
- It does not change the fiber context.
- When control is returned, only one ci prepared by the caller is popped.
If the ci equals cibase when called, the ci position does not change.
related commits:
- commit 4e84bdb507
- commit 34dd258c63
- commit ebd6636a1e
- commit c6736357a7
- commit 23a4e7149d
- commit 31a961acf1
- Don't create multiple envs on one ci.
- Don't share a env to different ci.
- Don't attach a closed env to any ci.
Changes in `envadjust()` can be simplified with those guarantees.
Immediately frees the call stack and data stack at the end of a non-root fiber.
If the env object needs to be detached, the data stack is reused through `mrb_realloc()`.
Previously, it was not necessary to take into account that `c->cibase` could be `NULL`.
Note that this is no longer the case due to this patch.
In fact, changes to "mruby-fiber" are now required.
The state of a fiber switched due to an exception occurrence was incorrectly set to "Suspended".
```ruby
Fiber.new {
begin
Fiber.new { 0 / 0 }.resume
rescue
p Fiber.current
# before => #<Fiber:0x159f61e43ce0 fiber.rb:1 (suspended by resuming)>
# after => #<Fiber:0x159f61e43ce0 fiber.rb:1 (resumed)>
end
}.resume
```
This reverts commit 26e436e247.
After investigation, it is possible to revert by commit e89cc9b9fa.
The build configuration file used in the investigation is shown below.
```ruby
MRuby::Build.new do |conf|
toolchain :clang
enable_debug
enable_bintest
enable_test
cc.command = "clang18"
linker.command = "clang18"
[cc, cxx].each { |c| c.defines << "MRB_GC_STRESS" }
[cc, cxx, linker].each { |cmd| cmd.flags << %w(-fsanitize=address) }
gem github: "iij/mruby-dir" # rev: "89dceefa1250fb1ae868d4cb52498e9e24293cd1"
gem github: "iij/mruby-env" # rev: "056ae324451ef16a50c7887e117f0ea30921b71b"
gem github: "iij/mruby-errno" # rev: "b4415207ff6ea62360619c89a1cff83259dc4db0"
gem github: "iij/mruby-require" # rev: "f0634d785e5cbb73cd7d118ee36deff499e4181e"
gem github: "iij/mruby-tempfile" # rev: "9b883438547020dae328e34c8a2fe736171cd0ab"
end
```
Since the `rake` command needs to be from the past, we used the Ruby 2.6 version.
Currently `e->cxt` is used exclusively to check for `break` / `return` availability.
In other words, there is no need to maintain a reference to a fiber that has reached its end.
Highlights are:
- `Integrate the blocks `if (!ci->proc || MRB_PROC_CFUNC_P(ci->proc))` and `if (loc.irep == NULL)`.
- Folding some other conditionals.
- Assertions ensure that procs are not aliases.