summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-fiber/src
AgeCommit message (Collapse)Author
2021-10-12Support Ruby3.0 keyword arguments.Yukihiro "Matz" Matsumoto
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 <[email protected]> or `@yukihiro_matz`. I can help you.
2021-09-15Use `struct` initializer instead of `memset`.Yukihiro "Matz" Matsumoto
2021-09-10mruby.h: remove `acc` from `callinfo`; add `cci` instead.Yukihiro "Matz" Matsumoto
`acc` was used as an index of the receiver (if positive), or a flag for methods implemented in C. We replace `regs[ci->acc]` by `ci[1].stack[0]`. And renamed `acc` (originally meant accumulator position) to `cci` (means callinfo for C implemented method).
2021-06-20Added `MRB_OBJ_ALLOC()` macro that does not require a castdearblue
The `MRB_OBJ_ALLOC()` macro function returns a pointer of the type corresponding to the constant literal defined in `enum mrb_vtype`.
2021-01-12Capture the return value of `Fiber.yield` via C; ref #5261dearblue
2021-01-10Revert "Save&restore execution point (`pc`) in `c->cibase->pc`; fix #5261"dearblue
This reverts commit a0c1e075e35c358d21934c28ff1bec4153502409. This is because the `mrb_callinfo::pc` has been reorganized, resulting in over-correction.
2021-01-10Unified `pc` and `err` of `mrb_callinfo`dearblue
This enhances self-containment. - Changed the `mrb_callinfo::pc` field to point to itself. Previously it indicated the return destination of the previous call level. `mrb_callinfo::pc` will now hold the address to its own `proc->body.irep->iseq`. - Removed `mrb_callinfo::err` field. This is because `mrb_callinfo::pc - 1` is semantically the same as the previous `err`. - The `pc0` and `pc_save` variables in `mrb_vm_exec()` are no longer needed and have been deleted. - It removes the argument because `cipush()` doesn't need to save the previous `pc`.
2021-01-10Changes `stackent` to `stack` of `mrb_callinfo`dearblue
This enhances self-containment. Previously `mrb_context::stack` had the current call level stack, but now it owns it. The `mrb_context::stack` field, which is no longer needed, will be removed.
2021-01-10Unified `target_class` and `env` of `mrb_callinfo`dearblue
If there is `env`, `env->c` means `target_class`.
2021-01-08Save&restore execution point (`pc`) in `c->cibase->pc`; fix #5261Yukihiro "Matz" Matsumoto
`mrb_fiber_resume()` can be called from C; ref #3056
2021-01-06Allow context switch from C using `mrb_fiber_resume()`.Yukihiro "Matz" Matsumoto
But you still cannot cross C function boundary.
2020-10-22Prohibit array changes by "a"/"*" specifier of `mrb_get_args()`dearblue
The "a"/"*" specifier of the `mrb_get_args()` function will now return `const mrb_value *`. This is because it is difficult for the caller to check if it is an array object and write-barrier if necessary. And it requires calling `mrb_ary_modify()` on the unmodified array object, which is also difficult (this is similar to #5087).
2020-06-20Add `mrb_get_arg1()` that retrieves single (and only) argument.Yukihiro "Matz" Matsumoto
`mrb_get_arg1()` raises `ArgumentError` if the method does not receive one argument. And replaces all `mrb_get_args(mrb, "o", &arg)` by the new function.
2019-10-30Fix argument specs to `Fiber`KOBAYASHI Shuji
2019-09-26Use type predicate macros instead of `mrb_type` if possibleKOBAYASHI Shuji
For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for all `enum mrb_vtype`).
2019-09-16Entrust "no block given" error to `mrb_get_args()`dearblue
Some error messages will be changed.
2019-09-02Small refactoring in `fiber.c`.Yukihiro "Matz" Matsumoto
2018-08-01Remove `nregs` member from `mrb_callinfo`.Yukihiro "Matz" Matsumoto
This means reducing one word per a call frame.
2018-06-14Check for switching to uninitialized fiber; fix #4041Yukihiro "Matz" Matsumoto
The problem was caused by `Fiber.current.resume'.
2018-06-11Revert "No longer need to insert write barriers for fibers."Yukihiro "Matz" Matsumoto
This reverts commit c6736357a72049a0eb2a31ccabcc3cd2baba7c9e. The assumption was wrong and caused the issue; fix #4020
2018-06-07Extend stack when pushing arguments that does not fit in; fix #4038Yukihiro "Matz" Matsumoto
2018-04-13No longer need to insert write barriers for fibers.Yukihiro "Matz" Matsumoto
Those barriers are inserted to fix #3699 but all living fibers are marked from `mark_context()` anyway now.
2018-03-02Add `mrb_fiber_alive_p` to C API.Yukihiro "Matz" Matsumoto
2017-10-28Heavily refactored how lexical scope links are implemented; fix #3821Yukihiro "Matz" Matsumoto
Instead of `irep` links, we added a `upper` link to `struct RProc`. To make a space for the `upper` link, we moved `target_class` reference. If a `Proc` does not have `env`, `target_class` is saved in an `union` shared with `env` (if a `Proc` has env, you can tell it by `MRB_PROC_ENV_P()). Otherwise `target_class` is referenced from `env->c`. We removed links in `env` as well. This change removes 2 members from `mrb_irep` struct, thus saving 2 words per method/proc/block. This also fixes potential memory leaks due to the circular references caused by a link from `mrb_irep`.
2017-09-27fix: mrbgems\mruby-fiber\src\fiber.c(215): warning C4244: '=': conversion ↵Tomasz Dąbrowski
from 'mrb_int' to 'int', possible loss of data
2017-07-12Use "$!" specifier of `mrb_get_args`.Yukihiro "Matz" Matsumoto
2017-06-21Add write barrier to protect proc reference from fiber; fix #3719Yukihiro "Matz" Matsumoto
2017-06-15Call write barriers for stack-modified fibers; fix #3699Yukihiro "Matz" Matsumoto
2017-06-15Prevent Fiber#initialize to be called twice; fix #3705Yukihiro "Matz" Matsumoto
2017-05-26Fixed off-by-one error in `fiber_switch()`; ref #3641Yukihiro "Matz" Matsumoto
2017-05-25Raise an exception when transfer arguments are too many; fix #3641Yukihiro "Matz" Matsumoto
The fix was proposed by @block8437. Thank you.
2017-04-21Assign to local variable first; cosmetic change.Yukihiro "Matz" Matsumoto
2017-04-11Fiber#yield should check C function boundary; fix #3582Yukihiro "Matz" Matsumoto
2016-01-02mruby-fiber: fiber_switch() to use nesting VM when it's called from C API or ↵Yukihiro "Matz" Matsumoto
mrb_funcall(); close #3056
2015-12-31mruby-fiber: add Fiber.yield description; close #3066Yukihiro "Matz" Matsumoto
Fiber.yield cannot be called from #initialize which is called by mrb_funcall(). It is mruby limitation.
2015-11-27include changed from by quotes ("") to by brackets (<>); close #3032Yukihiro "Matz" Matsumoto
2015-06-19Fix typos in documentation and error messages [skip ci]Anton Davydov
2015-02-19new API function mrb_fiber_resume(); ref #1269Yukihiro "Matz" Matsumoto
2015-02-19add description comment to mrb_fiber_yield()Yukihiro "Matz" Matsumoto
2014-08-29Fix mismatches for MRB_API declarations.Tatsuhiko Kubo
2014-08-25Remove spaces in end-of-line.Tatsuhiko Kubo
2014-07-16need to call write_barrier on every fiber context switchYukihiro "Matz" Matsumoto
2014-07-15fiber stack allocation size should be bigger than irep->nregsYukihiro "Matz" Matsumoto
2014-07-09Clean up value.h and mrb_value boxingDavid Turnbull
2014-07-03fiber stack should not be cleared by zero when MRB_NAN_BOXING is set; close ↵Yukihiro "Matz" Matsumoto
#2439
2014-04-30remove trailing spacesNobuyoshi Nakada
2014-04-25eliminate plain int except for a few cases like arena_indexYukihiro "Matz" Matsumoto
2014-04-21Remove unnecessary `require 'fiber'` note in Fiber.current document.take_cheeze
2014-04-21Add document to Fiber#transfer .take_cheeze
2014-04-11Qualify argv argument of API `const`.Takeshi Watanabe