191 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto e8574b6298 mruby-struct/test: call #remove_const via #__send__ 2025-03-07 17:17:41 +09:00
dearblue e476d9a344 Need to restore the GC arena after some function calls
When calling `mrb_equal()` or `mrb_funcall()` family functions, the GC arena should be restored if the loop is repeated by a non-immediate return value.

In my opinion, restoring the GC arena is unnecessary when a non-immediate (true) value causes the function to return (e.g. the `mrb_ary_index_m()` function).

The patch does not take into account the case of recursive calls and may be incomplete.
2024-09-03 21:29:06 +09:00
Yukihiro "Matz" Matsumoto db8d43090a mruby-struct: use presym for initialization 2024-06-14 01:59:30 +09:00
Yukihiro "Matz" Matsumoto 0a61a9a76f mruby-struct: adjust local variable declarations 2024-05-05 18:55:42 +09:00
dearblue 2a1ea7d605 Inherit MRB_FL_UNDEF_ALLOCATE in subclasses
If `Class#allocate` is prohibited, subclasses should also be implicitly prohibited.

```ruby
p Class.new(Struct).allocate.class
# => #<Class:0x82362ac00>                                             by #6122
# => allocator undefined for #<Class:0x000000083a983220> (TypeError)  by Ruby 3.2
```

Added `MRB_DEFINE_ALLOCATOR()` to allow subclasses to use `Class#allocate`.

Supplement to #6122.
2023-12-23 21:01:42 +09:00
dearblue 8ecfacefca Prohibit Class#allocate in a different way
The method introduced by #5979 causes a fault by swapping classes.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
zsh: segmentation fault (core dumped)  bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
```

After applying this patch, a `TypeError` exception will be raised.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
trace (most recent call last):
        [1] -e:1
-e:1:in method: allocation failure of Proc (TypeError)
```

However, if the `mrb_vtype` is the same object, the same care must still be taken as before.

