Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Mar 1, 2024
1 parent e1a173b commit 65d1be2
Show file tree
Hide file tree
Showing 260 changed files with 38,354 additions and 38,692 deletions.
3,855 changes: 1,927 additions & 1,928 deletions NiL.JS/BaseLibrary/Array.cs

Large diffs are not rendered by default.

235 changes: 117 additions & 118 deletions NiL.JS/BaseLibrary/ArrayBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,159 +4,158 @@
using NiL.JS.Core;
using NiL.JS.Core.Interop;

namespace NiL.JS.BaseLibrary
namespace NiL.JS.BaseLibrary;

#if !(PORTABLE || NETCORE)
[Serializable]
#endif
public sealed class ArrayBuffer : CustomType
{
#if !(PORTABLE || NETCORE)
[Serializable]
#endif
public sealed class ArrayBuffer : CustomType
private sealed class Element : JSValue
{
#if !(PORTABLE || NETCORE)
[Serializable]
#endif
private sealed class Element : JSValue
{
private int index;
private byte[] data;

public Element(int index, ArrayBuffer parent)
{
this._valueType = JSValueType.Integer;
this.index = index;
this._iValue = parent.data[index];
this.data = parent.data;
this._attributes |= JSValueAttributesInternal.Reassign;
}
private int index;
private byte[] data;

public override void Assign(JSValue value)
{
data[index] = (byte)Tools.JSObjectToInt32(value);
}
}

internal byte[] data;

[Hidden]
public byte this[int index]
public Element(int index, ArrayBuffer parent)
{
[Hidden]
get
{
return data[index];
}
[Hidden]
set
{
data[index] = value;
}
this._valueType = JSValueType.Integer;
this.index = index;
this._iValue = parent.data[index];
this.data = parent.data;
this._attributes |= JSValueAttributesInternal.Reassign;
}

[DoNotEnumerate]
public ArrayBuffer()
: this(0)
public override void Assign(JSValue value)
{
data[index] = (byte)Tools.JSObjectToInt32(value);
}
}

[DoNotEnumerate]
public ArrayBuffer(int length)
: this(new byte[length])
{
}
internal byte[] data;

[Hidden]
public byte this[int index]
{
[Hidden]
public ArrayBuffer(byte[] data)
get
{
if (data == null)
throw new ArgumentNullException();
this.data = data;
_attributes |= JSValueAttributesInternal.SystemObject;
return data[index];
}

public int byteLength
[Hidden]
set
{
[Hidden]
get
{
return data.Length;
}
data[index] = value;
}
}

[Hidden]
public ArrayBuffer slice(int begin, int end)
{
if (end < begin || begin >= data.Length || end >= data.Length)
ExceptionHelper.Throw((new RangeError("Invalid begin or end index")));
[DoNotEnumerate]
public ArrayBuffer()
: this(0)
{
}

var res = new ArrayBuffer(end - begin + 1);
for (int i = 0, j = begin; j <= end; j++, i++)
res.data[i] = data[j];
[DoNotEnumerate]
public ArrayBuffer(int length)
: this(new byte[length])
{
}

return res;
}
[Hidden]
public ArrayBuffer(byte[] data)
{
if (data == null)
throw new ArgumentNullException();
this.data = data;
_attributes |= JSValueAttributesInternal.SystemObject;
}

public int byteLength
{
[Hidden]
public ArrayBuffer slice(int begin)
get
{
return slice(begin, data.Length - 1);
return data.Length;
}
}

[EditorBrowsable(EditorBrowsableState.Never)]
public ArrayBuffer slice(Arguments args)
{
if (args == null)
throw new ArgumentNullException("args");
var l = Tools.JSObjectToInt32(args.GetProperty("length"));
if (l == 0)
return this;
if (l == 1)
return slice(Tools.JSObjectToInt32(args[0]), data.Length - 1);
else
return slice(Tools.JSObjectToInt32(args[0]), Tools.JSObjectToInt32(args[1]));
}
[Hidden]
public ArrayBuffer slice(int begin, int end)
{
if (end < begin || begin >= data.Length || end >= data.Length)
ExceptionHelper.Throw((new RangeError("Invalid begin or end index")));

[Hidden]
internal protected override JSValue GetProperty(JSValue key, bool forWrite, PropertyScope memberScope)
{
if (memberScope < PropertyScope.Super && key._valueType != JSValueType.Symbol)
{
uint index = 0;
double dindex = Tools.JSObjectToDouble(key);
if (!double.IsInfinity(dindex)
&& !double.IsNaN(dindex)
&& ((index = (uint)dindex) == dindex))
{
return getElement((int)index);
}
}
return base.GetProperty(key, forWrite, memberScope);
}
var res = new ArrayBuffer(end - begin + 1);
for (int i = 0, j = begin; j <= end; j++, i++)
res.data[i] = data[j];

private JSValue getElement(int index)
{
if (index < 0)
ExceptionHelper.Throw(new RangeError("Invalid array index"));
if (index >= data.Length)
return undefined;
return new Element(index, this);
}
return res;
}

protected internal override IEnumerator<KeyValuePair<string, JSValue>> GetEnumerator(bool hideNonEnumerable, EnumerationMode enumerationMode, PropertyScope propertyScope = PropertyScope.Common)
{
var be = base.GetEnumerator(hideNonEnumerable, enumerationMode, propertyScope);
while (be.MoveNext())
yield return be.Current;
[Hidden]
public ArrayBuffer slice(int begin)
{
return slice(begin, data.Length - 1);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public ArrayBuffer slice(Arguments args)
{
if (args == null)
throw new ArgumentNullException("args");
var l = Tools.JSObjectToInt32(args.GetProperty("length"));
if (l == 0)
return this;
if (l == 1)
return slice(Tools.JSObjectToInt32(args[0]), data.Length - 1);
else
return slice(Tools.JSObjectToInt32(args[0]), Tools.JSObjectToInt32(args[1]));
}

if (propertyScope is PropertyScope.Common or PropertyScope.Own)
[Hidden]
internal protected override JSValue GetProperty(JSValue key, bool forWrite, PropertyScope memberScope)
{
if (memberScope < PropertyScope.Super && key._valueType != JSValueType.Symbol)
{
uint index = 0;
double dindex = Tools.JSObjectToDouble(key);
if (!double.IsInfinity(dindex)
&& !double.IsNaN(dindex)
&& ((index = (uint)dindex) == dindex))
{
for (var i = 0; i < data.Length; i++)
yield return new KeyValuePair<string, JSValue>(Tools.Int32ToString(i), (int)enumerationMode > 0 ? getElement(i) : null);
return getElement((int)index);
}
}
return base.GetProperty(key, forWrite, memberScope);
}

[Hidden]
public byte[] GetData()
private JSValue getElement(int index)
{
if (index < 0)
ExceptionHelper.Throw(new RangeError("Invalid array index"));
if (index >= data.Length)
return undefined;
return new Element(index, this);
}

protected internal override IEnumerator<KeyValuePair<string, JSValue>> GetEnumerator(bool hideNonEnumerable, EnumerationMode enumerationMode, PropertyScope propertyScope = PropertyScope.Common)
{
var be = base.GetEnumerator(hideNonEnumerable, enumerationMode, propertyScope);
while (be.MoveNext())
yield return be.Current;

if (propertyScope is PropertyScope.Common or PropertyScope.Own)
{
return data;
for (var i = 0; i < data.Length; i++)
yield return new KeyValuePair<string, JSValue>(Tools.Int32ToString(i), (int)enumerationMode > 0 ? getElement(i) : null);
}
}

[Hidden]
public byte[] GetData()
{
return data;
}
}
Loading

0 comments on commit 65d1be2

Please sign in to comment.