Search in sources :

Example 11 with ScriptException

use of com.github.anba.es6draft.runtime.internal.ScriptException in project es6draft by anba.

the class DefaultCodeGenerator method asyncIteratorClose.

/**
     * Emit:
     * 
     * <pre>
     * Callable returnMethod = GetMethod(cx, iterator, "return");
     * if (returnMethod != null) {
     *   try {
     *     returnMethod.call(cx, iterator);
     *     await;
     *   } catch (ScriptException e) {
     *     if (throwable != e) {
     *       throwable.addSuppressed(e);
     *     }
     *   }
     * }
     * </pre>
     * 
     * @param node
     *            the ast node
     * @param iterator
     *            the script iterator object
     * @param mv
     *            the code visitor
     */
final void asyncIteratorClose(Node node, Variable<ScriptObject> iterator, Variable<? extends Throwable> throwable, CodeVisitor mv) {
    IteratorClose(node, iterator, returnMethod -> {
        TryCatchLabel startCatch = new TryCatchLabel();
        TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
        Jump noException = new Jump();
        mv.mark(startCatch);
        {
            InvokeMethod(node, mv, returnMethod, iterator);
            await(node, mv);
            mv.pop();
            mv.goTo(noException);
        }
        mv.mark(endCatch);
        mv.catchHandler(handlerCatch, Types.ScriptException);
        {
            mv.enterVariableScope();
            Variable<ScriptException> exception = mv.newVariable("exception", ScriptException.class);
            mv.store(exception);
            mv.load(throwable);
            mv.load(exception);
            mv.ifacmpeq(noException);
            {
                mv.load(throwable);
                mv.load(exception);
                mv.invoke(Methods.Throwable_addSuppressed);
            }
            mv.exitVariableScope();
        }
        mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
        mv.mark(noException);
    }, mv);
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Variable(com.github.anba.es6draft.compiler.assembler.Variable) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 12 with ScriptException

use of com.github.anba.es6draft.runtime.internal.ScriptException in project es6draft by anba.

the class MozShellFunctions method evaluate.

/**
     * shell-function: {@code evaluate(code, [options])}
     *
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param code
     *            the source code to evaluate
     * @param options
     *            additional options object
     * @return the eval result value
     */
@Function(name = "evaluate", arity = 2)
public Object evaluate(ExecutionContext cx, ExecutionContext caller, Object code, Object options) {
    if (!(Type.isString(code) && (Type.isUndefined(options) || Type.isObject(options)))) {
        throw Errors.newError(cx, "invalid arguments");
    }
    String sourceCode = Type.stringValue(code).toString();
    String sourceName = "@evaluate";
    int sourceLine = 1;
    boolean noScriptRval = false;
    boolean catchTermination = false;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            sourceName = Type.isNull(fileName) ? "" : ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            sourceLine = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            ScriptObject obj = ToObject(cx, g);
            if (!(obj instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) obj).getRealm();
        }
        noScriptRval = ToBoolean(Get(cx, opts, "noScriptRval"));
        catchTermination = ToBoolean(Get(cx, opts, "catchTermination"));
    }
    Source source = new Source(cx.getRealm().sourceInfo(caller), sourceName, sourceLine);
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        Object result = script.evaluate(realm);
        return (!noScriptRval ? result : UNDEFINED);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    } catch (ScriptException | StackOverflowError e) {
        throw e;
    } catch (Error | Exception e) {
        if (catchTermination) {
            return "terminated";
        }
        throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) URISyntaxException(java.net.URISyntaxException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ParserException(com.github.anba.es6draft.parser.ParserException) IOException(java.io.IOException) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ProxyObject(com.github.anba.es6draft.runtime.types.builtins.ProxyObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) BoundFunctionObject(com.github.anba.es6draft.runtime.types.builtins.BoundFunctionObject) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 13 with ScriptException

use of com.github.anba.es6draft.runtime.internal.ScriptException in project es6draft by anba.

the class SetConstructor method construct.

/**
     * 23.2.1.1 Set ([ iterable ])
     */
@Override
public SetObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    SetObject set = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.SetPrototype, SetObject::new);
    /* steps 5-6, 8 */
    if (Type.isUndefinedOrNull(iterable)) {
        return set;
    }
    /* step 7 */
    Object _adder = Get(calleeContext, set, "add");
    if (!IsCallable(_adder)) {
        throw newTypeError(calleeContext, Messages.Key.PropertyNotCallable, "add");
    }
    Callable adder = (Callable) _adder;
    boolean isBuiltin = SetPrototype.isBuiltinAdd(_adder);
    if (isBuiltin && iterable instanceof SetObject) {
        SetObject other = (SetObject) iterable;
        if (ScriptIterators.isBuiltinIterator(calleeContext, other)) {
            set.getSetData().setAll(other.getSetData());
            return set;
        }
    }
    ScriptIterator<?> iter = GetScriptIterator(calleeContext, iterable);
    /* step 9 */
    try {
        while (iter.hasNext()) {
            Object nextValue = iter.next();
            if (isBuiltin) {
                set.getSetData().set(nextValue, null);
            } else {
                adder.call(calleeContext, set, nextValue);
            }
        }
        return set;
    } catch (ScriptException e) {
        iter.close(e);
        throw e;
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 14 with ScriptException

use of com.github.anba.es6draft.runtime.internal.ScriptException in project es6draft by anba.

the class PromiseConstructor method construct.

/**
     * 25.4.3.1 Promise ( executor )
     */
@Override
public PromiseObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object executor = argument(args, 0);
    /* step 2 */
    if (!IsCallable(executor)) {
        throw newTypeError(calleeContext, Messages.Key.NotCallable);
    }
    /* steps 3-7 */
    PromiseObject promise = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.PromisePrototype, GetPromiseAllocator(calleeContext.getRealm()));
    /* step 8 */
    ResolvingFunctions resolvingFunctions = CreateResolvingFunctions(calleeContext, promise);
    /* steps 9-10 */
    try {
        /* step 9 */
        ((Callable) executor).call(calleeContext, UNDEFINED, resolvingFunctions.getResolve(), resolvingFunctions.getReject());
    } catch (ScriptException e) {
        /* step 10 */
        resolvingFunctions.getReject().call(calleeContext, UNDEFINED, e.getValue());
    }
    /* step 11 */
    return promise;
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) ResolvingFunctions(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.ResolvingFunctions) CreateResolvingFunctions(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.CreateResolvingFunctions) Callable(com.github.anba.es6draft.runtime.types.Callable)

Aggregations

ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)14 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)7 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)7 Callable (com.github.anba.es6draft.runtime.types.Callable)6 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)5 Realm (com.github.anba.es6draft.runtime.Realm)4 Script (com.github.anba.es6draft.Script)2 Jump (com.github.anba.es6draft.compiler.assembler.Jump)2 ExecutionContext.newScriptingExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext)2 Console (com.github.anba.es6draft.runtime.internal.Console)2 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)2 Source (com.github.anba.es6draft.runtime.internal.Source)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 IOException (java.io.IOException)2 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)1 Variable (com.github.anba.es6draft.compiler.assembler.Variable)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)1 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)1