@@ -35,7 +35,7 @@ use crate::{FnPtr, ImmutableString, NativeCallContext, Position, ThinVec, FUNC_T
3535
3636mod callback;
3737
38- use crate :: grain:: bytecode:: { code, AssignOp , Chain , Receiver , Root , Step , StepFlags , Tail } ;
38+ use crate :: grain:: bytecode:: { code, AssignOp , Chain , Chunk , Receiver , Root , Step , StepFlags , Tail } ;
3939use crate :: grain:: program:: { Program , SharedModule , SharedProgram } ;
4040
4141/// Rhai's own `RhaiResult`, which it does not re-export.
@@ -2234,7 +2234,10 @@ impl<'e> Vm<'e> {
22342234 Ok ( ( ) )
22352235 }
22362236
2237- // Some syntactic calls can be self-implemented or short-circuited.
2237+ /// Called only by [`call_syntactic_or_stacked`] after it has checked for a
2238+ /// syntactic call.
2239+ ///
2240+ /// Some syntactic calls can be self-implemented or short-circuited.
22382241 fn call_syntactic (
22392242 & mut self ,
22402243 program : & Program ,
@@ -2349,7 +2352,10 @@ impl<'e> Vm<'e> {
23492352 Ok ( None )
23502353 }
23512354
2352- /// Call `name` with `argc` arguments sitting contiguously from `first` up.
2355+ /// Called only by [`call_syntactic_or_stacked`] after it has checked for a
2356+ /// syntactic call.
2357+ ///
2358+ /// Call function with `argc` arguments sitting contiguously from `first` up.
23532359 ///
23542360 /// A function this compiler lowered is called directly, with no hash and no
23552361 /// module walk: the call site's name index and the function's come from the
@@ -2397,6 +2403,42 @@ impl<'e> Vm<'e> {
23972403 )
23982404 }
23992405
2406+ /// This is the main entry-point for function calls.
2407+ ///
2408+ /// First check whether the call is a syntactic one (e.g. `is_def_fn`)
2409+ /// which are self-implemented or directly called into the
2410+ /// corresponding Rhai functinon.
2411+ ///
2412+ /// If the call is not to a syntactic one, it calls the function
2413+ /// normally, with arguments pushed onto the stack.
2414+ fn call_syntactic_or_stacked (
2415+ & mut self ,
2416+ program : & Program ,
2417+ name_index : u32 ,
2418+ name : & str ,
2419+ argc : usize ,
2420+ first : usize ,
2421+ scope : & mut Scope ,
2422+ capture : bool ,
2423+ pos : Position ,
2424+ ) -> VmResult {
2425+ // Check if it is a built-in syntactic function.
2426+ match self . call_syntactic ( program, name, argc, first, scope, pos) ? {
2427+ Some ( value) => Ok ( value) ,
2428+ None => {
2429+ // Detach the scope with a new one if not capturing the parent's.
2430+ let mut detached;
2431+ let scope = if !capture {
2432+ detached = Scope :: new ( ) ;
2433+ & mut detached
2434+ } else {
2435+ scope
2436+ } ;
2437+ self . call_stacked ( program, name_index, name, argc, first, scope, pos)
2438+ }
2439+ }
2440+ }
2441+
24002442 /// The same call, with a variable as its first argument and Rhai's
24012443 /// method-call rewrite applied to it (`func/call.rs:1434-1460`).
24022444 ///
@@ -2413,7 +2455,7 @@ impl<'e> Vm<'e> {
24132455 receiver : Receiver ,
24142456 scope : & mut Scope ,
24152457 base : usize ,
2416- capture_parent_scope : bool ,
2458+ capture : bool ,
24172459 pos : Position ,
24182460 ) -> VmResult {
24192461 // Every argument count here includes the receiver, so zero of them
@@ -2429,15 +2471,7 @@ impl<'e> Vm<'e> {
24292471 // The register is not a scope entry, so it takes a path of its own
24302472 // rather than a third [`Site`].
24312473 if let Receiver :: This = receiver {
2432- return self . call_by_this (
2433- program,
2434- name_index,
2435- name,
2436- argc,
2437- scope,
2438- capture_parent_scope,
2439- pos,
2440- ) ;
2474+ return self . call_by_this ( program, name_index, name, argc, scope, capture, pos) ;
24412475 }
24422476
24432477 // A named receiver's value is already argument zero — [`Op::LoadNamed`]
@@ -2491,21 +2525,9 @@ impl<'e> Vm<'e> {
24912525 let value = scope. get_mut_by_index ( index) . flatten_clone ( ) ;
24922526 self . stack . insert ( first, value) ;
24932527 }
2494- // Check if it is a built-in syntactic function.
2495- let value =
2496- if let Some ( value) = self . call_syntactic ( program, name, argc, first, scope, pos) ? {
2497- value
2498- } else {
2499- // Detach the scope with a new one if not capturing the parent's.
2500- let mut detached;
2501- let scope = if !capture_parent_scope {
2502- detached = Scope :: new ( ) ;
2503- & mut detached
2504- } else {
2505- scope
2506- } ;
2507- self . call_stacked ( program, name_index, name, argc, first, scope, pos) ?
2508- } ;
2528+ let value = self . call_syntactic_or_stacked (
2529+ program, name_index, name, argc, first, scope, capture, pos,
2530+ ) ?;
25092531 self . stack . truncate ( first) ;
25102532 return Ok ( value) ;
25112533 }
@@ -2566,7 +2588,7 @@ impl<'e> Vm<'e> {
25662588 name : & str ,
25672589 argc : usize ,
25682590 scope : & mut Scope ,
2569- capture_parent_scope : bool ,
2591+ capture : bool ,
25702592 pos : Position ,
25712593 ) -> VmResult {
25722594 let first = self
@@ -2585,21 +2607,9 @@ impl<'e> Vm<'e> {
25852607 && program. function ( name_index, argc) . is_none ( ) ;
25862608
25872609 if !by_reference {
2588- // Check if it is a built-in syntactic function.
2589- let value =
2590- if let Some ( value) = self . call_syntactic ( program, name, argc, first, scope, pos) ? {
2591- value
2592- } else {
2593- // Detach the scope with a new one if not capturing the parent's.
2594- let mut detached;
2595- let scope = if !capture_parent_scope {
2596- detached = Scope :: new ( ) ;
2597- & mut detached
2598- } else {
2599- scope
2600- } ;
2601- self . call_stacked ( program, name_index, name, argc, first, scope, pos) ?
2602- } ;
2610+ let value = self . call_syntactic_or_stacked (
2611+ program, name_index, name, argc, first, scope, capture, pos,
2612+ ) ?;
26032613 self . stack . truncate ( first) ;
26042614 return Ok ( value) ;
26052615 }
@@ -2636,7 +2646,7 @@ impl<'e> Vm<'e> {
26362646 program : & Program ,
26372647 name : & str ,
26382648 params : & [ u32 ] ,
2639- chunk : crate :: grain :: bytecode :: Chunk ,
2649+ chunk : Chunk ,
26402650 first : usize ,
26412651 scope : & mut Scope ,
26422652 pos : Position ,
@@ -2665,7 +2675,7 @@ impl<'e> Vm<'e> {
26652675 program : & Program ,
26662676 name : & str ,
26672677 params : & [ u32 ] ,
2668- chunk : crate :: grain :: bytecode :: Chunk ,
2678+ chunk : Chunk ,
26692679 first : usize ,
26702680 scope : & mut Scope ,
26712681 rewind_scope : bool ,
@@ -2701,7 +2711,7 @@ impl<'e> Vm<'e> {
27012711 program : & Program ,
27022712 name : & str ,
27032713 params : & [ u32 ] ,
2704- chunk : crate :: grain :: bytecode :: Chunk ,
2714+ chunk : Chunk ,
27052715 first : usize ,
27062716 scope : & mut Scope ,
27072717 rewind_scope : bool ,
@@ -2781,7 +2791,7 @@ impl<'e> Vm<'e> {
27812791 & mut self ,
27822792 program : & Program ,
27832793 scope : & mut Scope ,
2784- chunk : crate :: grain :: bytecode :: Chunk ,
2794+ chunk : Chunk ,
27852795 base : usize ,
27862796 reached : & mut usize ,
27872797 ) -> VmResult {
@@ -3505,7 +3515,7 @@ impl<'e> Vm<'e> {
35053515 let name = program
35063516 . name ( name_index)
35073517 . ok_or_else ( || malformed ( format ! ( "no name {name_index}" ) ) ) ?;
3508- let capture_parent_scope = tag == code:: tag:: CALL_CAPTURE ;
3518+ let capture = tag == code:: tag:: CALL_CAPTURE ;
35093519 let argc = code[ pc + 3 ] as usize ;
35103520 let op = if tag == code:: tag:: CALL_OP {
35113521 let index = u32:: from ( small ( 4 ) ?) ;
@@ -3554,21 +3564,16 @@ impl<'e> Vm<'e> {
35543564 }
35553565
35563566 // Check if it is a built-in syntactic function.
3557- let value = if let Some ( value) =
3558- self . call_syntactic ( program, name, argc, first, scope, pos ( ) ) ?
3559- {
3560- value
3561- } else {
3562- // Detach the scope with a new one if not capturing the parent's.
3563- let mut detached;
3564- let scope = if !capture_parent_scope {
3565- detached = Scope :: new ( ) ;
3566- & mut detached
3567- } else {
3568- & mut * scope
3569- } ;
3570- self . call_stacked ( program, name_index, name, argc, first, scope, pos ( ) ) ?
3571- } ;
3567+ let value = self . call_syntactic_or_stacked (
3568+ program,
3569+ name_index,
3570+ name,
3571+ argc,
3572+ first,
3573+ scope,
3574+ capture,
3575+ pos ( ) ,
3576+ ) ?;
35723577 self . stack . truncate ( first) ;
35733578 self . stack . push ( value) ;
35743579 }
@@ -3598,7 +3603,7 @@ impl<'e> Vm<'e> {
35983603 }
35993604 _ => unreachable ! ( ) ,
36003605 } ;
3601- let capture_parent_scope = matches ! (
3606+ let capture = matches ! (
36023607 tag,
36033608 code:: tag:: CALL_LOCAL_REF_CAPTURE
36043609 | code:: tag:: CALL_NAMED_REF_CAPTURE
@@ -3613,7 +3618,7 @@ impl<'e> Vm<'e> {
36133618 receiver,
36143619 scope,
36153620 base,
3616- capture_parent_scope ,
3621+ capture ,
36173622 pos ( ) ,
36183623 ) ?;
36193624 self . stack . push ( value) ;
0 commit comments