164 Commits

Author SHA1 Message Date
Chris Hasiński b85c520843 Remove stale self-corruption workaround in recvfrom_nonblock
The s = self workaround and XXX comment in recvfrom_nonblock date back
to the initial import of mruby-socket. The underlying bug where self
became a SystemcallException inside ensure blocks has since been fixed.

Verified that self correctly refers to the socket object in ensure
blocks after exceptions from recvfrom.
2026-03-30 22:14:26 +02:00
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 16:50:58 +09:00
Yukihiro "Matz" Matsumoto 71cb3c2e3a class.c: allocate ROM table wrappers per mrb_state
ROM method tables used static mrb_mt_tbl variables shared
across the process. The next pointer in each wrapper was
mutated by mrb_mt_init_rom(), causing cross-state
contamination when multiple mrb_state instances existed.

Allocate mrb_mt_tbl wrappers per-state via mrb_malloc().
The const mrb_mt_entry[] arrays remain static and shared.
Wrappers are tracked in mrb->rom_mt and freed at mrb_close().

Remove MRB_MT_ROM_TAB macro; add MRB_MT_INIT_ROM macro that
auto-computes size and calls the new mrb_mt_init_rom().

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 22:24:31 +09:00
Yukihiro "Matz" Matsumoto b460554d33 vm.c: generalize pre-dispatch argument count check for C methods
Replace check_method_noarg() with check_argument_count() that validates
min <= argc <= max using the full aspec stored in mrb_method_t.flags.
This catches ArgumentError earlier at dispatch time, before entering
the C function.

The old check only handled the special case of aspec==0 (NOARG).
The new check extracts REQ, OPT, REST, POST, KEY, and KDICT from
the aspec and validates accordingly. Keyword hash is counted as
a positional arg only when the method doesn't accept keywords.

Remove MRB_METHOD_NOARG_P macro from proc.h (subsumed by aspec check).
Fix 15 incorrect aspec declarations across the codebase that were
exposed by the stricter enforcement.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 14:25:48 +09:00
Yukihiro "Matz" Matsumoto 483c155a41 class.c: store aspec in ROM method table entries
Restore MRB_ARGS_* argument specs and ISO section comments to all
709 ROM method table entries. The aspec is encoded in bits 4-27 of
the flags field; MRB_MT_NOARG is now auto-derived from aspec==0.

Add MRB_MT_ENTRY_PRIVATE() macro for private methods (53 entries)
and MRB_MT_ASPEC() accessor for extracting aspec from flags.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:44:28 +09:00
Yukihiro "Matz" Matsumoto 20b9002214 class.c: merge conditional methods into ROM tables
Move conditional mrb_define_method_id() calls into ROM entry
arrays using #ifdef guards. With linear search, sizeof in
MRB_MT_ROM_TAB() adjusts automatically after preprocessing.

Cross-class ROM tables (methods a gem defines on a class it does
not own) are reverted to mrb_define_method_id(). Multiple gems
should not add ROM table layers to the same class; each layer
costs a 16-byte mrb_mt_tbl struct in RAM and deepens the lookup
chain. Use mrb_define_method_id() for cross-class methods.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:05:03 +09:00
Yukihiro "Matz" Matsumoto 8adba34bd9 class.c: auto-set MRB_MT_FUNC in MRB_MT_ENTRY macro
Since ROM table entries are always C functions, have the
MRB_MT_ENTRY() macro set MRB_MT_FUNC automatically. This
simplifies entry definitions across all 32 source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 10:37:06 +09:00
Yukihiro "Matz" Matsumoto 0fab703028 class.c: use linear search for method tables; make ROM entries const
Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 08:26:05 +09:00
Yukihiro "Matz" Matsumoto bde2202100 class.c: refactor ROM method tables to array-of-structs layout
Replace the parallel-arrays (struct-of-arrays) ROM method table
layout with an array-of-structs layout where each mrb_mt_entry
bundles its function pointer and symbol key together.

New MRB_MT_ENTRY() and MRB_MT_ROM_TAB() macros simplify ROM table
definitions from a 3-part pattern (SIZE define + anonymous struct +
mrb_mt_tbl) to a 2-part pattern (entries array + mrb_mt_tbl).

