use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.
the class TestGlobals method compileScripts.
private List<Script> compileScripts() throws IOException {
List<?> scriptNames = configuration.getList("scripts", emptyList());
if (scriptNames.isEmpty()) {
return Collections.emptyList();
}
Path basedir = getBaseDirectory();
RuntimeContext context = createContext();
ScriptLoader scriptLoader = new ScriptLoader(context);
ArrayList<Script> scripts = new ArrayList<>();
for (String scriptName : nonEmpty(scriptNames)) {
Source source = new Source(Resources.resourcePath(scriptName, basedir), scriptName, 1);
Script script = scriptLoader.script(source, Resources.resource(scriptName, basedir));
scripts.add(script);
}
return scripts;
}
use of com.github.anba.es6draft.runtime.internal.ScriptLoader 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)
@AliasFunction(name = "dis")
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 {
debugInfo = currentExec.getRuntimeObject().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 moduleSource = new StringModuleSource(identifier, "disassemble", sourceCode);
SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, moduleSource);
debugInfo = module.getScriptCode().getRuntimeObject().debugInfo();
} else {
Source source = new Source("<disassemble>", 1);
Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
debugInfo = script.getRuntimeObject().debugInfo();
}
}
if (debugInfo != null) {
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
for (DebugInfo.Method method : debugInfo.getMethods()) {
writer.println(method.disassemble());
}
}
}
use of com.github.anba.es6draft.runtime.internal.ScriptLoader 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());
}
use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.
the class Test262Parser method runTest.
@Test
public void runTest() throws Throwable {
RuntimeContext context = realms.newContext(new SystemConsole(), test);
ScriptLoader loader = new ScriptLoader(context);
Source source = new Source(test.toFile(), test.getScript().toString(), 1);
String sourceCode = new String(Files.readAllBytes(test.toFile()), StandardCharsets.UTF_8);
if (test.isModule()) {
loader.parseModule(source, sourceCode);
} else {
loader.parseScript(source, sourceCode);
}
}
use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.
the class FunctionConstructor method CreateDynamicFunction.
/**
* 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
*
* @param callerContext
* the caller execution context
* @param cx
* the execution context
* @param newTarget
* the newTarget constructor function
* @param kind
* the function kind
* @param args
* the function arguments
* @return the new function object
*/
public static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, SourceKind kind, Object... args) {
/* steps 1-6 (not applicable) */
/* steps 7-9 */
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
FunctionCompiler compiler;
Intrinsics fallbackProto;
switch(kind) {
case AsyncFunction:
compiler = scriptLoader::asyncFunction;
fallbackProto = Intrinsics.AsyncFunctionPrototype;
break;
case AsyncGenerator:
compiler = scriptLoader::asyncGenerator;
fallbackProto = Intrinsics.AsyncGenerator;
break;
case Function:
compiler = scriptLoader::function;
fallbackProto = Intrinsics.FunctionPrototype;
break;
case Generator:
compiler = scriptLoader::generator;
fallbackProto = Intrinsics.Generator;
break;
default:
throw new AssertionError();
}
/* steps 10-15 */
int argCount = args.length;
String parameters, bodyText;
if (argCount == 0) {
parameters = "";
bodyText = "";
} else if (argCount == 1) {
parameters = "";
bodyText = ToFlatString(cx, args[0]);
} else {
StrBuilder sb = new StrBuilder(cx);
Object firstArg = args[0];
sb.append(ToFlatString(cx, firstArg));
int k = 2;
for (; k < argCount; ++k) {
Object nextArg = args[k - 1];
String nextArgString = ToFlatString(cx, nextArg);
sb.append(',').append(nextArgString);
}
parameters = sb.toString();
bodyText = ToFlatString(cx, args[k - 1]);
}
/* steps 16-17, 19-28 */
Source source = functionSource(kind, callerContext);
CompiledFunction compiledFunction;
try {
compiledFunction = compiler.compile(source, parameters, bodyText);
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 29 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* steps 18, 30-38 */
return CreateDynamicFunction(cx, kind, compiledFunction, proto);
}
Aggregations