Search in sources :

Example 11 with ModuleLoader

use of com.github.anba.es6draft.runtime.modules.ModuleLoader in project es6draft by anba.

the class ShellFunctions method disassemble.

/**
 * shell-function: {@code disassemble([function])}
 *
 * @param cx
 *            the execution context
 * @param caller
 *            the caller context
 * @param args
 *            the arguments
 * @throws IOException
 *             if there was any I/O error
 * @throws MalformedNameException
 *             if the module name cannot be normalized
 */
@Function(name = "disassemble", arity = 1)
@AliasFunction(name = "dis")
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
    DebugInfo debugInfo = null;
    if (args.length == 0) {
        FunctionObject currentFunction = caller.getCurrentFunction();
        Executable currentExec = caller.getCurrentExecutable();
        if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
            debugInfo = currentFunction.getCode().debugInfo();
        } else {
            debugInfo = currentExec.getRuntimeObject().debugInfo();
        }
    } else if (args[0] instanceof FunctionObject) {
        debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
    } else {
        String sourceCode = ToFlatString(cx, args[0]);
        boolean isModule = false;
        if (args.length > 1 && Type.isObject(args[1])) {
            isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
        }
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        if (isModule) {
            ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
            SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
            ModuleSource moduleSource = new StringModuleSource(identifier, "disassemble", sourceCode);
            SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, moduleSource);
            debugInfo = module.getScriptCode().getRuntimeObject().debugInfo();
        } else {
            Source source = new Source("<disassemble>", 1);
            Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
            debugInfo = script.getRuntimeObject().debugInfo();
        }
    }
    if (debugInfo != null) {
        PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
        for (DebugInfo.Method method : debugInfo.getMethods()) {
            writer.println(method.disassemble());
        }
    }
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) 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) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) 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) Executable(com.github.anba.es6draft.Executable) DebugInfo(com.github.anba.es6draft.runtime.internal.DebugInfo) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 12 with ModuleLoader

use of com.github.anba.es6draft.runtime.modules.ModuleLoader in project es6draft by anba.

the class Test262TestRealm method execute.

/**
 * Executes the supplied test object.
 *
 * @param test
 *            the test-info object
 * @throws IOException
 *             if there was any I/O error
 * @throws MalformedNameException
 *             if any imported module request cannot be normalized
 * @throws ParserException
 *             if the module source contains any syntax errors
 * @throws CompilationException
 *             if the parsed module source cannot be compiled
 */
void execute(Test262Info test) throws ParserException, CompilationException, IOException, MalformedNameException {
    // Return early if no source code is available.
    if (sourceCode == null) {
        return;
    }
    assert !test.isAsync() || async != null;
    Realm realm = get();
    // Parse and evaluate the test-script
    if (test.isModule()) {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        String moduleName = test.toModuleName();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleSource source = new StringModuleSource(moduleId, moduleName, sourceCode);
        ModuleRecord module = moduleLoader.define(moduleId, source, realm);
        try {
            module.instantiate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
        // Return after module instantiation if we test for module resolution errors.
        if (test.getErrorPhase() == ErrorPhase.Resolution) {
            return;
        }
        try {
            module.evaluate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
    } else {
        Source source = new Source(test.toFile(), test.getScript().toString(), 1);
        Script script = realm.getScriptLoader().script(source, sourceCode);
        script.evaluate(realm);
    }
    // Wait for pending jobs to finish
    waitForPendingJobs(realm, test);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) 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) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Aggregations

ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)12 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)11 Realm (com.github.anba.es6draft.runtime.Realm)7 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)7 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)6 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)6 Source (com.github.anba.es6draft.runtime.internal.Source)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)5 Script (com.github.anba.es6draft.Script)4 IOException (java.io.IOException)3 Executable (com.github.anba.es6draft.Executable)2 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)2 World (com.github.anba.es6draft.runtime.World)2 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)2 AliasFunction (com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)2 FileSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)2 RealmObject (com.github.anba.es6draft.runtime.objects.reflect.RealmObject)2