visitFunctionNode method

  1. @override
dynamic visitFunctionNode(
  1. FunctionNode node, {
  2. bool? inheritContext,
})

Implementation

@override
visitFunctionNode(FunctionNode node, {bool? inheritContext}) {
  final List<dynamic> args = computeArguments(node.params);
  return JavascriptFunction((List<dynamic>? _params) {
    /*
      1. create a map, parmValueMap
      2. go through params and create a args[i]: parm[i] entry in the map
      3. push the map to the context stack
      4. execute the blockstatement or expression
     */
    List<dynamic> params = _params ?? [];
    Map<String, dynamic> ctx = {};

    if (node.params != null) {
      for (int i = 0; i < node.params.length; i++) {
        // Use the value from params if available, otherwise use null
        ctx[node.params[i].value] = i < params.length ? params[i] : null;
      }
    }
    Context context = SimpleContext(ctx);
    JSInterpreter i = cloneForContext(node, context, inheritContext ?? false);
    dynamic rtn;
    try {
      if (node.body != null) {
        rtn = node.body.visitBy(i);
      }
    } on ControlFlowReturnException catch (e) {
      rtn = e.returnValue;
    }
    if (rtn is Node) {
      rtn = i.getValueFromNode(rtn);
    }
    return rtn;
  }, code.substring(node.start!, node.end));
}