Commit graph

22 commits

Author SHA1 Message Date
Nobuyoshi Nakada
6179cc0118
[DOC] Fill undocumented documents 2025-08-04 02:23:43 +09:00
Nobuyoshi Nakada
abafb662ea
Adjust indents [ci skip] 2025-07-26 22:07:28 +09:00
Aaron Patterson
3d5619c8b1 Introduce Namespace#eval
This commit adds an `eval` method to `Namespace` that takes a string and
evaluates the string as Ruby code within the context of that namespace.

For example:

```ruby
n = Namespace.new

n.eval("class TestClass; def hello; 'from namespace'; end; end")

instance = n::TestClass.new
instance.hello # => "from namespace"
```

[Feature #21365]
2025-06-26 10:18:14 -07:00
Jean Boussier
96a0c2065a Mark RClass instance that may be namespaced with RCLASS_NAMESPACEABLE 2025-06-23 10:04:58 +01:00
Jean Boussier
393e9a5f3e Optimize rb_namespace_available
Rather than to lazily check the env using a trinary
value, we can more straightforwardly check for the
env during the VM boot.

This allow `rb_namespace_available` to just be a pointer
dereference.
2025-06-23 10:04:58 +01:00
Jean Boussier
8b5ac5abf2 Fix class instance variable inside namespaces
Now that classes fields are delegated to an object with its own
shape_id, we no longer need to mark all classes as TOO_COMPLEX.
2025-06-12 13:43:29 +02:00
Satoshi Tagomori
dd4e39a115 Delete useless Namespace#current_details
The implementation of Namespace#current_details shows warning about
use of snprintf directive arguments (only in gcc environments?).
This method will be useless when the current namespace management
will be updated.
2025-06-07 18:19:59 +09:00
Nobuyoshi Nakada
aad9fa2853
Use RB_VM_LOCKING 2025-05-25 15:22:43 +09:00
Hiroshi SHIBATA
72387ebd0e
Fix typos: misspell -w -error -source=text namespace.c 2025-05-19 09:46:03 +09:00
Kazuhiro NISHIYAMA
fa2414f29a
Fix a typo 2025-05-13 16:00:42 +09:00
Nobuyoshi Nakada
b758b6f204
Prevent namespace inspected strings from GC 2025-05-13 09:05:08 +09:00
Takashi Kokubun
dc5555d74a Fix -Wmaybe-uninitialized
../namespace.c: In function ‘current_namespace’:
../namespace.c:221:48: warning: ‘proc_ns’ may be used uninitialized [-Wmaybe-uninitialized]
  221 |                     if (permit_calling_builtin || (proc_ns && NAMESPACE_USER_P(proc_ns)))
      |                         ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../namespace.c:204:31: note: ‘proc_ns’ was declared here
  204 |         const rb_namespace_t *proc_ns;
      |                               ^~~~~~~

In function ‘copy_ext_file’,
    inlined from ‘rb_namespace_local_extension’ at ../namespace.c:855:18:
../namespace.c:768:21: warning: ‘written’ may be used uninitialized [-Wmaybe-uninitialized]
  768 |             wrote = fwrite(buffer+written, 1, read-written, dst);
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../namespace.c: In function ‘rb_namespace_local_extension’:
../namespace.c:748:25: note: ‘written’ was declared here
  748 |     size_t read, wrote, written;
      |                         ^~~~~~~

In function ‘copy_ext_file’,
    inlined from ‘rb_namespace_local_extension’ at ../namespace.c:855:18:
../namespace.c:768:21: warning: ‘read’ may be used uninitialized [-Wmaybe-uninitialized]
  768 |             wrote = fwrite(buffer+written, 1, read-written, dst);
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../namespace.c: In function ‘rb_namespace_local_extension’:
../namespace.c:748:12: note: ‘read’ was declared here
  748 |     size_t read, wrote, written;
      |            ^~~~
2025-05-11 13:37:47 -07:00
Jean Boussier
54c2edc05d Fix Namespace#inspect to display its internal id 2025-05-11 17:38:14 +02:00
Satoshi Tagomori
b94df81be2 RUBY_TYPED_WB_PROTECTED should be specified with write barrier protection on this object.
https://github.com/tagomoris/ruby/pull/7
RUBY_TYPED_FREE_IMMEDIATELY can be added because namespace_entry_free does no IO nor
things to block.
2025-05-11 23:32:50 +09:00
Jean Boussier
e9b538bb35 Fix namespace_initialize to not crash on boot
```
/opt/rubies/head-namespaces/bin/ruby(sigsegv+0x84) [0x104e897e8]
/usr/lib/system/libsystem_platform.dylib(_sigtramp+0x38) [0x18de56de4]
/opt/rubies/head-namespaces/bin/ruby(object_id+0x80) [0x104d7d420]
/opt/rubies/head-namespaces/bin/ruby(object_id+0x80) [0x104d7d420]
/opt/rubies/head-namespaces/bin/ruby(rb_initialize_main_namespace+0xe4) [0x104ddaa20]
/opt/rubies/head-namespaces/bin/ruby(ruby_opt_init+0x120) [0x104e7f524]
/opt/rubies/head-namespaces/bin/ruby(ruby_process_options+0x1370) [0x104e7e31c]
/opt/rubies/head-namespaces/bin/ruby(ruby_options+0xb0) [0x104d69844]
/opt/rubies/head-namespaces/bin/ruby(main+0x64) [0x104ca8d54]
```

I'm not too sure why `rb_obj_id` crashes, but I suspect it's called too
early. Either way I don't think generating an object_id for namespaces
is a good idea. If all we need is a unique number we can do that
for much cheaper.
2025-05-11 23:32:50 +09:00
Satoshi Tagomori
8199e6e1a6 Show experimental warning when namespace is enabled 2025-05-11 23:32:50 +09:00
Satoshi Tagomori
82f645e818 Namespace::Entry is long living object 2025-05-11 23:32:50 +09:00
Satoshi Tagomori
294b52fb9b Follow the code style about else 2025-05-11 23:32:50 +09:00
Satoshi Tagomori
ff790c759e Compact prime classext readable/writable flags
To make RClass size smaller, move flags of prime classext readable/writable to:
 readable - use ns_classext_tbl is NULL or not (if NULL, it's readable)
 writable - use FL_USER2 of RBasic flags
2025-05-11 23:32:50 +09:00
Yusuke Endoh
4b33b468ac Fix function pointer type mismatch with rb_define_private_method
`rb_define_private_method` performs strict type checking on the function
pointer. As a result, we cannot pass the function a generic signature.

```
  /github/workspace/src/namespace.c:1097:72: note: expected 'VALUE (*)(void)' {aka 'long unsigned int (*)(void)'} but argument is of type 'VALUE (*)(int,  VALUE *, VALUE)' {aka 'long unsigned int (*)(int,  long unsigned int *, long unsigned int)'}
   1097 | namespace_define_loader_method(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
        |                                                                ~~~~~~~~^~~~~~~~~~~~~~
```

This commit defines the method directly to avoid the mismatch error.
2025-05-11 23:32:50 +09:00
Yusuke Endoh
1e4f7a28b8 Fix "old-style function definition"
```
namespace.c: In function ‘rb_namespace_available’:
namespace.c:55:1: warning: old-style function definition [-Wold-style-definition]
   55 | rb_namespace_available()
      | ^~~~~~~~~~~~~~~~~~~~~~
```
2025-05-11 23:32:50 +09:00
Satoshi Tagomori
382645d440 namespace on read 2025-05-11 23:32:50 +09:00