doc/mruby4.0.md
mruby4.0 from mruby3.4"NOTE:" are changes to be aware of.
mruby now supports pattern matching (case/in) syntax:
case/in syntax (dadfac6)[*pre, target, *post]) (6c4d98b)^variable) (1de6340)if/unless conditions) (07ac110)expr in pattern) (e76ce24)&nil in formal parameters to explicitly opt out of block arguments (b07518e)def foo(a, b,) (f78334b)[] and []= methods (#6675)OP_SETIDX optimization for Array and Hash (ddd8fe1)case/in without else now raises NoMatchingPatternError (d8de35b)mrb_alloca() renamed to mrb_temp_alloc() (7fe5c2e)mruby/ext/io.h renamed to mruby/io.h (2813f79)mrb_gc_add_region() for contiguous heap region support (072855a)mrb_class_outer() to get the outer class/module (3a1b771)MRB_ENSURE() macro for exception-safe cleanup (3ac682b)mrb_time_get_tm() for accessing struct tm (daaaafe)MRB_OPEN_FAILURE() macro for checking mrb_open result (40b0cb9)mrb_print_error() now handles NULL gracefully (8e50a45)mrb_open() returns mrb_state with exc set on init failure (05ffe0c)mrb_utf8_to_buf() for UTF-8 encoding consolidation (7e28e68)kh_is_end() macro for safe khash iteration (893cc75)mrb_bigint_p() always defined regardless of bigint gem presence (6c4a8c0)RInteger and RFloat added to RVALUE union (13dbca0)All built-in classes and most extension gems now use read-only method
tables stored in .rodata instead of heap-allocated hash tables. Method
definitions no longer consume heap memory, significantly reducing memory
footprint for embedded use.
Core classes converted: BasicObject, Object, Module, Class, Kernel, String, Array, Hash, Numeric, Integer, Float, NilClass, TrueClass, FalseClass, Range, Symbol, Exception, Proc.
Extension gems converted: mruby-string-ext, mruby-array-ext, mruby-set, mruby-struct, mruby-class-ext, mruby-numeric-ext, mruby-random, mruby-kernel-ext, mruby-complex, mruby-rational, mruby-io, mruby-socket, mruby-method, mruby-metaprog, mruby-time, mruby-hash-ext, mruby-proc-ext, mruby-symbol-ext, mruby-range-ext, mruby-object-ext.
MRB_NO_PRESYM removed; presym is now always enabled (81689045)gcnext gray linked list with fixed-size gray stack, reducing per-object overhead (31fea170)mrb_gc_add_region() for providing contiguous memory buffers as GC heap pages (072855a)IV_INITIAL_SIZE from 4 to 2 (6bd1f51)MRB_TT_OBJECT IV storage, sharing key layouts across objects with the same instance variable assignment order (8d10056)MRB_WORDBOX_NO_FLOAT_TRUNCATE renamed to MRB_WORDBOX_NO_INLINE_FLOAT (old name still works) (59e1fe2)MRB_INT64 on 32-bit now requires MRB_NO_BOXING (other boxing modes cannot guarantee alignment for heap-allocated 64-bit integers) (eaaa66b)rake amalgam task (d995ca2)MRUBY_MIRB_READLINE environment variable to control readline library selection (0aafb83)to_s (990ff90f)Hash#deconstruct_keys removed for CRuby compatibility (34b9412)Lazy#flat_map to handle non-enumerable block return values (#6765)Array#find and Array#rfind methodsIO#putc and Kernel#putc (baff6e6)no_return_value context flag for script optimization (613b03a)initialize_copy and respond_to_missing? defined as private (#6708)%w() and %i() literals to reduce register pressure (62cf0dc)New super-instructions that fuse common opcode sequences to reduce bytecode size and improve performance:
OP_SEND0/OP_SSEND0: Zero-argument method call, avoiding argument count setup (9123ef4)OP_TDEF/OP_SDEF: Fused method definition combining TCLASS/SCLASS+METHOD+DEF into single instruction, saving 4 bytes per method (8d4f47e)OP_GETIDX0: Fast path for array[0] and Array#first access (680f7ec)OP_ADDILV/OP_SUBILV: Local variable increment/decrement fusion for i += n patterns (43f64b9)OP_RETSELF: Single-byte instruction for return self pattern (a71db8c)OP_RETNIL: Single-byte instruction for return nil pattern (64e30bf)OP_RETTRUE/OP_RETFALSE: Single-byte instructions for return true/return false patterns (0b15727)OP_MATCHERR: Pattern matching error with conditional execution (944168a)OP_BLKCALL: Direct block call for yield, bypassing method dispatch (13-17% faster) (3aa2872)Other optimizations:
%w()/%i() register pressure with large literalscase/in without else should raise NoMatchingPatternErrormrb_gc_unregister() not removing all matching entriesls-lint with GitHub ActionsMRB_ENDIAN_LOHI() that is no longer in usebuild.yml try windows-2025 imagev22.14.0 LTSmodule_functiontools/lrama to .prettierignore.codespellrcrand(Range) and unify implementations of Random#rand and Kernel#randextended callbackcdump.c to remove the @brief tagmrb_mt_foreach() needs to update the pointer at each loopiv_foreach() needs to update the pointer at each loopCrossBuild#run_bintestcmd_bin method in bintestmrb_obj_iv_inspect() into mrb_obj_inspect()mrb_obj_alloc()help targetcheck-zip-file-is-not-committed#method_missingmarkdown-link-check, oxipng, prettier manuallydreamcast_shelf build config: update to use KallistiOS wrappersnew and !=MRuby::Build.current at the top level of mrbgem.rakeFile.absolute_path logiccodegen_masgnArray#__combination_initkh_is_end() macro functionSet#join--color=alwaysinitialize_copy and respond_to_missing? defined as privateMRB_ENSURE() macroRInteger and RFloat to RVALUEmruby/presym.h for all source files%w() and %i() literals to reduce register pressureNoMatchingPatternError in case/in without elseArray#productattr_reader-generated methods accepting extra argumentsArray#productattr_reader procs as noargci after mrb_hash_delete_key() in keyword argument handlingmrb_vm_exec() callsmrb_funcall() directly to regsmrb_bigint_p() alwaysmrb_gc_unregister() to remove all matching entriesLazy#flat_map to handle non-enumerable block return valuesString#prepend with self-referencing argumentssprintf format string from mutation during callbacks#method_missing (550d10a)Array#product (8441eaf)String#prepend with self-referencing arguments (18ba026)sprintf via to_s callback mutating format string (48fc422)