185 Commits
Author SHA1 Message Date
Hiroshi SHIBATAandClaude Opus 5.5 cd3657cf06 Remove CharNext from eval_intern.h
ruby_push_include was its last user. A path separator never appears inside a multibyte character, so walk RUBYLIB and -I by bytes everywhere. This also fixes the same skipped separator on Cygwin, where CharNext came from windows.h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 08:26:13 +09:00
Takashi Kokubun a5c7644944 ZJIT: Keep surviving JIT frame across longjmp
A tag established inside a live ZJIT frame can catch a longjmp without unwinding that native frame. Record that fact on rb_vm_tag and materialize only frames above the target, preserving its JITFrame link and keeping JITFrame saves at a single store.
2026-07-21 14:14:32 -04:00
Shugo MaedaandClaude Opus 4.8 cce7041630 [Feature #22097] Add Proc#refined
Add Proc#refined(mod, ...), which returns a new Proc that behaves like
the receiver but with the refinements activated by the given modules in
effect inside its body, without affecting the original Proc.  This is
an alternative to the previously proposed Proc#using
(https://bugs.ruby-lang.org/issues/16461) that does not modify the
existing block and needs no `using Proc::Refinements` declaration.

Refinements are resolved at run time through the cref, so a refined
proc gets two pieces of its own:

* A cref carrying the additional refinements, built by activating the
  modules (rb_using_module_recursive, under the VM lock) on a duplicate
  of the block's captured cref.  It is stored in a hidden ivar on the
  Proc and injected into the block frame on every proc-invocation path
  (Proc#call, yield from C methods, invokeblock/opt_call, and
  instance_eval/instance_exec/module_eval/class_eval via yield_under).
  The captured environment stays shared, so closure variables are still
  shared with the original Proc.  `using` inside the body is rejected
  (CREF_REFINED_PROC), so the cref's refinements never change after
  publication.

* A recursive deep copy of the block's instruction sequences
  (rb_iseq_dup_with_independent_caches) so the new Proc has its own
  inline method caches.  This is required for correctness: the VM
  caches refinement method resolution per call site assuming a single
  lexical cref per iseq, so sharing the iseq would leak the refined
  methods into the original Proc.  Coverage keeps measuring the copied
  iseq.

The copy and the cref are memoized in a hidden identity Hash keyed by
the source iseq (one slot per iseq, matched by the captured cref and
the module arguments, accessed under the VM lock), so repeated calls at
the same site reuse one copy and procs refined with the same modules
share the iseq and the cref.  A differing module set or a ruby2_keywords
flag mismatch rebuilds the entry (with a performance warning).  The
refinements table of the memoized cref is frozen and marked shareable so
the memo also serves other Ractors.

Procs without an iseq block (Symbol#to_proc, C-function procs), procs
created from methods, and procs that already have refinements are
rejected with ArgumentError.

The YJIT/ZJIT Rust bindings are regenerated for the new rb_proc_t field.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 15:21:01 +09:00
Hiroshi SHIBATA 9e46ef483d Remove workarounds for GCC 3 and GCC 4.6-4.8
Ruby requires GCC 4.9 or later as noted in configure.ac. The x86
jmp hint in vm_exec.h was only enabled on GCC 3, and the warning
suppressions in eval_intern.h and iseq.c only fired on GCC 4.6-4.8,
so none of them have any effect on supported compilers.
2026-07-14 13:33:10 +09:00
Takashi KokubunandAlan Wu d485eb663c ZJIT: Store JITFrame in the native stack (#16955)
Co-authored-by: Alan Wu <alanwu@ruby-lang.org>
2026-05-22 10:12:29 -07:00
John Hawthorn 91ae698605 Use compile-time flag to indicate dynamic CREFs
The inline constant cache previously used `RCLASS_SINGLETON_P` to detect
"unstable" CREFs that need ic_cref stored and checked on every IC hit.
This caused the `class << self` pattern to create inline caches which
requires extra checks and can't optimize as well by the JITs.

We can avoid this by replacing the `RCLASS_SINGLETON_P` check with a
`VM_DEFINECLASS_FLAG_DYNAMIC_CREF` flag added to the defineclass
instruction at compile time to indicate a dynamic class scope and
specifically avoid setting it for the `class << self` pattern.

We can apply the same logic to fix dynamic CREF on `module (expr)::Foo`.
We can say that any class definition which uses only constant references
is stable, or at least as stable as the cref it was declared inside.

[Bug #20948]
2026-04-01 21:13:06 -07:00
Satoshi Tagomori d2a587c791 renaming internal data structures and functions from namespace to box 2025-11-07 13:14:54 +09:00
Satoshi Tagomori 76c4663a77 Fix Namespace.current to show its caller's namespace
Calling rb_current_namespace() in rb_namespace_current() means to show
the definition namespace of Namespace.current itself (it's the root always)
but the users' expectation is to show the namespace of the place where
the Namespace.current is called.
2025-09-29 01:15:38 +09:00
Satoshi Tagomori 4f47327287 Update current namespace management by using control frames and lexical contexts
to fix inconsistent and wrong current namespace detections.

This includes:
* Moving load_path and related things from rb_vm_t to rb_namespace_t to simplify
  accessing those values via namespace (instead of accessing either vm or ns)
* Initializing root_namespace earlier and consolidate builtin_namespace into root_namespace
* Adding VM_FRAME_FLAG_NS_REQUIRE for checkpoints to detect a namespace to load/require files
* Removing implicit refinements in the root namespace which was used to determine
  the namespace to be loaded (replaced by VM_FRAME_FLAG_NS_REQUIRE)
* Removing namespaces from rb_proc_t because its namespace can be identified by lexical context
* Starting to use ep[VM_ENV_DATA_INDEX_SPECVAL] to store the current namespace when
  the frame type is MAGIC_TOP or MAGIC_CLASS (block handlers don't exist in this case)
2025-09-29 01:15:38 +09:00
Satoshi Tagomori 382645d440 namespace on read 2025-05-11 23:32:50 +09:00
Yuta Saito 30e5e7c005 Revert "Fix jump buffer leak in setjmp handler in WASI builds"
This reverts the following commits as it's causing OOM in some cases in
ruby/ruby.wasm test suite.
* 372515f33c
* 3a730be8b4
2025-04-02 01:11:01 +09:00
刘皓 3a730be8b4 Fix jump buffer leak in setjmp handler in WASI builds 2025-03-31 11:59:33 +09:00
Masataka Pocke KuwabaraandJeremy Evans 0cab608d3a [Bug #21127] Thread deadlock does not display backtraces (#12721)
Previously, Ruby displayed backtraces for each thread on deadlock. However, it has not been shown since Ruby 3.0.
It should display the backtrace for debugging.

Co-authored-by: Jeremy Evans <code@jeremyevans.net>
2025-02-14 16:31:58 +09:00
Nobuyoshi Nakada 0620f006c2 Remove translit_char
It has been used only for DOSISH other than Windows.
2024-04-07 17:29:23 +09:00
Nobuyoshi Nakada 28a2105a55 Prefer enum ruby_tag_type over int 2024-03-17 15:57:19 +09:00
Nobuyoshi Nakada a91800c4a0 Validate tag type to jump 2024-03-17 15:57:19 +09:00
Samuel Williams 6a0b05f413 Remove SAVE_ROOT_JMPBUF as it no longer has any effect. (#10066) 2024-02-22 22:35:54 +13:00
Yuta Saito 50a5b76dec [wasm] allocate Asyncify setjmp buffer in heap
`rb_jmpbuf_t` type is considerably large due to inline-allocated
Asyncify buffer, and it leads to stack overflow even with small number
of C-method call frames. This commit allocates the Asyncify buffer used
by `rb_wasm_setjmp` in heap to mitigate the issue.

This patch introduces a new type `rb_vm_tag_jmpbuf_t` to abstract the
representation of a jump buffer, and init/deinit hook points to manage
lifetime of the buffer. These changes are effectively NFC for non-wasm
platforms.
2023-11-13 19:17:16 +09:00
Nobuyoshi Nakada 11f33ba620 Add hints of tag jumps for optimization
Just as the state was originally passed as the return value of
`longjmp`, the state stored in EC cannot be 0 when the jump is
captured now too.
2023-08-07 10:32:47 +09:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Nobuyoshi Nakada d650b17686 rb_fiber_terminate must not return [Bug #18497]
In a forked process from a fiber, the fiber becomes the only
fiber, `fiber_switch` does nothing as there is no other fibers,
`rb_fiber_terminate` does not terminate the fiber.  In that case,
reaches the end of `fiber_entry` finaly, which is declared as
"COROUTINE" and should never return.
2022-01-19 19:57:16 +09:00
Yuta Saito 23de01c7aa [wasm] eval_inter.h gc.c vm_core.h: include wasm/setjmp.h instead of sysroot header 2022-01-19 11:19:06 +09:00
Nobuyoshi Nakada 4f74153846 Adjust styles [ci skip] 2021-12-24 21:17:27 +09:00
John HawthornandMatthew Draper 733500e9d0 Lazily create singletons on instance_{exec,eval} (#5146)
* Lazily create singletons on instance_{exec,eval}

Previously when instance_exec or instance_eval was called on an object,
that object would be given a singleton class so that method
definitions inside the block would be added to the object rather than
its class.

This commit aims to improve performance by delaying the creation of the
singleton class unless/until one is needed for method definition. Most
of the time instance_eval is used without any method definition.

This was implemented by adding a flag to the cref indicating that it
represents a singleton of the object rather than a class itself. In this
case CREF_CLASS returns the object's existing class, but in cases that
we are defining a method (either via definemethod or
VM_SPECIAL_OBJECT_CBASE which is used for undef and alias).

This also happens to fix what I believe is a bug. Previously
instance_eval behaved differently with regards to constant access for
true/false/nil than for all other objects. I don't think this was
intentional.

    String::Foo = "foo"
    "".instance_eval("Foo")   # => "foo"
    Integer::Foo = "foo"
    123.instance_eval("Foo")  # => "foo"
    TrueClass::Foo = "foo"
    true.instance_eval("Foo") # NameError: uninitialized constant Foo

This also slightly changes the error message when trying to define a method
through instance_eval on an object which can't have a singleton class.

Before:

    $ ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in <main>': no class/module to add method (TypeError)

After:

    $ ./ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in <main>': can't define singleton (TypeError)

IMO this error is a small improvement on the original and better matches
the (both old and new) message when definging a method using `def self.`

    $ ruby -e '123.instance_eval{ def self.foo; end }'
    -e:1:in `block in <main>': can't define singleton (TypeError)

Co-authored-by: Matthew Draper <matthew@trebex.net>

* Remove "under" argument from yield_under

* Move CREF_SINGLETON_SET into vm_cref_new

* Simplify vm_get_const_base

* Fix leaf VM_SPECIAL_OBJECT_CONST_BASE

Co-authored-by: Matthew Draper <matthew@trebex.net>
2021-12-02 15:53:39 -08:00
Nobuyoshi Nakada 28d03ee776 Remove root_jmpbuf in rb_thread_struct
It has not been used since 1b82c877df.
2021-08-10 19:08:38 +09:00
Samuel Williams 42130a64f0 Replace copy coroutine with pthread implementation. 2021-07-01 11:23:03 +12:00
Benoit Dalozeandxtkoba 68d6bd0873 Fix trivial -Wundef warnings
* See [Feature #17752]

Co-authored-by: xtkoba (Tee KOBAYASHI) <xtkoba+ruby@gmail.com>
2021-05-04 14:56:55 +02:00
Nobuyoshi Nakada 0395aa6bbb Make the return type of rb_char_next the same as CharNext 2021-04-08 21:41:00 +09:00
Nobuyoshi Nakada f748b911c9 Fix infinite loop at illegal sequence [Bug #17729]
As mblen returns -1 on failure, skip the first byte and try the
succeeding bytes in that case.

Close https://github.com/ruby/ruby/pull/4281
2021-03-19 07:15:55 +09:00
Koichi Sasada db31ace934 Threads in a ractor will be killed with the ractor
If a terminating ractor has child threads, then kill all child
threads.
2020-11-11 15:49:02 +09:00
Benoit Daloze cffdacb15a Ignore <internal: entries from core library methods for Kernel#warn(message, uplevel: n)
* Fixes [Bug #17259]
2020-10-26 08:47:33 +01:00
Koichi Sasada ae693fff74 fix releasing timing.
(1) recorded_lock_rec > current_lock_rec should not be occurred
    on rb_ec_vm_lock_rec_release().
(2) should be release VM lock at EXEC_TAG(), not POP_TAG().
(3) some refactoring.
2020-10-14 16:36:55 +09:00
Koichi Sasada c3ba3fa8d0 support exception when lock_rec > 0
If a ractor getting a VM lock (monitor) raises an exception,
unlock can be skipped. To release VM lock correctly on exception
(or other jumps with JUMP_TAG), EC_POP_TAG() releases VM lock.
2020-10-14 14:02:06 +09:00
Nobuyoshi Nakada f3b1b645ed Create backtrace location array directly 2019-12-13 20:47:07 +09:00
卜部昌平 c9ffe751d1 delete unused functions
Looking at the list of symbols inside of libruby-static.a, I found
hundreds of functions that are defined, but used from nowhere.

There can be reasons for each of them (e.g. some functions are
specific to some platform, some are useful when debugging, etc).
However it seems the functions deleted here exist for no reason.

This changeset reduces the size of ruby binary from 26,671,456
bytes to 26,592,864 bytes on my machine.
2019-11-14 20:35:48 +09:00
Koichi Sasada b004d3e830 solve "duplicate :raise event" [Bug #15877]
Without this patch, "raise" event invoked twice when raise an
exception in "load"ed script.
This patch by  danielwaterworth (Daniel Waterworth).
[Bug #15877]
2019-08-08 17:56:04 +09:00
Nobuyoshi Nakada b1aecef873 Use UNALIGNED_MEMBER_PTR
* internal.h (UNALIGNED_MEMBER_ACCESS, UNALIGNED_MEMBER_PTR):
  moved from eval_intern.h.

* compile.c iseq.c, vm.c: use UNALIGNED_MEMBER_PTR for `entries`
  in `struct iseq_catch_table`.

* vm_eval.c, vm_insnhelper.c: use UNALIGNED_MEMBER_PTR for `body`
  in `rb_method_definition_t`.
2019-05-31 16:04:16 +09:00
Nobuyoshi Nakada 83f9183a7e Also GCC 9 provides -Waddress-of-packed-member 2019-05-31 14:05:01 +09:00
shyouhei d83536c980 reduce copy & paste
We see several occurrence of "diagnostic push/pop" so why not
make them macros.  Tested on GCC8 / Clang 6.

Note that ruby.h is intentionally left untouched because we don't
want to introduce new public macros.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 07:07:48 +00:00
nobu 440a6476f2 eval_intern.h: duplicate check
* eval_intern.h (pass_passed_block_handler): remove duplicate
  check.  vm_block_handler_verify() is called in the successive
  function call.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61903 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-18 07:53:26 +00:00
mame cd83444eb3 eval_intern.h: fix a typo
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61061 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-06 22:43:54 +00:00
mame 3af095f84c eval_intern.h: prevent core dump with clang and make test-all
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-06 03:19:17 +00:00
ko1 0e34638f79 remove PUSH_TAG/EXEC_AG/POP_TAG/JUMO_TAG.
* eval_intern.h: remove non-`EC_` prefix *_TAG() macros.
  Use `EC_` prefix macros explicitly.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-06 03:16:08 +00:00
ko1 a52603a8ea make a func static.
* vm_backtrace.c (rb_ec_backtrace_location_ary): make it static and
  remove `rb_` prefix.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-16 09:00:15 +00:00
ko1 3936aaa59b make a func static.
* vm_eval.c (rb_raise_method_missing): make this func static and renmae with
  vm_ prefix.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60798 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-16 07:38:41 +00:00
ko1 8dbaf1bbdd rb_execution_context_t * should not be th
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-07 08:01:26 +00:00
ko1 8d8e339d76 fix comment
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-07 05:41:43 +00:00
ko1 7e9c5aca4c * eval_intern.h (rb_threadptr_tag_state): rename to rb_ec_tag_state.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60687 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-07 05:41:15 +00:00
ko1 48ce4cb414 * eval_intern.h: rename macros rb_thread_raised_* to
rb_ec_raised_*.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60684 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-07 05:22:09 +00:00
ko1 ee189904f0 rename to rb_ec_set_raised().
* thread.c (rb_threadptr_set_raised): rename to rb_ec_set_raised
  and accepts `ec`


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-07 05:12:39 +00:00