Internal mt_* functions in class.c are simplified: single memmove/
memcpy operations replace paired key+value operations.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 23:59:30 +09:00
Yukihiro "Matz" Matsumoto 0ed26f8352 class.c: rename mt_/MT_ to mrb_mt_/MRB_MT_ for non-static identifiers
Follow mruby's naming convention: non-static types, macros, and
functions use the mrb_/MRB_ prefix. Renamed:
- union mt_ptr -> union mrb_mt_ptr
- mt_tbl -> mrb_mt_tbl
- MT_KEY(), MT_FUNC, MT_NOARG, MT_PUBLIC, MT_PRIVATE -> MRB_MT_*
- MT_KEY_SHIFT, MT_READONLY_BIT, MT_REMOVED_P -> MRB_MT_*
- mt_init_rom() -> mrb_mt_init_rom()
File-local static functions and macros in class.c are unchanged.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 15:22:55 +09:00
Yukihiro "Matz" Matsumoto 52c71f5b99 mrbgems: remove MRB_NO_PRESYM guards from additional gems
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 12:17:43 +09:00
Yukihiro "Matz" Matsumoto 653bf360bc mruby-socket: ROM method tables for Socket classes
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 09:39:30 +09:00
Yukihiro "Matz" Matsumoto 2813f794a2 mruby-io: rename mruby/ext/io.h to mruby/io.h
Simplify the header path to be consistent with mruby/time.h.
The ext/ subdirectory was unnecessary.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:11 +09:00
Yukihiro "Matz" Matsumoto 3cc4a77f49 mruby-socket: standardize block parameter spacing
changed block spacing from { | to {| for consistency.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:09 +09:00
Yukihiro "Matz" Matsumoto ac103ac300 mruby-socket: add error helper function to eliminate goto
Add invalid_address_error() helper function to replace goto statements
used for error handling. This function is marked with mrb_noreturn
attribute since it calls mrb_raise() which never returns.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-12 10:25:36 +09:00
Yukihiro "Matz" Matsumoto ad51bf848b mrbgem.rake: simplify hal selection logic
remove redundant visualcpp and mingw checks since for_windows? already
detects all windows builds including visual c++ and mingw.

ref #6653

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 11:11:27 +09:00
Yukihiro "Matz" Matsumoto 28b567ae78 Merge pull request #6653 from dearblue/mingw
Improve HAL-related components for MinGW
2025-10-27 10:56:28 +09:00
Yukihiro "Matz" Matsumoto c1653debb1 mruby-socket: combine variable declaration with initialization 2025-10-26 20:00:04 +09:00
dearblue ea215bc19c Fixed HAL auto-detection order
Because MinGW was not recognized as Windows during cross-builds.
2025-10-25 21:06:16 +09:00
Yukihiro "Matz" Matsumoto 917add467b mruby-socket: use hal-win-socket for mingw
mingw uses winsock2 instead of posix sockets (sys/socket.h). removed
mingw from linux/bsd pattern to let for_windows? predicate select
hal-win-socket instead.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 22:45:19 +09:00
Yukihiro "Matz" Matsumoto feca90ceab hal: fix selection to use toolchain instead of RUBY_PLATFORM
when building with MSVC on Windows, RUBY_PLATFORM (from the Ruby
installation running rake) may indicate "mingw" if Ruby was installed
via RubyInstaller, causing incorrect selection of POSIX HALs instead
of Windows HALs.

fixed by checking spec.build.primary_toolchain first:
- if toolchain is "visualcpp", select Windows HALs
- otherwise fall through to existing platform checks

this ensures MSVC builds use hal-win-* gems even when Ruby itself
was installed with MinGW.

affected gems:
- mruby-dir
- mruby-io
- mruby-socket
- mruby-task

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 18:20:32 +09:00
Yukihiro "Matz" Matsumoto 610ff67906 HAL: rename functions to mrb_hal_<feature>_<name> convention
rename all HAL functions from mrb_<feature>_hal_<name>() to
mrb_hal_<feature>_<name>() for better grouping and clarity. this makes all
HAL functions immediately identifiable with the mrb_hal_* prefix.

affected gems:
- mruby-task: mrb_task_hal_* -> mrb_hal_task_*
- mruby-io: mrb_io_hal_* -> mrb_hal_io_*
- mruby-socket: mrb_socket_hal_* -> mrb_hal_socket_*
- mruby-dir: mrb_dir_hal_* -> mrb_hal_dir_*

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 09:59:03 +09:00
Yukihiro "Matz" Matsumoto f81cadfed5 mrbgems: standardize HAL header include patterns
changed from angle brackets to quotes for gem-local HAL headers
(task.h, io_hal.h, socket_hal.h), and removed relative path prefix
from task.h include. this follows the mrbgem build system convention
where gem/include/ is automatically added to the include path.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 18:52:28 +09:00
Yukihiro "Matz" Matsumoto 0524526e41 mruby-socket: introduce HAL for platform abstraction
separate platform-specific socket operations into HAL implementations
for POSIX (Linux/macOS/BSD/Unix) and Windows platforms to improve
portability and maintainability

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 17:26:40 +09:00
Yukihiro "Matz" Matsumoto 34ccec6600 mruby-socket: optimize protocol family lookup with compact table
Replace switch statement in socket_option_inspect() with memory-efficient
lookup table following mruby's memory-first design philosophy. Uses compact
linear search over 6 entries instead of large switch statement.

Memory usage: ~200 bytes vs ~1KB switch table (80% reduction)
Performance: O(6) linear search, negligible impact for small table
Behavior: Identical functionality, all tests pass (1723/1724)

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-15 11:38:51 +09:00
Yukihiro "Matz" Matsumoto 80a2183b46 mruby-socket: optimize address family dispatch with compact lookup table
Replace switch statement in sa2addrlist() with memory-efficient lookup table
following mruby's memory-first design philosophy. Uses compact structure with
only valid address family entries instead of wasteful 256-entry array.

Changes:
- Add af_info_t structure for address family metadata
- Create compact af_table[] with only valid entries (~6-8 families)
- Replace manual switch with get_af_info() linear search lookup
- Support platform-specific families (AF_UNIX, AF_LOCAL, AF_LINK, etc.)
- Use offset-based port extraction for better performance

Performance characteristics:
- O(n) linear search where n=6-8 (negligible vs switch statement)
- Eliminates branch prediction overhead
- Easier addition of new address families
- Consistent optimization pattern following mruby memory priority

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-15 11:03:30 +09:00
Yukihiro "Matz" Matsumoto b817e2e7e2 mruby-socket: add comprehensive call-seq documentation for Ruby and C methods
Added complete call-seq documentation for socket programming methods across
all major socket classes in both mrblib/socket.rb (64 Ruby methods) and
src/socket.c (35 C methods):

- Addrinfo: Complete documentation for address information handling including
  creation (new, foreach, ip, tcp, udp, unix), inspection (inspect,
  inspect_sockaddr, to_s), address queries (afamily, pfamily, ipv4?, ipv6?,
  ip?, unix?), data extraction (ip_address, ip_port, ip_unpack, unix_path),
  and conversion methods (to_sockaddr, getnameinfo)

- BasicSocket: Core socket functionality including class configuration
  (do_not_reverse_lookup, do_not_reverse_lookup=), object creation (for_fd),
  address retrieval (local_address, remote_address), and non-blocking
  operations (recv_nonblock)

- IPSocket: Internet protocol socket operations including address information
  (addr, peeraddr), connection methods (bind, connect), data transfer
  (send, recvfrom, recvfrom_nonblock), and address resolution (getaddress)

- TCPSocket/TCPServer: TCP client and server socket operations including
  connection establishment (new, open), server operations (accept,
  accept_nonblock, listen, sysaccept)

- UDPSocket: UDP socket operations for datagram communication including
  initialization and internal address handling

- Socket: Low-level socket operations including creation (new, open),
  address manipulation (sockaddr_in, sockaddr_un, unpack_sockaddr_in,
  unpack_sockaddr_un), connection management (bind, connect, listen),
  data transfer (recvfrom, recvfrom_nonblock), socket pairs (pair),
  and name resolution (getaddrinfo, getnameinfo)

- UNIXSocket/UNIXServer: Unix domain socket operations for local IPC
  including creation (new, socketpair), path handling (path, addr, peeraddr),
  server operations (accept, accept_nonblock, listen, sysaccept), and
  data transfer (recvfrom)

- Addrinfo: Core address resolution methods including getaddrinfo for name
  resolution, getnameinfo for reverse lookups, and unix_path for Unix
  domain socket paths

- BasicSocket: Low-level socket operations including getpeereid for peer
  credentials, getpeername/getsockname for address retrieval, recv/send
  for data transfer, getsockopt/setsockopt for option management,
  shutdown for connection termination, and Windows-specific overrides
  (close, sysread, sysseek, syswrite)

- IPSocket: Internet protocol utilities including ntop/pton for address
  conversion and recvfrom for receiving data with sender information

- Socket: Core socket creation and management including gethostname,
  internal methods (_accept, _bind, _connect, _listen, _socket),
  address utilities (sockaddr_un, socketpair), and platform-specific
  implementations

- Socket::Option: Socket option handling including creation from boolean/
  integer values, accessor methods (family, level, optname, data),
  type conversion (int, bool), and debugging support (inspect)

All methods now have comprehensive call-seq documentation with practical
This significantly improves maintainability and usability of errno
handling for developers working with system call errors and file
operations in embedded Ruby environments.

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:44 +09:00
Yukihiro "Matz" Matsumoto d25ae92c06 mruby-socket: fix file descriptor leaks in accept2 and socketpair
Fixed critical resource leaks by pre-allocating mruby objects before system
calls. Since mrb_str_resize to smaller size and mrb_ary_push within
pre-allocated size cannot fail, moving allocations before socket creation
eliminates all leak potential with minimal code changes.

Co-authored-by: Atlassian Rovo Dev
2025-07-09 14:18:47 +09:00
Yukihiro "Matz" Matsumoto f7813268d6 mruby-socket: inline #_new_with_prelude that is only called once. 2025-06-14 13:39:12 +09:00
Yukihiro "Matz" Matsumoto bf0f37833d mruby-socket: fixed raising RuntimeError from #accept; #6554
In mruby, bare `raise` in rescue clause does not re-raise the last
exception. You should explicitly specify the exception.
2025-06-14 01:56:43 +09:00
Yukihiro "Matz" Matsumoto b5197c22f6 mruby-socket: should not directly call private #initialize; fix #6554 2025-06-10 14:28:26 +09:00
dearblue 1326017d1d Omit the _WIN64 definition check
Checking the official MSVC documentation, if `_WIN64` is defined, then `_WIN32` is also defined.
https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros

I could not find any documentation on MinGW, but I assume it is not a problem.
2024-12-07 22:47:28 +09:00
Yukihiro "Matz" Matsumoto ba8ee7b895 mruby-socket (inet_pton): need to initialize loop variable 2024-09-17 12:05:46 +09:00
Yukihiro "Matz" Matsumoto 921a54ef33 mruby-socket/socket.rb: done some small refactoring 2024-06-14 06:14:27 +09:00
Yukihiro "Matz" Matsumoto 39d577799f mruby-socket: use presym for initialization 2024-06-14 06:14:07 +09:00
Yukihiro "Matz" Matsumoto bff68cbf17 mruby-socket (inet_ntop): refactor addrinfo loop 2024-05-25 16:39:31 +09:00
Yukihiro "Matz" Matsumoto 4e824048c0 mruby-socket: adjust local variable declarations 2024-05-25 16:39:31 +09:00
Yukihiro "Matz" Matsumoto 87b358a342 Including header files in include/* by <> 2024-03-26 13:59:59 +09:00
Yukihiro "Matz" Matsumoto 771b56c796 mruby-socket: hide internal method new_with_prelude
- rename new_with_prelude to _new_with_prelude
- take a block instead of proc
2023-09-28 14:36:42 +09:00
Yukihiro "Matz" Matsumoto 8afb73f3e2 mruby-socket/socket.rb: should not call unix_path for non UNIX Addrinfo
ref #6051
2023-09-19 14:22:49 +09:00
Yukihiro "Matz" Matsumoto 52082ba399 mruby-socket/socket.c (mrb_addrinfo_unix_path): check sockaddr type
`sockaddr` should be a string; fix #6051
2023-09-19 14:22:49 +09:00
Yukihiro "Matz" Matsumoto f43f2fc8f3 mruby-socket/socket.c: use internal name for instance variables
For Socket::Option objects.
2023-08-23 11:27:03 +09:00
Yukihiro "Matz" Matsumoto 2160be7db7 mruby-socket/socket.rb: remove unused canonname accessor 2023-08-22 23:27:08 +09:00
Yukihiro "Matz" Matsumoto bd41dfbcb9 mruby-socket/socket.c (socket_option_inspect): readable family name
Print readable name for know protocol families.
2023-08-17 10:54:59 +09:00
Yukihiro "Matz" Matsumoto 58e10f16e9 mruby-socket/socket.c: include Socket::Constants in C init 2023-08-16 13:23:56 +09:00
Yukihiro "Matz" Matsumoto 1d7c00e920 mruby-socket/socket.c: implement whole Socket::Option class in C
`mruby-pack` gem no longer needed.
2023-08-15 15:29:36 +09:00
Yukihiro "Matz" Matsumoto 83b6a63c16 mruby-socket/socket.c: implement accessors in C 2023-08-15 07:35:29 +09:00
Yukihiro "Matz" Matsumoto e209f0a220 mruby-socket/socket.c (socket_option_init): add integer type checks 2023-08-15 07:31:04 +09:00
Yukihiro "Matz" Matsumoto 892596d372 mruby-socket/socket.c: implement Socket::Option#initialize in C 2023-08-14 23:32:22 +09:00
Yukihiro "Matz" Matsumoto 435cce7efd mruby-socket/socket.c: use mrb_iv_get() instead of accessors 2023-08-12 20:19:32 +09:00