Exception raising can now be controlled by the caller.
The main purpose on this patch is:
- Suppress exceptions from `obj_free()` in `src/gc.c` with `mrb_env_unshare()`.
- Consider the possibility that calls to `mrb_malloc()` may cause `e` objects to be subject to GC.
When control is returned to `mrb_env_unshare()`, `struct free_obj::next` in the same offset as `struct REnv::stack` is rewritten.
Unexpected results then occur when the object is reused.
Also, if `mrb_heap_page` containing an `e` object is freed, it may cause `SIGSEGV` at that point.
- Protects the value of the stack on `callinfo` that just exits if GC occurs inside `mrb_env_unshare()`.
```ruby
def m
b = -> { b }
end
p m.call
# => print block object, not nil
```
This patch does not raise a `NoMemoryError` exception in `mrb_env_unshare()` and can detect that error.
Thus, the problem fixed in # 3087 is not resurrected.
Also, it may seem that this patch should suppress exceptions raised by `cipop()` during `mrb_protect_error()` and `mrb_vm_exec()` unwinds.
However, `mrb_callinfo::u.env` by `CINFO_DIRECT` is not seen to be set.
So in that case `mrb_env_unshare()` is assumed to be originally exception-free.
- mrb_div_int() does integer division in Ruby way (mdiv)
returns mrb_int
- mrb_div_int_value() division with zero div and overflow checks.
returns mrb_value
- remove `lex_strterm_before_heredoc` that does not nest
- remove `all_heredocs` that cannot distinguish nested and followed
here-doc
- replace `lex_strterm` represented by cons list by C struct
- push/pop `lex_strterm` before/after interpolation
Users can now switch to their own implementation of GEM.
However, we do not guarantee that this will not be a problem in the current situation.
This may need to be improved in the future.
Normal return of control from the `mrb_sys_fail()` function has unexpected results.
Example: double free, malloc with huge size used as error integer
The following is an example of an actual crash:
```console
% cat crash.rb
def SystemCallError._sys_fail(*a)
nil
end
File.readlink("/404")
% bin/mruby crash.rb
zsh: segmentation fault (core dumped) bin/mruby crash.rb
```
Internal functions can only be called from within the library.
Functions listed in `mruby/internal.h` can be called from:
* core (src/*.c)
* gems (mrbgems/**/*.c)
But not from the application linked with `libmruby`.
Since the possible values of the backtrace are limited to `nil`, `RData`, and `RAray`, they are now stored as object pointers.
This change saves memory by eliminating the need to use instance variables for common exceptions.
ref. #2485
There is no need to limit the type to `struct RString`.
Also, the `MRB_EXC_MESG_STRING_FLAG` flag can be eliminated by checking if `struct RException::mesg` is `NULL` or not.
ref. #2485
To more consistent names.
* mrb_num_plus() -> mrb_num_add()
* mrb_num_minus() -> mrb_num_sub()
Since no one seems to use those functions, there should be no problem.
We added migration macros for compatibility for safety.