<commit_before>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.types.builtins;

import static com.github.anba.es6draft.runtime.AbstractOperations.OrdinaryCreateFromConstructor;
import static com.github.anba.es6draft.runtime.internal.Errors.throwTypeError;
import static com.github.anba.es6draft.runtime.objects.iteration.IterationAbstractOperations.GeneratorStart;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.FunctionInitialise;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.OrdinaryConstruct;

import com.github.anba.es6draft.runtime.EnvironmentRecord;
import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.FunctionEnvironmentRecord;
import com.github.anba.es6draft.runtime.LexicalEnvironment;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.CompatibilityOption;
import com.github.anba.es6draft.runtime.internal.Messages;
import com.github.anba.es6draft.runtime.internal.ObjectAllocator;
import com.github.anba.es6draft.runtime.internal.RuntimeInfo;
import com.github.anba.es6draft.runtime.objects.iteration.GeneratorObject;
import com.github.anba.es6draft.runtime.types.Constructor;
import com.github.anba.es6draft.runtime.types.Intrinsics;
import com.github.anba.es6draft.runtime.types.ScriptObject;
import com.github.anba.es6draft.runtime.types.Symbol;

/**
 * <h1>9 ECMAScript Ordinary and Exotic Objects Behaviours</h1><br>
 * <h2>9.1 Ordinary Object Internal Methods and Internal Data Properties</h2>
 * <ul>
 * <li>9.1.15 Ordinary Function Objects
 * </ul>
 */
public class OrdinaryGenerator extends FunctionObject {
    public OrdinaryGenerator(Realm realm) {
        super(realm);
    }

    private static class OrdinaryConstructorGenerator extends OrdinaryGenerator implements
            Constructor {
        public OrdinaryConstructorGenerator(Realm realm) {
            super(realm);
        }

        @Override
        public final boolean isConstructor() {
            return isConstructor;
        }

        /**
         * 9.1.15.2 [[Construct]] (argumentsList)
         */
        @Override
        public ScriptObject construct(ExecutionContext callerContext, Object... args) {
            return OrdinaryConstruct(callerContext, this, args);
        }
    }

    @Override
    public OrdinaryGenerator rebind(ExecutionContext cx, ScriptObject newHomeObject) {
        assert isInitialised() : "uninitialised function object";
        Object methodName = getMethodName();
        OrdinaryGenerator f;
        if (methodName instanceof String) {
            f = GeneratorFunctionCreate(cx, getFunctionKind(), getFunction(), getScope(),
                    getPrototypeOf(cx), newHomeObject, (String) methodName);
        } else {
            assert methodName instanceof Symbol;
            f = GeneratorFunctionCreate(cx, getFunctionKind(), getFunction(), getScope(),
                    getPrototypeOf(cx), newHomeObject, (Symbol) methodName);
        }
        f.isConstructor = this.isConstructor;
        return f;
    }

    /**
     * 9.1.15.1 [[Call]] Internal Method
     */
    @Override
    public GeneratorObject call(ExecutionContext callerContext, Object thisValue, Object... args) {
        if (!isInitialised()) {
            throw throwTypeError(callerContext, Messages.Key.IncompatibleObject);
        }

        /* steps 1-11 */
        ExecutionContext calleeContext = ExecutionContext.newFunctionExecutionContext(this,
                thisValue);
        /* steps 12-13 */
        getFunction().functionDeclarationInstantiation(calleeContext, this, args);
        /* steps 14-15 */
        GeneratorObject result;
        if (getFunctionKind() != FunctionKind.Arrow) {
            result = EvaluateBody(calleeContext, this);
        } else {
            result = EvaluateBodyComprehension(calleeContext, this);
        }
        /* step 16 */
        return result;
    }

