Search in sources :

Example 1 with ParserException

use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.

the class GeneratorFunctionConstructor method CreateDynamicGenerator.

private static OrdinaryGenerator CreateDynamicGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.Generator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.generator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryGenerator f = OrdinaryGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
    f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) OrdinaryGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryGenerator) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Property(com.github.anba.es6draft.runtime.types.Property) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 2 with ParserException

use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.

the class RealmConstructor method construct.

/**
     * 26.?.1.1 Reflect.Realm ( [ target , handler ] )
     */
@Override
public RealmObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* steps 2-3 */
    RealmObject realmObject = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.RealmPrototype, RealmObject::new);
    /* steps 4-5 */
    ScriptObject newGlobal;
    if (args.length != 0) {
        /* step 4 */
        Object target = argument(args, 0);
        Object handler = argument(args, 1);
        newGlobal = ProxyCreate(calleeContext, target, handler);
    } else {
        /* step 5 */
        newGlobal = null;
    }
    /* steps 6-7 */
    Realm realm = CreateRealmAndSetRealmGlobalObject(calleeContext, realmObject, newGlobal, newGlobal);
    /* step 17 (Moved before extracting extension hooks to avoid uninitialized object state) */
    realmObject.setRealm(realm);
    // Run any initialization scripts, if required. But do _not_ install extensions!
    try {
        GlobalObject globalTemplate = realm.getGlobalObjectTemplate();
        assert globalTemplate != null;
        globalTemplate.initializeScripted();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(calleeContext);
    } catch (IOException | URISyntaxException e) {
        throw newError(calleeContext, e.getMessage());
    }
    /* steps 8-9 */
    Callable translate = GetMethod(calleeContext, realmObject, "directEval");
    /* steps 10-11 */
    Callable fallback = GetMethod(calleeContext, realmObject, "nonEval");
    /* steps 12-13 */
    Callable indirectEval = GetMethod(calleeContext, realmObject, "indirectEval");
    /* steps 14-16 */
    realm.setExtensionHooks(translate, fallback, indirectEval);
    /* steps 18-19 */
    Callable initGlobal = GetMethod(calleeContext, realmObject, "initGlobal");
    /* steps 20-21 */
    if (initGlobal != null) {
        /* step 20 */
        initGlobal.call(calleeContext, realmObject);
    } else {
        /* step 21 */
        SetDefaultGlobalBindings(calleeContext, realm);
    }
    /* step 22 */
    return realmObject;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) CreateRealmAndSetRealmGlobalObject(com.github.anba.es6draft.runtime.Realm.CreateRealmAndSetRealmGlobalObject) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Callable(com.github.anba.es6draft.runtime.types.Callable) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) CreateRealmAndSetRealmGlobalObject(com.github.anba.es6draft.runtime.Realm.CreateRealmAndSetRealmGlobalObject) Realm(com.github.anba.es6draft.runtime.Realm)

Example 3 with ParserException

use of com.github.anba.es6draft.parser.ParserException 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());
    }
}
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) Realm(com.github.anba.es6draft.runtime.Realm) 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) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 4 with ParserException

use of com.github.anba.es6draft.parser.ParserException 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";
}
Also used : SharedFunctions.absolutePath(com.github.anba.es6draft.repl.global.SharedFunctions.absolutePath) Path(java.nio.file.Path) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) IOException(java.io.IOException) 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 5 with ParserException

use of com.github.anba.es6draft.parser.ParserException 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);
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

ParserException (com.github.anba.es6draft.parser.ParserException)20 CompilationException (com.github.anba.es6draft.compiler.CompilationException)12 Source (com.github.anba.es6draft.runtime.internal.Source)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)8 Realm (com.github.anba.es6draft.runtime.Realm)7 IOException (java.io.IOException)6 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)5 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)5 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)5 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)4 FunctionConstructor.functionSource (com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource)4 Script (com.github.anba.es6draft.Script)3 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)3 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)3 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)3 Path (java.nio.file.Path)3 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)2 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)2 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)2 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)2