Search in sources :

Example 1 with ScriptException

use of com.github.anba.es6draft.runtime.internal.ScriptException 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 2 with ScriptException

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

the class ScriptEngineImpl method eval.

Object eval(Script script, ScriptContext context) throws javax.script.ScriptException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getWorld().getContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        // Prepare a new execution context before calling the generated code.
        ExecutionContext evalCxt = newScriptingExecutionContext(realm, script, new LexicalEnvironment<>(realm.getGlobalEnv(), new ScriptContextEnvironmentRecord(realm.defaultContext(), context)));
        Object result = script.evaluate(evalCxt);
        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)

Example 3 with ScriptException

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

the class SourceBuilder method objectToSource.

private String objectToSource(ExecutionContext cx, HashSet<ScriptObject> stack, ScriptObject object) {
    Iterator<?> keys = object.ownKeys(cx);
    if (!keys.hasNext()) {
        return "{}";
    }
    StringBuilder properties = new StringBuilder();
    for (int i = 0; keys.hasNext() && i < maxObjectProperties; ) {
        Object k = keys.next();
        String key = propertyKeyToSource(cx, k);
        Property prop;
        try {
            prop = object.getOwnProperty(cx, k);
        } catch (ScriptException e) {
            continue;
        }
        if (!prop.isEnumerable()) {
            continue;
        }
        String value;
        if (prop.isDataDescriptor()) {
            value = toSource(cx, stack, prop.getValue());
        } else {
            value = accessorToSource(prop);
        }
        properties.append(", ").append(key).append(": ").append(value);
        i += 1;
    }
    if (keys.hasNext()) {
        properties.append(", [...]");
    }
    properties.append(" }").setCharAt(0, '{');
    return properties.toString();
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) DateObject(com.github.anba.es6draft.runtime.objects.date.DateObject) RegExpObject(com.github.anba.es6draft.runtime.objects.text.RegExpObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Property(com.github.anba.es6draft.runtime.types.Property)

Example 4 with ScriptException

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

the class SharedFunctions method loadScript.

/**
     * Reads a file and evalutes its content.
     * 
     * @param cx
     *            the execution context
     * @param fileName
     *            the file name
     * @param path
     *            the file path
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source cannot be compiled
     */
static void loadScript(ExecutionContext cx, Path fileName, Path path) throws ParserException, CompilationException {
    if (!Files.exists(path)) {
        throw new ScriptException(String.format("can't open '%s'", fileName.toString()));
    }
    try {
        Realm realm = cx.getRealm();
        Source source = new Source(path, fileName.toString(), 1);
        Script script = realm.getScriptLoader().script(source, path);
        script.evaluate(realm);
    } catch (IOException e) {
        throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Script(com.github.anba.es6draft.Script) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 5 with ScriptException

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

the class MapConstructor method construct.

/**
     * 23.1.1.1 Map ([ iterable ])
     */
@Override
public MapObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    MapObject map = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.MapPrototype, MapObject::new);
    /* steps 5-6, 8 */
    if (Type.isUndefinedOrNull(iterable)) {
        return map;
    }
    /* step 7 */
    Object _adder = Get(calleeContext, map, "set");
    if (!IsCallable(_adder)) {
        throw newTypeError(calleeContext, Messages.Key.PropertyNotCallable, "set");
    }
    Callable adder = (Callable) _adder;
    boolean isBuiltin = MapPrototype.isBuiltinSet(_adder);
    if (isBuiltin && iterable instanceof MapObject) {
        MapObject other = (MapObject) iterable;
        if (ScriptIterators.isBuiltinIterator(calleeContext, other)) {
            map.getMapData().setAll(other.getMapData());
            return map;
        }
    }
    ScriptIterator<?> iter = GetScriptIterator(calleeContext, iterable);
    /* step 9 */
    try {
        while (iter.hasNext()) {
            Object nextItem = iter.next();
            if (!Type.isObject(nextItem)) {
                throw newTypeError(calleeContext, Messages.Key.MapPairNotObject);
            }
            ScriptObject item = Type.objectValue(nextItem);
            Object k = Get(calleeContext, item, 0);
            Object v = Get(calleeContext, item, 1);
            if (isBuiltin) {
                map.getMapData().set(k, v);
            } else {
                adder.call(calleeContext, map, k, v);
            }
        }
        return map;
    } catch (ScriptException e) {
        iter.close(e);
        throw e;
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) 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