Commit Graph

1053 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 2fb06aabe6 parse.y: refactor mrb_parser_parse().
- remove `mrb_jmpbuf` from `truct mrb_parser_state`
- unify exception handling of `mrb_state` and `mrb_parser_state`.
2021-09-07 07:31:21 +09:00
Yukihiro "Matz" Matsumoto ab3db07752 boxing_word.h: avoid mrb_float_p() when MRB_NO_FLOAT is on; fix #5546 2021-09-05 20:59:49 +09:00
Yukihiro "Matz" Matsumoto f6e5c902f0 string.h: obsolete mrb_str_to_str(), even from examples. 2021-09-01 07:00:54 +09:00
Yukihiro "Matz" Matsumoto 572a43de84 mruby.h: reorganize mrb_ensure/check functions in headers. 2021-09-01 07:00:53 +09:00
Yukihiro "Matz" Matsumoto ba6e121537 boxing_nan.h: rename prefix BOXNAN to NANBOX. 2021-08-26 16:06:02 +09:00
Yukihiro "Matz" Matsumoto 9d6755569e boxing_word.h: rename prefix BOXWORD to WORDBOX. 2021-08-26 16:06:01 +09:00
Yukihiro "Matz" Matsumoto fdfa0caa46 boxing_word.h: rename configuration macro name.
`MRB_WORDBOX_USE_HEAP_FLOAT` instead of `MRB_USE_FLOAT_FULL_PRECISION`.
2021-08-26 16:06:01 +09:00
Yukihiro "Matz" Matsumoto e9c6e52f5b value.h: reconstruct mrb_ro_data_p().
* use predefined `mrb_ro_data_p()` for user-mode Linux and macOS
* define `MRB_LINK_TIME_RO_DATA_P` if predefined one is used
* configure macro `MRB_USE_LINK_TIME_RO_DATA_P` is no longer used
* contributions for new platforms are welcome
2021-08-26 16:06:01 +09:00
Yukihiro "Matz" Matsumoto 36632f55f4 boxing_word.h: embed mrb_float in mrb_value if possible.
Embedding reduce memory consumption, sacrificing precision. It clips least
significant 2 bits from `mrb_float`, so if you need to keep float precision,
define `MRB_USE_FLOAT_FULL_PRECISION`.

`MRB_WORD_BOXING` and `MRB_INT64`:

`mrb_float` (`double`) is embedded in `mrb_value` clipped last 2 bits.

`MRB_WORD_BOXING` and `MRB_INT64` and `MRB_USE_FLOAT_FULL_PRECISION`:

`mrb_float` is allocated in the heaps wrapped by `struct RFloat`.

`MRB_WORD_BOXING` and `MRB_INT32` and `MRB_USE_FLOAT32`:

`mrb_float` (`float`) is embedded in `mrb_value` clipped last 2 bits.
In addition, to reserve bit space in the `mrb_value`, maximum inline
symbol length become 4 (instead of 5) in the configuration.

`MRB_WORD_BOXING` and `MRB_INT32`:

