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);
}
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(), ""));
}
}
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;
}
}
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;
}
Aggregations