Search in sources :

Example 1 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader 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 ScriptLoader

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

the class AsyncGeneratorFunctionConstructor 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 async generator function object
     */
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.AsyncGenerator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.AsyncGenerator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.asyncGenerator(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 */
    OrdinaryAsyncGenerator 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));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.AsyncGeneratorPrototype);
    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) OrdinaryAsyncGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncGenerator) 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 3 with ScriptLoader

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

the class TestGlobals method compileModules.

private PreloadModules compileModules() throws IOException, MalformedNameException {
    List<?> moduleNames = configuration.getList("modules", emptyList());
    if (moduleNames.isEmpty()) {
        return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
    }
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    TestModuleLoader<?> moduleLoader = this.moduleLoader.apply(context, scriptLoader);
    ArrayList<ModuleRecord> modules = new ArrayList<>();
    for (String moduleName : nonEmpty(moduleNames)) {
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        modules.add(moduleLoader.load(moduleId));
    }
    return new PreloadModules(modules, moduleLoader.getModules());
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) ArrayList(java.util.ArrayList) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 4 with ScriptLoader

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

the class TestRealms method compileScripts.

private List<Script> compileScripts() throws IOException {
    List<String> scriptNames = Resources.list(configuration, "scripts", emptyList());
    if (scriptNames.isEmpty()) {
        return Collections.emptyList();
    }
    Path basedir = getBaseDirectory();
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    ArrayList<Script> scripts = new ArrayList<>();
    for (String scriptName : scriptNames) {
        Map.Entry<Either<Path, URL>, InputStream> resourceScript = Resources.resourceScript(scriptName, basedir);
        Source source = resourceScript.getKey().map(path -> new Source(path, scriptName, 1), url -> new Source(new URLSourceIdentifier(url), scriptName, 1));
        Script script = scriptLoader.script(source, resourceScript.getValue());
        scripts.add(script);
    }
    return scripts;
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Map(java.util.Map) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 5 with ScriptLoader

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

the class Test262TestRealm method executeStrict.

/**
 * Executes the supplied test object.
 *
 * @param test
 *            the test-info object
 * @throws ParserException
 *             if the module source contains any syntax errors
 * @throws CompilationException
 *             if the parsed module source cannot be compiled
 */
void executeStrict(Test262Info test) throws ParserException, CompilationException {
    // Return early if no source code is available.
    if (sourceCode == null) {
        return;
    }
    assert !test.isAsync() || async != null;
    assert !(test.isModule() || test.isRaw());
    Realm realm = get();
    // Parse the test-script
    Source source = new Source(test.toFile(), test.getScript().toString(), 1);
    ScriptLoader scriptLoader = realm.getScriptLoader();
    EnumSet<Parser.Option> parserOptions = EnumSet.copyOf(realm.getRuntimeContext().getParserOptions());
    parserOptions.add(Parser.Option.Strict);
    Parser parser = new Parser(realm.getRuntimeContext(), source, parserOptions);
    com.github.anba.es6draft.ast.Script parsedScript = parser.parseScript(sourceCode);
    // Evaluate the test-script
    Script script = scriptLoader.load(parsedScript);
    script.evaluate(realm);
    // Wait for pending jobs to finish
    waitForPendingJobs(realm, test);
}
Also used : Script(com.github.anba.es6draft.Script) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) Parser(com.github.anba.es6draft.parser.Parser)

Aggregations

ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)15 Source (com.github.anba.es6draft.runtime.internal.Source)13 CompilationException (com.github.anba.es6draft.compiler.CompilationException)6 ParserException (com.github.anba.es6draft.parser.ParserException)6 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)6 Script (com.github.anba.es6draft.Script)5 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)5 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)4 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)4 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)4 FunctionConstructor.functionSource (com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource)4 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)4 ArrayList (java.util.ArrayList)4 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)3 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 Path (java.nio.file.Path)3