This repository was archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptValue.cs
More file actions
177 lines (142 loc) · 5.19 KB
/
JavaScriptValue.cs
File metadata and controls
177 lines (142 loc) · 5.19 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
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
175
176
177
using System.ComponentModel;
using System.Runtime.InteropServices;
using static JavaScript.v8.NativeMethods;
namespace JavaScript.v8;
public enum JavaScriptValueType
{
Unknown,
Undefined,
Object,
Array,
String,
Integer,
Number,
True,
False,
Null,
Function,
}
[StructLayout(LayoutKind.Sequential)]
public unsafe readonly struct JavaScriptValue
{
private readonly IntPtr _;
public JavaScriptValueType Type => js_value_type(this);
public string AsString(JavaScriptScope scope) => FromJsString(scope, this);
public long AsInteger(JavaScriptScope scope) => js_integer_value(scope, this);
public double AsNumber(JavaScriptScope scope) => js_number_value(scope, this);
public int ArrayLength() => (int)js_array_length(this);
public JavaScriptValue ArrayGetIndex(JavaScriptScope scope, int index) => js_array_get_index(scope, this, (uint)index);
public void ArraySetIndex(JavaScriptScope scope, int index, JavaScriptValue value) => js_array_set_index(scope, this, (uint)index, value);
public JavaScriptValue ObjectGetProperty(JavaScriptScope scope, string key) => js_object_get_property(scope, this, ToJsString(scope, key));
public void ObjectSetProperty(JavaScriptScope scope, string key, JavaScriptValue value) => js_object_set_property(scope, this, ToJsString(scope, key), value);
public ObjectEnumerator EnumerateObject(JavaScriptScope scope) => new ObjectEnumerator(scope, this);
public ArrayEnumerator EnumerateArray(JavaScriptScope scope) => new ArrayEnumerator(scope, this);
public JavaScriptValue CallFunction(JavaScriptScope scope, JavaScriptValue self, params JavaScriptValue[] args)
{
fixed (JavaScriptValue* argv = args)
{
return CallFunction(scope, self, argv, args.Length);
}
}
public JavaScriptValue CallFunction(JavaScriptScope scope, JavaScriptValue self, ReadOnlySpan<JavaScriptValue> args)
{
fixed (JavaScriptValue* argv = args)
{
return CallFunction(scope, self, argv, args.Length);
}
}
public JavaScriptValue CallFunction(JavaScriptScope scope, JavaScriptValue self, JavaScriptValue arg0)
{
return CallFunction(scope, self, &arg0, 1);
}
private JavaScriptValue CallFunction(JavaScriptScope scope, JavaScriptValue self, JavaScriptValue* argv, int argc)
{
return js_function_call(scope, this, self, argv, argc, out var result) == 0
? result
: throw new JavaScriptException(result.AsString(scope));
}
public ref struct ObjectEnumerator
{
private readonly JavaScriptScope _scope;
private readonly JavaScriptValue _value;
private readonly JavaScriptValue _propertyNames;
private readonly uint _length;
private int _index;
internal ObjectEnumerator(JavaScriptScope scope, JavaScriptValue value)
{
_index = -1;
_scope = scope;
_value = value;
_propertyNames = js_object_get_own_property_names(_scope, _value);
_length = js_array_length(_propertyNames);
}
public JavaScriptProperty Current
{
get
{
var propertyName = js_array_get_index(_scope, _propertyNames, (uint)_index);
var propertyValue = js_object_get_property(_scope, _value, propertyName);
return new(_scope, propertyName, propertyValue);
}
}
public ObjectEnumerator GetEnumerator()
{
var result = this;
result._index = -1;
return result;
}
public bool MoveNext()
{
return ++_index < _length;
}
}
public ref struct ArrayEnumerator
{
private readonly JavaScriptScope _scope;
private readonly JavaScriptValue _value;
private readonly uint _length;
private int _index;
internal ArrayEnumerator(JavaScriptScope scope, JavaScriptValue value)
{
_index = -1;
_scope = scope;
_value = value;
_length = js_array_length(_value);
}
public JavaScriptValue Current
{
get
{
return js_array_get_index(_scope, _value, (uint)_index);
}
}
public ArrayEnumerator GetEnumerator()
{
var result = this;
result._index = -1;
return result;
}
public bool MoveNext()
{
return ++_index < _length;
}
}
}
public unsafe readonly ref struct JavaScriptProperty
{
private readonly JavaScriptScope _scope;
private readonly JavaScriptValue _propertyName;
private readonly JavaScriptValue _propertyValue;
public JavaScriptProperty(JavaScriptScope scope, JavaScriptValue propertyName, JavaScriptValue propertyValue)
{
_scope = scope;
_propertyName = propertyName;
_propertyValue = propertyValue;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Deconstruct(out string key, out JavaScriptValue value)
{
key = FromJsString(_scope, _propertyName);
value = _propertyValue;
}
}