forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsRuntime.cs
More file actions
227 lines (207 loc) · 6.93 KB
/
JsRuntime.cs
File metadata and controls
227 lines (207 loc) · 6.93 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
/// <summary>
/// The Chakra runtime
/// </summary>
/// <remarks>
/// <para>
/// Each Chakra runtime has its own independent execution engine, JIT compiler, and garbage
/// collected heap. As such, each runtime is completely isolated from other runtimes.
/// </para>
/// <para>
/// Runtimes can be used on any thread, but only one thread can call into a runtime at any
/// time.
/// </para>
/// <para>
/// NOTE: A JavaScriptRuntime, unlike other objects in the Chakra hosting API, is not
/// garbage collected since it contains the garbage collected heap itself. A runtime will
/// continue to exist until Dispose is called.
/// </para>
/// </remarks>
internal struct JsRuntime : IDisposable
{
/// <summary>
/// The handle
/// </summary>
private IntPtr _handle;
/// <summary>
/// Gets a value indicating whether the runtime is valid
/// </summary>
public bool IsValid
{
get { return _handle != IntPtr.Zero; }
}
/// <summary>
/// Gets a current memory usage for a runtime
/// </summary>
/// <remarks>
/// Memory usage can be always be retrieved, regardless of whether or not the runtime is active
/// on another thread.
/// </remarks>
public UIntPtr MemoryUsage
{
get
{
UIntPtr memoryUsage;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetRuntimeMemoryUsage(this, out memoryUsage));
return memoryUsage;
}
}
/// <summary>
/// Gets or sets a current memory limit for a runtime
/// </summary>
/// <remarks>
/// The memory limit of a runtime can be always be retrieved, regardless of whether or not the
/// runtime is active on another thread.
/// </remarks>
public UIntPtr MemoryLimit
{
get
{
UIntPtr memoryLimit;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetRuntimeMemoryLimit(this, out memoryLimit));
return memoryLimit;
}
set
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetRuntimeMemoryLimit(this, value));
}
}
/// <summary>
/// Gets or sets a value indicating whether script execution is disabled in the runtime
/// </summary>
public bool Disabled
{
get
{
bool isDisabled;
JsErrorHelpers.ThrowIfError(NativeMethods.JsIsRuntimeExecutionDisabled(this, out isDisabled));
return isDisabled;
}
set
{
JsErrorHelpers.ThrowIfError(value ?
NativeMethods.JsDisableRuntimeExecution(this)
:
NativeMethods.JsEnableRuntimeExecution(this))
;
}
}
/// <summary>
/// Creates a new runtime
/// </summary>
/// <returns>The runtime created</returns>
public static JsRuntime Create()
{
return Create(JsRuntimeAttributes.None, null);
}
/// <summary>
/// Creates a new runtime
/// </summary>
/// <param name="attributes">The attributes of the runtime to be created</param>
/// <returns>The runtime created</returns>
public static JsRuntime Create(JsRuntimeAttributes attributes)
{
return Create(attributes, null);
}
/// <summary>
/// Creates a new runtime
/// </summary>
/// <param name="attributes">The attributes of the runtime to be created</param>
/// <param name="threadServiceCallback">The thread service for the runtime. Can be null</param>
/// <returns>The runtime created</returns>
public static JsRuntime Create(JsRuntimeAttributes attributes, JsThreadServiceCallback threadServiceCallback)
{
JsRuntime handle;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateRuntime(attributes, threadServiceCallback, out handle));
return handle;
}
/// <summary>
/// Performs a full garbage collection
/// </summary>
public void CollectGarbage()
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsCollectGarbage(this));
}
/// <summary>
/// Sets a memory allocation callback for specified runtime
/// </summary>
/// <remarks>
/// <para>
/// Registering a memory allocation callback will cause the runtime to call back to the host
/// whenever it acquires memory from, or releases memory to, the OS. The callback routine is
/// called before the runtime memory manager allocates a block of memory. The allocation will
/// be rejected if the callback returns false. The runtime memory manager will also invoke the
/// callback routine after freeing a block of memory, as well as after allocation failures.
/// </para>
/// <para>
/// The callback is invoked on the current runtime execution thread, therefore execution is
/// blocked until the callback completes.
/// </para>
/// <para>
/// The return value of the callback is not stored; previously rejected allocations will not
/// prevent the runtime from invoking the callback again later for new memory allocations.
/// </para>
/// </remarks>
/// <param name="callbackState">
/// User provided state that will be passed back to the callback.
/// </param>
/// <param name="allocationCallback">
/// Memory allocation callback to be called for memory allocation events.
/// </param>
public void SetMemoryAllocationCallback(IntPtr callbackState, JsMemoryAllocationCallback allocationCallback)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetRuntimeMemoryAllocationCallback(this, callbackState, allocationCallback));
}
/// <summary>
/// Sets a callback function that is called by the runtime before garbage collection
/// </summary>
/// <remarks>
/// <para>
/// The callback is invoked on the current runtime execution thread, therefore execution is
/// blocked until the callback completes.
/// </para>
/// <para>
/// The callback can be used by hosts to prepare for garbage collection. For example, by
/// releasing unnecessary references on Chakra objects.
/// </para>
/// </remarks>
/// <param name="callbackState">User provided state that will be passed back to the callback</param>
/// <param name="beforeCollectCallback">The callback function being set</param>
public void SetBeforeCollectCallback(IntPtr callbackState, JsBeforeCollectCallback beforeCollectCallback)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetRuntimeBeforeCollectCallback(this, callbackState, beforeCollectCallback));
}
/// <summary>
/// Creates a script context for running scripts
/// </summary>
/// <remarks>
/// Each script context has its own global object that is isolated from all other script
/// contexts.
/// </remarks>
/// <returns>The created script context</returns>
public JsContext CreateContext()
{
JsContext reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateContext(this, out reference));
return reference;
}
/// <summary>
/// Disposes a runtime
/// </summary>
/// <remarks>
/// Once a runtime has been disposed, all resources owned by it are invalid and cannot be used.
/// If the runtime is active (i.e. it is set to be current on a particular thread), it cannot
/// be disposed.
/// </remarks>
public void Dispose()
{
if (IsValid)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsDisposeRuntime(this));
}
_handle = IntPtr.Zero;
}
}
}