-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
JsObject.cs
174 lines (146 loc) · 4.01 KB
/
JsObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using Microsoft.Collections.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using Tenray.Topaz.Core;
namespace Tenray.Topaz.API;
public partial class JsObject : IJsObject, IDictionary
{
private const string NullString = "null";
readonly DictionarySlim<string, object> dictionary = new();
public object this[object key]
{
get
{
if (key == null)
key = NullString;
if (dictionary.TryGetValue(key.ToString(), out var value))
return value;
return Undefined.Value;
}
set
{
Add(key, ConvertJsonElementToJsObject(value));
}
}
public bool IsFixedSize => false;
public bool IsReadOnly => false;
public ICollection Keys
{
get
{
var list = new List<object>(dictionary.Count);
var keys = GetObjectKeys();
foreach (var key in keys)
{
list.Add(key);
}
return list;
}
}
public ICollection Values
{
get
{
var list = new List<object>(dictionary.Count);
foreach (var entry in dictionary)
{
list.Add(entry.Value);
}
return list;
}
}
public int Count => dictionary.Count;
public bool IsSynchronized => false;
public object SyncRoot => null;
public void Add(object key, object value)
{
if (key == null)
key = NullString;
ref var entry = ref dictionary.GetOrAddValueRef(key.ToString());
entry = value;
}
public void Clear()
{
dictionary.Clear();
}
public bool Contains(object key)
{
if (key == null)
key = NullString;
return dictionary.ContainsKey(key.ToString());
}
public void CopyTo(Array array, int index)
{
throw new NotSupportedException("Tenray.Topaz.API.JsObject: CopyTo(Array,index) method is not supported.");
}
public IDictionaryEnumerator GetEnumerator()
{
return dictionary.GetEnumerator();
}
public void Remove(object key)
{
if (key == null)
key = NullString;
dictionary.Remove(key.ToString());
}
IEnumerator IEnumerable.GetEnumerator()
{
return dictionary.GetEnumerator();
}
public bool TryGetValue(object key, out object value)
{
if (key == null)
key = NullString;
return dictionary.TryGetValue(key.ToString(), out value);
}
public IEnumerable GetObjectKeys()
{
return GetObjectKeysInternal();
}
protected virtual IEnumerable GetObjectKeysInternal()
{
foreach (var key in dictionary)
{
yield return key.Key;
}
}
void IJsObject.UnwrapObject(ScriptExecutor scriptExecutor)
{
var keys = GetObjectKeys();
foreach (var key in keys)
{
var skey = (string)key;
ref var entry = ref dictionary.GetOrAddValueRef(skey);
entry = scriptExecutor.GetValue(entry);
}
}
public void SetValue(object key, object value)
{
if (key == null)
key = NullString;
var skey = key.ToString();
ref var entry = ref dictionary.GetOrAddValueRef(skey);
entry = value;
}
bool IJsObject.IsPrototypeProperty(object member)
{
if (member == null)
return false;
if (member is not string memberName)
return false;
return IsPrototypePropertyInternal(memberName);
}
protected virtual bool IsPrototypePropertyInternal(string memberName)
{
return false;
}
#pragma warning disable IDE1006 // Naming Styles
public virtual string toString()
{
return "[object Object]";
}
public bool hasOwnProperty(object key) => Contains(key);
public bool hasOwn(object key) => Contains(key);
#pragma warning restore IDE1006 // Naming Styles
}