104 Commits

Author SHA1 Message Date
Chris Hasiński c0b1e87c09 Fix attr_reader-generated methods accepting extra arguments
attr_reader-generated getter methods silently ignored any arguments
passed to them. CRuby raises ArgumentError in this case.

Add mrb_get_args(mrb, "") to enforce zero arguments, matching CRuby.
2026-03-19 23:12:14 +01:00
dearblue 4417321d1c Add more test code for method visibility
This test corresponds to the first issue of #6494.
Complement to #6512.
2025-05-18 18:50:02 +09:00
Yukihiro "Matz" Matsumoto aec8d0c58b Merge branch 'visibility' of github.com:dearblue/mruby into dearblue-visibility 2025-05-07 15:37:08 +09:00
Yukihiro "Matz" Matsumoto 6c72f8b378 class.c (extend_object): remove method; implement Kernel#extend in C 2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 6c25b5896a class.c (prepend_features): remove the method
Just like `#append_features`, we remove `#prepend_features` and
implemented `#prepend` directly in C.
2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 9387cd382e class.c (append_features): remove the method
The technique is called "double dispatch" (that was popular in
Smalltalk), but it does not work well with mruby. It's slower and
consumes more memory. Even thought `#append_features` defined in ISO
standard (15.2.2.4.11), we decided to remove it. Strictly speaking, it
is mruby limitation. And it should be documented clearly.
2025-04-28 10:30:00 +09:00
Yukihiro "Matz" Matsumoto 9cc6d39956 test/module.rb: add test for #6506 2025-04-14 22:09:35 +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
dearblue 84e1b0045c Fixed class method visibility via module_function
The following code should work

```ruby
module M
  def me
    p self
  end

  module_function :me
end

M.me
```
2025-04-07 21:40:56 +09:00
Mark Delk 6396aac434 fix a typo, update specs 2025-03-17 10:47:33 -05:00
Yukihiro "Matz" Matsumoto 18674c0237 test/module.rb: add visibility tests; ref #1835 2025-03-07 17:17:48 +09:00
Yukihiro "Matz" Matsumoto 384579fd21 test/module.rb: rename to pass the spell check 2025-03-07 17:17:48 +09:00
Yukihiro "Matz" Matsumoto 4d547b6cbb test/module.rb: call #remove_const via #__send__ to skip private check 2025-03-07 17:17:40 +09:00
Yukihiro "Matz" Matsumoto e0cb99aad4 test/module.rb: call #extend_object via #__send__ to skip private check 2025-03-07 17:17:40 +09:00
Yukihiro "Matz" Matsumoto 8a409feaad test/module.rb: call #prepend_features via #__send__ to skip private check 2025-03-07 17:17:40 +09:00
Yukihiro "Matz" Matsumoto 701a18deda test/module.rb: call #append_features via #__send__ to skip private check 2025-03-07 17:17:40 +09:00
dearblue f1c0752ab8 Shared empty iv_tbl of module
This will be a partial merge of #5317 with the following changes.

- Remove `iclass->iv_c` since `iclass->iv_c` is equivalent to `iclass->c`.
- `class_iv_ptr()` returns a single pointer instead of a double pointer.
2024-03-27 21:39:32 +09:00
John Bampton 2438195289 ruby: remove unneeded parenthesis from methods
commit eeb33da43035b5400f8d1830ea52a68691b3a678
Author: John Bampton <jbampton@gmail.com>
Date:   Mon Oct 31 23:53:43 2022 +1000

    Fix up

commit 80923c8f92a58b2f33a36d6403de7f6341973110
Merge: bc89500f6 416f012f6
Author: John Bampton <jbampton@users.noreply.github.com>
Date:   Mon Oct 31 20:12:59 2022 +1000

    Merge branch 'master' into ruby-remove-parenthesis

commit bc89500f6357c453b457516452ffcc7fd03fee88
Author: John Bampton <jbampton@gmail.com>
Date:   Mon Oct 31 14:39:20 2022 +1000

    ruby: remove unneeded parenthesis from methods
2022-11-01 00:25:09 +10:00
John Bampton ea8964ef35 ruby: standardize whitespace 2022-10-31 16:25:56 +10:00
John Bampton 7870409cf1 Remove unneeded trailing semi-colons from Ruby files 2022-10-26 23:48:27 +10:00
John Bampton 9d32d440eb feat(CI): add the GitHub Super Linter
The GitHub Super Linter is a more robust and better supported
tool than the current GitHub Actions we are using.

Running these checks:

ERROR_ON_MISSING_EXEC_BIT: true
VALIDATE_BASH: true
VALIDATE_BASH_EXEC: true
VALIDATE_EDITORCONFIG: true
VALIDATE_MARKDOWN: true
VALIDATE_SHELL_SHFMT: true
VALIDATE_YAML: true

