forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsPropertyId.cs
More file actions
148 lines (130 loc) · 3.6 KB
/
JsPropertyId.cs
File metadata and controls
148 lines (130 loc) · 3.6 KB
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
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
using System;
/// <summary>
/// The property identifier
/// </summary>
/// <remarks>
/// Property identifiers are used to refer to properties of JavaScript objects instead of using
/// strings.
/// </remarks>
internal struct JsPropertyId : IEquatable<JsPropertyId>
{
/// <summary>
/// The id
/// </summary>
private readonly IntPtr _id;
/// <summary>
/// Gets a invalid ID
/// </summary>
public static JsPropertyId Invalid
{
get { return new JsPropertyId(IntPtr.Zero); }
}
/// <summary>
/// Gets a name associated with the property ID
/// </summary>
/// <remarks>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
public string Name
{
get
{
string name;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetPropertyNameFromId(this, out name));
return name;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="JsPropertyId"/> struct
/// </summary>
/// <param name="id">The ID</param>
internal JsPropertyId(IntPtr id)
{
_id = id;
}
/// <summary>
/// Gets a property ID associated with the name
/// </summary>
/// <remarks>
/// <para>
/// Property IDs are specific to a context and cannot be used across contexts.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <param name="name">The name of the property ID to get or create.
/// The name may consist of only digits.</param>
/// <returns>The property ID in this runtime for the given name</returns>
public static JsPropertyId FromString(string name)
{
JsPropertyId id;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetPropertyIdFromName(name, out id));
return id;
}
/// <summary>
/// The equality operator for property IDs
/// </summary>
/// <param name="left">The first property ID to compare</param>
/// <param name="right">The second property ID to compare</param>
/// <returns>Whether the two property IDs are the same</returns>
public static bool operator ==(JsPropertyId left, JsPropertyId right)
{
return left.Equals(right);
}
/// <summary>
/// The inequality operator for property IDs
/// </summary>
/// <param name="left">The first property ID to compare</param>
/// <param name="right">The second property ID to compare</param>
/// <returns>Whether the two property IDs are not the same</returns>
public static bool operator !=(JsPropertyId left, JsPropertyId right)
{
return !left.Equals(right);
}
/// <summary>
/// Checks for equality between property IDs
/// </summary>
/// <param name="obj">The other property ID to compare</param>
/// <returns>Whether the two property IDs are the same</returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is JsPropertyId && Equals((JsPropertyId)obj);
}
/// <summary>
/// The hash code
/// </summary>
/// <returns>The hash code of the property ID</returns>
public override int GetHashCode()
{
return _id.ToInt32();
}
/// <summary>
/// Converts the property ID to a string
/// </summary>
/// <returns>The name of the property ID</returns>
public override string ToString()
{
return Name;
}
#region IEquatable<T> implementation
/// <summary>
/// Checks for equality between property IDs
/// </summary>
/// <param name="other">The other property ID to compare</param>
/// <returns>Whether the two property IDs are the same</returns>
public bool Equals(JsPropertyId other)
{
return _id == other._id;
}
#endregion
}
}