use of com.github.anba.es6draft.Script in project es6draft by anba.
the class ShellFunctions method disassemble.
/**
* shell-function: {@code disassemble([function])}
*
* @param cx
* the execution context
* @param caller
* the caller context
* @param args
* the arguments
* @throws IOException
* if there was any I/O error
* @throws MalformedNameException
* if the module name cannot be normalized
*/
@Function(name = "disassemble", arity = 1)
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
DebugInfo debugInfo = null;
if (args.length == 0) {
FunctionObject currentFunction = caller.getCurrentFunction();
Executable currentExec = caller.getCurrentExecutable();
if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
debugInfo = currentFunction.getCode().debugInfo();
} else if (currentExec != null && currentExec.getSourceObject() != null) {
debugInfo = currentExec.getSourceObject().debugInfo();
}
} else if (args[0] instanceof FunctionObject) {
debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
} else {
String sourceCode = ToFlatString(cx, args[0]);
boolean isModule = false;
if (args.length > 1 && Type.isObject(args[1])) {
isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
}
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
if (isModule) {
ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
ModuleSource src = new StringModuleSource(identifier, sourceCode);
SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
debugInfo = module.getScriptCode().getSourceObject().debugInfo();
} else {
Source source = new Source("<disassemble>", 1);
Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
debugInfo = script.getSourceObject().debugInfo();
}
}
if (debugInfo != null) {
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
for (DebugInfo.Method method : debugInfo.getMethods()) {
writer.println(method.disassemble());
}
}
}
use of com.github.anba.es6draft.Script in project es6draft by anba.
the class ShellGlobalObject method eval.
/**
* Parses, compiles and executes the javascript file.
*
* @param fileName
* the file name for the script file
* @param file
* the absolute path to the file
* @return the evaluation result
* @throws IOException
* if there was any I/O error
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
*/
public Object eval(Path fileName, Path file) throws IOException, ParserException, CompilationException {
Realm realm = getRealm();
Source source = new Source(file, fileName.toString(), 1);
Script script = realm.getScriptLoader().script(source, file);
return script.evaluate(realm);
}
use of com.github.anba.es6draft.Script 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.Script in project es6draft by anba.
the class NativeCode method load.
/**
* Parses, compiles and executes the javascript file. (Uses the script cache.)
* <p>
* The script file is loaded as a native script with elevated privileges.
*
* @param realm
* the realm instance
* @param name
* the script name
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* 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 static void load(Realm realm, String name) throws IOException, URISyntaxException, ParserException, CompilationException {
RuntimeContext context = realm.getWorld().getContext();
ScriptCache scriptCache = context.getScriptCache();
Script script = scriptCache.get(createNativeScriptLoader(context), getScriptURL(name));
script.evaluate(realm);
}
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 path
* @return the compiled script
* @throws IOException
* if there was any I/O error
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
*/
public Script get(ScriptLoader scriptLoader, Path file) throws IOException, ParserException, CompilationException {
CacheKey cacheKey = keyFor(file);
Script cachedScript = cache.get(cacheKey);
if (cachedScript != null) {
return cachedScript;
}
Source source = new Source(file, Objects.requireNonNull(file.getFileName()).toString(), 1);
Script script = scriptLoader.script(source, file);
cache.put(cacheKey, script);
return script;
}
Aggregations