Search in sources :

Example 21 with Function

use of com.github.anba.es6draft.runtime.internal.Properties.Function 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(), ""));
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) URISyntaxException(java.net.URISyntaxException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ParserException(com.github.anba.es6draft.parser.ParserException) IOException(java.io.IOException) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ProxyObject(com.github.anba.es6draft.runtime.types.builtins.ProxyObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) BoundFunctionObject(com.github.anba.es6draft.runtime.types.builtins.BoundFunctionObject) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 22 with Function

use of com.github.anba.es6draft.runtime.internal.Properties.Function in project es6draft by anba.

the class ShellFunctions method loadModule.

/**
     * shell-function: {@code loadModule(moduleName, [realmObject])}
     * 
     * @param cx
     *            the execution context
     * @param moduleName
     *            the module name
     * @param realmObject
     *            the optional realm object
     * @return the module namespace object
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     */
@Function(name = "loadModule", arity = 1)
public ScriptObject loadModule(ExecutionContext cx, String moduleName, Object realmObject) throws MalformedNameException, ResolutionException {
    Realm realm;
    if (!Type.isUndefined(realmObject)) {
        if (!(realmObject instanceof RealmObject)) {
            throw Errors.newTypeError(cx, Messages.Key.IncompatibleObject);
        }
        realm = ((RealmObject) realmObject).getRealm();
    } else {
        realm = cx.getRealm();
    }
    try {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleRecord module = moduleLoader.resolve(moduleId, realm);
        module.instantiate();
        module.evaluate();
        return GetModuleNamespace(cx, module);
    } catch (IOException e) {
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    }
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Aggregations

Function (com.github.anba.es6draft.runtime.internal.Properties.Function)22 PrintWriter (java.io.PrintWriter)12 Source (com.github.anba.es6draft.runtime.internal.Source)6 BuiltinFunction (com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)6 Script (com.github.anba.es6draft.Script)5 SharedFunctions.absolutePath (com.github.anba.es6draft.repl.global.SharedFunctions.absolutePath)5 Realm (com.github.anba.es6draft.runtime.Realm)5 Path (java.nio.file.Path)5 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)4 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)4 CompilationException (com.github.anba.es6draft.compiler.CompilationException)3 ParserException (com.github.anba.es6draft.parser.ParserException)3 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)3 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)3 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)3 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)3 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 IOException (java.io.IOException)3