Commit Graph

54 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto b716bf005e mruby-range-ext/range.c: use the reference from mrb structure.
Avoid `mrb_class_get()` if possible.
2022-09-12 10:29:45 +09:00
Yukihiro "Matz" Matsumoto 2bea39f339 mruby-range-ext/range.c: Range#cover?() takes a range too. 2022-04-24 14:02:00 +09:00
Yukihiro "Matz" Matsumoto d912b864df mruby-range-ext/range.c: cover? should work with begin-less ranges. 2022-02-09 13:32:20 +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
dearblue 5a57602860 Organize the include of header files
- `#include <math.h>` is done in `mruby.h`.
  Eliminate the need to worry about the `MRB_NO_FLOAT` macro.

- Include mruby header files before standard header files.
  If the standard header file is already placed before `mruby.h`, the standard header file added in the future tends to be placed before `mruby.h`.

This change should some reduce the chances of macros that must be defined becoming undefined in C++ or including problematic header files in a particular mruby build configuration.
2021-08-21 15:42:57 +09:00
Yukihiro "Matz" Matsumoto 2bed750eb0 range.c: avoid use of mrb_fixnum_p.
`mrb_int` may not fit in `fixnum` (inline integer).
2021-05-13 15:35:34 +09:00
John Bampton 4fa3359d44 refactor: remove trailing whitespace from C, Header, Ruby and YAML files
Lint
2020-12-15 19:44:02 +10:00
KOBAYASHI Shuji 353ecd7119 Fix typo in Range#last 2020-10-29 18:40:42 +09:00
Yukihiro "Matz" Matsumoto 32ffd81011 Add beginless/endless ranges tests. 2020-10-29 10:43:09 +09:00
Yukihiro "Matz" Matsumoto cb636215bb Raise exception from Range#first with beginless ranges. 2020-10-29 10:43:08 +09:00
Yukihiro "Matz" Matsumoto d35677f213 Implement Range#last in mruby-range-ext in Ruby.
This reduce unnecessary calls of `mrb_funcall()`.
2020-10-29 10:43:08 +09:00
Yukihiro "Matz" Matsumoto 6b457d2c00 Merge branch 'work_for_merge' of https://github.com/zubycz/mruby into zubycz-work_for_merge 2020-10-15 18:35:13 +09:00
taiyoslime bec4d05340 Introduce endless range (a part of #5085)
Co-Authored-By: n4o847 <22975590+n4o847@users.noreply.github.com>
Co-Authored-By: smallkirby <ssmallkirby@gmail.com>
2020-10-13 14:09:36 +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
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 8a87549315 Rename float configuration option names.
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT`
- `MRB_USE_FLOAT` => `MRB_USE_FLOAT32`

The former is to use `USE_XXX` naming convention. The latter is to make
sure `float` is 32bit float and not floating point number in general.
2020-10-12 16:21:40 +09:00
Yukihiro "Matz" Matsumoto 00f5ddc9ae Use mrb_funcall_id() extensively.
Except for support files e.g. `mruby-test/driver.c`, which are not
target of symbol collection via `rake gensym`.
2020-10-12 16:20:58 +09:00
taiyoslime c4f75c7de0 fix improper test of Range#min 2020-10-12 13:42:27 +09:00
Yukihiro "Matz" Matsumoto 49ae2a69f2 Add mrb_get_arg1() that retrieves single (and only) argument.
`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.
2020-06-20 12:49:46 +09:00
Yukihiro "Matz" Matsumoto 76f1aa7de5 Remove mrb_funcall from <=> operations. 2019-09-14 09:05:32 +09:00
KOBAYASHI Shuji 6cfcff6f10 Fix Range#max test (TypeError is raised) on 32-bit mode with word boxing 2019-09-05 19:51:32 +09:00
KOBAYASHI Shuji 13f58f9bc2 Fix Range#max test (TypeError is raised) on 32-bit mode 2019-08-24 14:22:10 +09:00
Yukihiro "Matz" Matsumoto cdf5e3963d Merge branch 'master' into fix-range-min-with-mruby-range-ext 2019-07-17 22:33:55 +09:00
KOBAYASHI Shuji 1b7516a4ae Fix (fixnum_value ... float_value).min with mruby-range-ext
Before this patch:

  $ bin/mruby -e 'p (2...4.0).min'  #=> TypeError

After this patch (same as CRuby and without `mruby-range-ext`):

  $ bin/mruby -e 'p (2...4.0).min'  #=> 2
2019-07-17 19:18:23 +09:00
KOBAYASHI Shuji 8cb37804c1 Allow mruby-range-ext to work with MRB_WITHOUT_FLOAT; ref 2add8641 2019-07-17 18:58:21 +09:00
Yukihiro "Matz" Matsumoto 2add864164 Allow mruby-range-ext to work with MRB_WITHOUT_FLOAT; ref #4576 2019-07-17 10:36:10 +09:00
dearblue c96b517ea2 Error needed/conflicts configuration
The purpose is to clarify the error if there is a needed/conflicts
configuration at compile time.
2019-07-14 00:29:17 +09:00
Ryan Lopopolo 56929362f5 Fix Range#min fast path with exclusive range 2019-07-09 01:48:15 -07:00
Ryan Lopopolo 0ad5ba7a0f Add a fast path for Float and Fixnum ranges for Range#max and Range#min
If no block is given and the Range has Fixnum or Float endpoints, do not iterate with
each and instead compare the endpoints directly. This implementation passes all of
the applicable specs from Ruby Spec.
2019-07-09 01:42:40 -07:00
Ryan Lopopolo eab07daf92 Add Range#max and Range#min tests from Ruby Spec 2019-07-09 01:41:10 -07:00
Yukihiro "Matz" Matsumoto 1c574b065d Remove mrb_ prefix from static functions in mruby-range-ext; #4213 2019-01-04 14:46:17 +09:00
KOBAYASHI Shuji 648b57620a range: Embed edges in RRange on boxing environment.
[Breaking changes]

Developers must use following APIs for accessing attributes of RRange
because RRange structure depends on boxing setting.

- mrb_range_beg
- mrb_range_end
- mrb_range_excl_p
2019-01-01 15:35:56 +09:00
Yukihiro "Matz" Matsumoto afca99a40b Remove implicit conversion using to_int method.
The ISO standard does not include implicit type conversion using
`to_int`. This implicit conversion often causes vulnerability.
There will be no more attacks like #4120.

In addition, we have added internal convenience method `__to_int` which
does type check and conversion (from floats).
2018-11-19 11:28:51 +09:00
Yukihiro "Matz" Matsumoto 1d781da8a6 Should not include float.h here. 2017-08-11 13:52:01 +09:00
Yukihiro "Matz" Matsumoto bc1259de3b add explicit casts 2017-01-02 17:48:44 +09:00
Yukihiro "Matz" Matsumoto 79a621dd73 Check before retrieving struct RRange pointer; fix #3320
range->edges may be NULL for example when #initialize_copy removed.
2016-12-03 12:30:01 +09:00
Tomasz Dąbrowski 72ed7eebc3 Fixed Range.size to use proper floating point tolerance 2016-11-24 12:50:49 +01:00
Yukihiro "Matz" Matsumoto a70104798e remove unnecessary ".!"; ref #3261 2016-11-22 16:55:18 +09:00
ksss 682237be6e Reimplements Range#first
Range#first shouldn't call `Range#to_a` on infinity range.
2016-11-21 16:55:49 +09:00
ksss 22390aa1c8 Use mrb_float instead of double 2016-11-19 23:25:08 +09:00
ksss a2fbd80a47 Fix Range#size results 2016-11-19 23:25:07 +09:00
Yukihiro "Matz" Matsumoto 5c405dea3d include changed from by quotes ("") to by brackets (<>); close #3032 2015-11-27 17:48:23 +09:00
Jun Hiroe 4e4929bc72 Rename extended xxxx class or module to xxxx class or module extension 2015-06-28 11:18:52 +09:00
Jun Hiroe c69d1201e1 Fix typo; Replace extensional with extended 2015-06-28 00:05:58 +09:00
Yukihiro "Matz" Matsumoto 91a025b1e6 reimplement Range#size using double with fixing corner cases; ref #2293 2014-05-19 18:04:27 +09:00
Jun Hiroe 4b3ff090ab Add Range#size 2014-05-19 16:17:10 +09:00
Yukihiro "Matz" Matsumoto 7c82b9e1bf use mrb_get_args("i") 2014-03-21 23:36:03 +09:00
take_cheeze c73f8d4def move summary of mrbgems in default gembox to its spec 2014-03-17 00:34:24 +09:00
cremno 09bb30870b tiny word boxing optimization 2014-01-02 06:36:29 +01:00
Daniel Bovensiepen d10763190e Improve test of mruby-range-ext GEM 2013-08-04 22:42:06 +08:00