Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary ObjectClass types #1172

Merged
merged 1 commit into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ArrayInstance : ObjectInstance, IEnumerable<JsValue>

private ObjectChangeFlags _objectChangeFlags;

public ArrayInstance(Engine engine, uint capacity = 0) : base(engine, ObjectClass.Array)
public ArrayInstance(Engine engine, uint capacity = 0) : base(engine)
{
if (capacity > engine.Options.Constraints.MaxArraySize)
{
Expand All @@ -40,7 +40,7 @@ public ArrayInstance(Engine engine, uint capacity = 0) : base(engine, ObjectClas
/// <summary>
/// Possibility to construct valid array fast, requires that supplied array does not have holes.
/// </summary>
public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, ObjectClass.Array)
public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine)
{
int length = 0;
if (items == null || items.Length == 0)
Expand All @@ -57,7 +57,7 @@ public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, O
_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
}

public ArrayInstance(Engine engine, Dictionary<uint, PropertyDescriptor> items) : base(engine, ObjectClass.Array)
public ArrayInstance(Engine engine, Dictionary<uint, PropertyDescriptor> items) : base(engine)
{
_sparse = items;
var length = items?.Count ?? 0;
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Iterator/IteratorInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public IteratorInstance(Engine engine)

public IteratorInstance(
Engine engine,
IEnumerable<JsValue> enumerable) : base(engine, ObjectClass.Iterator)
IEnumerable<JsValue> enumerable) : base(engine)
{
_enumerable = enumerable.GetEnumerator();
_prototype = engine.Realm.Intrinsics.IteratorPrototype;
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Json/JsonInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class JsonInstance : ObjectInstance
Engine engine,
Realm realm,
ObjectPrototype objectPrototype)
: base(engine, objectClass: ObjectClass.JSON)
: base(engine, objectClass: ObjectClass.Object)
{
_realm = realm;
_prototype = objectPrototype;
Expand Down
35 changes: 25 additions & 10 deletions Jint/Native/Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using Jint.Collections;
using Jint.Native.Array;
using Jint.Native.BigInt;
using Jint.Native.Boolean;
using Jint.Native.Global;
using Jint.Native.Number;
using Jint.Native.Object;
using Jint.Native.Proxy;
using Jint.Native.String;
using Jint.Pooling;
using Jint.Runtime;
Expand Down Expand Up @@ -205,21 +207,34 @@ private JsValue SerializeJSONProperty(JsValue key, JsValue holder)
ExceptionHelper.ThrowTypeError(_engine.Realm, "Do not know how to serialize a BigInt");
}

var isCallable = value.IsObject() && value.IsCallable;

if (value.IsObject() && isCallable == false)
if (value is ObjectInstance { IsCallable: false } objectInstance)
{
return SerializesAsArray(value)
? SerializeJSONArray(value)
: SerializeJSONObject(value.AsObject());
return SerializesAsArray(objectInstance)
? SerializeJSONArray(objectInstance)
: SerializeJSONObject(objectInstance);
}

return JsValue.Undefined;
}

private static bool SerializesAsArray(JsValue value)
private static bool SerializesAsArray(ObjectInstance value)
{
return value.AsObject().Class == ObjectClass.Array || value is ObjectWrapper { IsArrayLike: true };
if (value is ArrayInstance)
{
return true;
}

if (value is ProxyInstance proxyInstance && SerializesAsArray(proxyInstance._target))
{
return true;
}

if (value is ObjectWrapper { IsArrayLike: true })
{
return true;
}

return false;
}

/// <summary>
Expand Down Expand Up @@ -275,11 +290,11 @@ private static string QuoteJSONString(string value)
/// <summary>
/// https://tc39.es/ecma262/#sec-serializejsonarray
/// </summary>
private string SerializeJSONArray(JsValue value)
private string SerializeJSONArray(ObjectInstance value)
{
_stack.Enter(value);
var stepback = _indent;
_indent = _indent + _gap;
_indent += _gap;
var partial = new List<string>();
var len = TypeConverter.ToUint32(value.Get(CommonProperties.Length, value));
for (int i = 0; i < len; i++)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Math/MathInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class MathInstance : ObjectInstance
{
private Random _random;

internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine, ObjectClass.Math)
internal MathInstance(Engine engine, ObjectPrototype objectPrototype) : base(engine)
{
_prototype = objectPrototype;
}
Expand Down
13 changes: 2 additions & 11 deletions Jint/Native/Object/ObjectClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ namespace Jint.Native.Object
internal enum ObjectClass : byte
{
Arguments,
Array,
Boolean,
Date,
Error,
Function,
Iterator,
JSON,
Math,
Number,
Object,
Promise,
Proxy,
Reflect,
RegExp,
String,
Symbol,
TypeReference
String
}
}
}
45 changes: 24 additions & 21 deletions Jint/Native/Object/ObjectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public ObjectInstance(Engine engine) : this(engine, ObjectClass.Object)
{
}

