use of com.github.anba.es6draft.compiler.CompilationException in project es6draft by anba.
the class GeneratorFunctionConstructor method CreateDynamicGenerator.
private static OrdinaryGenerator CreateDynamicGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
/* step 1 (not applicable) */
/* step 2 (not applicable) */
/* step 3 */
Intrinsics fallbackProto = Intrinsics.Generator;
/* steps 4-10 */
String[] sourceText = functionSourceText(cx, args);
String parameters = sourceText[0], bodyText = sourceText[1];
/* steps 11, 13-20 */
Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
RuntimeInfo.Function function;
try {
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
function = scriptLoader.generator(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 */
OrdinaryGenerator f = OrdinaryGenerator.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.GeneratorPrototype);
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.compiler.CompilationException in project es6draft by anba.
the class MozShellFunctions method evalcx.
/**
* shell-function: {@code evalcx(s, [o])}
*
* @param cx
* the execution context
* @param caller
* the caller context
* @param sourceCode
* the source to evaluate
* @param o
* the global object
* @return the eval result value
*/
@Function(name = "evalcx", arity = 1)
public Object evalcx(ExecutionContext cx, ExecutionContext caller, String sourceCode, Object o) {
ScriptObject global;
if (Type.isUndefinedOrNull(o)) {
global = newGlobal(cx);
} else {
global = ToObject(cx, o);
}
if (sourceCode.isEmpty() || "lazy".equals(sourceCode)) {
return global;
}
if (!(global instanceof GlobalObject)) {
throw Errors.newError(cx, "invalid global argument");
}
Source source = new Source(cx.getRealm().sourceInfo(caller), "evalcx", 1);
Realm realm = ((GlobalObject) global).getRealm();
try {
Script script = realm.getScriptLoader().script(source, sourceCode);
return script.evaluate(realm);
} catch (ParserException | CompilationException e) {
// Create a script exception from the requested code realm, not from the caller's realm.
throw e.toScriptException(realm.defaultContext());
}
}
use of com.github.anba.es6draft.compiler.CompilationException in project es6draft by anba.
the class ShellFunctions method compile.
/**
* shell-function: {@code compile(filename)}
*
* @param cx
* the execution context
* @param filename
* the file to load
* @return the status message
*/
@Function(name = "compile", arity = 1)
public String compile(ExecutionContext cx, String filename) {
try {
Path file = absolutePath(cx, Paths.get(filename));
cx.getRealm().getScriptLoader().script(new Source(file, filename, 1), file);
} catch (ParserException | CompilationException | IOException e) {
return "error: " + e.getMessage();
}
return "success";
}
use of com.github.anba.es6draft.compiler.CompilationException in project es6draft by anba.
the class RuntimeFunctions method Include.
/**
* Native function: {@code %Include(<file>)}.
* <p>
* Loads and evaluates the script file.
*
* @param cx
* the execution context
* @param file
* the file path
* @return the script evaluation result
*/
public static Object Include(ExecutionContext cx, CharSequence file) {
Realm realm = cx.getRealm();
Source base = realm.sourceInfo(cx);
if (base == null || base.getFile() == null) {
throw newInternalError(cx, Messages.Key.InternalError, "No source: " + Objects.toString(base));
}
Path path = Objects.requireNonNull(base.getFile().getParent()).resolve(file.toString());
Source source = new Source(path, Objects.requireNonNull(path.getFileName()).toString(), 1);
Script script;
try {
script = realm.getScriptLoader().script(source, path);
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
} catch (IOException e) {
throw newInternalError(cx, e, Messages.Key.InternalError, e.toString());
}
return script.evaluate(cx);
}
use of com.github.anba.es6draft.compiler.CompilationException 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;
}
Aggregations