```console
% bin/mruby -e 'Method = Binding; p method(:puts).eval("12345")'
trace (most recent call last):
        [1] -e:1
-e:1:in eval: wrong argument type nil (expected Proc) (TypeError)
```
2023-12-22 21:59:34 +09:00
Yukihiro "Matz" Matsumoto 0ac917e228 Revert "mrbgems/mruby-struct/src/struct.c: use direct array access"
This reverts commit 159ff5b28d.
Fix #6066
2023-09-28 07:40:31 +09:00
Yukihiro "Matz" Matsumoto 159ff5b28d mrbgems/mruby-struct/src/struct.c: use direct array access
Unless we need boundary check, we don't need to use mrb_ary_ref().
2023-09-05 23:29:23 +09:00
Yukihiro "Matz" Matsumoto 6ae6b63aaf mruby-struct: add recursive inspect check instead of _inspect hack 2023-06-22 12:53:51 +09:00
Yukihiro "Matz" Matsumoto de69190df5 mruby-struct/struct.c: need to copy the argument array
Just in case for stack reallocations.
2023-05-25 09:05:02 +09:00
Yukihiro "Matz" Matsumoto e05ac91217 mruby-struct/struct.c: update the behavior when actual size differs
Assertions may be false and terminate the whole process when actual size
for struct objects are different.
2023-05-25 09:05:02 +09:00
Yukihiro "Matz" Matsumoto f50e40f291 mruby-struct/struct.c: use mrb_ary_set to simplify the code
We don't need to do `mrb_struct_modify()` and calling write barriers by
ourselves for the small performance issue.
2023-05-25 09:05:02 +09:00
Yukihiro "Matz" Matsumoto 0295e6a688 mruby-struct/struct.c (struct_aref_int): update index check
Check should use the number of members, not the actual size of the
struct object, which may be smaller than the declared size.
2023-05-25 08:40:46 +09:00
Yukihiro "Matz" Matsumoto ed5fbb5d63 mruby-struct.c (mrb_struct_s_def): check duplicate members 2023-05-24 22:43:09 +09:00
Yukihiro "Matz" Matsumoto f80192e8ac mruby-struct/struct.c (struct_s_member): avoid break
Direct `return` is more readable, IMO.
2023-05-24 17:59:32 +09:00
Yukihiro "Matz" Matsumoto c2f3f103bb mruby-struct/struct.c (mrb_struct_ref): update the function
- use `num_members`
- ensure the function takes no argument
- avoid out-of-bound access when struct is failed to initialize
2023-05-24 17:53:41 +09:00
Yukihiro "Matz" Matsumoto 69d33de527 mruby-struct/struct.c (num_members): simplify the code by new argument 2023-05-24 17:51:44 +09:00
Yukihiro "Matz" Matsumoto 52fb2f59d8 mruby-struct/struct.c (struct_aref_sym): simplify the logic 2023-05-24 17:51:03 +09:00
Yukihiro "Matz" Matsumoto 7b7ea70b84 mruby-struct/struct.c (mrb_struct_to_h): RSTRUCT_PTR() may be NULL
Especially when you define a member named `initialize`.
2023-05-24 17:48:51 +09:00
Yukihiro "Matz" Matsumoto eea72ec84a fix spaces in the type cast expressions (cosmetic changes) 2023-05-18 23:29:16 +09:00
Yukihiro "Matz" Matsumoto 4f125a3f19 mruby-struct/struct.c: allow struct size differ from members
The situation may be caused by `Struct.new(...).alloc` etc.
2023-04-13 07:39:05 +09:00
Yukihiro "Matz" Matsumoto 8af45425d0 mruby-struct/struct.c: Struct.new should take 1+ arguments 2023-04-13 07:39:05 +09:00
Yukihiro "Matz" Matsumoto bb7b4b67ae mruby-struct/struct.c (struct_corrupted): fix error message 2023-04-13 07:39:05 +09:00
Yukihiro "Matz" Matsumoto a7c96edf6f mruby-struct/struct.c: prohibit Struct.allocate; ref #5979 2023-04-13 07:39:05 +09:00
Yukihiro "Matz" Matsumoto 7327ca898c mruby-struct: fix uninitialized variable error 2023-03-13 08:08:30 +09:00
Yukihiro "Matz" Matsumoto c212edec5f mruby-struct/struct.c: allow empty Struct when a name is not given 2023-03-10 08:52:06 +09:00
Yukihiro "Matz" Matsumoto 38c50b3094 mruby-struct (struct_index): avoid direct array indexing
Caused crash probably due to the combination of C++ UB & optimizer.
2022-12-10 08:00:31 +09:00
Yukihiro "Matz" Matsumoto a7e304a51c mruby-struct (struct_index): unify indexing access code 2022-12-08 08:01:53 +09:00
Yukihiro "Matz" Matsumoto de0ed25fbf mruby-struct (struct_corrupted): unify error raising code 2022-12-07 17:07:33 +09:00
Yukihiro "Matz" Matsumoto 16ed467266 mruby-struct: avoid using mrb_bug(); ref #5852
Use mrb_assert() instead.
2022-11-25 16:17:29 +09:00
Yukihiro "Matz" Matsumoto 9c5dc42e59 small cosmetic changes.
I prefer `i++` style unless absolutely necessary.
This commit is an addition to 41e4148.
2022-11-19 17:11:56 +09:00
Yukihiro "Matz" Matsumoto d84daa56fa mruby-struct/struct.c (mrb_struct_modify): remove the write barrier.
Instead of calling write barriers (mrb_write_barrier) in the function,
call field write barriers (mrb_field_write_barrier_value) from the
individual functions.
2022-10-14 15:54:09 +09:00
Yukihiro "Matz" Matsumoto 6c40d64327 mruby-struct/struct.c: refactoring. 2022-10-13 23:12:53 +09:00
Yukihiro "Matz" Matsumoto af9ce1f64c mruby-struct/struct.c: adjust spaces. 2022-10-12 22:02:21 +09:00
Yukihiro "Matz" Matsumoto 37a3252ed3 mruby-struct/struct.c: need to use MRB_TT_STRUCT. 2022-10-11 23:24:19 +09:00
dearblue 366185a54d Corrected document directive to "call-seq" 2022-10-06 21:56:38 +09:00
Yukihiro "Matz" Matsumoto c6daaa8c07 struct.c (make_struct): use preallocated symbols. 2022-08-22 16:12:59 +09:00
Yukihiro "Matz" Matsumoto 521072ca3a mruby-struct/struct.rb: include Enumerable module.
Struct.ancestors should be [Struct, Enumerable, Object, Kernel, BasicObject]
2022-08-06 16:03:26 +09:00
Yukihiro "Matz" Matsumoto b99c389ec3 internal.h: aggregate internal functions.
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`.
2022-04-02 18:25:13 +09:00
Yukihiro "Matz" Matsumoto dccd66f9ef Support Ruby3.0 keyword arguments.
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
<matz@ruby.or.jp> or `@yukihiro_matz`. I can help you.
2021-10-12 20:16:36 +09:00
Yukihiro "Matz" Matsumoto bb3cd69aff value.h: introduce MRB_TT_STRUCT for mruby-struct. 2021-09-30 12:46:45 +09:00
Yukihiro "Matz" Matsumoto 2c41739b66 mruby.h: obsolete mrb_to_str().
Replace them by `mrb_ensure_string_type()`.
2021-09-01 07:00:55 +09:00
Yukihiro "Matz" Matsumoto 5eebbd7df2 Global renaming regarding integer and float.
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-05-17 15:07:05 +09:00
dearblue 0f8d3d8777 No need to check class definition
Because now the `Struct` class is always defined when this file is included.
2021-02-06 18:05:05 +09:00
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00
Yukihiro "Matz" Matsumoto dc51d89ac2 Minimize the changes in #5277
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-22 18:38:53 +09:00
KOBAYASHI Shuji 90b53f4c29 Avoid including presym.inc in existing header files
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-11 09:21:07 +09:00
John Bampton 940dec5e7d Fix spelling 2020-12-13 18:38:22 +10:00
dearblue f0a64329b1 Prohibit array changes by "a"/"*" specifier of mrb_get_args()
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-10-22 22:55:35 +09:00
Yukihiro "Matz" Matsumoto 2b188ed8a1 Reorganize Integer system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
2020-10-12 18:19:54 +09:00