use of com.github.anba.es6draft.compiler.CompilationException 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