implements context-aware tab completion for mirb supporting all readline
variants (GNU readline, libedit, linenoise) with graceful degradation
when no readline library is available.
completion features:
- method names on objects (e.g., "hello".re<Tab> completes to reverse, replace)
- local variables from compiler context
- global variables via Ruby introspection
- constants and class names
- Ruby keywords
architecture:
- core completion engine is library-agnostic
- thin adapters for readline/libedit and linenoise
- context detection based on cursor position analysis
- safe receiver evaluation with exception handling
- proper word break characters so "String.new" works correctly
implementation adds:
- mirb_completion.h: interface definitions and data structures
- mirb_completion.c: complete implementation (~670 lines)
- mirb.c: integration with setup/cleanup calls
Co-authored-by: Claude <noreply@anthropic.com>
addresses #6626 where users building portable binaries need explicit control
over readline detection instead of auto-detection.
MRUBY_MIRB_READLINE values:
auto (default) - auto-detect: try readline, then edit, then linenoise
readline, gnu - force GNU readline only
edit, libedit - force libedit only
linenoise - force linenoise only
none, off, false, disabled - use plain input mode (no readline)
close#6626
Co-authored-by: Claude <noreply@anthropic.com>
runtime errors now distinguish between:
- errors in current input: show relative line number
- errors from previously defined methods: show method context
examples:
1> a.foo
line 1: undefined method 'a' for Object (NoMethodError)
1> def foo
2* bar
3* end
1> foo
(mirb):in foo: undefined method 'bar' for Object (NoMethodError)
this provides better context since method name is more useful
than line number for errors in previously defined code
Co-authored-by: Claude <noreply@anthropic.com>
syntax errors now show:
- line:column format with relative line numbers (matching prompt)
- source line from user input
- caret indicator pointing to error position
example:
1> x = @@@
line 1:6: syntax error, unexpected invalid token
x = @@@
^
multi-line example:
1> class Foo
2* def bar
3* x = @@@
line 3:6: syntax error, unexpected invalid token
x = @@@
^
Co-authored-by: Claude <noreply@anthropic.com>
add minimal line number decoration to prompts to help track position
within multi-line code blocks. format is 'N>' for initial line and
'N*' for continuation lines. line counter resets after each complete
evaluation for clarity and minimal visual noise.
example:
1> def foo
2* x = 1
3* end
=> :foo
1> 1 + 1
=> 2
Co-authored-by: Claude <noreply@anthropic.com>
Add build configuration for Cosmopolitan Libc, enabling mruby to be
compiled as an "Actually Portable Executable" (APE) that runs natively
on multiple platforms from a single binary.
Supported platforms:
- Linux (x86_64, ARM64)
- macOS (x86_64, ARM64)
- Windows (x86_64)
- FreeBSD (x86_64)
- OpenBSD (x86_64)
- NetBSD (x86_64)
Included binaries:
- mruby.com - mruby interpreter
- mrbc.com - bytecode compiler
- mirb.com - interactive Ruby shell
- mrdb.com - debugger
- mruby-strip.com - debug info stripper
Usage:
COSMO_ROOT=~/cosmo rake MRUBY_CONFIG=cosmopolitan
The cosmocc toolchain can be downloaded from https://cosmo.zip/pub/cosmocc/
benchmarking tools (mruby-benchmark) have been implemented.
update entry to focus on remaining profiler features:
method call tracing, stack profiling, and detailed memory analysis.
Co-authored-by: Claude <noreply@anthropic.com>
add 24 test cases covering all benchmark functionality:
- Benchmark.measure and Benchmark.realtime
- Benchmark::Tms class and its methods (total, to_s, format)
- Benchmark.bm for formatted comparison reports
- Benchmark::Report class
- memory tracking with ObjectSpace integration
- consistency and realistic usage scenarios
suppress output during tests by temporarily setting $stdout to nil
for tests that call Benchmark.bm or Report#report to avoid printing
garbage during test execution.
all tests pass successfully.
Co-authored-by: Claude <noreply@anthropic.com>
add pattern matching section to limitations.md clarifying that only
rightward assignment (expr => var) is currently supported, while
case/in syntax and other pattern types are not yet implemented.
Co-authored-by: Claude <noreply@anthropic.com>
reduce code duplication by introducing MRB_PROC_RESOLVE_ALIAS macro
to handle alias proc resolution in a consistent way across 5 locations.
Co-authored-by: Claude <noreply@anthropic.com>
fix implementation to work correctly in mruby:
- use String#% instead of sprintf for formatting
- use $stdout directly for output instead of bare print/puts
- add nil check for $stdout to handle test environments
- use Object.const_defined? instead of defined? keyword
- create new Tms instance with label instead of instance_variable_set
- add dependencies: mruby-sprintf and mruby-io
Co-authored-by: Claude <noreply@anthropic.com>
add full pattern matching implementation (case/in syntax, array/hash
patterns, guards, etc.) to the todo list for after mruby 3.4.
Co-authored-by: Claude <noreply@anthropic.com>
follow alias chains in mrb_proc_eql() to compare underlying procs,
making Method#== return true for aliased methods as in CRuby.
also fix typo where p1 was checked instead of p2 in CFUNC comparison.
Co-authored-by: Claude <noreply@anthropic.com>
when mpz_mod_2exp() is called with z == x (in-place operation), the
function was calling mpz_clear(ctx, z) which freed x's memory, then
attempting to access x->p[i] - reading freed memory. this caused
Barrett reduction to produce incorrect results in modular
exponentiation.
the fix checks if z == x and handles in-place modification by
adjusting the size and masking directly, without clearing. this is
similar to the memory leak fix for pool→heap transitions.
Co-authored-by: Claude <noreply@anthropic.com>
mpz_mod_2exp() was reinitializing its output parameter without clearing
existing heap memory. When the parameter contained heap allocations from
pool->heap transitions in mpz_mul()->mpz_realloc(), reinitializing would
overwrite the pointer and leak memory. Added mpz_clear() before each
mpz_init() or mpz_init_heap() call to properly free existing heap memory.
Co-authored-by: Claude <noreply@anthropic.com>
the previous fixed safety margin of 8 limbs was insufficient for certain
edge cases involving deep recursion levels in karatsuba multiplication,
as discovered by oss-fuzz. changed to proportional margin (~12.5% plus
fixed overhead of 16) that scales with input size.
this prevents potential buffer overruns in deeply nested karatsuba
multiplications while maintaining efficiency for typical cases.
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>
refactored the NULL pointer guard in mrb_str_cmp() from an if-else
block to a more concise ternary operator. functionality remains the
same: avoids undefined behavior by skipping memcmp() when comparing
zero-length strings.
Co-authored-by: Claude <noreply@anthropic.com>
passing NULL pointers to memcmp() is undefined behavior per C standard,
even when size is 0. memcmp() is declared with nonnull attributes,
and ASAN can detect this violation.
in mrb_str_cmp(), when comparing two empty strings or when the minimum
length is 0, we now skip the memcmp() call and directly set retval to 0.
this avoids the undefined behavior while maintaining correct comparison
semantics.
Co-authored-by: Claude <noreply@anthropic.com>
refactored five functions to use mrb_ensure() instead of MRB_TRY/MRB_CATCH:
- ary_subtract_internal(): body/ensure pattern for set cleanup
- ary_union_internal(): body/ensure pattern for set cleanup
- ary_intersection_internal(): body/ensure pattern for set cleanup
- ary_intersect_p(): body/ensure pattern for set cleanup
- ary_uniq_bang(): body/ensure pattern for set cleanup
each function now uses a context struct containing set pointer and other
necessary data, with separate body and ensure functions that guarantee
cleanup on exception. this allows array-ext to compile as pure C without
requiring C++ compiler when enable_cxx_exception is set.
added mruby-error dependency to access mrb_ensure(). changed include
from throw.h to error.h. fix#6667.
Co-authored-by: Claude <noreply@anthropic.com>
restore correct argument passing for Regexp.compile when encoding is
present but flags are not. regexp literals like /a/n should compile to
Regexp.compile("a", nil, "n") with 3 arguments, not
Regexp.compile("a", "n") with 2 arguments.
the bug was introduced during refactoring when the nil-insertion logic
for the options parameter was accidentally omitted. now properly inserts
OP_LOADNIL when flags are absent but encoding is present.
Co-authored-by: Claude <noreply@anthropic.com>