https://github.com/marketplace/actions/super-linter
https://github.com/github/super-linter

Added the GitHub Super Linter badge to the README.

Also updated the pre-commit framework and added
more documentation on pre-commit.

Added one more pre-commit check: check-executables-have-shebangs

Added one extra check for merge conflicts to our
GitHub Actions.

EditorConfig and Markdown linting.

Minor grammar and spelling fixes.

Update linter.yml
2021-04-16 16:37:52 +09:00
KOBAYASHI Shuji 98d091436d Reduce memory usage of instance variable table
## Implementation Summary

* Only keys and only values of hash table are contiguous to eliminate
  structure padding.
* Change upper limit of `iv_tbl` size to `UINT16_MAX` (it seems to be
  acceptable in mruby because the total number of classes/modules
  immediately after starting Redmine is 20,000 or less).
* `iv_tbl*` point hash buckets directly.

## Benchmark Summary

Only the results of typical situations on 64-bit Word-boxing are present
here. For more detailed information, including consideration, see below
report (although most of the body is written in Japanese).

* https://shuujii.github.io/mruby-iv-benchmark

### Memory Usage

Lower value is better.

| iv_tbl Size |    Baseline    |       New      |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 |            88B |            52B |   0.59091x |
|          30 |           536B |           388B |   0.72388x |
|         100 |          2072B |          1540B |   0.74324x |
|         200 |          4120B |          3076B |   0.74660x |

Although not mentioned in the above report, the memory usage of `mrbtest`
(full-core gembox) is as follows in the result by Valgrind.

* Baseline: 108,086 allocs, 16,313,122 bytes allocated
* New:       94,273 allocs, 15,875,214 bytes allocated

### Performance

Higher value is better.

#### `mrb_obj_iv_set`

| iv_tbl Size |    Baseline    |       New      |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 |  88.63003M i/s |  92.60611M i/s |   1.04486x |
|          30 |  32.97066M i/s |  25.25095M i/s |   0.76586x |
|         100 |  16.33224M i/s |  22.74998M i/s |   1.39295x |
|         200 |   5.64484M i/s |   6.79949M i/s |   1.20455x |

#### `mrb_obj_iv_get`

| iv_tbl Size |    Baseline    |      New       |   Factor   |
|------------:|---------------:|---------------:|-----------:|
|           4 | 217.58391M i/s | 237.59912M i/s |   1.09199x |
|          30 | 139.56195M i/s | 160.49470M i/s |   1.14999x |
|         100 | 143.09716M i/s | 190.95047M i/s |   1.33441x |
|         200 |  89.75291M i/s | 134.78717M i/s |   1.50176x |

### Binary Size

Lower value is better.

|    File     |    Baseline    |      New       |   Factor   |
|:------------|---------------:|---------------:|-----------:|
| mruby       |       697,520B |       697,520B |   1.00000x |
| libmruby.a  |     1,046,570B |     1,046,682B |   0.99989x |

## Note

The address in `struct RObject::iv` may change after initialization because
`iv_tbl*` points directly to hash buckets. Therefore, the address cannot be
copied and shared when include/prepend. So, when sharing `iv_tbl`, refer to
it via the sharing source class. As a result, the following bug have also
been fixed.

