use of com.github.anba.es6draft.Script in project es6draft by anba.
the class AtomicsTestFunctions method evalInWorker.
/**
* shell-function: {@code evalInWorker(sourceString)}
*
* @param cx
* the execution context
* @param caller
* the caller execution context
* @param sourceString
* the script source code
* @return {@code true} if a new script worker was started, otherwise returns {@code false}
*/
@Function(name = "evalInWorker", arity = 1)
public boolean evalInWorker(ExecutionContext cx, ExecutionContext caller, String sourceString) {
Source baseSource = Objects.requireNonNull(cx.getRealm().sourceInfo(caller));
try {
// TODO: Initialize extensions (console.jsm, window timers?).
CompletableFuture.supplyAsync(() -> {
// Set 'executor' to null so it doesn't get shared with the current runtime context.
/* @formatter:off */
RuntimeContext context = new RuntimeContext.Builder(cx.getRuntimeContext()).setExecutor(null).build();
/* @formatter:on */
World world = new World(context);
Realm realm;
try {
realm = world.newInitializedRealm();
} catch (IOException | URISyntaxException e) {
throw new CompletionException(e);
}
// Bind test functions to this instance.
realm.createGlobalProperties(this, AtomicsTestFunctions.class);
// TODO: Add proper abstraction.
ModuleLoader moduleLoader = world.getModuleLoader();
if (moduleLoader instanceof NodeModuleLoader) {
try {
((NodeModuleLoader) moduleLoader).initialize(realm);
} catch (IOException | URISyntaxException | MalformedNameException | ResolutionException e) {
throw new CompletionException(e);
}
}
// Evaluate the script source code and then run pending jobs.
Source source = new Source(baseSource, "evalInWorker-script", 1);
Script script = realm.getScriptLoader().script(source, sourceString);
Object result = script.evaluate(realm);
world.runEventLoop();
return result;
}, cx.getRuntimeContext().getWorkerExecutor()).whenComplete((r, e) -> {
if (e instanceof CompletionException) {
Throwable cause = ((CompletionException) e).getCause();
cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, (cause != null ? cause : e));
} else if (e != null) {
cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, e);
}
});
return true;
} catch (RejectedExecutionException e) {
return false;
}
}
use of com.github.anba.es6draft.Script in project es6draft by anba.
the class MozShellFunctions method evalcx.
/**
* shell-function: {@code evalcx(s, [o])}
*
* @param cx
* the execution context
* @param caller
* the caller context
* @param sourceCode
* the source to evaluate
* @param o
* the global object
* @return the eval result value
*/
@Function(name = "evalcx", arity = 1)
public Object evalcx(ExecutionContext cx, ExecutionContext caller, String sourceCode, Object o) {
ScriptObject global;
if (Type.isUndefinedOrNull(o)) {
global = newGlobal(cx);
} else {
global = ToObject(cx, o);
}
if (sourceCode.isEmpty() || "lazy".equals(sourceCode)) {
return global;
}
if (!(global instanceof GlobalObject)) {
throw Errors.newError(cx, "invalid global argument");
}
Source source = new Source(cx.getRealm().sourceInfo(caller), "evalcx", 1);
Realm realm = ((GlobalObject) global).getRealm();
try {
Script script = realm.getScriptLoader().script(source, sourceCode);
return script.evaluate(realm);
} catch (ParserException | CompilationException e) {
// Create a script exception from the requested code realm, not from the caller's realm.
throw e.toScriptException(realm.defaultContext());
}
}
use of com.github.anba.es6draft.Script 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.Script in project es6draft by anba.
the class RuntimeFunctions method Include.
/**
* Native function: {@code %Include(<file>)}.
* <p>
* Loads and evaluates the script file.
*
* @param cx
* the execution context
* @param file
* the file path
* @return the script evaluation result
*/
public static Object Include(ExecutionContext cx, CharSequence file) {
Realm realm = cx.getRealm();
Source base = realm.sourceInfo(cx);
if (base == null || base.getFile() == null) {
throw newInternalError(cx, Messages.Key.InternalError, "No source: " + Objects.toString(base));
}
Path path = Objects.requireNonNull(base.getFile().getParent()).resolve(file.toString());
Source source = new Source(path, Objects.requireNonNull(path.getFileName()).toString(), 1);
Script script;
try {
script = realm.getScriptLoader().script(source, path);
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
} catch (IOException e) {
throw newInternalError(cx, e, Messages.Key.InternalError, e.toString());
}
return script.evaluate(cx);
}
use of com.github.anba.es6draft.Script in project es6draft by anba.
the class ScriptCache method get.
/**
* Compiles {@code file} to a {@link Script} and caches the result.
*
* @param scriptLoader
* the script loader
* @param file
* the script file URL
* @return the compiled script
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* if the URL is not a valid URI
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
*/
public Script get(ScriptLoader scriptLoader, URL file) throws IOException, URISyntaxException, ParserException, CompilationException {
CacheKey cacheKey = keyFor(file);
Script cachedScript = cache.get(cacheKey);
if (cachedScript != null) {
return cachedScript;
}
Source source = new Source(file.getPath(), 1);
Script script = scriptLoader.script(source, file);
cache.put(cacheKey, script);
return script;
}
Aggregations