visitYieldFunction()

in lib/generator.js [3629:3674]


  visitYieldFunction(ast, level, env) {
    env.routine = true;
    const args = [];
    const pointerParams = [];
    this.emit('defer close(_yield)\n', level);
    args.push({
      name: '_yield chan',
      type: ast.returnType.valueType,
    });
    if(env.hasThrow) {
      this.emit('defer close(_yieldErr)\n', level);
      args.push({
        name: '_yieldErr chan error',
      });
    }

    const functionName = `${_lowerFirst(env.funcName)}_opYieldFunc`;
    this.emit(`${functionName}(_yield`, level);
    if(env.hasThrow) {
      this.emit(', _yieldErr');
    }
    for (var i = 0; i < ast.params.params.length; i++) {
      this.emit(', ');
      const node = ast.params.params[i];
      assert.equal(node.type, 'param');
      const name = _avoidReserveName(_name(node.paramName));
      pointerParams.push(name);
      this.emit(name);
      if (node.paramType) {
        args.push({
          name,
          type: node.paramType,
        });
      }
    }

    this.emit(')\n');
    this.yieldFunc.push({
      name: functionName,
      args,
      functionBody: ast.functionBody,
      pointerParams,
    });

    this.emit('return\n', level);
  }