Search in sources :

Example 1 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable 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 IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable in project es6draft by anba.

the class SourceBuilder method source.

private String source(ExecutionContext cx, HashSet<ScriptObject> stack, Object value) {
    switch(Type.of(value)) {
        case Null:
            return "null";
        case Boolean:
            return Type.booleanValue(value) ? "true" : "false";
        case String:
            return Strings.quote(Type.stringValue(value).toString());
        case Symbol:
            return Type.symbolValue(value).toString();
        case Number:
            return ToFlatString(cx, value);
        case SIMD:
            return Type.simdValue(value).toString();
        case Object:
            ScriptObject objValue = Type.objectValue(value);
            if (IsCallable(objValue)) {
                return ((Callable) objValue).toSource(cx);
            }
            if (stack.contains(objValue) || stack.size() > maxStackDepth) {
                return "« ... »";
            }
            stack.add(objValue);
            try {
                if (objValue instanceof DateObject) {
                    return DatePrototype.Properties.toString(cx, value).toString();
                } else if (objValue instanceof RegExpObject) {
                    return RegExpPrototype.Properties.toString(cx, value).toString();
                } else if (objValue instanceof ArrayObject) {
                    return arrayToSource(cx, stack, objValue);
                } else {
                    return objectToSource(cx, stack, objValue);
                }
            } finally {
                stack.remove(objValue);
            }
        case Undefined:
        default:
            return "(void 0)";
    }
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RegExpObject(com.github.anba.es6draft.runtime.objects.text.RegExpObject) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable) DateObject(com.github.anba.es6draft.runtime.objects.date.DateObject)

Example 3 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable 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)

Example 4 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable in project es6draft by anba.

the class WeakMapConstructor method construct.

/**
     * 23.3.1.1 WeakMap ([ iterable ])
     */
@Override
public WeakMapObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    WeakMapObject map = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.WeakMapPrototype, WeakMapObject::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;
    ScriptIterator<?> iter = GetScriptIterator(calleeContext, iterable);
    /* step 9 */
    try {
        while (iter.hasNext()) {
            Object nextItem = iter.next();
            if (!Type.isObject(nextItem)) {
                throw newTypeError(calleeContext, Messages.Key.WeakMapPairNotObject);
            }
            ScriptObject item = Type.objectValue(nextItem);
            Object k = Get(calleeContext, item, 0);
            Object v = Get(calleeContext, item, 1);
            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)

Example 5 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable in project es6draft by anba.

the class WeakSetConstructor method construct.

/**
     * 23.4.1.1 WeakSet ([ iterable ])
     */
@Override
public WeakSetObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    WeakSetObject set = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.WeakSetPrototype, WeakSetObject::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;
    ScriptIterator<?> iter = GetScriptIterator(calleeContext, iterable);
    /* step 9 */
    try {
        while (iter.hasNext()) {
            Object nextValue = iter.next();
            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)

Aggregations

IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)7 Callable (com.github.anba.es6draft.runtime.types.Callable)7 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)5 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)5 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 ExecutionContext.newScriptingExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext)1 Realm (com.github.anba.es6draft.runtime.Realm)1 Console (com.github.anba.es6draft.runtime.internal.Console)1 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)1 DateObject (com.github.anba.es6draft.runtime.objects.date.DateObject)1 PromiseObject (com.github.anba.es6draft.runtime.objects.promise.PromiseObject)1 RegExpObject (com.github.anba.es6draft.runtime.objects.text.RegExpObject)1 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1