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>
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>
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>
Move MRB_METHOD_FUNC_FL to bit 24 and visibility flags to
bits 25-26 so that MRB_ARGS_*() values (bits 0-23) can be
stored directly without shifting. This makes MRB_MT_PRIVATE
and MRB_METHOD_PRIVATE_FL the same value, eliminating the
dual-constant confusion and simplifying the MRB_MT_ENTRY()
macro to a single OR operation.
Co-authored-by: Claude <noreply@anthropic.com>
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>
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>
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>
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>
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>
OUTINT macro checked ayear > INT_MAX, but timegm() later computes
tm_year + TM_YEAR_BASE (1900), which overflows when tm_year is near
INT_MAX. Tighten the upper bound to INT_MAX - TM_YEAR_BASE.
Found by ClusterFuzz.
Co-authored-by: Claude <noreply@anthropic.com>
When year value is close to MRB_INT_MIN, subtracting TM_YEAR_BASE (1900)
causes signed integer overflow. Add underflow check before the subtraction.
Co-authored-by: Claude <noreply@anthropic.com>
when converting microseconds to nanoseconds, multiplying very large
usec values by 1000 can cause signed integer overflow. for example,
Time.at(0, 9999999999990768) would trigger ASAN runtime error.
fixed by normalizing microseconds >= 1000000 (or <= -1000000) to
seconds before the multiplication, preventing overflow while maintaining
correct time representation. this normalization converts excess
microseconds to seconds, leaving only the fractional part for
multiplication.
applied fix to both time_alloc() and mrb_time_at() functions.
Co-authored-by: Claude <noreply@anthropic.com>
add public api function to retrieve struct tm from time object.
this enables other gems to access time components for formatting
while maintaining encapsulation of internal mrb_time structure.
Co-authored-by: Claude <noreply@anthropic.com>
Replace microsecond storage with nanosecond storage in struct mrb_time
while maintaining full backward compatibility and zero memory increase.
Changes:
- Replace 'usec' field with 'nsec' field in struct mrb_time
- Preserve full nanosecond precision from timespec_get/clock_gettime
- Add Time#nsec and Time#tv_nsec methods for Ruby spec compliance
- Update Time#usec to compute microseconds from nanoseconds
- Convert all arithmetic operations to handle nanosecond precision
- Add comprehensive tests for nanosecond functionality
Platform support:
- Modern systems: True nanosecond precision via timespec_get/clock_gettime
- Older systems: Microsecond precision converted to nanoseconds (gettimeofday)
- Minimal systems: Second precision with synthetic microseconds (time)
Benefits:
- Zero memory overhead (struct remains 80 bytes)
- 100% backward compatible (all existing tests pass)
- Better precision for time arithmetic and comparisons
- Ruby API compliant with standard nanosecond methods
- Automatic precision upgrade on capable systems
Co-Authored-By: Claude <noreply@anthropic.com>
implement gmt_offset, utc_offset, and gmtoff methods as aliases to
complete the ruby time api. all three methods return timezone offset
in seconds, with utc times returning 0 and local times returning the
appropriate offset value.
Co-authored-by: Claude <noreply@anthropic.com>
consolidate repeated Windows platform detection into single macro
MRB_TIME_WINDOWS_NO_STRFTIME_Z and simplify nested conditional blocks
in gettimeofday polyfill for better maintainability.
Co-authored-by: Claude <noreply@anthropic.com>
standardize error messages and types across the codebase:
- use E_RANGE_ERROR consistently for time range violations
- consolidate "uninitialized time" errors with helper function
- clarify epoch-1 detection logic with better comments and structure
- unify "Time out of range" messaging
Co-authored-by: Claude <noreply@anthropic.com>
consolidate time_day and time_mday into single implementation, and
create generic time_wday_p function for all weekday methods, reducing
code duplication and improving maintainability.
Co-authored-by: Claude <noreply@anthropic.com>
skip time_update_datetime() call when timezone conversion is not needed,
eliminating expensive gmtime_r/localtime_r system calls for redundant
conversions like time.utc.getutc or time.local.getlocal.
Co-authored-by: Claude <noreply@anthropic.com>
optimize time_to_s() by using combined strftime format on platforms with
%z support, eliminating redundant function calls for local times.
fix timezone calculation in time_zonename() by copying actual date
components instead of using arbitrary year, ensuring accurate dst handling.
Co-authored-by: Claude <noreply@anthropic.com>
Added complete call-seq documentation for all 39 public methods in the
Time class, improving documentation coverage from 0% to 100%.
Documentation added includes:
Class methods (6):
- Time.now: Get current system time
- Time.at: Create time from epoch seconds
- Time.gm/utc: Create UTC time from components
- Time.local/mktime: Create local time from components
Instance methods (33):
- Arithmetic: +, -, <=> for time calculations and comparisons
- Accessors: year, month, day, hour, min, sec, usec, wday, yday
- Timezone: zone, utc, localtime, getutc, getlocal, utc?, gmt?, dst?
- Conversion: to_i, to_f, to_s, inspect, asctime, ctime, hash
- Initialization: new, initialize_copy
- Weekday helpers: sunday?, monday?, tuesday?, wednesday?, thursday?, friday?, saturday?
Each method now includes:
- Clear method signatures with parameter and return types
- Detailed descriptions of time handling behavior
- Practical examples showing common usage patterns
- Notes about timezone handling and precision
- Consistent formatting following mruby documentation standards
This represents a major improvement in maintainability and usability of
time functionality for developers working with date/time operations in
embedded Ruby environments. The Time class is now fully documented with
comprehensive examples covering all aspects of time manipulation.
Co-authored-by: Atlassian Rovo Dev