* [An `iv_tbl` is not shared when a class includes or prepends an empty module](https://gist.github.com/shuujii/0ac23fa24b0c55b2c602b534d81e4a95)
2021-02-03 23:16:09 +09:00
John Bampton 940dec5e7d Fix spelling 2020-12-13 18:38:22 +10:00
dearblue 80fe9838d2 Integrate Fixnum class into Integer class
* The `Fixnum` constant is now an alias for the `Integer` class.
* Remove `struct mrb_state::fixnum_class` member.
  If necessary, use `struct mrb_state::integer_class` instead.
2020-10-12 16:21:44 +09:00
Yukihiro "Matz" Matsumoto 2ebc6d5b6f Merge pull request #4407 from shuujii/add-assert_raise_with_message-and-assert_raise_with_message_pattern
Add `assert_raise_with_message` and `assert_raise_with_message_pattern`
2019-07-29 01:05:26 +09:00
KOBAYASHI Shuji 7675fcb5d3 Fix Module#dup to frozen module
Before this patch:

  $ bin/mruby -e 'p Module.new.freeze.dup.frozen?'  #=> true

After this patch (same as Ruby):

  $ bin/mruby -e 'p Module.new.freeze.dup.frozen?'  #=> false
2019-07-20 22:41:57 +09:00
KOBAYASHI Shuji 97c9e6b000 Fix include, prepend and extend to frozen object 2019-06-30 15:06:39 +09:00
KOBAYASHI Shuji 030dd6655e Fix cvar, ivar, const and method can be removed to frozen object 2019-06-16 20:43:23 +09:00
KOBAYASHI Shuji dc1905e1bd Fix missing assertions in test/t/module.rb 2019-06-05 19:13:36 +09:00
KOBAYASHI Shuji 1fb635ac03 Add assert_raise_with_message and assert_raise_with_message_pattern 2019-04-26 21:48:40 +09:00
KOBAYASHI Shuji cc7f9190ba Fix name assignment to frozen anonymous class/module
Fix the following issues:

  A = Class.new.freeze              #=> FrozenError
  Module.new::B = Class.new.freeze  #=> FrozenError
  String::B = Module.new.freeze     #=> FrozenError
2019-04-23 20:45:38 +09:00
Yukihiro "Matz" Matsumoto 7cf1bc39a7 Merge pull request #4356 from shuujii/add-assert_match-and-assert_not_match
Add `assert_match` and `assert_not_match`
2019-04-22 22:29:15 +09:00
KOBAYASHI Shuji 716a99b069 Add assert_match and assert_not_match 2019-04-14 19:06:28 +09:00
KOBAYASHI Shuji 623e15936a Module#alias_method should return self in ISO standard 2019-04-05 19:03:46 +09:00
KOBAYASHI Shuji 16b1b2978e Fix constant name validation
`X!` etc are invalid constant name.
2019-03-14 23:09:05 +09:00
KOBAYASHI Shuji 8a7298e069 Use more appropriate assertion methods 2019-02-19 20:02:50 +09:00
KOBAYASHI Shuji abbc501433 class/module expression with empty body should return nil
Before:

  p(class A end)          #=> A
  p(class << self; end)   #=> #<Class:#<Object:0x7fdc3880e420>>
  p(module B end)         #=> B

After/Ruby:

  p(class A end)          #=> nil
  p(class << self; end)   #=> nil
  p(module B end)         #=> nil
2019-01-28 21:29:55 +09:00
Yukihiro "Matz" Matsumoto b80e0ef742 Move Kernel#send to mruby-metaprog gem.
But `BasicObject#__send__` is still available from the core.
2018-09-01 11:20:30 +09:00
Yukihiro "Matz" Matsumoto e471d37ca5 Separate meta-programming features to mruby-metaprog gem.
We assume meta-programming is less used in embedded environments.
We have moved following methods:

 * Kernel module
   global_variables, local_variables, singleton_class,
   instance_variables, instance_variables_defined?, instance_variable_get,
   instance_variable_set, methods, private_methods, public_methods,
   protected_methods, singleton_methods, define_singleton_methods

 * Module class
   class_variables, class_variables_defined?, class_variable_get,
   class_variable_set, remove_class_variable, included_modules,
   instance_methods, remove_method, method_removed, constants

 * Module class methods
   constants, nesting

Note:
Following meta-programming methods are kept in the core:

 * Module class
   alias_method, undef_method, ancestors, const_defined?, const_get,
   const_set, remove_const, method_defined?, define_method

 * Toplevel object
   define_method

`mruby-metaprog` gem is linked by default (specified in default.gembox).
When it is removed, it will save 40KB (stripped:8KB) on x86-64
environment last time I measured.
2018-08-30 22:30:36 +09:00
Kazuhiro Sera b03f1f78e3 Fix misspelling words in comments 2018-08-11 00:28:32 +09:00
Christopher Aue 4b0b8f7e1a Refactored #assert_raise and #assert_nothing_raised 2017-08-29 23:25:53 +02:00
Yukihiro "Matz" Matsumoto 9e10afe1d0 Implements `Module::nesting' (15.2.2.3.2); ref #600, #3200 2017-08-01 15:37:21 +09:00
Yukihiro "Matz" Matsumoto b290e98f3b Merge pull request #3757 from christopheraue/module_const_get_class_path
Extended Module#const_get to support class paths
2017-07-29 07:05:37 +09:00
Christopher Aue f937af5745 Extended Module#const_get to support class paths 2017-07-28 23:19:49 +02:00
Christopher Aue b499ad6749 Fixed return value of Module#include and #prepend 2017-07-28 17:38:40 +02:00
Christopher Aue 468cc34c93 Fixed Module#to_s and #name for #const_set modules 2017-07-18 22:55:37 +02:00
Kouichi Nakanishi 507e00e86f Modify to get constant of parent module in singleton class; fix #3568 2017-03-31 13:43:20 +09:00
ksss efe4f30b39 Module#define_method supports proc argument 2016-10-12 22:19:54 +09:00
Blaž Hrastnik 78462c9181 Clean up tests 2015-07-13 23:46:41 +02:00
Blaž Hrastnik 8c4da7accd assert() cannot be nested 2015-07-13 23:38:37 +02:00