Search in sources :

Example 1 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class GeneratorFunctionConstructor method CreateDynamicGenerator.

private static OrdinaryGenerator CreateDynamicGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.Generator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.generator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryGenerator f = OrdinaryGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
    f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) OrdinaryGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryGenerator) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Property(com.github.anba.es6draft.runtime.types.Property) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 2 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class FinalizablePromiseObject method notifyRejectReaction.

@Override
void notifyRejectReaction(PromiseReaction reaction) {
    assert getState() == State.Pending || getState() == State.Rejected;
    if (reaction.getType() != PromiseReaction.Type.Thrower) {
        trackRejection = false;
        rejectReason.clear();
    } else {
        ScriptObject promise = reaction.getCapabilities().getPromise();
        if (promise instanceof FinalizablePromiseObject) {
            FinalizablePromiseObject promiseObject = (FinalizablePromiseObject) promise;
            if (promiseObject.getState() == State.Pending) {
                // TODO: This is not quite correct when `promiseObject` is rejected on its own.
                // Don't track rejection for dependent promises.
                promiseObject.trackRejection = false;
                promiseObject.rejectReason = rejectReason;
            }
        }
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Example 3 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class RealmConstructor method construct.

/**
     * 26.?.1.1 Reflect.Realm ( [ target , handler ] )
     */
@Override
public RealmObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* steps 2-3 */
    RealmObject realmObject = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.RealmPrototype, RealmObject::new);
    /* steps 4-5 */
    ScriptObject newGlobal;
    if (args.length != 0) {
        /* step 4 */
        Object target = argument(args, 0);
        Object handler = argument(args, 1);
        newGlobal = ProxyCreate(calleeContext, target, handler);
    } else {
        /* step 5 */
        newGlobal = null;
    }
    /* steps 6-7 */
    Realm realm = CreateRealmAndSetRealmGlobalObject(calleeContext, realmObject, newGlobal, newGlobal);
    /* step 17 (Moved before extracting extension hooks to avoid uninitialized object state) */
    realmObject.setRealm(realm);
    // Run any initialization scripts, if required. But do _not_ install extensions!
    try {
        GlobalObject globalTemplate = realm.getGlobalObjectTemplate();
        assert globalTemplate != null;
        globalTemplate.initializeScripted();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(calleeContext);
    } catch (IOException | URISyntaxException e) {
        throw newError(calleeContext, e.getMessage());
    }
    /* steps 8-9 */
    Callable translate = GetMethod(calleeContext, realmObject, "directEval");
    /* steps 10-11 */
    Callable fallback = GetMethod(calleeContext, realmObject, "nonEval");
    /* steps 12-13 */
    Callable indirectEval = GetMethod(calleeContext, realmObject, "indirectEval");
    /* steps 14-16 */
    realm.setExtensionHooks(translate, fallback, indirectEval);
    /* steps 18-19 */
    Callable initGlobal = GetMethod(calleeContext, realmObject, "initGlobal");
    /* steps 20-21 */
    if (initGlobal != null) {
        /* step 20 */
        initGlobal.call(calleeContext, realmObject);
    } else {
        /* step 21 */
        SetDefaultGlobalBindings(calleeContext, realm);
    }
    /* step 22 */
    return realmObject;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) CreateRealmAndSetRealmGlobalObject(com.github.anba.es6draft.runtime.Realm.CreateRealmAndSetRealmGlobalObject) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Callable(com.github.anba.es6draft.runtime.types.Callable) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) CreateRealmAndSetRealmGlobalObject(com.github.anba.es6draft.runtime.Realm.CreateRealmAndSetRealmGlobalObject) Realm(com.github.anba.es6draft.runtime.Realm)

Example 4 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class ScriptEngineImpl method invoke.

private Object invoke(ScriptObject thisValue, String name, Object... args) throws javax.script.ScriptException, NoSuchMethodException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getWorld().getContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        Object[] arguments = TypeConverter.fromJava(args);
        if (thisValue == null) {
            thisValue = realm.getGlobalThis();
        }
        ExecutionContext cx = realm.defaultContext();
        Object func = thisValue.get(cx, name, thisValue);
        if (!IsCallable(func)) {
            throw new NoSuchMethodException(name);
        }
        Object result = ((Callable) func).call(cx, thisValue, arguments);
        realm.getWorld().runEventLoop();
        return TypeConverter.toJava(result);
    } catch (ScriptException e) {
        throw new javax.script.ScriptException(e);
    } finally {
        runtimeContext.setConsole(console);
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptingExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext) Console(com.github.anba.es6draft.runtime.internal.Console) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 5 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class RegExpConstructor method RegExpCreate.

private static RegExpObject RegExpCreate(ExecutionContext cx, Constructor newTarget, Object pattern, Object flags, boolean patternIsRegExp) {
    /* steps 5-7 */
    Object p, f;
    if (pattern instanceof RegExpObject) {
        /* step 5 */
        RegExpObject regexp = (RegExpObject) pattern;
        /* step 5.a */
        p = regexp.getOriginalSource();
        /* steps 5.b-5.c */
        f = Type.isUndefined(flags) ? regexp.getOriginalFlags() : flags;
    } else if (patternIsRegExp) {
        /* step 6 */
        ScriptObject patternObject = Type.objectValue(pattern);
        /* steps 6.a-b */
        p = Get(cx, patternObject, "source");
        /* steps 6.c-d */
        f = Type.isUndefined(flags) ? Get(cx, patternObject, "flags") : flags;
    } else {
        /* step 7 */
        p = pattern;
        f = flags;
    }
    /* steps 8-9 */
    RegExpObject obj = RegExpAlloc(cx, newTarget);
    /* step 10 */
    return RegExpInitialize(cx, obj, p, f);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Aggregations

ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)129 Callable (com.github.anba.es6draft.runtime.types.Callable)43 Property (com.github.anba.es6draft.runtime.types.Property)32 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)26 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)21 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Constructor (com.github.anba.es6draft.runtime.types.Constructor)11 Realm (com.github.anba.es6draft.runtime.Realm)9 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)9 ParserException (com.github.anba.es6draft.parser.ParserException)8 CompilationException (com.github.anba.es6draft.compiler.CompilationException)7 Source (com.github.anba.es6draft.runtime.internal.Source)7 ArrayList (java.util.ArrayList)7 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ImmutablePrototypeObject (com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject)6 OrdinaryCreateFromConstructor (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor)6 Test (org.junit.Test)6 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)5 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)4