use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.
the class MozShellFunctions method run.
/**
* shell-function: {@code run(file)}
*
* @param cx
* the execution context
* @param fileName
* the file to evaluate
* @return the execution time in milli-seconds
*/
@Function(name = "run", arity = 1)
public double run(ExecutionContext cx, String fileName) {
long start = System.nanoTime();
Path file = Paths.get(fileName);
loadScript(cx, file, absolutePath(cx, file));
long end = System.nanoTime();
return (double) TimeUnit.NANOSECONDS.toMillis(end - start);
}
use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.
the class ShellFunctions method compile.
/**
* shell-function: {@code compile(filename)}
*
* @param cx
* the execution context
* @param filename
* the file to load
* @return the status message
*/
@Function(name = "compile", arity = 1)
public String compile(ExecutionContext cx, String filename) {
try {
Path file = absolutePath(cx, Paths.get(filename));
cx.getRealm().getScriptLoader().script(new Source(file, filename, 1), file);
} catch (ParserException | CompilationException | IOException e) {
return "error: " + e.getMessage();
}
return "success";
}
use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.
the class ShellFunctions method dump.
/**
* shell-function: {@code dump(object)}
*
* @param cx
* the execution context
* @param object
* the object to inspect
*/
@Function(name = "dump", arity = 1)
public void dump(ExecutionContext cx, Object object) {
String id;
if (!Type.isType(object)) {
// foreign object or null
id = Objects.toString(object, "<null>");
} else if (!Type.isObject(object)) {
// primitive value
id = String.format("%s (%s)", object.toString(), object.getClass().getSimpleName());
} else {
// script objects
id = String.format("%s@%x", object.getClass().getSimpleName(), System.identityHashCode(object));
}
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
writer.println(id);
}
use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.
the class ShellFunctions method dumpScope.
/**
* shell-function: {@code dumpScope()}
*
* @param cx
* the execution context
* @param caller
* the caller context
*/
@Function(name = "dumpScope", arity = 0)
public void dumpScope(ExecutionContext cx, ExecutionContext caller) {
if (caller.getLexicalEnvironment() != null) {
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
writer.println(caller.getLexicalEnvironment().toString());
}
}
use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.
the class ShellFunctions method evalScript.
/**
* shell-function: {@code evalScript(sourceString, [options])}
*
* @param cx
* the execution context
* @param sourceString
* the source string
* @param options
* the options object (optional)
* @return the evaluation result
*/
@Function(name = "evalScript", arity = 1)
public Object evalScript(ExecutionContext cx, String sourceString, Object options) {
String name = "";
int line = 1;
Realm realm = cx.getRealm();
if (Type.isObject(options)) {
ScriptObject opts = Type.objectValue(options);
Object fileName = Get(cx, opts, "fileName");
if (!Type.isUndefined(fileName)) {
name = ToFlatString(cx, fileName);
}
Object lineNumber = Get(cx, opts, "lineNumber");
if (!Type.isUndefined(lineNumber)) {
line = ToInt32(cx, lineNumber);
}
Object g = Get(cx, opts, "global");
if (!Type.isUndefined(g)) {
if (!(g instanceof GlobalObject)) {
throw Errors.newError(cx, "invalid global argument");
}
realm = ((GlobalObject) g).getRealm();
}
Object r = Get(cx, opts, "realm");
if (!Type.isUndefined(r)) {
if (!(r instanceof RealmObject)) {
throw Errors.newError(cx, "invalid realm argument");
}
realm = ((RealmObject) r).getRealm();
}
}
Source source = new Source(name, line);
Script script = realm.getScriptLoader().script(source, sourceString);
return script.evaluate(realm);
}
Aggregations