use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.
the class NodeModuleResolution method readPackage.
private static Path readPackage(Path path) {
Path jsonPackage = path.resolve(PACKAGE_FILE_NAME);
if (!Files.isRegularFile(jsonPackage)) {
return null;
}
String executable;
try {
String json = new String(Files.readAllBytes(jsonPackage), StandardCharsets.UTF_8);
executable = JSONParser.parse(json, new ExecJSONBuilder());
} catch (IOException | ParserException e) {
// ignore?
return null;
}
if (executable == null || executable.isEmpty()) {
return null;
}
return Paths.get(executable);
}
use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.
the class AsyncGeneratorFunctionConstructor 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 args
* the function arguments
* @return the new async generator function object
*/
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
/* step 1 (not applicable) */
/* step 2 (not applicable) */
/* step 3 */
Intrinsics fallbackProto = Intrinsics.AsyncGenerator;
/* steps 4-10 */
String[] sourceText = functionSourceText(cx, args);
String parameters = sourceText[0], bodyText = sourceText[1];
/* steps 11, 13-20 */
Source source = functionSource(SourceKind.AsyncGenerator, cx.getRealm(), callerContext);
RuntimeInfo.Function function;
try {
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
function = scriptLoader.asyncGenerator(source, parameters, bodyText).getFunction();
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 12 */
boolean strict = function.isStrict();
/* steps 21-22 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* step 23 */
OrdinaryAsyncGenerator f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
/* steps 24-25 */
LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
/* step 26 */
FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
/* step 27 */
OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.AsyncGeneratorPrototype);
f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
/* step 28 (not applicable) */
/* step 29 */
SetFunctionName(f, "anonymous");
/* step 30 */
return f;
}
use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.
the class Eval method script.
private static Script script(ExecutionContext cx, ExecutionContext caller, String sourceCode, int flags) {
try {
Realm realm = cx.getRealm();
Source source = evalSource(realm, caller);
EnumSet<Parser.Option> options = EvalFlags.toOptions(flags);
return realm.getScriptLoader().evalScript(source, sourceCode, options);
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
}
use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.
the class Repl method newRealm.
private Realm newRealm() {
ObjectAllocator<? extends ShellGlobalObject> allocator;
if (options.shellMode == ShellMode.Mozilla) {
allocator = MozShellGlobalObject::new;
} else if (options.shellMode == ShellMode.V8) {
allocator = V8ShellGlobalObject::new;
} else {
allocator = SimpleShellGlobalObject::new;
}
BiFunction<RuntimeContext, ScriptLoader, ModuleLoader> moduleLoader;
switch(options.moduleLoaderMode) {
case Default:
moduleLoader = FileModuleLoader::new;
break;
case Node:
moduleLoader = NodeModuleLoader::new;
break;
case NodeStandard:
moduleLoader = NodeStandardModuleLoader::new;
break;
default:
throw new AssertionError();
}
/* @formatter:off */
RuntimeContext context = new RuntimeContext.Builder().setBaseDirectory(Paths.get("").toAbsolutePath()).setGlobalAllocator(allocator).setModuleLoader(moduleLoader).setConsole(console).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
/* @formatter:on */
World world = new World(context);
Realm realm = world.newRealm();
ExecutionContext cx = realm.defaultContext();
// Add completion to console
console.addCompleter(new ShellCompleter(realm));
// Execute global specific initialization
enqueueScriptTask(realm, () -> {
InitializeHostDefinedRealm(realm);
// Add global "arguments" property
ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
});
if (options.console) {
enqueueScriptTask(realm, () -> {
ScriptObject console = ConsoleObject.createConsole(realm);
CreateMethodProperty(cx, realm.getGlobalObject(), "console", console);
});
}
if (options.moduleLoaderMode == ModuleLoaderMode.Node) {
// TODO: Add default initialize(Realm) method to ModuleLoader interface?
enqueueScriptTask(realm, () -> {
NodeModuleLoader nodeLoader = (NodeModuleLoader) world.getModuleLoader();
nodeLoader.initialize(realm);
});
}
// Run eval expressions and files
for (EvalScript evalScript : options.evalScripts) {
if (options.module) {
enqueueScriptTask(realm, () -> {
ModuleSource moduleSource = evalScript.getModuleSource();
SourceIdentifier moduleName = evalScript.getModuleName();
try {
ModuleEvaluationJob(realm, moduleName, moduleSource);
} catch (ParserException e) {
Source source = moduleSource.toSource();
String file = e.getFile();
if (file.equals(source.getFileString())) {
throw new ParserExceptionWithSource(e, source, moduleSource.sourceCode());
}
Path filePath = Paths.get(file).toAbsolutePath();
Source errorSource = new Source(filePath, file, 1);
String code = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
throw new ParserExceptionWithSource(e, errorSource, code);
}
});
} else {
enqueueScriptTask(realm, () -> {
Source source = evalScript.getSource();
String sourceCode = evalScript.getSourceCode();
try {
eval(realm, parse(realm, source, sourceCode));
} catch (ParserException e) {
throw new ParserExceptionWithSource(e, source, sourceCode);
}
});
}
}
return realm;
}
use of com.github.anba.es6draft.parser.ParserException in project es6draft by anba.
the class Repl method handleException.
private void handleException(ParserExceptionWithSource exception) {
ParserException e = exception.getCause();
String sourceCode = exception.getSourceCode();
int lineOffset = exception.getSource().getLine();
String sourceInfo = String.format("%s:%d:%d", e.getFile(), e.getLine(), e.getColumn());
int start = skipLines(sourceCode, e.getLine() - lineOffset);
int end = nextLineTerminator(sourceCode, start);
String offendingLine = sourceCode.substring(start, end);
String marker = Strings.repeat('.', Math.max(e.getColumn() - 1, 0)) + '^';
console.printf("%s %s: %s%n", sourceInfo, e.getType(), e.getFormattedMessage());
console.printf("%s %s%n", sourceInfo, offendingLine);
console.printf("%s %s%n", sourceInfo, marker);
printStackTrace(e);
}
Aggregations