Assume `MRB_USE_FLOAT_FULL_PRECISION` and allocate Float values in heap.
2021-08-21 15:25:58 +09:00
Yukihiro "Matz" Matsumoto 6f305c01a2 value.h: fixed warnings on __APPLE__. 2021-08-19 13:08:39 +09:00
Yukihiro "Matz" Matsumoto 959383df75 value.h: enable mrb_ro_data_p() on __linux__ and __APPLE__.
Probably we should add conditions for FreeBSD, etc.
2021-08-19 12:52:10 +09:00
Yukihiro "Matz" Matsumoto 96a8703ca8 boxing_word.h: simplifies inline symbols on MRB_64BIT. 2021-08-17 16:03:42 +09:00
Yukihiro "Matz" Matsumoto cd02cef465 array.h: avoid duplicated defined of MRB_ARY_NO_EMBED; fix #5520 2021-08-07 06:48:03 +09:00
Yukihiro "Matz" Matsumoto 43999492a7 codegen.c: mrb_prev_pc() to take previous instruction position.
It rescans `s->iseq` so that peephole optimizer can take multiple
previous instructions for constant folding, etc.
2021-07-31 21:11:36 +09:00
Yukihiro "Matz" Matsumoto 6175db2746 irep.h: operand c should be fit in uint8_t. 2021-07-26 10:47:40 +09:00
Yukihiro "Matz" Matsumoto 5c804cf68f Remove redundant include headers.
- stdlib.h
- stddef.h
- stdint.h
- stdarg.h
- limits.h
- float.h
2021-07-25 13:07:10 +09:00
Yukihiro "Matz" Matsumoto b5039fdb6c debug.h: use uint8_t instead of char for BER compressed binary. 2021-07-09 14:10:48 +09:00
Yukihiro "Matz" Matsumoto f16ea05a17 debug.c: new debug line information format mrb_debug_line_packed_map.
It uses BER number compression of delta of instruction positions and line
numbers. BER compression is a variable length number representation.

* `mrb_debug_line_ary`: array of line numbers represented in `uint16_t`.
  `[lineno, lineno, ...]`

* `mrb_debug_line_flat_map`: array of `mrb_irep_debug_info_line`, which
  is `struct {uint32_t pos; uint16_t lineno}`, for each line.

* `mrb_debug_line_packed_map` [new]: sequence of BER compressed 2
  numbers, `pos_delta, lineno_delta`. Deltas are differences from
  previous values (starting `0`). `line_entry_counts` represents total
  length of a packed map string for this type.
2021-07-08 07:24:12 +09:00
Yukihiro "Matz" Matsumoto 39aed39139 ops.h: made terms consistent.
- `Lit` -> `Pool`
- `SEQ` -> `Irep`
2021-07-03 12:06:44 +09:00
Yukihiro "Matz" Matsumoto d9a8981c26 vm.c: OP_DEF to push a symbol to a register.
The code generator no longer need to emit `OP_LOADSYM` after `OP_DEF`.
`doc/opcode.md` is also updated.
2021-07-03 06:40:05 +09:00
Yukihiro "Matz" Matsumoto 99dbcec89c Revert "Remove OP_EXT[123] from operands."
This reverts commit fd10c72319.

I thought it was OK to restrict index value within 1 byte, but in some
cases index value could be 16 bits (2 bytes). I had several ideas to
address the issue, but reverting `fd10c72` is the easiest way. The
biggest reason is `mruby/c` still supports `OP_EXT[123]`, so that they
don't need any additional work.
2021-06-30 22:41:13 +09:00
dearblue cc95e346fd Added MRB_OBJ_ALLOC() macro that does not require a cast
The `MRB_OBJ_ALLOC()` macro function returns a pointer of the type corresponding to the constant literal defined in `enum mrb_vtype`.
2021-06-20 11:08:28 +09:00
Yukihiro "Matz" Matsumoto 2341e4f5e5 Merge pull request #5445 from jbampton/add-codespell-pre-commit-hook
Run pre-commit with GitHub Actions
2021-06-16 09:15:07 +09:00
John Bampton dab5502e8a Run pre-commit with GitHub Actions
Running pre-commit with GitHub Actions now gives us more tests and coverage

Remove duplicate GitHub Actions for merge conflicts and trailing whitespace

Remove duplicate checks for markdownlint and yamllint from the GitHub Super-Linter

Add new custom pre-commit hook running with a shell script to sort alphabetically and uniquify codespell.txt

Add new pre-commit hook to check spelling with codespell

https://github.com/codespell-project/codespell

Fix spelling
2021-06-16 08:34:54 +10:00
Yukihiro "Matz" Matsumoto 7c8a91ff5a numeric.c: restore fmt argument for backward compatibility.
`mrb_float_to_str()` used to take `fmt` argument. We thought no one used
the function, and OK to remove the argument. But at least `mruby-redis`
gem used the function.
2021-06-15 16:19:16 +09:00
Yukihiro "Matz" Matsumoto 49af1fca03 readint.c: add new function mrb_int_read.
Difference from `strtoul(3)`:

