/**
 * Copyright (c) 2012-2013 André Bargull
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.repl;

import static com.github.anba.es6draft.runtime.internal.Properties.createProperties;

import java.nio.file.Path;
import java.util.Set;

import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.Realm.GlobalObjectCreator;
import com.github.anba.es6draft.runtime.internal.CompatibilityOption;
import com.github.anba.es6draft.runtime.internal.ScriptCache;
import com.github.anba.es6draft.runtime.internal.Properties.Function;

/**
 *
 */
public class SimpleShellGlobalObject extends ShellGlobalObject {
    public SimpleShellGlobalObject(Realm realm, ShellConsole console, Path baseDir, Path script,
            ScriptCache scriptCache) {
        super(realm, console, baseDir, script, scriptCache);
    }

    @Override
    public void initialise(ExecutionContext cx) {
        super.initialise(cx);
        createProperties(this, cx, SimpleShellGlobalObject.class);
    }

    public static SimpleShellGlobalObject newGlobal(final ShellConsole console, final Path baseDir,
            final Path script, final ScriptCache scriptCache, final Set<CompatibilityOption> options) {
        Realm realm = Realm.newRealm(new GlobalObjectCreator<SimpleShellGlobalObject>() {
            @Override
            public SimpleShellGlobalObject createGlobal(Realm realm) {
                return new SimpleShellGlobalObject(realm, console, baseDir, script, scriptCache);
            }
        }, options);
        return (SimpleShellGlobalObject) realm.getGlobalThis();
    }

    /** shell-function: {@code print(message)} */
    @Function(name = "print", arity = 1)
    public void print(String message) {
        console.print(message);
    }

    /** shell-function: {@code quit()} */
    @Function(name = "quit", arity = 0)
    public void quit() {
        throw new StopExecutionException(StopExecutionException.Reason.Quit);
    }
}

@@ -309,45 +309,220 @@
 ava.
-nio.file.Path;%0Aimport java.util.Set;%0A
+io.IOException;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.Set;%0A%0Aimport com.github.anba.es6draft.compiler.CompilationException;%0Aimport com.github.anba.es6draft.parser.ParserException;
 %0Aimp
@@ -1924,32 +1924,432 @@
 lThis();%0A    %7D%0A%0A
+    /** shell-function: %7B@code compile(filename)%7D */%0A    @Function(name = %22compile%22, arity = 1)%0A    public String compile(String filename) %7B%0A        try %7B%0A            scriptCache.script(filename, 1, absolutePath(Paths.get(filename)));%0A        %7D catch (ParserException %7C CompilationException %7C IOException e) %7B%0A            return %22error: %22 + e.getMessage();%0A        %7D%0A        return %22success%22;%0A    %7D%0A%0A
     /** shell-fu

Add test function for compiler