Search in sources :

Example 11 with ParserException

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

the class Repl method read.

/**
     * REPL: Read
     * 
     * @param realm
     *            the realm instance
     * @param line
     *            the current line
     * @return the parsed script node or {@coden null} if the end of stream has been reached
     */
private com.github.anba.es6draft.ast.Script read(Realm realm, int[] line) {
    StringBuilder sourceBuffer = new StringBuilder();
    for (String prompt = PROMPT; ; prompt = "") {
        String s = console.readLine(prompt);
        if (s == null) {
            return null;
        }
        sourceBuffer.append(s).append('\n');
        String sourceCode = sourceBuffer.toString();
        Source source = new Source(Paths.get(".").toAbsolutePath(), "typein", line[0]);
        try {
            com.github.anba.es6draft.ast.Script script = parse(realm, source, sourceCode);
            line[0] += script.getEndLine() - script.getBeginLine();
            return script;
        } catch (ParserEOFException e) {
            continue;
        } catch (ParserException e) {
            throw new ParserExceptionWithSource(e, source, sourceCode);
        }
    }
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) ParserEOFException(com.github.anba.es6draft.parser.ParserEOFException) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Example 12 with ParserException

use of com.github.anba.es6draft.parser.ParserException 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 13 with ParserException

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

the class AsyncFunctionConstructor method CreateDynamicFunction.

/**
     * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
     * 
     * @param callerContext
     *            the caller execution context
     * @param cx
     *            the execution context
     * @param newTarget
     *            the newTarget constructor function
     * @param args
     *            the function arguments
     * @return the new generator function object
     */
private static OrdinaryAsyncFunction CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.AsyncFunction;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.AsyncFunction, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.asyncFunction(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 */
    OrdinaryAsyncFunction f = 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));
    /* steps 27-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) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 14 with ParserException

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

the class GeneratorFunctionConstructor method CreateDynamicConstructorGenerator.

private static OrdinaryConstructorGenerator CreateDynamicConstructorGenerator(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 */
    OrdinaryConstructorGenerator f = OrdinaryConstructorGenerator.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);
    MakeConstructor(f, true, prototype);
    /* 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) OrdinaryConstructorGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorGenerator) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 15 with ParserException

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

the class FunctionConstructor method CreateDynamicFunction.

/**
     * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
     * 
     * @param callerContext
     *            the caller execution context
     * @param cx
     *            the execution context
     * @param newTarget
     *            the newTarget constructor function
     * @param args
     *            the function arguments
     * @return the new function object
     */
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 */
    Intrinsics fallbackProto = Intrinsics.FunctionPrototype;
    /* step 3 (not applicable) */
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Function, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.function(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* steps 12, 21-30 */
    return CreateDynamicFunction(cx, source, function, newTarget, fallbackProto);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Source(com.github.anba.es6draft.runtime.internal.Source) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

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