internal ObjectInstance(Engine engine, ObjectClass objectClass, InternalTypes type = InternalTypes.Object)
internal ObjectInstance(
Engine engine,
ObjectClass objectClass = ObjectClass.Object,
InternalTypes type = InternalTypes.Object)
: base(type)
{
_engine = engine;
Expand Down Expand Up @@ -914,26 +917,6 @@ private object ToObject(ObjectTraverseStack stack)
object converted = null;
switch (Class)
{
case ObjectClass.Array:
if (this is ArrayInstance arrayInstance)
{
var result = new object[arrayInstance.Length];
for (uint i = 0; i < result.Length; i++)
{
var value = arrayInstance[i];
object valueToSet = null;
if (!value.IsUndefined())
{
valueToSet = value is ObjectInstance oi
? oi.ToObject(stack)
: value.ToObject();
}
result[i] = valueToSet;
}
converted = result;
}
break;

case ObjectClass.String:
if (this is StringInstance stringInstance)
{
Expand Down Expand Up @@ -981,6 +964,26 @@ private object ToObject(ObjectTraverseStack stack)

case ObjectClass.Arguments:
case ObjectClass.Object:

if (this is ArrayInstance arrayInstance)
{
var result = new object[arrayInstance.Length];
for (uint i = 0; i < result.Length; i++)
{
var value = arrayInstance[i];
object valueToSet = null;
if (!value.IsUndefined())
{
valueToSet = value is ObjectInstance oi
? oi.ToObject(stack)
: value.ToObject();
}
result[i] = valueToSet;
}
converted = result;
break;
}

var o = _engine.Options.Interop.CreateClrObject(this);
foreach (var p in GetOwnProperties())
{
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Promise/PromiseInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal sealed class PromiseInstance : ObjectInstance
internal List<PromiseReaction> PromiseRejectReactions = new();
internal List<PromiseReaction> PromiseFulfillReactions = new();

internal PromiseInstance(Engine engine) : base(engine, ObjectClass.Promise)
internal PromiseInstance(Engine engine) : base(engine)
{
}

Expand Down Expand Up @@ -186,4 +186,4 @@ private void Settle(PromiseState state, JsValue result)
Value = result;
}
}
}
}
2 changes: 1 addition & 1 deletion Jint/Native/Reflect/ReflectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class ReflectInstance : ObjectInstance
internal ReflectInstance(
Engine engine,
Realm realm,
ObjectPrototype objectPrototype) : base(engine, ObjectClass.Reflect)
ObjectPrototype objectPrototype) : base(engine)
{
_realm = realm;
_prototype = objectPrototype;
Expand Down
6 changes: 4 additions & 2 deletions Jint/Native/Symbol/SymbolInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ namespace Jint.Native.Symbol
{
public sealed class SymbolInstance : ObjectInstance, IPrimitiveInstance
{
internal SymbolInstance(Engine engine, SymbolPrototype prototype, JsSymbol symbol)
: base(engine, ObjectClass.Symbol)
internal SymbolInstance(
Engine engine,
SymbolPrototype prototype,
JsSymbol symbol) : base(engine)
{
_prototype = prototype;
SymbolData = symbol;
Expand Down
5 changes: 3 additions & 2 deletions Jint/Runtime/Interop/NamespaceReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)
var genericTypeReference = arguments[i];
if (genericTypeReference.IsUndefined()
|| !genericTypeReference.IsObject()
|| genericTypeReference.AsObject().Class != ObjectClass.TypeReference)
|| genericTypeReference.AsObject() is not TypeReference tr)
{
ExceptionHelper.ThrowTypeError(_engine.Realm, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?");
return default;
}

genericTypes[i] = ((TypeReference) genericTypeReference).ReferenceType;
genericTypes[i] = tr.ReferenceType;
}

var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interop/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapp
private readonly record struct MemberAccessorKey(Type Type, string PropertyName);

private TypeReference(Engine engine, Type type)
: base(engine, engine.Realm, _name, FunctionThisMode.Global, ObjectClass.TypeReference)
: base(engine, engine.Realm, _name, FunctionThisMode.Global)
{
ReferenceType = type;

Expand Down