@@ -34,6 +34,7 @@ const EvalError = interp.EvalError;
3434
3535const async_gen_request_reserve_granularity : usize = 16 ;
3636const native_tier_entry_threshold : u32 = 3 ;
37+ const inline_call_depth_limit : u8 = 32 ;
3738
3839fn bindThisForCall (vm : * Interpreter , func : * Function , this_val : Value ) EvalError ! Value {
3940 if (func .is_arrow ) return func .arrow_this ;
@@ -2335,6 +2336,15 @@ fn runChunk(vm: *Interpreter, exec: *Exec, chunk: *Chunk, frame: ?*Frame, gen: ?
23352336 try stack .append (stack_alloc , result );
23362337 continue ;
23372338 }
2339+ if (func .vm_inline_calls_safe and ! vm .vm_inline_calls_disabled ) {
2340+ const result = if (vm .vm_inline_call_depth < inline_call_depth_limit )
2341+ try runInlineFunction (vm , func , func .chunk .? , stack .items [base .. ], Value .undef (), Value .undef ())
2342+ else
2343+ try callValueWithInlineCallsDisabled (vm , callee , stack .items [base .. ], Value .undef ());
2344+ stack .shrinkRetainingCapacity (base - 1 );
2345+ try stack .append (stack_alloc , result );
2346+ continue ;
2347+ }
23382348 }
23392349 }
23402350 // Trampoline plain JS→JS calls under the driver: build the callee
@@ -3847,6 +3857,7 @@ fn makeClosure(vm: *Interpreter, tmpl: *bc.FnTemplate, frame: ?*Frame) EvalError
38473857 .import_meta_slot = vm .import_meta_slot ,
38483858 .module_referrer = vm .cur_module ,
38493859 .chunk = if (tmpl .is_generator or tmpl .is_async ) null else tmpl .chunk ,
3860+ .vm_inline_calls_safe = if (tmpl .chunk ) | compiled | interp .vmChunkAllowsInlineCalls (compiled ) else false ,
38503861 .gen_chunk = if (tmpl .is_generator ) tmpl .chunk else null ,
38513862 .frame = frame ,
38523863 .local_count = tmpl .local_count ,
@@ -4101,6 +4112,32 @@ fn inheritCallerState(dst: *Activation, src: *const Activation) void {
41014112 dst .saved_pm = src .saved_pm ;
41024113}
41034114
4115+ /// Execute a shallow ordinary VM call directly, retaining the same activation,
4116+ /// root registration, handler unwinding, step accounting, and state restoration
4117+ /// as a driver-owned activation. The depth bound keeps native stack use fixed;
4118+ /// the boundary call disables this path while a nested heap driver handles the
4119+ /// remainder of a deep chain.
4120+ fn runInlineFunction (vm : * Interpreter , func : * Function , fchunk : * Chunk , args : []const Value , this_val : Value , new_target : Value ) EvalError ! Value {
4121+ try vm .stackGuard ();
4122+ vm .depth += 1 ;
4123+ defer vm .depth -= 1 ;
4124+ const act = try buildActivation (vm , func , fchunk , args , this_val , new_target );
4125+ defer {
4126+ popActivation (vm , act );
4127+ releaseActivation (vm , act );
4128+ }
4129+ vm .vm_inline_call_depth += 1 ;
4130+ defer vm .vm_inline_call_depth -= 1 ;
4131+ return execLoop (vm , & act .exec , fchunk , act .frame , null );
4132+ }
4133+
4134+ fn callValueWithInlineCallsDisabled (vm : * Interpreter , callee : Value , args : []const Value , this_val : Value ) EvalError ! Value {
4135+ const saved = vm .vm_inline_calls_disabled ;
4136+ vm .vm_inline_calls_disabled = true ;
4137+ defer vm .vm_inline_calls_disabled = saved ;
4138+ return callValue (vm , callee , args , this_val );
4139+ }
4140+
41044141/// If `callee` is a plain JS-chunk function (not generator/async/native/bound/
41054142/// proxy), return it — those are the calls the trampoline pushes onto its
41064143/// activation stack. Everything else takes the native call path.
@@ -4502,7 +4539,7 @@ test "vm: recursive calls throw a catchable RangeError before native stack overf
45024539 defer arena .deinit ();
45034540 const a = arena .allocator ();
45044541 var parser = try Parser .init (a ,
4505- \\function recurse(n) { return recurse(n + 1); }
4542+ \\function recurse(n) { return 1 + recurse(n + 1); }
45064543 \\recurse(0)
45074544 );
45084545 const prog = try parser .parseProgram ();
0 commit comments