forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsValueExtensions.cs
More file actions
111 lines (101 loc) · 3.79 KB
/
JsValueExtensions.cs
File metadata and controls
111 lines (101 loc) · 3.79 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
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
/// <summary>
/// Extensions for the JavaScript value
/// </summary>
internal static class JsValueExtensions
{
/// <summary>
/// Gets a property descriptor for an object's own property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="propertyName">The name of the property</param>
/// <returns>The property descriptor</returns>
public static JsValue GetOwnPropertyDescriptor(this JsValue source, string propertyName)
{
JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
JsValue resultValue = source.GetOwnPropertyDescriptor(propertyId);
return resultValue;
}
/// <summary>
/// Determines whether an object has a property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="propertyName">The name of the property</param>
/// <returns>Whether the object (or a prototype) has the property</returns>
public static bool HasProperty(this JsValue source, string propertyName)
{
JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
bool result = source.HasProperty(propertyId);
return result;
}
/// <summary>
/// Gets an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="name">The name of the property</param>
/// <returns>The value of the property</returns>
public static JsValue GetProperty(this JsValue source, string name)
{
JsPropertyId id = JsPropertyId.FromString(name);
JsValue resultValue = source.GetProperty(id);
return resultValue;
}
/// <summary>
/// Sets an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="name">The name of the property</param>
/// <param name="value">The new value of the property</param>
/// <param name="useStrictRules">The property set should follow strict mode rules</param>
public static void SetProperty(this JsValue source, string name, JsValue value, bool useStrictRules)
{
JsPropertyId id = JsPropertyId.FromString(name);
source.SetProperty(id, value, useStrictRules);
}
/// <summary>
/// Deletes an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="propertyName">The name of the property</param>
/// <param name="useStrictRules">The property set should follow strict mode rules</param>
/// <returns>Whether the property was deleted</returns>
public static JsValue DeleteProperty(this JsValue source, string propertyName, bool useStrictRules)
{
JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
JsValue resultValue = source.DeleteProperty(propertyId, useStrictRules);
return resultValue;
}
/// <summary>
/// Defines a new object's own property from a property descriptor
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="source">The JavaScript value</param>
/// <param name="propertyName">The name of the property</param>
/// <param name="propertyDescriptor">The property descriptor</param>
/// <returns>Whether the property was defined</returns>
public static bool DefineProperty(this JsValue source, string propertyName, JsValue propertyDescriptor)
{
JsPropertyId propertyId = JsPropertyId.FromString(propertyName);
bool result = source.DefineProperty(propertyId, propertyDescriptor);
return result;
}
}
}