Skip to content

Commit

Permalink
Rename FunctionInstance to Function (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jan 5, 2024
1 parent 69b33ac commit 982a751
Show file tree
Hide file tree
Showing 38 changed files with 86 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Jint.Tests.PublicInterface/PublicInterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SetTimeoutEmulator(Engine engine)
{
lock (_engine)
{
if (queueEntry is FunctionInstance fi)
if (queueEntry is Function fi)
{
_engine.Invoke(fi);
}
Expand Down
4 changes: 2 additions & 2 deletions Jint.Tests/Runtime/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ public void CanInvokeCallForFunctionInstance()
function foo(a = 123) { return a; }
foo()
})")
.As<FunctionInstance>().Call();
.As<Function>().Call();

var result = _engine.Evaluate(@"
(function () {
class Foo { test() { return 123 } }
let f = new Foo()
return f.test()
})")
.As<FunctionInstance>().Call();
.As<Function>().Call();

Assert.True(result.IsInteger());
Assert.Equal(123, result.AsInteger());
Expand Down
32 changes: 16 additions & 16 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ internal ManualPromise RegisterPromise()
var (resolve, reject) = promise.CreateResolvingFunctions();


Action<JsValue> SettleWith(FunctionInstance settle) => value =>
Action<JsValue> SettleWith(Function settle) => value =>
{
settle.Call(JsValue.Undefined, new[] { value });
RunAvailableContinuations();
Expand Down Expand Up @@ -759,7 +759,7 @@ JsValue DoInvoke()
// ensure logic is in sync between Call, Construct, engine.Invoke and JintCallExpression!
JsValue result;
var thisObject = JsValue.FromObject(this, thisObj);
if (callable is FunctionInstance functionInstance)
if (callable is Function functionInstance)
{
var callStack = CallStack;
callStack.Push(functionInstance, expression: null, ExecutionContext);
Expand Down Expand Up @@ -1023,11 +1023,11 @@ private void GlobalDeclarationInstantiation(
/// https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
/// </summary>
internal JsArguments? FunctionDeclarationInstantiation(
FunctionInstance functionInstance,
Function function,
JsValue[] argumentsList)
{
var calleeContext = ExecutionContext;
var func = functionInstance._functionDefinition;
var func = function._functionDefinition;

var env = (FunctionEnvironment) ExecutionContext.LexicalEnvironment;
var strict = _isStrict || StrictModeScope.IsStrictModeCode;
Expand All @@ -1053,7 +1053,7 @@ private void GlobalDeclarationInstantiation(
{
// NOTE: mapped argument object is only provided for non-strict functions that don't have a rest parameter,
// any parameter default value initializers, or any destructured parameters.
ao = CreateMappedArgumentsObject(functionInstance, parameterNames, argumentsList, env, configuration.HasRestParameter);
ao = CreateMappedArgumentsObject(function, parameterNames, argumentsList, env, configuration.HasRestParameter);
}

if (strict)
Expand Down Expand Up @@ -1164,7 +1164,7 @@ private void GlobalDeclarationInstantiation(
}

private JsArguments CreateMappedArgumentsObject(
FunctionInstance func,
Function func,
Key[] formals,
JsValue[] argumentsList,
DeclarativeEnvironment envRec,
Expand Down Expand Up @@ -1435,7 +1435,7 @@ JsValue Callback()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal JsValue Call(ICallable callable, JsValue thisObject, JsValue[] arguments, JintExpression? expression)
{
if (callable is FunctionInstance functionInstance)
if (callable is Function functionInstance)
{
return Call(functionInstance, thisObject, arguments, expression);
}
Expand Down Expand Up @@ -1482,26 +1482,26 @@ internal ObjectInstance Construct(
JsValue newTarget,
JintExpression? expression)
{
if (constructor is FunctionInstance functionInstance)
if (constructor is Function functionInstance)
{
return Construct(functionInstance, arguments, newTarget, expression);
}

return ((IConstructor) constructor).Construct(arguments, newTarget);
}

internal JsValue Call(FunctionInstance functionInstance, JsValue thisObject)
=> Call(functionInstance, thisObject, Arguments.Empty, null);
internal JsValue Call(Function function, JsValue thisObject)
=> Call(function, thisObject, Arguments.Empty, null);

internal JsValue Call(
FunctionInstance functionInstance,
Function function,
JsValue thisObject,
JsValue[] arguments,
JintExpression? expression)
{
// ensure logic is in sync between Call, Construct, engine.Invoke and JintCallExpression!

var recursionDepth = CallStack.Push(functionInstance, expression, ExecutionContext);
var recursionDepth = CallStack.Push(function, expression, ExecutionContext);

if (recursionDepth > Options.Constraints.MaxRecursionDepth)
{
Expand All @@ -1512,7 +1512,7 @@ internal JsValue Call(
JsValue result;
try
{
result = functionInstance.Call(thisObject, arguments);
result = function.Call(thisObject, arguments);
}
finally
{
Expand All @@ -1527,14 +1527,14 @@ internal JsValue Call(
}

private ObjectInstance Construct(
FunctionInstance functionInstance,
Function function,
JsValue[] arguments,
JsValue newTarget,
JintExpression? expression)
{
// ensure logic is in sync between Call, Construct, engine.Invoke and JintCallExpression!

var recursionDepth = CallStack.Push(functionInstance, expression, ExecutionContext);
var recursionDepth = CallStack.Push(function, expression, ExecutionContext);

if (recursionDepth > Options.Constraints.MaxRecursionDepth)
{
Expand All @@ -1545,7 +1545,7 @@ private ObjectInstance Construct(
ObjectInstance result;
try
{
result = ((IConstructor) functionInstance).Construct(arguments, newTarget);
result = ((IConstructor) function).Construct(arguments, newTarget);
}
finally
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/JsValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ public static double[] AsFloat64Array(this JsValue value)

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FunctionInstance AsFunctionInstance(this JsValue value)
public static Function AsFunctionInstance(this JsValue value)
{
if (value is not FunctionInstance instance)
if (value is not Function instance)
{
ThrowWrongTypeException(value, "FunctionInstance");
return null!;
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Argument/JsArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class JsArguments : ObjectInstance
// cache property container for array iteration for less allocations
private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new(() => new HashSet<string>(StringComparer.Ordinal));

private FunctionInstance _func = null!;
private Function.Function _func = null!;
private Key[] _names = null!;
private JsValue[] _args = null!;
private DeclarativeEnvironment _env = null!;
Expand All @@ -32,7 +32,7 @@ internal JsArguments(Engine engine)
}

internal void Prepare(
FunctionInstance func,
Function.Function func,
Key[] names,
JsValue[] args,
DeclarativeEnvironment env,
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Constructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Jint.Native;

public abstract class Constructor : FunctionInstance, IConstructor
public abstract class Constructor : Function.Function, IConstructor
{
protected Constructor(Engine engine, string name) : this(engine, engine.Realm, new JsString(name))
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Function/BindFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BindFunction(Engine engine,

JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)
{
var f = BoundTargetFunction as FunctionInstance;
var f = BoundTargetFunction as Function;
if (f is null)
{
ExceptionHelper.ThrowTypeError(_realm);
Expand Down Expand Up @@ -78,7 +78,7 @@ ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)

internal override bool OrdinaryHasInstance(JsValue v)
{
var f = BoundTargetFunction as FunctionInstance;
var f = BoundTargetFunction as Function;
if (f is null)
{
ExceptionHelper.ThrowTypeError(_realm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace Jint.Native.Function;

internal sealed class EvalFunctionInstance : FunctionInstance
internal sealed class EvalFunction : Function
{
private static readonly JsString _functionName = new("eval");

private static readonly ParserOptions _parserOptions = ParserOptions.Default with { Tolerant = true };
private readonly JavaScriptParser _parser = new(_parserOptions);

public EvalFunctionInstance(
public EvalFunction(
Engine engine,
Realm realm,
FunctionPrototype functionPrototype)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Jint.Native.Function
{
[DebuggerDisplay("{ToString(),nq}")]
public abstract partial class FunctionInstance : ObjectInstance, ICallable
#pragma warning disable MA0049
public abstract partial class Function : ObjectInstance, ICallable
#pragma warning restore MA0049
{
protected PropertyDescriptor? _prototypeDescriptor;

Expand All @@ -29,15 +31,15 @@ public abstract partial class FunctionInstance : ObjectInstance, ICallable
internal PrivateEnvironment? _privateEnvironment;
private readonly IScriptOrModule? _scriptOrModule;

protected FunctionInstance(
protected Function(
Engine engine,
Realm realm,
JsString? name)
: this(engine, realm, name, FunctionThisMode.Global)
{
}

internal FunctionInstance(
internal Function(
Engine engine,
Realm realm,
JintFunctionDefinition function,
Expand All @@ -53,7 +55,7 @@ internal FunctionInstance(
_environment = env;
}

internal FunctionInstance(
internal Function(
Engine engine,
Realm realm,
JsString? name,
Expand Down Expand Up @@ -250,7 +252,7 @@ internal ObjectInstance GetPrototypeFromConstructor(JsValue constructor, Func<In
/// </summary>
internal Realm GetFunctionRealm(JsValue obj)
{
if (obj is FunctionInstance functionInstance && functionInstance._realm is not null)
if (obj is Function functionInstance && functionInstance._realm is not null)
{
return functionInstance._realm;
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/FunctionConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
/// <summary>
/// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
/// </summary>
internal FunctionInstance InstantiateFunctionObject(
internal Function InstantiateFunctionObject(
JintFunctionDefinition functionDeclaration,
Environment scope,
PrivateEnvironment? privateEnv)
Expand Down
8 changes: 5 additions & 3 deletions Jint/Native/Function/FunctionInstance.Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

namespace Jint.Native.Function;

public partial class FunctionInstance
#pragma warning disable MA0049
public partial class Function
#pragma warning restore MA0049
{
private static readonly JsString _functionNameAnonymous = new JsString("anonymous");

/// <summary>
/// https://tc39.es/ecma262/#sec-createdynamicfunction
/// </summary>
internal FunctionInstance CreateDynamicFunction(
internal Function CreateDynamicFunction(
ObjectInstance constructor,
JsValue newTarget,
FunctionKind kind,
Expand Down Expand Up @@ -157,7 +159,7 @@ internal FunctionInstance CreateDynamicFunction(
PrivateEnvironment? privateEnv = null;

var definition = new JintFunctionDefinition(function);
FunctionInstance F = OrdinaryFunctionCreate(proto, definition, function.Strict ? FunctionThisMode.Strict : FunctionThisMode.Global, scope, privateEnv);
Function F = OrdinaryFunctionCreate(proto, definition, function.Strict ? FunctionThisMode.Strict : FunctionThisMode.Global, scope, privateEnv);
F.SetFunctionName(_functionNameAnonymous, force: true);

if (kind == FunctionKind.Generator)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/FunctionPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Jint.Native.Function
/// <summary>
/// https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object
/// </summary>
internal sealed class FunctionPrototype : FunctionInstance
internal sealed class FunctionPrototype : Function
{
internal FunctionPrototype(
Engine engine,
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/ScriptFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Jint.Native.Function
{
public sealed class ScriptFunction : FunctionInstance, IConstructor
public sealed class ScriptFunction : Function, IConstructor
{
internal bool _isClassConstructor;
internal JsValue? _classFieldInitializerName;
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/ThrowTypeError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Jint.Native.Function
{
internal sealed class ThrowTypeError : FunctionInstance
internal sealed class ThrowTypeError : Function
{
public ThrowTypeError(Engine engine, Realm realm)
: base(engine, realm, null)
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Object/ObjectInstance.Private.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal JsValue PrivateGet(PrivateName property)
ExceptionHelper.ThrowTypeError(_engine.Realm, $"'#{property}' was defined without a getter");
}

var functionInstance = (FunctionInstance) getter;
var functionInstance = (Function.Function) getter;
var privateGet = functionInstance._engine.Call(functionInstance, this);
return privateGet;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ internal sealed class ClassFieldDefinition

internal sealed class ClassStaticBlockDefinition
{
public required FunctionInstance BodyFunction { get; set; }
public required Function.Function BodyFunction { get; set; }
}

internal sealed class PrivateElement
Expand Down
6 changes: 3 additions & 3 deletions Jint/Native/Object/ObjectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private static JsValue UnwrapFromGetter(PropertyDescriptor desc, JsValue thisObj
return Undefined;
}

var functionInstance = (FunctionInstance) getter;
var functionInstance = (Function.Function) getter;
return functionInstance._engine.Call(functionInstance, thisObject);
}

Expand Down Expand Up @@ -614,7 +614,7 @@ private bool SetUnlikely(JsValue property, JsValue value, JsValue receiver)
}
}

if (ownDesc.Set is not FunctionInstance setter)
if (ownDesc.Set is not Function.Function setter)
{
return false;
}
Expand Down Expand Up @@ -1646,7 +1646,7 @@ internal static void DefineField(ObjectInstance receiver, ClassFieldDefinition f
if (initializer is not null)
{
initValue = receiver._engine.Call(initializer, receiver);
if (initValue is FunctionInstance functionInstance)
if (initValue is Function.Function functionInstance)
{
functionInstance.SetFunctionName(fieldName);
}
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Promise/JsPromise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ JsValue Handler
);

internal sealed record ResolvingFunctions(
FunctionInstance Resolve,
FunctionInstance Reject
Function.Function Resolve,
Function.Function Reject
);

public sealed record ManualPromise(
Expand Down
Loading

0 comments on commit 982a751

Please sign in to comment.