use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.
the class FunctionCodeGenerator method generateLegacyFunctionConstruct.
/**
* Generate bytecode for:
*
* <pre>
* oldCaller = function.getLegacyCaller()
* oldArguments = function.getLegacyArguments()
* function.setLegacyCaller(callerContext.getCurrentFunction())
* try {
* thisArgument = OrdinaryCreateFromConstructor(callerContext, newTarget, %ObjectPrototype%)
* calleeContext = newFunctionExecutionContext(function, newTarget, thisArgument)
* result = OrdinaryCallEvaluateBody(function, argumentsList)
* return returnResultOrThis(result)
* } finally {
* function.restoreLegacyProperties(oldCaller, oldArguments)
* }
* </pre>
*
* @param node
* the function node
* @param mv
* the instruction visitor
*/
private void generateLegacyFunctionConstruct(FunctionNode node, InstructionVisitor mv) {
final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
Variable<LegacyConstructorFunction> function = mv.getParameter(FUNCTION, LegacyConstructorFunction.class);
Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<Constructor> newTarget = mv.getParameter(NEW_TARGET, Constructor.class);
Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
Variable<ScriptObject> thisArg = mv.newVariable("thisArgument", ScriptObject.class);
Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
Variable<FunctionObject> oldCaller = mv.newVariable("oldCaller", FunctionObject.class);
Variable<LegacyConstructorFunction.Arguments> oldArguments = mv.newVariable("oldArguments", LegacyConstructorFunction.Arguments.class);
Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
// (1) Retrieve 'caller' and 'arguments' and store in local variables
if (hasCaller) {
mv.load(function);
mv.invoke(Methods.LegacyConstructorFunction_getLegacyCaller);
} else {
mv.anull();
}
mv.store(oldCaller);
if (hasArguments) {
mv.load(function);
mv.invoke(Methods.LegacyConstructorFunction_getLegacyArguments);
} else {
mv.anull();
}
mv.store(oldArguments);
// (2) Update 'caller' and 'arguments' properties
if (hasCaller) {
setLegacyCaller(function, callerContext, mv);
}
if (hasArguments) {
setLegacyArguments(function, arguments, mv);
}
TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
TryCatchLabel handlerFinally = new TryCatchLabel();
mv.mark(startFinally);
{
// (3) Create this-argument
ordinaryCreateFromConstructor(callerContext, newTarget, thisArg, mv);
// (4) Create a new ExecutionContext
prepareCallAndBindThis(node, calleeContext, function, newTarget, thisArg, mv);
// (5) Call OrdinaryCallEvaluateBody
ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
// (6) Restore 'caller' and 'arguments'
restoreLegacyProperties(function, oldCaller, oldArguments, mv);
// (7) Return result value
returnResultOrThis(thisArg, false, mv);
}
mv.mark(endFinally);
// Exception: Restore 'caller' and 'arguments' and then rethrow exception
mv.finallyHandler(handlerFinally);
mv.store(throwable);
restoreLegacyProperties(function, oldCaller, oldArguments, mv);
mv.load(throwable);
mv.athrow();
mv.tryFinally(startFinally, endFinally, handlerFinally);
}
use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.
the class Bootstrap method callSetup.
@SuppressWarnings("unused")
private static MethodHandle callSetup(MutableCallSite callsite, Object function, ExecutionContext cx, Object thisValue, Object[] arguments) {
MethodHandle target, test;
if (function instanceof FunctionObject) {
FunctionObject fn = (FunctionObject) function;
test = MethodHandles.insertArguments(testFunctionObjectMH, 1, fn.getMethodInfo());
target = fn.getCallMethod();
} else if (function instanceof BuiltinFunction) {
BuiltinFunction fn = (BuiltinFunction) function;
test = MethodHandles.insertArguments(testBuiltinFunctionMH, 1, fn.getMethodInfo());
target = fn.getCallMethod();
} else {
target = test = null;
}
return setCallSiteTarget(callsite, target, test, callGenericMH);
}
use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.
the class FunctionConstructor method CreateDynamicFunction.
/**
* 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
*
* @param cx
* the execution context
* @param source
* the source object
* @param function
* the compiled function
* @param newTarget
* the newTarget constructor function
* @param fallbackProto
* the fallback prototype
* @return the new function object
*/
private static FunctionObject CreateDynamicFunction(ExecutionContext cx, Source source, RuntimeInfo.Function function, Constructor newTarget, Intrinsics fallbackProto) {
/* steps 1-11, 13-20 (not applicable) */
/* step 12 */
boolean strict = function.isStrict();
/* steps 21-22 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* step 23 */
FunctionObject f;
if (function.is(RuntimeInfo.FunctionFlags.Legacy)) {
assert !strict;
f = LegacyConstructorFunction.FunctionAllocate(cx, proto);
} else {
f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal, ConstructorKind.Base);
}
/* steps 24-25 */
LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
/* step 26 */
FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
// Work around for: https://bugs.eclipse.org/bugs/show_bug.cgi?id=479802
if (f instanceof LegacyConstructorFunction) {
MakeConstructor(cx, (LegacyConstructorFunction) f);
} else {
MakeConstructor(cx, (OrdinaryConstructorFunction) f);
}
/* step 29 */
SetFunctionName(f, "anonymous");
/* step 30 */
return f;
}
use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.
the class FunctionDeclarationInstantiationGenerator method generate.
private void generate(FunctionNode function, CodeVisitor mv) {
Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<LexicalEnvironment<FunctionEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
Variable<FunctionEnvironmentRecord> envRec = mv.newVariable("envRec", FunctionEnvironmentRecord.class);
Variable<FunctionObject> fo = null;
Variable<Undefined> undefined = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undefined);
FunctionScope fscope = function.getScope();
boolean hasParameters = !function.getParameters().getFormals().isEmpty();
Variable<Iterator<?>> iterator = null;
if (hasParameters) {
iterator = mv.newVariable("iterator", Iterator.class).uncheckedCast();
mv.loadParameter(ARGUMENTS, Object[].class);
mv.invoke(Methods.Arrays_asList);
mv.invoke(Methods.List_iterator);
mv.store(iterator);
}
/* step 1 (omitted) */
/* step 2 */
getLexicalEnvironment(context, env, mv);
/* step 3 */
getEnvironmentRecord(env, envRec, mv);
/* step 4 */
// RuntimeInfo.Function code = func.getCode();
/* step 5 */
boolean strict = IsStrict(function);
/* step 6 */
FormalParameterList formals = function.getParameters();
/* step 7 */
List<Name> parameterNames = BoundNames(formals);
HashSet<Name> parameterNamesSet = new HashSet<>(parameterNames);
/* step 8 */
boolean hasDuplicates = parameterNames.size() != parameterNamesSet.size();
/* step 9 */
boolean simpleParameterList = IsSimpleParameterList(formals);
/* step 10 */
boolean hasParameterExpressions = ContainsExpression(formals);
// invariant: hasDuplicates => simpleParameterList
assert !hasDuplicates || simpleParameterList;
// invariant: hasParameterExpressions => !simpleParameterList
assert !hasParameterExpressions || !simpleParameterList;
/* step 11 */
Set<Name> varNames = VarDeclaredNames(function);
/* step 12 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(function);
/* step 13 */
Set<Name> lexicalNames = LexicallyDeclaredNames(function);
/* step 14 */
HashSet<Name> functionNames = new HashSet<>();
/* step 15 */
ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
/* step 16 */
for (StatementListItem item : reverse(varDeclarations)) {
if (item instanceof HoistableDeclaration) {
HoistableDeclaration d = (HoistableDeclaration) item;
Name fn = BoundName(d);
if (functionNames.add(fn)) {
functionsToInitialize.addFirst(d);
}
}
}
if (!functionsToInitialize.isEmpty()) {
fo = mv.newVariable("fo", FunctionObject.class);
}
/* step 17 */
// Optimization: Skip 'arguments' allocation if it's not referenced within the function.
boolean argumentsObjectNeeded = function.getScope().needsArguments();
Name arguments = function.getScope().arguments();
argumentsObjectNeeded &= arguments != null;
/* step 18 */
if (function.getThisMode() == FunctionNode.ThisMode.Lexical) {
argumentsObjectNeeded = false;
} else /* step 19 */
if (parameterNamesSet.contains(arguments)) {
argumentsObjectNeeded = false;
} else /* step 20 */
if (!hasParameterExpressions) {
if (functionNames.contains(arguments) || lexicalNames.contains(arguments)) {
argumentsObjectNeeded = false;
}
}
/* step 21 */
for (Name paramName : function.getScope().parameterNames()) {
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, paramName);
op.createMutableBinding(envRec, paramName, false, mv);
if (hasDuplicates) {
op.initializeBinding(envRec, paramName, undefined, mv);
}
}
/* step 22 */
if (argumentsObjectNeeded) {
assert arguments != null;
Variable<ArgumentsObject> argumentsObj = mv.newVariable("argumentsObj", ArgumentsObject.class);
if (strict || !simpleParameterList) {
CreateUnmappedArgumentsObject(mv);
} else if (formals.getFormals().isEmpty()) {
CreateMappedArgumentsObject(mv);
} else {
CreateMappedArgumentsObject(env, formals, mv);
}
mv.store(argumentsObj);
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, arguments);
if (strict) {
op.createImmutableBinding(envRec, arguments, false, mv);
} else {
op.createMutableBinding(envRec, arguments, false, mv);
}
op.initializeBinding(envRec, arguments, argumentsObj, mv);
parameterNames.add(arguments);
parameterNamesSet.add(arguments);
}
/* steps 24-26 */
if (hasParameters) {
if (hasDuplicates) {
/* step 24 */
BindingInitialization(codegen, function, env, iterator, mv);
} else {
/* step 25 */
BindingInitialization(codegen, function, env, envRec, iterator, mv);
}
}
/* steps 27-28 */
HashSet<Name> instantiatedVarNames;
Variable<? extends LexicalEnvironment<?>> varEnv;
Variable<? extends DeclarativeEnvironmentRecord> varEnvRec;
if (!hasParameterExpressions) {
assert fscope == fscope.variableScope();
/* step 27.a (note) */
/* step 27.b */
instantiatedVarNames = new HashSet<>(parameterNames);
/* step 27.c */
for (Name varName : varNames) {
if (instantiatedVarNames.add(varName)) {
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, varName);
op.createMutableBinding(envRec, varName, false, mv);
op.initializeBinding(envRec, varName, undefined, mv);
}
}
/* steps 27.d-27.e */
varEnv = env;
varEnvRec = envRec;
} else {
assert fscope != fscope.variableScope();
mv.enterScope(fscope.variableScope());
/* step 28.a (note) */
/* step 28.b */
varEnv = mv.newVariable("varEnv", LexicalEnvironment.class).uncheckedCast();
newDeclarativeEnvironment(env, mv);
mv.store(varEnv);
/* step 28.c */
varEnvRec = mv.newVariable("varEnvRec", DeclarativeEnvironmentRecord.class);
getEnvironmentRecord(varEnv, varEnvRec, mv);
/* step 28.d */
setVariableEnvironment(varEnv, mv);
/* step 28.e */
instantiatedVarNames = new HashSet<>();
/* step 28.f */
Variable<Object> tempValue = null;
for (Name varName : varNames) {
if (instantiatedVarNames.add(varName)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, varName);
op.createMutableBinding(varEnvRec, varName, false, mv);
if (!parameterNamesSet.contains(varName) || functionNames.contains(varName)) {
op.initializeBinding(varEnvRec, varName, undefined, mv);
} else {
BindingOp.of(envRec, varName).getBindingValue(envRec, varName, strict, mv);
if (tempValue == null) {
tempValue = mv.newVariable("tempValue", Object.class);
}
mv.store(tempValue);
op.initializeBinding(varEnvRec, varName, tempValue, mv);
}
}
}
}
/* step 29 (B.3.3 Block-Level Function Declarations Web Legacy Compatibility Semantics) */
for (Name fname : function.getScope().blockFunctionNames()) {
if (instantiatedVarNames.add(fname)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, fname);
op.createMutableBinding(varEnvRec, fname, false, mv);
op.initializeBinding(varEnvRec, fname, undefined, mv);
}
}
/* steps 30-32 */
Variable<? extends LexicalEnvironment<?>> lexEnv;
Variable<? extends DeclarativeEnvironmentRecord> lexEnvRec;
assert strict || fscope.variableScope() != fscope.lexicalScope();
if (!strict || fscope.variableScope() != fscope.lexicalScope()) {
// NB: Scopes are unmodifiable once constructed, that means we need to emit the extra
// scope for functions with deferred strict-ness, even if this scope is not present in
// the specification.
mv.enterScope(fscope.lexicalScope());
if (!lexicalNames.isEmpty()) {
/* step 30 */
lexEnv = mv.newVariable("lexEnv", LexicalEnvironment.class).uncheckedCast();
newDeclarativeEnvironment(varEnv, mv);
mv.store(lexEnv);
/* step 32 */
lexEnvRec = mv.newVariable("lexEnvRec", DeclarativeEnvironmentRecord.class);
getEnvironmentRecord(lexEnv, lexEnvRec, mv);
} else {
// Optimization: Skip environment allocation if no lexical names are defined.
/* step 30 */
lexEnv = varEnv;
/* step 32 */
lexEnvRec = varEnvRec;
}
} else {
/* step 30 */
lexEnv = varEnv;
/* step 32 */
lexEnvRec = varEnvRec;
}
/* step 33 */
if (lexEnv != env) {
setLexicalEnvironment(lexEnv, mv);
}
/* step 34 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(function);
/* step 35 */
for (Declaration d : lexDeclarations) {
assert !(d instanceof HoistableDeclaration);
for (Name dn : BoundNames(d)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(lexEnvRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(lexEnvRec, dn, true, mv);
} else {
op.createMutableBinding(lexEnvRec, dn, false, mv);
}
}
}
/* step 36 */
for (HoistableDeclaration f : functionsToInitialize) {
Name fn = BoundName(f);
// stack: [] -> [fo]
InstantiateFunctionObject(context, lexEnv, f, mv);
mv.store(fo);
// stack: [fo] -> []
// Resolve the actual binding name: function(a){ function a(){} }
// TODO: Can be removed when StaticIdResolution handles this case.
Name name = fscope.variableScope().resolveName(fn, false);
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, name);
op.setMutableBinding(varEnvRec, name, fo, false, mv);
}
/* step 37 */
mv._return();
}
use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.
the class GlobalDeclarationInstantiationGenerator method generate.
private void generate(Script script, InstructionVisitor mv) {
Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<LexicalEnvironment<GlobalEnvironmentRecord>> env = mv.newVariable("globalEnv", LexicalEnvironment.class).uncheckedCast();
Variable<GlobalEnvironmentRecord> envRec = mv.newVariable("envRec", GlobalEnvironmentRecord.class);
Variable<FunctionObject> fo = null;
/* steps 1-2 */
getLexicalEnvironment(context, env, mv);
getEnvironmentRecord(env, envRec, mv);
/* step 3 */
HashSet<Name> lexNames = new HashSet<>();
/* step 4 */
HashSet<Name> varNames = new HashSet<>();
// Iterate over declarations to be able to emit line-info entries.
for (Declaration d : LexicallyScopedDeclarations(script)) {
assert !(d instanceof HoistableDeclaration);
for (Name name : BoundNames(d)) {
if (lexNames.add(name)) {
canDeclareLexicalScopedOrThrow(context, envRec, d, name, mv);
}
}
}
// Iterate over declarations to be able to emit line-info entries.
for (StatementListItem item : VarScopedDeclarations(script)) {
if (item instanceof VariableStatement) {
for (VariableDeclaration vd : ((VariableStatement) item).getElements()) {
for (Name name : BoundNames(vd)) {
if (varNames.add(name)) {
canDeclareVarScopedOrThrow(context, envRec, vd, name, mv);
}
}
}
} else {
HoistableDeclaration d = (HoistableDeclaration) item;
Name name = BoundName(d);
if (varNames.add(name)) {
canDeclareVarScopedOrThrow(context, envRec, d, name, mv);
}
}
}
/* step 7 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(script);
/* step 8 */
ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
/* step 9 */
HashSet<Name> declaredFunctionNames = new HashSet<>();
/* step 10 */
for (StatementListItem item : reverse(varDeclarations)) {
if (item instanceof HoistableDeclaration) {
HoistableDeclaration d = (HoistableDeclaration) item;
Name fn = BoundName(d);
if (declaredFunctionNames.add(fn)) {
canDeclareGlobalFunctionOrThrow(context, envRec, d, fn, mv);
functionsToInitialize.addFirst(d);
}
}
}
if (!functionsToInitialize.isEmpty()) {
fo = mv.newVariable("fo", FunctionObject.class);
}
/* step 11 */
LinkedHashMap<Name, VariableDeclaration> declaredVarNames = new LinkedHashMap<>();
/* step 12 */
for (StatementListItem d : varDeclarations) {
if (d instanceof VariableStatement) {
for (VariableDeclaration vd : ((VariableStatement) d).getElements()) {
for (Name vn : BoundNames(vd)) {
if (!declaredFunctionNames.contains(vn)) {
canDeclareGlobalVarOrThrow(context, envRec, vd, vn, mv);
declaredVarNames.put(vn, vd);
}
}
}
}
}
// ES2016: Block-scoped global function declarations
if (hasBlockFunctions(script)) {
int idCounter = 0;
HashSet<Name> declaredFunctionOrVarNames = new HashSet<>();
declaredFunctionOrVarNames.addAll(declaredFunctionNames);
declaredFunctionOrVarNames.addAll(declaredVarNames.keySet());
for (FunctionDeclaration f : script.getScope().blockFunctions()) {
Name fn = BoundName(f);
Jump next = new Jump();
// Runtime check always required for global block-level function declarations.
f.setLegacyBlockScopeId(++idCounter);
// FIXME: spec issue - avoid (observable!) duplicate checks for same name?
// FIXME: spec issue - property creation order important?
canDeclareGlobalFunction(envRec, f, fn, next, mv);
setLegacyBlockFunction(context, f, mv);
if (declaredFunctionOrVarNames.add(fn)) {
createGlobalFunctionBinding(envRec, f, fn, false, mv);
}
mv.mark(next);
}
}
/* step 14 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(script);
/* step 15 */
for (Declaration d : lexDeclarations) {
assert !(d instanceof HoistableDeclaration);
mv.lineInfo(d);
for (Name dn : BoundNames(d)) {
BindingOp<GlobalEnvironmentRecord> op = BindingOp.of(envRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(envRec, dn, true, mv);
} else {
op.createMutableBinding(envRec, dn, false, mv);
}
}
}
/* step 16 */
for (HoistableDeclaration f : functionsToInitialize) {
Name fn = BoundName(f);
InstantiateFunctionObject(context, env, f, mv);
mv.store(fo);
createGlobalFunctionBinding(envRec, f, fn, fo, false, mv);
}
/* step 17 */
for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
createGlobalVarBinding(envRec, e.getValue(), e.getKey(), false, mv);
}
/* step 18 */
mv._return();
}
Aggregations