summaryrefslogtreecommitdiffhomepage
path: root/src/class.c
AgeCommit message (Collapse)Author
2022-01-05class.c: cancel #5620 which is no longer needed since #5622Yukihiro "Matz" Matsumoto
This reverts commit d3b7601af96c9e0eeba4c89359289661c755a74a.
2022-01-02Merge pull request #5620 from dearblue/adjust-stacksYukihiro "Matz" Matsumoto
Adjusting the stack for after it enters the virtual machine
2021-12-31class.c, variable,c: replace `size_t` by `int`.Yukihiro "Matz" Matsumoto
That reduce memory consumption by iv/mt tables.
2021-12-30Adjusting the stack for after it enters the virtual machinedearblue
ref. #5613. I mentioned in #5540 that there was no reentrant to the virtual machine, but in fact it was still a possibility at that point. Also, the variable `ci` needs to be recalculated at the same time.
2021-12-27class.c: remove `mt_elem` structure to avoid alignment gaps.Yukihiro "Matz" Matsumoto
2021-12-14proc.c: add `mrb_state` argument to `mrb_proc_copy()`.Yukihiro "Matz" Matsumoto
The function may invoke the garbage collection and it requires `mrb_state` to run.
2021-12-13class.c: increase first allocated page size.Yukihiro "Matz" Matsumoto
2021-12-13class.c: implement method cache (off by default).Yukihiro "Matz" Matsumoto
2021-10-29Improved `Class#new` methoddearblue
The number of registers used is reduced. Also, previously `R6` and` R7` were used, which exceeded the limit of `new_irep.nregs = 6`. This could cause the VM stack to overrun.
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-20Update `struct` initializer to work with relatively older `C++`.Yukihiro "Matz" Matsumoto
2021-09-15Use `struct` initializer instead of `memset`.Yukihiro "Matz" Matsumoto
2021-09-01Do no use return values from `mrb_ensure_` functions.Yukihiro "Matz" Matsumoto
They return the checking argument without modification, so the values are already there. Maybe we should change the return type to `void` but keep them unchanged for compatibility.
2021-08-30Merge pull request #5542 from dearblue/mrb_get_args-cIYukihiro "Matz" Matsumoto
Allow `nil` for `c!` and `I!` specifiers of `mrb_get_args()`
2021-08-28Allow `nil` for `c!` and `I!` specifiers of `mrb_get_args()`dearblue
2021-08-28Integrate the processing of similar specifiers of `mrb_get_args()`dearblue
It is `o`, `C`, `S`, `A` and `H` specifiers that are integrated. As a side effect, the `C!` Specifier can now be used.
2021-08-27Refactor the `mrb_get_args()` functiondearblue
- Removed the `ARGV` macro. The current path doesn't go into the mruby VM and there's also no need to separate variables. - Use common functions to check object types. - Use `mrb_ensure_string_type()` to check the string instead of `mrb_to_str()`. This is for consistency with array and hash. - Use `mrb_ensure_array_type()` to check the array instead of `to_ary()`. - Use `mrb_ensure_hash_type()` to check the hash instead of `to_hash()`. - Add and use `ensure_class_type()` to check class and module. - Changed the argument index type from `mrb_int` to `int`. Even if it is `int16_t`, it is enough. `mrb_int` is overkill, especially if `MRB_32BIT` and `MRB_INT64` are defined.
2021-08-23Checks the frozen object with `mrb_get_args()`dearblue
This now works with the `+` modifier that can be added after each specifier. - `nil` is bypassed. - The `s` and `z` specifiers are received in C as a `const char *`, so adding a `+` modifier will raise an exception. - The `a` specifier is received in C as `const mrb_value *`, so adding a `+` modifier will raise an exception. - The `|`, `*`, `&`, `?` and `:` specifiers with `+` modifier raises an exception. If `!`/`+` exceeds one for each specifier, an exception will occur in the subsequent processing. This is the same behavior as before.
2021-08-23Integrate each element expansion process of the argumentdearblue
The previously used `given` variable will be merged into the `pickarg` pointer variable, which points to the argument currently being processed for each loop.
2021-08-13Check the class with `I` specifier of `mrb_get_args()`dearblue
Previously, the `I` specifier only checked if the object was `MRB_TT_ISTRUCT`. So it was at risk of getting pointers to different C structs if multiple classes were to use the `MRB_TT_ISTRUCT` instance. Change this behavior and change the C argument corresponding to the `I` specifier to `(void *, struct RClass)`. This change is not compatible with the previous mruby. Please note that if the user uses the previous specifications, `SIGSEGV` may occur or the machine stack may be destroyed. resolve #5527
2021-08-12class.c: `const_missing` do not have to be in the backtrace; ref #5528Yukihiro "Matz" Matsumoto
2021-07-25Remove redundant include headers.Yukihiro "Matz" Matsumoto
- stdlib.h - stddef.h - stdint.h - stdarg.h - limits.h - float.h
2021-06-25class.c: call `method_added` hooks on alias definitions; #2339Yukihiro "Matz" Matsumoto
2021-06-24class.c: call hook methods on method definitions; close #2339Yukihiro "Matz" Matsumoto
- `Module#method_added` - `BasicObject#singleton_method_added`
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-06-19Added `MRB_API` function to get block arguments info.dearblue
- ` 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-17class.c: use `MRB_FLAG_TEST()` macro.Yukihiro "Matz" Matsumoto
2021-05-24class.c: no need to create aliases if both symbols are same.Yukihiro "Matz" Matsumoto
2021-05-24class.c: add write barrier for aliases.Yukihiro "Matz" Matsumoto
2021-05-17Global renaming regarding `integer` and `float`.Yukihiro "Matz" Matsumoto
Consistent number conversion function names: * `mrb_value` to immediate (C) value * `mrb_int()` -> `mrb_as_int()` * `mrb_to_flo()` -> `mrb_as_float()` * `mrb_value` to `mrb_value` (converted) * `mrb_to_int()' * `mrb_Integer()` - removed * `mrb_Float()` -> `mrb_to_float` Consistent function name (avoid `_flo` suffix): * `mrb_div_flo()` -> `mrb_div_float`
2021-04-26Merge pull request #5424 from dearblue/new_iseqYukihiro "Matz" Matsumoto
Fix annotations for inline iseq of `Class.new` [ci skip]
2021-04-24Fix annotations for inline iseq of `Class.new` [ci skip]dearblue
2021-04-24Introduce `MRB_GC_RED`dearblue
Replaces the magic number `7` except in `src/gc.c`.
2021-04-04Making a proc object static for a method with static irepdearblue
The following methods will be made static. - `Class#new` - `Proc#call` - `Kernel#catch` Previously, static const RProc could not be registered as a method, but this has been changed to allow it.
2021-02-01Copy `iv` table with `#prepend`; fix #5309Yukihiro "Matz" Matsumoto
In addition, stop eager allocation of `mt` table.
2021-01-31Introduced `MRB_PRESYM_INIT_SYMBOLS()`dearblue
The `init_SYMBOLS()` function implicitly defined in `MRB_PRESYM_DEFINE_VAR_AND_INITER()` requires some familiarity when trying to find it from the caller. By introducing `MRB_PRESYM_INIT_SYMBOLS()`, it is possible to find directly from the identifier.
2021-01-26Revert "Minimize the changes in #5277"Yukihiro "Matz" Matsumoto
This reverts commit dc51d89ac22acc60b9bfeed87115863565b74085.
2021-01-22Minimize the changes in #5277Yukihiro "Matz" Matsumoto
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-21Merge branch 'avoid-including-presym.inc-in-existing-header-files' of ↵Yukihiro "Matz" Matsumoto
https://github.com/shuujii/mruby into shuujii-avoid-including-presym.inc-in-existing-header-files
2021-01-12Merge branch 'reorganize-ci' of https://github.com/dearblue/mruby into ↵Yukihiro "Matz" Matsumoto
dearblue-reorganize-ci
2021-01-12Save `NOARG` information in `struct mt_elem`; fix #5257Yukihiro "Matz" Matsumoto
2021-01-11Avoid including `presym.inc` in existing header filesKOBAYASHI Shuji
Addressed an issue where existing programs linking `libmruby.a` could only be built by adding `<build-dir>/include` to compiler's include path.
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-06Merge branch 'improve-source-scanning-for-presym' of ↵Yukihiro "Matz" Matsumoto
https://github.com/shuujii/mruby into shuujii-improve-source-scanning-for-presym
2021-01-02Refine "wrong number of arguments" message in `mrb_get_args`KOBAYASHI Shuji
#### Before this patch: ```ruby __send__ #=> wrong number of arguments {}.default(1,2) #=> wrong number of arguments ``` #### After this patch: ```ruby __send__ #=> wrong number of arguments (given 0, expected 1+) {}.default(1,2) #=> wrong number of arguments (given 2, expected 0..1) ```
2020-12-29Refine error message from `mrb_get_arg1`KOBAYASHI Shuji
#### Before this patch: ```console $ bin/mruby -e '{}.key?' trace (most recent call last): -e:1: wrong number of arguments (ArgumentError) ``` #### After this patch: ```console $ bin/mruby -e '{}.key?' trace (most recent call last): -e:1: wrong number of arguments (given 0, expected 1) (ArgumentError) ```
2020-12-15refactor: remove trailing whitespace from C, Header, Ruby and YAML filesJohn Bampton
Lint
2020-12-13Improve source scanning for presymKOBAYASHI Shuji
The accuracy is greatly improved by using the C preprocessor to scan C sources for presym. C preprocessor can perfectly interpret all comments and preprocessor directives, so it can detect all symbols defined, for example `mrbgems/mruby-socket/src/const.cstub`. Also, as described later, this change will greatly improve the accuracy of presym detection from Ruby sources. ## Result The number of lines in the `presym` file for all gems is as follows: ```console Previous: 999 (false positive = 89, undetected = 297) New: 1207 ``` ## Build process The new build process (with presym) is as follows: 1. Build `mrbc` without presym (more on building without presym later). 2. Compile Ruby sources to C struct format with the `mrbc` created in step 1, and create` mrblib.c` and `gem_init.c`. Note that the symbols in the created files are output as `MRB_SYM` family macros or `mrb_intern_lit` instead of IDs (details will be described later). 3. C preprocessor processes C sources including the created files of step 2 and outputs them as `.i` files. In these files, for example, `MRB_IVSYM(foo)` is converted to `<@! "@" "foo" !@>` and `mrb_define_module(mrb, "Foo")` is converted to `<@! "Foo" !@>`. 4. Scan the files created in step 3 and create `presym` and` presym.inc` files. The files created in step 2 should output all static symbols defined in Ruby sources, including local variables, so we can detect all presyms by just scanning C sources without scanning Ruby sources directly. Further, by this process, the files to be scanned becomes the same as the files to be compiled, so that there is no excess or deficiency. ## Related changes The following changes have been made in relation to realizing this feature. ### Allow build without presym It enables build without presym to achieve the "Build process: 1". This incorporates #5202, see its issue for details. Note that when presym is enabled, even adding a local variable to a Ruby source may change contents of presym and require recompilation of almost all C sources. This is inconvenient, especially during trial and error in development, but this feature is also useful because it does not cause this problem if presym is disabled. ### Automatically create build target for `mrbc` without presym The `mrbc` used in the "Build process: 1" will be built by automatically creating a build target for it. The build name is `SOURCE_BUILD_NAME/mrbc`. ### Constantize output of C struct format by `mrbc` To realizing the "Build process: 2", as mentioned above, symbol IDs are not output directly in C struct format output by `mrbc`. As a result, the output becomes constant regardless of the state of presym at the time of `mrbc` build, and it is possible to detect symbols of Ruby sources in the same way as other C sources. Note that `mrb_intern_lit` is used for symbols that do not become presym, but in this state, the corresponding element in the symbol array cannot be statically initialized, so it is initialized at run time (therefore, in this case, the `const` qualifier is not added to the symbol array). ### Specify arbitrary `mrbc` file To realizing the "Build process: 2", enabled to specify `mrbc` created by another build target or pre-built` mrbc`. Use `MRuby::Build#mrbcfile =` to specify it explicitly. You can omit the "Build process: 1" by specifying pre-built `mrbc`, and you can always use an optimized build to compile Ruby sources faster. I think changes that affect the output of `mrbc` are rare, so in many cases it helps to improve efficiency. With presym, the build will be a little slower due to more build steps, but this feature will improve it a bit. ### Create presym files for each build target This feature was proposed at #5194 and merged once, but was reverted in 5c205e6e due to problems especially with cross-compilation. It has been introduced again because this change solves the problem. The presym files will be created below. * `build/NAME/presym` * `build/NAME/include/mruby/presym.inc` ### Other changes * Because presym detection accuracy is greatly improved as mentioned above, `MRuby::Gem::Specification#cdump?` is set to true by default, and `disable_cdump` is added instead of `enable_cdump`. Also, support for gem specific presym files has been discontinued (https://github.com/mruby/mruby/issues/5151#issuecomment-730967232). * Previously, `mrbc` was automatically created for the `host` build, but it will not be created if the build target for `mrbc` mentioned above is automatically created. At this time, `mrbc` file of the `mrbc` build is copied to` bin/`. * Two types of `.d` files will be created, `.o.d` and `.i.d`. oThis is because if `.i` depends on `presym.inc`, the dependency will circulate, so the `.d` file cannot be shared. * Changed file created with `enable_cxx_exception` to `X-cxx.cxx` from `X.cxx` to use the mruby standard Rake rule. ### Note Almost all C sources will need to be recompiled if there are any changes to `persym.inc` (if not recompiled properly, it will often result in run-time error). If `gcc` toolchain is used, dependencies are resolved by the `.d` file, so it become automatically recompile target, but if not (e.g. MSVC), it is necessary to manually make it recompile target. Also, even if `gcc` toolchain is used, it may not become recompile target if external gems does not use the mruby standard Rake rule. In particular, if the standard rule is overwritten, such as https://github.com/mruby/mruby/pull/5112/files, `.d` file will not be read, so be careful.
2020-12-05Fixed a bug with modules prepended many times.Yukihiro "Matz" Matsumoto
Adjust insertion point in `fix_prepend_module()`.
2020-12-04Make `Module#include` and `Module#prepend` behave like Ruby3.0.Yukihiro "Matz" Matsumoto
Module#include and Module#prepend now affect classes and modules that have already included or prepended the receiver, mirroring the behavior if the arguments were included in the receiver before the other modules and classes included or prepended the receiver. ```ruby class C; end module M1; end module M2; end C.include M1 M1.include M2 p C.ancestors #=> [C, M1, M2, Object, Kernel, BasicObject] ```