Search in sources :

Example 1 with FileSourceIdentifier

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

the class NodeModuleResolution method resolve.

/**
 * Resolves a module name.
 *
 * @param baseDirectory
 *            the base directory
 * @param normalizedName
 *            the normalized module identifier
 * @param unnormalizedName
 *            the unnormalized module identifier
 * @param referrerId
 *            the referrer module identifier or {@code null}
 * @return the resolved and normalized module identifier
 * @throws MalformedNameException
 *             if the name cannot be normalized
 */
static FileSourceIdentifier resolve(Path baseDirectory, FileSourceIdentifier normalizedName, String unnormalizedName, SourceIdentifier referrerId) throws MalformedNameException {
    if (referrerId != null) {
        try {
            Path unnormalizedPath = Paths.get(unnormalizedName);
            Path referrer = Paths.get(baseDirectory.toUri().resolve(referrerId.toUri()));
            if (unnormalizedName.startsWith("./") || unnormalizedName.startsWith("../")) {
                Path path = referrer.resolveSibling(unnormalizedPath);
                Path file = loadAsFile(path);
                if (file != null) {
                    return new FileSourceIdentifier(file);
                }
                file = loadAsDirectory(path);
                if (file != null) {
                    return new FileSourceIdentifier(file);
                }
            }
            Path file = loadNodeModules(unnormalizedPath, referrer, baseDirectory);
            if (file != null) {
                return new FileSourceIdentifier(file);
            }
        } catch (InvalidPathException e) {
            throw new MalformedNameException(unnormalizedName);
        }
    }
    // If node module resolution failed, use default module name resolution.
    return normalizedName;
}
Also used : Path(java.nio.file.Path) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) InvalidPathException(java.nio.file.InvalidPathException)

Example 2 with FileSourceIdentifier

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

the class TraceurFileModuleLoader method normalizeName.

@Override
public FileSourceIdentifier normalizeName(String unnormalizedName, SourceIdentifier referrerId) throws MalformedNameException {
    FileSourceIdentifier normalizedName = super.normalizeName(unnormalizedName, referrerId);
    try {
        Path fileName = normalizedName.getPath().getFileName();
        if (!fileName.toString().endsWith(".js")) {
            Path path = Paths.get(normalizedName.getPath() + ".js");
            normalizedName = new FileSourceIdentifier(getBaseDirectory(), path);
        }
        return normalizedName;
    } catch (InvalidPathException e) {
        throw new MalformedNameException(unnormalizedName);
    }
}
Also used : Path(java.nio.file.Path) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) InvalidPathException(java.nio.file.InvalidPathException)

Example 3 with FileSourceIdentifier

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

the class ScriptLoading method eval.

/**
 * Parses, compiles and executes the javascript source code.
 *
 * @param realm
 *            the realm instance
 * @param sourceName
 *            the file name for the script file
 * @param sourceCode
 *            the script source code
 * @return the evaluation result
 * @throws ParserException
 *             if the source contains any syntax errors
 * @throws CompilationException
 *             if the parsed source could not be compiled
 */
public static Object eval(Realm realm, String sourceName, String sourceCode) throws ParserException, CompilationException {
    Source source = new Source(new FileSourceIdentifier(Paths.get("")), sourceName, 1);
    Script script = realm.getScriptLoader().script(source, sourceCode);
    return script.evaluate(realm);
}
Also used : Script(com.github.anba.es6draft.Script) URLModuleSource(com.github.anba.es6draft.runtime.modules.loader.URLModuleSource) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)

Example 4 with FileSourceIdentifier

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

the class TestRealms method compileModules.

private PreloadModules compileModules() throws IOException, MalformedNameException {
    List<String> moduleNames = Resources.list(configuration, "modules", emptyList());
    if (moduleNames.isEmpty()) {
        return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
    }
    Path basedir = getBaseDirectory();
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    TestModuleLoader<?> moduleLoader = getModuleLoader().apply(context, scriptLoader);
    ArrayList<ModuleRecord> modules = new ArrayList<>();
    for (String moduleName : moduleNames) {
        Map.Entry<Path, String> resourceModule = Resources.resourceModule(moduleName);
        ModuleRecord module;
        if (resourceModule == null) {
            SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
            module = moduleLoader.load(moduleId);
        } else {
            Path modulePath = basedir.resolve(resourceModule.getKey());
            String sourceCode = resourceModule.getValue();
            ResourceModuleSource moduleSource = new ResourceModuleSource(modulePath, sourceCode);
            FileSourceIdentifier sourceId = new FileSourceIdentifier(modulePath);
            module = moduleLoader.defineUnlinked(sourceId, moduleSource);
        }
        modules.add(module);
    }
    return new PreloadModules(modules, moduleLoader.getModules());
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Map(java.util.Map) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)

Aggregations

FileSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)4 Path (java.nio.file.Path)3 MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)2 InvalidPathException (java.nio.file.InvalidPathException)2 Script (com.github.anba.es6draft.Script)1 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)1 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)1 URLModuleSource (com.github.anba.es6draft.runtime.modules.loader.URLModuleSource)1 URLSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1