Commit Graph

17808 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 1efaaa5570 mruby-io,mruby-dir: improve mingw detection for native builds
add mingw pattern to RUBY_PLATFORM check. native mingw builds were
falling through to windows hal because previous detection only worked
for cross-compilation. now checks RUBY_PLATFORM for mingw along with
linux/darwin/bsd.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:38:37 +09:00
Yukihiro "Matz" Matsumoto 0c849e9020 mruby-bigint: add explicit cast to mp_limb for range-checked values
add explicit cast when assigning mrb_int to mp_limb. the value is
already validated to fit within mp_limb range by checking against
DIG_BASE, but explicit cast silences msvc warning c4244.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:06:31 +09:00
Yukihiro "Matz" Matsumoto e8bcfa71c3 hal-win-task: fix integer conversion warning on msvc
add explicit cast to DWORD when passing usec to Sleep(). Sleep() takes
32-bit DWORD but usec is mrb_int which can be 64-bit, causing warning
c4244.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 17:02:23 +09:00
Yukihiro "Matz" Matsumoto 3d1c4981e7 mruby-io: conditionally compile mrb_lstat for symlink support
only define mrb_lstat when symbolic link macros are available. on
windows/mingw, symlinks are not supported and the function is unused,
causing -Wunused-function warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:56:42 +09:00
Yukihiro "Matz" Matsumoto eba41be3a2 hal-win-dir: rewrite comment to avoid comment nesting warning
remove example containing /* sequence from comment. this triggers
-Wcomment warning on mingw about nested comments.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:54:25 +09:00
Yukihiro "Matz" Matsumoto ed4fb26d75 hal-win-socket: guard _WIN32_WINNT definition to prevent redefinition
only define _WIN32_WINNT if not already defined. mingw headers may
predefine this macro, causing redefinition warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:54:11 +09:00
Yukihiro "Matz" Matsumoto 34205d6ba3 mruby-task: fix integer conversion warning by using uint32_t for sleep functions
change sleep_us_impl and sleep_ms_impl parameters from mrb_int to uint32_t.
this makes the type requirement explicit and resolves msvc warning c4244.
all type conversions happen at ruby boundary functions after validation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:34:01 +09:00
Yukihiro "Matz" Matsumoto 0082dfb7e5 mruby-random: fix unary minus on unsigned type warning
replace (-rot) with (32 - rot) to avoid msvc warning c4146. both
expressions are equivalent when masked with & 31, but the latter
is clearer and doesn't trigger warnings about negating unsigned values.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:12:11 +09:00
Yukihiro "Matz" Matsumoto f25fadf46c mruby-io: fix const qualifier warnings on msvc
remove const qualifier from variables passed to free functions.
msvc is stricter about const correctness than gcc. variables from
mrb_utf8_from_locale and mrb_locale_from_utf8 are dynamically allocated
and need to be freed, so they should not be const.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:10:52 +09:00
Yukihiro "Matz" Matsumoto 91922e05af mruby-io,mruby-dir: use posix hal for mingw instead of windows hal
mingw provides posix-compatible functions (readlink, symlink, opendir, etc.)
so it should use hal-posix-io/dir instead of hal-win-io/dir. detect mingw by
checking if host_target or compiler command contains "mingw". check posix
platforms first so mingw is caught before for_windows check.

this fixes test failures on mingw where readlink returned absolute paths
instead of relative paths, and symlink/socket tests failed due to api
differences between windows native apis and posix apis.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 16:05:49 +09:00
Yukihiro "Matz" Matsumoto b424dfa331 mruby-dir: increase buffer size to prevent truncation warning
increase sandbox path buffer from 1024 to 2048 bytes to accommodate
full path with suffix without truncation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 15:49:45 +09:00
Yukihiro "Matz" Matsumoto 43695029bc mruby-dir: add missing unistd.h include for posix systems
mkdtemp() requires unistd.h on posix systems like macos and linux.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 15:44:51 +09:00
Yukihiro "Matz" Matsumoto 06eba9653b mruby-dir: use hal functions in test code for cross-platform compatibility
replace posix directory functions with hal interface functions in dirtest.c
to fix windows linking errors. test code now uses mrb_hal_dir_open/read/close
instead of opendir/readdir/closedir, and mrb_hal_dir_* for filesystem
operations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 15:27:19 +09:00
Yukihiro "Matz" Matsumoto 96e91004cd mruby-task: reduce initial stack sizes to match fiber allocations
reduced TASK_STACK_INIT_SIZE from 64 to 16 and TASK_CI_INIT_SIZE from 8 to 4,
matching mruby-fiber's conservative allocations. this saves 56 bytes per task
(320 bytes down to 160 bytes for initial allocations). stacks grow dynamically
via mrb_stack_extend when needed.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:13 +09:00
Yukihiro "Matz" Matsumoto 510f1ddb75 mruby-task: fix proc retrieval in execute_task to ensure coherence
use t->c.ci->proc directly with explicit null check instead of falling
back to t->proc (which was removed). with c function boundary checks
preventing suspension in c functions, proc should always be valid on resume.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:13 +09:00
Yukihiro "Matz" Matsumoto 4b25faace0 mruby-task: further optimize struct with redundancy removal and unions
removes duplicate proc field and adds state-based union for result/timeslice,
achieving 16 bytes total savings per task (12.5% reduction):

optimizations:
- removed proc field (stored in c.ci->proc, already marked by gc): 8 bytes
- unified result/timeslice into state union (mutually exclusive): ~4 bytes
- combined with previous commit savings (priority_preemption, started, etc)

total reduction: 128 -> 112 bytes per task

impact:
- 10 tasks: 160 bytes saved
- 50 tasks: 800 bytes saved
- 100 tasks: 1.6 KB saved

all 1770 tests pass with zero functionality changes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:13 +09:00
Yukihiro "Matz" Matsumoto 6d4fecc57c mruby-task: optimize mrb_task struct memory footprint
reduces per-task memory usage by 8 bytes (6.2%) through:
- removing priority_preemption field (always equals priority)
- removing started flag (inferred from context status)
- unifying wakeup_tick/join/mutex into single union

old size: 128 bytes
new size: 120 bytes

all tests pass with no functionality changes.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:12 +09:00
Yukihiro "Matz" Matsumoto 413446657c mruby-random: update readme to reflect pcg algorithm
updates algorithm section to document the change from xoshiro128++
to PCG-XSH-RR. highlights key benefits including 50% memory reduction,
platform-adaptive optimization, and excellent statistical quality.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:12 +09:00
Yukihiro "Matz" Matsumoto f1bab01b4c mruby-random: replace xoshiro with pcg for better memory efficiency
replaces xoshiro128++/xorshift96 with PCG-XSH-RR algorithm. PCG uses
64-bit state compared to xoshiro's 128-bit state, reducing memory
footprint by 50% while maintaining excellent statistical quality.

on 32-bit platforms, uses optimized 32-bit multiplier (0xf13283ad)
requiring only 2 multiplies instead of 3. on 64-bit platforms, uses
standard 64-bit multiplier for maximum quality.

all existing tests pass. api compatibility maintained.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:56:12 +09:00
Yukihiro "Matz" Matsumoto 5058c94d57 hal-win-io: fix windows compilation errors
add missing headers (direct.h for _getcwd, stdint.h for intptr_t) and
fix handle/int pointer truncation warnings by casting through intptr_t.
handles are 64-bit pointers on x64 windows but the hal interface uses
int for pid, requiring intermediate cast to suppress warnings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 14:55:23 +09:00
Yukihiro "Matz" Matsumoto d72b5d5fbc mruby-task: raise exception when task.pass is called from C function
when task.pass is called from within a C function (such as Module.new's
block evaluation), attempting to yield would cause a segfault because C
functions lack valid bytecode program counters (see #6642).

this commit adds C function boundary detection to task.pass, raising a
runtime error when cci > 0 (indicating execution is inside a C function).
this matches fiber's behavior and provides a clear error message instead of
a cryptic segfault.

unlike the previous commit which allowed sleep to fall back to blocking
sleep, task.pass raises an exception because its sole purpose is cooperative
yielding - there is no sensible blocking fallback behavior.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 10:49:26 +09:00
Yukihiro "Matz" Matsumoto f8883178c5 mruby-task: prevent segfault when sleep is called from C function; fix #6642
when sleep() was called from within a C function (such as module.new's block
evaluation), the task scheduler would segfault while attempting to resume the
task. this occurred because C functions don't execute bytecode and thus their
callinfo has no valid program counter (pc). when the task tried to resume
execution, mrb_vm_exec() received a null pc, causing a segmentation fault.

the fix adds two safeguards in task.c:

1. C function boundary detection: before suspending a task for sleep, check
   if we're inside a C function by examining the cci (c call info) field.
   if cci > 0, fall back to blocking sleep via HAL instead of attempting
   cooperative context switch. this preserves sleep functionality without
   raising exceptions, though it blocks other tasks during the sleep period.

2. proc fallback in execute_task(): use the task's stored proc if the
   current callinfo's proc is null, ensuring mrb_vm_exec() always receives
   a valid proc pointer.

this approach prioritizes functionality over strict cooperative multitasking
semantics - tasks can still sleep inside C functions, but the sleep becomes
blocking. the alternative would be raising an exception like fiber does, but
that would break existing code unexpectedly.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 10:45:10 +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 74c0fb9c6e mruby-dir: introduce HAL for platform abstraction
platform-specific directory operations separated into hal-posix-dir and
hal-win-dir gems. this allows mruby-dir to support embedded platforms and
simplifies platform-specific implementations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 08:17:43 +09:00
Yukihiro "Matz" Matsumoto 74a5c840f8 mruby-io: fix const qualifier warning in path_gethome
home variable should be const char* to match mrb_io_hal_gethome return type.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-16 08:00:06 +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 f4dcc3dc3d mruby-io: refactor popen to use HAL functions
eliminates platform-specific popen implementations by using
mrb_io_hal_pipe and mrb_io_hal_spawn_process. removes io_cloexec_pipe,
io_pipe, and io_process_exec functions. io.pipe now also uses
mrb_io_hal_pipe. reduces platform conditionals and improves portability.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 14:17:27 +09:00
Yukihiro "Matz" Matsumoto 74ca22f281 mruby-io: introduce HAL for platform abstraction
separates platform-specific code into hal-posix-io and hal-win-io gems,
making mruby-io platform-independent. HAL interface defined in
mrbgems/mruby-io/include/io_hal.h covers file operations, I/O operations,
and process operations. follows mruby-task dependency pattern where HAL
gems depend on feature gem. ws2_32 library linked in hal-win-io gem.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 11:53:38 +09:00
Yukihiro "Matz" Matsumoto 87c8889726 mruby-task: move platform-specific sleep to hal
task.c used clock_gettime() directly, breaking portability. added
mrb_task_hal_sleep_us() to hal interface.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 08:00:34 +09:00
Yukihiro "Matz" Matsumoto bc10fbd49e mruby-compiler: fix memory leak in gen_literal_array
segment nodes allocated with mrbc_malloc were leaked if gen_string
raised an exception via longjmp. fix by avoiding allocation entirely:
temporarily modify tree structure by saving and clearing cdr pointer,
call gen_string, then restore cdr. no memory is allocated so nothing
leaks even on longjmp.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-15 07:50:51 +09:00
Yukihiro "Matz" Matsumoto 6ff5c7bfa9 mruby-task: rename mrb_tasks_run to mrb_task_run
follows mrb_{gem_name}_{operation} naming convention consistently
with other hal functions like mrb_task_hal_init. the plural form was
semantically correct but inconsistent with gem naming patterns.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 23:11:10 +09:00
Yukihiro "Matz" Matsumoto 4fa51c8ea0 task_hal.h: remove non-HAL function declarations
removes mrb_tasks_run and mrb_task_mark_all from task_hal.h as these
are core scheduler functions, not HAL interface functions. only
mrb_tick remains as it must be called by HAL timer callbacks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 23:10:34 +09:00
Yukihiro "Matz" Matsumoto 9c100ab844 mruby-task: introduce HAL (hardware abstraction layer) for platform support
separates platform-specific timer and interrupt code into hal-posix-task
and hal-win-task gems. mruby-task now uses HAL interface defined in
task_hal.h, making it easier to port to new platforms.

hal-posix-task: uses sigalrm/setitimer for timer, sigprocmask for irq
protection, and SA_RESTART flag to prevent EINTR on system calls.

hal-win-task: uses multimedia timer API and critical_section for irq
protection.

both HALs support multiple mrb_state instances with single shared timer.
auto-detection loads appropriate HAL based on platform.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 09:14:59 +09:00
Yukihiro "Matz" Matsumoto 4181a42cd2 mruby-task: remove unnecessary mruby-fiber dependency
mruby-task uses mrb_context and mrb_fiber_state enum, but these are
part of core mruby, not the mruby-fiber gem. the dependency was not
needed.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 09:14:55 +09:00
Yukihiro "Matz" Matsumoto 4c1b2a59bd mruby-class-ext: fix crash in module comparison with invalid types
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-14 08:22:41 +09:00
Yukihiro "Matz" Matsumoto 4f52868923 mruby-bigint: clean up preprocessor directives in mpz_clear
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 11:30:15 +09:00
Yukihiro "Matz" Matsumoto f69fe329a9 mruby-bigint: fix mrb_bint_copy to properly clone bigints
mrb_bint_copy was creating reference to destination then destroying it
with mpz_init, causing copy to happen in orphaned memory. this made
clone return 0 instead of copying the bigint value.

fix extracts common mpz_t-to-rbigint transfer logic into bint_set
helper, used by both bint_new and mrb_bint_copy. eliminates code
duplication and properly copies source data to destination rbigint
structure, handling both embedded and heap storage cases.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 09:19:29 +09:00
Yukihiro "Matz" Matsumoto 003bdf5031 mruby-bigint: fix null pointer dereference in xor fast path
when xoring bigint with small integer, the fast path assumes source
bigint has allocated limbs. malformed bigints with sn > 0 but sz == 0
caused null pointer access. add defensive check to allocate storage
before accessing c.p[0].

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 09:09:47 +09:00
Yukihiro "Matz" Matsumoto f4d6e67656 throw.h: exclude arm64 from mingw64 builtin setjmp/longjmp; fix #6637
__builtin_setjmp/longjmp are x86/x86_64 specific gcc intrinsics
and not supported on arm64. windows arm64 with msys2 clangarm64
now correctly falls through to standard setjmp/longjmp.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-13 08:41:34 +09:00
Yukihiro "Matz" Matsumoto c04fdf862b mruby-strftime: add readme documentation
add comprehensive readme covering usage, format specifiers,
features, and implementation details.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-12 23:01:28 +09:00
Yukihiro "Matz" Matsumoto b31e22f0bc mruby-strftime: implement time#strftime method
add new mruby-strftime gem providing time#strftime for formatting
time objects using standard format specifiers.

implementation features:
- uses mrb_time_get_tm() api for accessing time components
- handles nul bytes in format strings correctly
- dynamic buffer allocation for variable-length output
- comprehensive test coverage including edge cases

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-12 20:05:08 +09:00
Yukihiro "Matz" Matsumoto daaaafeff8 mruby-time: add mrb_time_get_tm() API for accessing struct tm
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>
2025-10-12 09:33:49 +09:00
Yukihiro "Matz" Matsumoto 572c674c2b mruby-task: fix memory leak in task context cleanup; ref #6641
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 23:01:33 +09:00
Yukihiro "Matz" Matsumoto 48e7592801 mruby-bigint: fix clang warning for struct initialization
add extra braces for nested struct initialization to satisfy
clang's -Wmissing-braces warning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 13:59:54 +09:00
Yukihiro "Matz" Matsumoto 99d4629d59 mruby-pack: add explicit casts to fix msvc warnings
added explicit (int) casts when passing mrb_int count to pack/unpack
functions that expect int parameters. fixes C4244 warnings on windows
msvc builds where mrb_int is 64-bit but int is 32-bit.

count is validated to not exceed INT_MAX by read_tmpl, making these
casts safe.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:47:54 +09:00
Yukihiro "Matz" Matsumoto d1a48c03e2 gc.c: add mrbc stub for mrb_task_mark_all
added forward declaration in gc.c and stub implementation in mrbc stub.c
for mrb_task_mark_all to avoid link errors when mrbc is built without
mruby-task gem.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:41:39 +09:00
Yukihiro "Matz" Matsumoto 7469b79aa8 mruby-task: link with winmm library on windows
windows multimedia timer api requires linking with winmm.lib. added
conditional linker library using spec.for_windows? to match mruby
build system conventions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 12:23:33 +09:00
Yukihiro "Matz" Matsumoto 34bf0c1fd2 mruby-task: add macos and windows hal support
extended posix platform detection to include macos via __APPLE__ and
__MACH__ defines. implemented full windows hal using multimedia timer
(timeSetEvent) and CRITICAL_SECTION for thread synchronization. added
task_count_update stub for unsupported platforms with clear warnings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 11:52:58 +09:00
Yukihiro "Matz" Matsumoto 14b761e200 vm.c: suppress GCC 12+ dangling pointer warning for jmpbuf
add pragma to suppress -Wdangling-pointer warning for intentional
stack variable address storage in exception handling. the pointer
is safely managed and cleared before function returns.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-11 11:10:16 +09:00