-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathJSValueScope.cs
More file actions
428 lines (381 loc) · 16 KB
/
JSValueScope.cs
File metadata and controls
428 lines (381 loc) · 16 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtime;
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
namespace Microsoft.JavaScript.NodeApi;
/// <summary>
/// Indicates the type of <see cref="JSValueScope" /> within the hierarchy of scopes.
/// </summary>
public enum JSValueScopeType
{
/// <summary>
/// A limited scope without any <see cref="JSRuntimeContext" /> or <see cref="JSModuleContext" />.
/// Used by the Node API .NET native host to set up callbacks before the managed host is
/// initialized.
/// </summary>
NoContext,
/// <summary>
/// A parent scope shared by all (non-AOT) .NET modules loaded in the same process. It has
/// a <see cref="JSRuntimeContext" /> but no <see cref="JSModuleContext" />.
/// </summary>
/// <remarks>
/// AOT modules do not have any root scope, so each module scope has a separate
/// <see cref="JSRuntimeContext"/>.
/// </remarks>
Root,
/// <summary>
/// A scope specific to each module. It inherits the <see cref="JSRuntimeContext" /> from the root
/// scope, and has a unique <see cref="JSModuleContext" />.
/// </summary>
/// <remarks>
/// AOT modules do not have any root scope, so each module also has a separate
/// <see cref="JSRuntimeContext"/>.
/// </remarks>
Module,
/// <summary>
/// Callback scope within a module; inherits context from the module.
/// </summary>
Callback,
/// <summary>
/// Handle scope within a callback; inherits context from the module.
/// </summary>
Handle,
/// <summary>
/// Escapable handle scope within a callback; inherits context from the module.
/// </summary>
Escapable,
}
/// <summary>
/// A scope that controls the lifetime of JS values. When the scope is disposed, any
/// JS values created within the scope are released unless they are held by a strong
/// <see cref="JSReference" />.
/// </summary>
/// <remarks>
/// Every call from JS to .NET creates a separate scope for the duration of the call.
/// That means any JS values created during the call are released when the call returns,
/// unless they are returned to JS or held by a strong <see cref="JSReference" />.
/// </remarks>
public sealed class JSValueScope : IDisposable
{
private readonly JSValueScope? _parentScope;
#pragma warning disable IDE0032 // Use auto property
private readonly napi_env _env;
#pragma warning restore IDE0032
private readonly SynchronizationContext? _previousSyncContext;
private readonly nint _scopeHandle;
[ThreadStatic] private static JSValueScope? s_currentScope;
public JSValueScopeType ScopeType { get; }
/// <summary>
/// Gets the current JS value scope.
/// </summary>
/// <exception cref="JSInvalidThreadAccessException">No scope was established for the current
/// thread.</exception>
public static JSValueScope Current => s_currentScope ??
throw new JSInvalidThreadAccessException(currentScope: null);
/// <summary>
/// Gets the environment handle for the scope, or throws an exception if the scope is
/// disposed or access from the current thread is invalid.
/// </summary>
/// <exception cref="JSValueScopeClosedException">The scope has been closed.</exception>
/// <exception cref="JSInvalidThreadAccessException">The scope is not valid on the current
/// thread.</exception>
public napi_env EnvironmentHandle
{
get
{
ThrowIfDisposed();
ThrowIfInvalidThreadAccess();
return _env;
}
}
public static explicit operator napi_env(JSValueScope scope)
{
if (scope is null) throw new ArgumentNullException(nameof(scope));
return scope.EnvironmentHandle;
}
/// <summary>
/// Gets the environment handle without checking whether the scope is disposed or
/// whether access from the current thread is valid. WARNING: This must only be used
/// to avoid redundant handle checks when there is another (checked) access to
/// <see cref="EnvironmentHandle" /> for the same call.
/// </summary>
internal napi_env UncheckedEnvironmentHandle => _env;
/// <summary>
/// Gets the environment handle for the current thread scope, or throws an exception if
/// there is no environment for the current thread. For use only with static operations
/// not related to any <see cref="JSValue" />; for value operations use the
/// <see cref="EnvironmentHandle" /> from the value's <see cref="JSValue.Scope"/> instead.
/// </summary>
/// <exception cref="JSInvalidThreadAccessException">No scope was established for the current
/// thread.</exception>
internal static napi_env CurrentEnvironmentHandle => Current.EnvironmentHandle;
internal int ThreadId { get; }
public bool IsDisposed { get; private set; }
public JSRuntime Runtime { get; }
public JSRuntimeContext RuntimeContext { get; }
internal nint RuntimeContextHandle { get; }
internal static JSRuntime CurrentRuntime => Current.Runtime;
internal static JSRuntimeContext? CurrentRuntimeContext => s_currentScope?.RuntimeContext;
public JSModuleContext? ModuleContext { get; internal set; }
/// <summary>
/// Creates a new instance of a <see cref="JSValueScope"/> with a specified scope type.
/// </summary>
/// <param name="scopeType">The type of scope to create; default is
/// <see cref="JSValueScopeType.Handle" />.</param>
public JSValueScope(JSValueScopeType scopeType = JSValueScopeType.Handle)
: this(scopeType, env: default, runtime: default)
{
}
/// <summary>
/// Creates a new instance of a <see cref="JSValueScope"/>, which may be a parentless scope
/// with initial environment handle and JS runtime.
/// </summary>
/// <param name="scopeType">The type of scope to create.</param>
/// <param name="env">JS environment handle, required only for creating a scope
/// without a parent, otherwise the environment is inherited from the parent scope.</param>
/// <param name="runtime">JS runtime interface, required only for creating a scope
/// without a parent, otherwise the JS runtime is inherited from the parent scope.</param>
/// <param name="synchronizationContext">Optional synchronization context to use for async
/// operations; if omitted then a default synchronization context is used.</param>
public JSValueScope(
JSValueScopeType scopeType,
napi_env env,
JSRuntime? runtime,
JSSynchronizationContext? synchronizationContext = null)
{
ScopeType = scopeType;
if (scopeType == JSValueScopeType.NoContext)
{
// A NoContext scope can inherit the env from a parent NoContext scope.
_parentScope = s_currentScope;
if (_parentScope != null && _parentScope.ScopeType != JSValueScopeType.NoContext)
{
throw new InvalidOperationException(
"A NoContext scope cannot be created within another type of scope.");
}
if (env.IsNull)
{
env = _parentScope?._env ??
throw new ArgumentNullException(nameof(env), "An environment is required.");
}
runtime ??= _parentScope?.Runtime ??
throw new ArgumentNullException(nameof(runtime), "A runtime is required.");
_env = env;
ThreadId = Environment.CurrentManagedThreadId;
Runtime = runtime;
}
else if (scopeType == JSValueScopeType.Root)
{
_parentScope = s_currentScope;
if (_parentScope != null)
{
if (_parentScope.ScopeType == JSValueScopeType.Root)
{
// When there are multiple instances of the managed host in a process
// (created by separate workers), they do not inherit scope.
_parentScope = null;
}
else
{
throw new InvalidOperationException(
"A Root scope cannot be created within another scope.");
}
}
if (env.IsNull)
{
throw new ArgumentNullException(
nameof(env), "An environment is required for a root scope.");
}
else if (runtime == null)
{
throw new ArgumentNullException(
nameof(runtime), "A runtime is required for a root scope.");
}
_env = env;
ThreadId = Environment.CurrentManagedThreadId;
Runtime = runtime;
}
else
{
_parentScope = s_currentScope;
if (scopeType == JSValueScopeType.Module &&
_parentScope != null && _parentScope.ScopeType == JSValueScopeType.Module)
{
// When there are multiple AOT modules in a process, they do not inherit scope.
_parentScope = null;
}
if (_parentScope == null)
{
// Module scopes may be created without a parent scope (for AOT modules).
if (scopeType != JSValueScopeType.Module)
{
throw new InvalidOperationException(
$"A {scopeType} scope cannot be created without a parent scope.");
}
// AOT module scopes are constructed with an env parameter
// but without a pre-initialized runtime.
_env = env.IsNull ? throw new ArgumentNullException(nameof(env)) : env;
ThreadId = Environment.CurrentManagedThreadId;
Runtime = runtime ?? new NodejsRuntime();
}
else if (_parentScope.IsDisposed)
{
// This should never happen because disposing a scope removes it from
// s_currentScope (which is used to initialize _parentScope above).
throw new InvalidOperationException("Parent scope is disposed.");
}
else if (scopeType == JSValueScopeType.Callback &&
_parentScope.ScopeType != JSValueScopeType.Callback &&
_parentScope.ScopeType != JSValueScopeType.Module &&
_parentScope.ScopeType != JSValueScopeType.Root &&
_parentScope.ScopeType != JSValueScopeType.NoContext)
{
throw new InvalidOperationException(
$"A Callback scope must be created within a Root, Module, or Callback scope. " +
$"Current scope: {scopeType}");
}
else if (!env.IsNull && env != _parentScope._env)
{
throw new ArgumentException(
"Environment must not be provided for a non-root scope.",
nameof(env));
}
else if (runtime != null && runtime != _parentScope.Runtime)
{
throw new ArgumentException(
"Runtime must not be provided for a non-root scope.",
nameof(runtime));
}
else
{
_parentScope.ThrowIfInvalidThreadAccess();
_env = _parentScope._env;
ThreadId = _parentScope.ThreadId;
Runtime = _parentScope.Runtime;
}
if (scopeType == JSValueScopeType.Module)
{
if (_parentScope?.ModuleContext != null)
{
throw new InvalidOperationException("Module scope cannot be nested.");
}
ModuleContext = new JSModuleContext();
}
else
{
ModuleContext = _parentScope!.ModuleContext;
}
}
_scopeHandle = ScopeType switch
{
JSValueScopeType.Handle
=> Runtime.OpenHandleScope(_env, out napi_handle_scope handleScope)
.ThrowIfFailed(handleScope).Handle,
JSValueScopeType.Escapable
=> Runtime.OpenEscapableHandleScope(
_env, out napi_escapable_handle_scope handleScope)
.ThrowIfFailed(handleScope).Handle,
_ => default,
};
JSValueScope? previousScope = s_currentScope;
try
{
s_currentScope = this;
if (scopeType == JSValueScopeType.NoContext)
{
// NoContext scopes do not have a runtime context.
RuntimeContext = null!;
RuntimeContextHandle = default;
}
else if (_parentScope?.RuntimeContext != null)
{
// Nested scopes inherit the runtime context from the parent scope.
RuntimeContext = _parentScope.RuntimeContext;
RuntimeContextHandle = _parentScope.RuntimeContextHandle;
}
else
{
// Unparented scopes initialize a new runtime context.
RuntimeContext = new JSRuntimeContext(env, Runtime, synchronizationContext);
RuntimeContextHandle = (nint)GCHandle.Alloc(RuntimeContext);
}
if (scopeType == JSValueScopeType.Root || scopeType == JSValueScopeType.Callback)
{
_previousSyncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(
RuntimeContext.SynchronizationContext);
}
}
catch (Exception)
{
s_currentScope = previousScope;
throw;
}
}
public void Dispose()
{
if (IsDisposed) return;
IsDisposed = true;
if (ScopeType != JSValueScopeType.NoContext)
{
napi_env env = RuntimeContext.EnvironmentHandle;
switch (ScopeType)
{
case JSValueScopeType.Handle:
Runtime.CloseHandleScope(
env, new napi_handle_scope(_scopeHandle)).ThrowIfFailed();
break;
case JSValueScopeType.Escapable:
Runtime.CloseEscapableHandleScope(
env, new napi_escapable_handle_scope(_scopeHandle)).ThrowIfFailed();
break;
default:
SynchronizationContext.SetSynchronizationContext(_previousSyncContext);
break;
}
}
s_currentScope = _parentScope;
}
public JSValue Escape(JSValue value)
{
if (_parentScope == null)
throw new InvalidOperationException("Parent scope must not be null.");
if (ScopeType != JSValueScopeType.Escapable)
throw new InvalidOperationException(
"It can be called only for Escapable value scopes.");
Runtime.EscapeHandle(
(napi_env)this,
new napi_escapable_handle_scope(_scopeHandle),
(napi_value)value,
out napi_value result);
return new JSValue(result, _parentScope);
}
/// <summary>
/// Checks that this scope has not been closed (disposed).
/// </summary>
/// <exception cref="JSValueScopeClosedException">The scope is closed.</exception>
internal void ThrowIfDisposed()
{
if (IsDisposed)
{
throw new JSValueScopeClosedException(scope: this);
}
}
/// <summary>
/// Checks that the current thread is the thread that is running the JavaScript environment
/// that this scope is in.
/// </summary>
/// <exception cref="JSInvalidThreadAccessException">The scope cannot be accessed from the current
/// thread.</exception>
internal void ThrowIfInvalidThreadAccess()
{
if (s_currentScope?._env != _env)
{
throw new JSInvalidThreadAccessException(currentScope: s_currentScope, targetScope: this);
}
}
}