* reads `mrb_int` based on configuration
* specifies the end of the string
* no sign interpretation
* base 10 only
2021-06-11 15:14:17 +09:00
Yukihiro "Matz" Matsumoto d8ec544564 string.c: remove two unused functions.
* `mrb_cstr_to_inum()`
* `mrb_cstr_to_dbl()`
2021-06-08 17:00:54 +09:00
Yukihiro "Matz" Matsumoto cb7d351bfb ops.h: fix term consistency. Lit -> Pool. 2021-06-05 08:08:25 +09:00
Yukihiro "Matz" Matsumoto 8a4bcc58c9 numeric.c: introduce mrb_int_to_cstr() to dump mrb_int.
* refactor `mrb_integer_to_str()`
* refactor `mrb_str_format()`
2021-05-30 08:15:51 +09:00
Yukihiro "Matz" Matsumoto 48b08fecba array.c: unify mrb_ary_ref and mrb_ary_entry
Use only `mrb_ary_entry` hereafter.
2021-05-27 21:43:43 +09:00
Yukihiro "Matz" Matsumoto 5c7fe225a6 fp_fmt.c: remove mrb_float_to_cstr().
The function was intended to be a utility function for `mruby-sprintf`.
The functionality was integrated into `sprintf.c`.
2021-05-22 14:16:55 +09:00
Yukihiro "Matz" Matsumoto 328563995f fmt_fp.c: replace with public domain float format routine; ref #5448
The original code can be found in `https://github.com/dhylands/format-float`.

Changes:
- support `double`
- support `#` (alt_form) modifier
- small refactoring
2021-05-22 09:35:27 +09:00
Yukihiro "Matz" Matsumoto beaf365dd4 numeric.h: remove 2 functions from MRB_API
- `mrb_float_to_str()`
- `mrb_float_to_cstr()`

Both functions will be replaced to support new coming `format-float.c`.
2021-05-21 08:13:38 +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
Yukihiro "Matz" Matsumoto ea7f1953c3 Rename mrb_fixnum_to_str to mrb_integer_to_str. 2021-05-17 13:59:01 +09:00
Yukihiro "Matz" Matsumoto 080fdbf9e8 Rename mrb_flo_to_fixnum to mrb_float_to_integer. 2021-05-17 13:58:54 +09:00
Yukihiro "Matz" Matsumoto e7db158321 numeric.h: reduce conditional compilation branch on MRB_NO_FLOAT. 2021-05-16 18:21:08 +09:00
Yukihiro "Matz" Matsumoto 1d0cfb359c Update mrb_bool definition; close #2385
- move `TRUE/FALSE` definition from `mrbconf.h` (not configurable)
- use C/C++ definition of boolean type for `mrb_bool`

