Search in sources :

Example 1 with MalformedNameException

use of com.github.anba.es6draft.runtime.modules.MalformedNameException 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
     */
public static FileSourceIdentifier resolve(Path baseDirectory, FileSourceIdentifier normalizedName, String unnormalizedName, SourceIdentifier referrerId) throws MalformedNameException {
    try {
        Path normalizedPath = normalizedName.getPath();
        boolean isRelative = unnormalizedName.startsWith("./") || unnormalizedName.startsWith("../");
        if (isRelative) {
            Path file = findModuleFile(baseDirectory, normalizedPath, true);
            if (file != null) {
                return new FileSourceIdentifier(baseDirectory, file);
            }
        } else if (referrerId != null) {
            for (Path p : new NodeModulePaths(baseDirectory, referrerId)) {
                Path file = findModuleFile(baseDirectory, p.resolve(normalizedPath), true);
                if (file != null) {
                    return new FileSourceIdentifier(baseDirectory, file);
                }
            }
        }
        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 2 with MalformedNameException

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

the class ModuleNamespaceObject method getValue.

/** 9.4.6.8 [[Get]] (P, Receiver) */
@Override
protected Object getValue(ExecutionContext cx, String propertyKey, Object receiver) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Set<String> exports = this.exports;
    /* step 4 */
    if (!exports.contains(propertyKey)) {
        return UNDEFINED;
    }
    /* step 5 */
    ModuleRecord m = this.module;
    /* steps 6-8 */
    ModuleExport binding;
    try {
        /* steps 6, 8 */
        binding = m.resolveExport(propertyKey, new HashMap<>(), new HashSet<>());
    } catch (IOException e) {
        /* step 7 */
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    } catch (ResolutionException | MalformedNameException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    } catch (ParserException | CompilationException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    }
    /* step 8 */
    assert binding != null && !binding.isAmbiguous();
    /* step 9 */
    ModuleRecord targetModule = binding.getModule();
    /* step 10 */
    assert targetModule != null;
    /* step 11 */
    LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
    /* step 12 */
    if (targetEnv == null) {
        throw newReferenceError(cx, Messages.Key.UninitializedBinding, binding.getBindingName());
    }
    /* step ? (Extension: Export From) */
    if (binding.isNameSpaceExport()) {
        try {
            return GetModuleNamespace(cx, targetModule);
        } catch (IOException e) {
            throw Errors.newInternalError(cx, Messages.Key.ModulesIOException, e.getMessage());
        } catch (MalformedNameException | ResolutionException e) {
            throw e.toScriptException(cx);
        }
    }
    /* step 13 */
    EnvironmentRecord targetEnvRec = targetEnv.getEnvRec();
    /* step 14 */
    return targetEnvRec.getBindingValue(binding.getBindingName(), true);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) HashMap(java.util.HashMap) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ToString(com.github.anba.es6draft.runtime.AbstractOperations.ToString) IOException(java.io.IOException) ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) HashSet(java.util.HashSet)

Example 3 with MalformedNameException

use of com.github.anba.es6draft.runtime.modules.MalformedNameException 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)

Aggregations

MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)3 FileSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)2 InvalidPathException (java.nio.file.InvalidPathException)2 Path (java.nio.file.Path)2 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 ToString (com.github.anba.es6draft.runtime.AbstractOperations.ToString)1 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)1 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)1 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)1 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1