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;
}
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);
}
}
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);
}
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());
}
Aggregations