The fix is originally written by @take-cheeze.
2021-05-13 19:30:41 +09:00
dearblue 94c5c8f44c Introduce MRB_GC_RED
Replaces the magic number `7` except in `src/gc.c`.
2021-04-24 10:31:21 +09:00
Yukihiro "Matz" Matsumoto 1a82bab1b0 error.h: rename mrb_protect_raw to mrb_protect_error; #5415
- `_raw` does not describe the nature of the function
- the function protect errors during C function execution
2021-04-22 17:01:33 +09:00
KOBAYASHI Shuji 903fbd767a Remove unused struct in include/mruby/variable.h 2021-04-20 16:53:11 +09:00
Yukihiro "Matz" Matsumoto ff366b65eb Merge pull request #5415 from dearblue/unwind-mrb_protect
Introducing the `mrb_protect_raw()` API function
2021-04-19 22:34:20 +09:00
dearblue 891e852286 Introducing the mrb_protect_raw() API function
The purpose is two-fold:

 1. to be able to specify a pointer directly when user data is used

    When using `mrb_protect()`, it is necessary to allocate objects by `mrb_obj_cptr()` function when using user data.

    Adding `mrb_protect_raw()` will make it simpler to reimplement `mrbgems/mruby-error`.

 2. to correctly unwind callinfo when an exception is raised from a C function defined as a method (the main topic)

    If a method call is made directly under `mrb_protect()` and a C function is called, control is returned from `mrb_protect()` if an exception occurs there.
    In this case, callinfo is not restored, so it is out of sync.
    Moreover, returning to mruby VM (`mrb_vm_exec()` function) in this state will indicate `ci->pc` of C function which is equal to `NULL`, and subsequent `JUMP` will cause `SIGSEGV`.

    Following is an example that actually causes `SIGSEGV`:

    - `crash.c`

      ```c
      #include <mruby.h>
      #include <mruby/compile.h>
      #include <mruby/error.h>

      static mrb_value
      level1_body(mrb_state *mrb, mrb_value self)
      {
        return mrb_funcall(mrb, self, "level2", 0);
      }

      static mrb_value
      level1(mrb_state *mrb, mrb_value self)
      {
        return mrb_protect(mrb, level1_body, self, NULL);
      }

      static mrb_value
      level2(mrb_state *mrb, mrb_value self)
      {
        mrb_raise(mrb, E_RUNTIME_ERROR, "error!");
        return mrb_nil_value();
      }

      int
      main(int argc, char *argv[])
      {
        mrb_state *mrb = mrb_open();
        mrb_define_method(mrb, mrb->object_class, "level1", level1, MRB_ARGS_NONE());
        mrb_define_method(mrb, mrb->object_class, "level2", level2, MRB_ARGS_NONE());
        mrb_p(mrb, mrb_load_string(mrb, "p level1"));
        mrb_close(mrb);
        return 0;
      }
      ```

    - compile & run

      ```console
      % `bin/mruby-config --cc --cflags --ldflags` crash.c `bin/mruby-config --libs`

      % ./a.out
      zsh: segmentation fault (core dumped)  ./a.out
      ```

    After applying this patch, it will print exception object and exit normally.

The `mrb_protect()`, `mrb_ensure()` and `mrb_rescue_exceptions()` in `mrbgems/mruby-error` have been rewritten using `mrb_protect_raw()`.
2021-04-19 22:11:57 +09:00
Yukihiro "Matz" Matsumoto 9d362f2c27 backtrace.c: remove MRB_API from internal functions.
- `mrb_exc_backtrace` to implement `Exception#backtrace`
- `mrb_get_backtrace` to implement `#caller`
2021-04-19 15:49:51 +09:00
Yukihiro "Matz" Matsumoto ff2ffe3b07 proc.h: add type cast to silence warning; ref #5402 2021-04-12 15:21:54 +09:00
John Bampton d8622fc68b chore: fix spelling 2021-04-03 19:51:01 +10:00
Yukihiro "Matz" Matsumoto 08f9d5bab5 rational.c: overhaul rational operators.
- define `MRB_TT_RATIONAL`
- change object structure (`struct RRational`)
- add memory management for `MRB_TT_RATIONAL`
- avoid operator overloading as much as possible
- implement division overloading in C
- as a result, performance improved a lot
2021-03-19 10:37:10 +09:00
Yukihiro "Matz" Matsumoto 4ae5ae3535 codegen.c: no integer overflow error in codegen; close #5376
Add new pool value type `IREP_TT_BIGINT` and generate integer overflow
error in the VM. In the future, `mruby` will support `Bignum` for
integers bigger than `mrb_int` (probably using `mpz`).
2021-03-12 16:21:32 +09:00
Yukihiro "Matz" Matsumoto cb3a6dd168 ISO C99 doesn't support unnamed unions; fix #5354 2021-03-08 11:50:52 +09:00
Yukihiro "Matz" Matsumoto 111e4e1ba8 Merge pull request #5373 from mruby/stable
Merge mruby 3.0.0
2021-03-05 17:36:12 +09:00