use of com.github.anba.es6draft.runtime.modules.ModuleRecord in project es6draft by anba.
the class ShellGlobalObject method eval.
/**
* Parses, compiles and executes the javascript module file.
*
* @param moduleName
* the unnormalized module name
* @throws IOException
* if there was any I/O error
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
* @throws ParserException
* if the module source contains any syntax errors
* @throws CompilationException
* if the parsed module source cannot be compiled
*/
public void eval(String moduleName) throws IOException, MalformedNameException, ResolutionException, ParserException, CompilationException {
Realm realm = getRealm();
ModuleLoader moduleLoader = realm.getModuleLoader();
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
ModuleRecord module = moduleLoader.resolve(moduleId, realm);
module.instantiate();
module.evaluate();
}
use of com.github.anba.es6draft.runtime.modules.ModuleRecord in project es6draft by anba.
the class ConsoleObject method createConsole.
/**
* Creates a new {@code console} object.
*
* @param realm
* the realm instance
* @return the console object
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
public static ScriptObject createConsole(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
ExecutionContext cx = realm.defaultContext();
ModuleRecord module = NativeCode.loadModule(realm, "console.jsm");
ScriptObject console = NativeCode.getModuleExport(module, "default", ScriptObject.class);
Callable inspectFn = Properties.createFunction(cx, new InspectFunction(), InspectFunction.class);
CreateMethodProperty(cx, console, "_inspect", inspectFn);
return console;
}
use of com.github.anba.es6draft.runtime.modules.ModuleRecord in project es6draft by anba.
the class ShellFunctions method loadModule.
/**
* shell-function: {@code loadModule(moduleName, [realmObject])}
*
* @param cx
* the execution context
* @param moduleName
* the module name
* @param realmObject
* the optional realm object
* @return the module namespace object
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
@Function(name = "loadModule", arity = 1)
public ScriptObject loadModule(ExecutionContext cx, String moduleName, Object realmObject) throws MalformedNameException, ResolutionException {
Realm realm;
if (!Type.isUndefined(realmObject)) {
if (!(realmObject instanceof RealmObject)) {
throw Errors.newTypeError(cx, Messages.Key.IncompatibleObject);
}
realm = ((RealmObject) realmObject).getRealm();
} else {
realm = cx.getRealm();
}
try {
ModuleLoader moduleLoader = realm.getModuleLoader();
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
ModuleRecord module = moduleLoader.resolve(moduleId, realm);
module.instantiate();
module.evaluate();
return GetModuleNamespace(cx, module);
} catch (IOException e) {
throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
}
}
use of com.github.anba.es6draft.runtime.modules.ModuleRecord 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);
}
use of com.github.anba.es6draft.runtime.modules.ModuleRecord in project es6draft by anba.
the class TestGlobals method newGlobal.
public final GLOBAL newGlobal(Console console, TEST test) throws MalformedNameException, ResolutionException, IOException, URISyntaxException {
RuntimeContext context = createContext(console, test);
World world = new World(context);
Realm realm = world.newInitializedRealm();
// Evaluate additional initialization scripts and modules
TestModuleLoader<?> moduleLoader = (TestModuleLoader<?>) world.getModuleLoader();
for (ModuleRecord module : modules.allModules) {
moduleLoader.defineFromTemplate(module, realm);
}
for (ModuleRecord module : modules.mainModules) {
ModuleRecord testModule = moduleLoader.get(module.getSourceCodeId(), realm);
testModule.instantiate();
testModule.evaluate();
}
for (Script script : scripts) {
script.evaluate(realm);
}
@SuppressWarnings("unchecked") GLOBAL global = (GLOBAL) realm.getGlobalObject();
return global;
}
Aggregations