adds efficient trailing zero counting and power-of-2 detection
with fast paths for common cases involving powers of 2
Co-authored-by: Claude <noreply@anthropic.com>
optimizes gcd for single-limb numbers using binary algorithm,
avoiding multi-precision overhead for most common cases
Co-authored-by: Claude <noreply@anthropic.com>
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
Rename internal functions to follow mruby's snake_case naming convention:
- mrb_struct_initialize_withArg -> mrb_struct_init_with_args
- mrb_struct_initialize_withKw -> mrb_struct_init_with_keywords
Update all function calls to use the new names. This improves code
consistency and follows established mruby naming conventions.
Co-authored-by: Atlassian Rovo Dev
Replace mrb_funcall_id call with direct mrb_ary_join function call
in error message generation to comply with VM callback restrictions.
This prevents re-entrant VM execution which can cause crashes and
undefined behavior, following mruby's policy of avoiding VM callbacks
from C code.
Co-authored-by: Atlassian Rovo Dev
Replace mrb_intern_lit calls with MRB_SYM and MRB_IVSYM macros for
better performance and consistency. Convert mrb_funcall with string
literals to mrb_funcall_id with MRB_SYM for the keyword_init feature
and other method calls.
Key optimizations:
- keyword_init symbol access using MRB_SYM(keyword_init)
- Instance variable access using MRB_IVSYM(__keyword_init__)
- Method calls using mrb_funcall_id with MRB_SYM(join)
This improves runtime performance by avoiding symbol table lookups
for commonly used symbols and follows mruby's presym conventions.
Co-authored-by: Atlassian Rovo Dev
Updated README.md and C documentation to reflect the new keyword_init
feature added in commit 512d25607b.
Changes include:
- README.md: Added comprehensive examples showing keyword initialization
usage, including basic usage, partial initialization, and error cases
- struct.c: Updated call-seq documentation for Struct.new to include
keyword_init parameter and added examples of keyword-based struct
creation and initialization
The keyword_init option allows structs to accept keyword arguments
instead of positional arguments, providing a more explicit and
Ruby-like interface for struct initialization.
Examples added:
- Basic keyword initialization with keyword_init: true
- Partial initialization with missing keys defaulting to nil
- Error handling for mixed positional/keyword arguments
- Empty initialization behavior
Co-authored-by: Atlassian Rovo Dev
Fix incomplete digit processing in power-of-2 base string conversion:
- Add handling for remaining bits after processing all limbs
- Ensure all significant bits are converted to digits
- Maintain correct conversion for large numbers with partial bit patterns
- Add comments clarifying the conversion process
This fixes cases where the last few bits of a number might not be
converted when the total bit count doesn't align perfectly with the
base's bit width, ensuring complete and correct string representation.
Co-authored-by: Claude <noreply@anthropic.com>
Replace expensive pow() calls with pre-computed lookup tables for powers of 10.
Use integer arithmetic during parsing to avoid floating-point precision loss.
Add overflow detection for large numbers while maintaining compatibility.
Co-authored-by: Claude <noreply@anthropic.com>
Replace the traditional Euclidean GCD algorithm with Stein's binary GCD algorithm
for improved performance on large numbers:
- Implement binary GCD (Stein's algorithm) avoiding expensive division operations
- Use bit shifts and subtraction instead of modulo operations
- Handle special cases (zero values) efficiently
- Preserve common factors of 2 for correct results
- Maintain full compatibility with existing rational number functionality
Binary GCD is significantly faster for large numbers as it avoids the costly
division operations used in the Euclidean algorithm, using only bit operations,
addition, and subtraction.
Co-authored-by: Claude <noreply@anthropic.com>
Add overflow protection and memory safety improvements to bigint operations:
- Add overflow check in mpz_realloc to prevent integer overflow in size calculations
- Fix zero-initialization loop by preserving original size during reallocation
- Improve mpz_clear to prevent double-free by nullifying pointer after free
- Add bounds checking to mpz_get_str for string conversion buffer allocation
- Add documentation comments clarifying memory allocation strategies
- Add helper macros MPZ_TMP_INIT/CLEAR for safer temporary variable management
These changes prevent potential memory corruption, buffer overflows, and crashes
while maintaining full compatibility with existing bigint functionality.
Co-authored-by: Claude <noreply@anthropic.com>
Instances cannot be created with `MRB_TT_FALSE`.
_**Compatibility Note**_
This change may cause runtime errors.
However, that is probably because it is not set correctly by `MRB_SET_INSTANCE_TT()`.
The purpose is to force the setting of the type tag.
This is in preparation for subsequent commits that will prevent the creation of instances with `MRB_TT_FALSE`.
Added complete call-seq documentation for proc binding functionality across
both source and test files:
## Core Implementation (src/proc_binding.c):
### Main Method:
- binding: returns Binding object capturing proc's execution context,
includes comprehensive examples showing local variable access, parameter
binding, and scope retention with practical usage patterns
### Helper Functions:
- mrb_mruby_proc_binding_gem_init: initializes gem by adding binding method
to Proc class, explains method signature and return type behavior
Co-authored-by: Atlassian Rovo Dev