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