    /**
     * 14.4 Generator Function Definitions
     * <p>
     * Runtime Semantics EvaluateBody
     * 
     * <pre>
     * GeneratorBody : FunctionBody
     * </pre>
     */
    public static GeneratorObject EvaluateBody(ExecutionContext cx, OrdinaryGenerator functionObject) {
        /* step 1 */
        assert cx.getLexicalEnvironment().getEnvRec() instanceof FunctionEnvironmentRecord;
        /* step 2 */
        EnvironmentRecord env = cx.getThisEnvironment();
        /* step 3 */
        Object g = env.getThisBinding();
        /* step 4 */
        GeneratorObject gen;
        if (!(g instanceof GeneratorObject) || ((GeneratorObject) g).getState() != null) {
            GeneratorObject newG = OrdinaryCreateFromConstructor(cx, functionObject,
                    Intrinsics.GeneratorPrototype, GeneratorObjectAllocator.INSTANCE);
            gen = newG;
        } else {
            gen = (GeneratorObject) g;<commit_msg>Update step comments<commit_after>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.types.builtins;

import static com.github.anba.es6draft.runtime.AbstractOperations.OrdinaryCreateFromConstructor;
import static com.github.anba.es6draft.runtime.internal.Errors.throwTypeError;
import static com.github.anba.es6draft.runtime.objects.iteration.IterationAbstractOperations.GeneratorStart;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.FunctionInitialise;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.OrdinaryConstruct;

import com.github.anba.es6draft.runtime.EnvironmentRecord;
import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.FunctionEnvironmentRecord;
import com.github.anba.es6draft.runtime.LexicalEnvironment;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.CompatibilityOption;
import com.github.anba.es6draft.runtime.internal.Messages;
import com.github.anba.es6draft.runtime.internal.ObjectAllocator;
import com.github.anba.es6draft.runtime.internal.RuntimeInfo;
import com.github.anba.es6draft.runtime.objects.iteration.GeneratorObject;
import com.github.anba.es6draft.runtime.types.Constructor;
import com.github.anba.es6draft.runtime.types.Intrinsics;
import com.github.anba.es6draft.runtime.types.ScriptObject;
import com.github.anba.es6draft.runtime.types.Symbol;

/**
 * <h1>9 ECMAScript Ordinary and Exotic Objects Behaviours</h1><br>
 * <h2>9.1 Ordinary Object Internal Methods and Internal Data Properties</h2>
 * <ul>
 * <li>9.1.15 Ordinary Function Objects
 * </ul>
 */
public class OrdinaryGenerator extends FunctionObject {
    public OrdinaryGenerator(Realm realm) {
        super(realm);
    }

    private static class OrdinaryConstructorGenerator extends OrdinaryGenerator implements
            Constructor {
        public OrdinaryConstructorGenerator(Realm realm) {
            super(realm);
        }

        @Override
        public final boolean isConstructor() {
            return isConstructor;
        }

        /**
         * 9.1.15.2 [[Construct]] (argumentsList)
         */
        @Override
        public ScriptObject construct(ExecutionContext callerContext, Object... args) {
            return OrdinaryConstruct(callerContext, this, args);
        }
    }

    @Override
    public OrdinaryGenerator rebind(ExecutionContext cx, ScriptObject newHomeObject) {
        assert isInitialised() : "uninitialised function object";
        Object methodName = getMethodName();
        OrdinaryGenerator f;
        if (methodName instanceof String) {
            f = GeneratorFunctionCreate(cx, getFunctionKind(), getFunction(), getScope(),
                    getPrototypeOf(cx), newHomeObject, (String) methodName);
        } else {
            assert methodName instanceof Symbol;
            f = GeneratorFunctionCreate(cx, getFunctionKind(), getFunction(), getScope(),
                    getPrototypeOf(cx), newHomeObject, (Symbol) methodName);
        }
        f.isConstructor = this.isConstructor;
        return f;
    }

    /**
     * 9.1.15.1 [[Call]] Internal Method
     */
    @Override
    public GeneratorObject call(ExecutionContext callerContext, Object thisValue, Object... args) {
        if (!isInitialised()) {
            throw throwTypeError(callerContext, Messages.Key.IncompatibleObject);
        }

        /* steps 1-12 */
        ExecutionContext calleeContext = ExecutionContext.newFunctionExecutionContext(this,
                thisValue);
        /* steps 13-14 */
        getFunction().functionDeclarationInstantiation(calleeContext, this, args);
        /* steps 15-16 */
        GeneratorObject result;
        if (getFunctionKind() != FunctionKind.Arrow) {
            result = EvaluateBody(calleeContext, this);
        } else {
            result = EvaluateBodyComprehension(calleeContext, this);
        }
        /* step 17 */
        return result;
    }

    /**
     * 14.4 Generator Function Definitions
     * <p>
     * Runtime Semantics EvaluateBody
     * 
     * <pre>
     * GeneratorBody : FunctionBody
     * </pre>
     */
    public static GeneratorObject EvaluateBody(ExecutionContext cx, OrdinaryGenerator functionObject) {
        /* step 1 */
        assert cx.getLexicalEnvironment().getEnvRec() instanceof FunctionEnvironmentRecord;
        /* step 2 */
        EnvironmentRecord env = cx.getThisEnvironment();
        /* step 3 */
        Object g = env.getThisBinding();
        /* step 4 */
        GeneratorObject gen;
        if (!(g instanceof GeneratorObject) || ((GeneratorObject) g).getState() != null) {
            GeneratorObject newG = OrdinaryCreateFromConstructor(cx, functionObject,
                    Intrinsics.GeneratorPrototype, GeneratorObjectAllocator.INSTANCE);
            gen = newG;
        } else {
            gen = (GeneratorObject) g;
{'fraction_non_alphanumeric': 0.10020671325917906, 'fraction_numerical': 0.013780883945270203, 'mean_word_length': 3.0047299960583365, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047660', 'n_tokens_mistral': 2827, 'n_tokens_neox': 2745, 'n_words': 561}