use of com.github.anba.es6draft.runtime.modules.loader.StringModuleSource in project es6draft by anba.
the class Test262GlobalObject method evalModule.
/**
* Parses, compiles and executes the javascript module file.
*
* @param moduleName
* the module name
* @param sourceCode
* the source code
* @param sourceLine
* the source line offset
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
* @throws MalformedNameException
* if the module name cannot be normalized
* @throws ResolutionException
* if the module exports cannot be resolved
* @throws IOException
* if there was any I/O error
*/
void evalModule(String moduleName, String sourceCode, int sourceLine) throws ParserException, CompilationException, MalformedNameException, ResolutionException, IOException {
ModuleLoader moduleLoader = getRealm().getModuleLoader();
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
ModuleSource source = new StringModuleSource(moduleId, sourceCode, sourceLine);
ModuleRecord module = moduleLoader.define(moduleId, source, getRealm());
module.instantiate();
module.evaluate();
}
use of com.github.anba.es6draft.runtime.modules.loader.StringModuleSource 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)
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 if (currentExec != null && currentExec.getSourceObject() != null) {
debugInfo = currentExec.getSourceObject().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 src = new StringModuleSource(identifier, sourceCode);
SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
debugInfo = module.getScriptCode().getSourceObject().debugInfo();
} else {
Source source = new Source("<disassemble>", 1);
Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
debugInfo = script.getSourceObject().debugInfo();
}
}
if (debugInfo != null) {
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
for (DebugInfo.Method method : debugInfo.getMethods()) {
writer.println(method.disassemble());
}
}
}
Aggregations