Search in sources :

Example 1 with RealmObject

use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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);
}
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) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) Script(com.github.anba.es6draft.Script) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) ErrorObject(com.github.anba.es6draft.runtime.objects.ErrorObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayBufferObject(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 2 with RealmObject

use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject in project es6draft by anba.

the class ShellFunctions method evalScript.

/**
 * shell-function: {@code evalScript(sourceString, [options])}
 *
 * @param cx
 *            the execution context
 * @param caller
 *            the caller 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, ExecutionContext caller, 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(caller.sourceInfo(), name, line);
    Script script = realm.getScriptLoader().script(source, sourceString);
    return script.evaluate(realm);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.loadScript(com.github.anba.es6draft.repl.functions.SharedFunctions.loadScript) CompiledScript(com.github.anba.es6draft.compiler.CompiledScript) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.functions.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) ErrorObject(com.github.anba.es6draft.runtime.objects.ErrorObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayBufferObject(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 3 with RealmObject

use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 4 with RealmObject

use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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

Realm (com.github.anba.es6draft.runtime.Realm)4 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)4 RealmObject (com.github.anba.es6draft.runtime.objects.reflect.RealmObject)4 Script (com.github.anba.es6draft.Script)2 AliasFunction (com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)2 Source (com.github.anba.es6draft.runtime.internal.Source)2 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)2 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)2 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)2 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)2 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)2 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)2 ErrorObject (com.github.anba.es6draft.runtime.objects.ErrorObject)2 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)2 ArrayBufferObject (com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject)2 WeakMapObject (com.github.anba.es6draft.runtime.objects.collection.WeakMapObject)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)2 IOException (java.io.IOException)2 CompiledScript (com.github.anba.es6draft.compiler.CompiledScript)1