|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading; |
| 6 | +using Microsoft.JavaScript.NodeApi.Interop; |
| 7 | + |
| 8 | +namespace Microsoft.JavaScript.NodeApi; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// An exception that was caused by an attempt to access a JavaScript value without any |
| 12 | +/// <see cref="JSValueScope" /> established on the current thread, or from a thread associated |
| 13 | +/// with a different environment / root scope. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// All JavaScript values are created within a scope that is bound to the thread that runs the |
| 17 | +/// JS environment. They can only be accessed from the same thread and only as long as the scope |
| 18 | +/// is still valid (not disposed). |
| 19 | +/// </remarks> |
| 20 | +/// <seealso cref="JSSynchronizationContext"/> |
| 21 | +public class JSInvalidThreadAccessException : InvalidOperationException |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Creates a new instance of <see cref="JSInvalidThreadAccessException" /> with a |
| 25 | + /// current scope and message. |
| 26 | + /// </summary> |
| 27 | + public JSInvalidThreadAccessException( |
| 28 | + JSValueScope? currentScope, |
| 29 | + string? message = null) |
| 30 | + : this(currentScope, targetScope: null, message) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Creates a new instance of <see cref="JSInvalidThreadAccessException" /> with current |
| 36 | + /// and target scopes and a message. |
| 37 | + /// </summary> |
| 38 | + public JSInvalidThreadAccessException( |
| 39 | + JSValueScope? currentScope, |
| 40 | + JSValueScope? targetScope, |
| 41 | + string? message = null) |
| 42 | + : base(message ?? GetMessage(currentScope, targetScope)) |
| 43 | + { |
| 44 | + CurrentScope = currentScope; |
| 45 | + TargetScope = targetScope; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the scope associated with the current thread (<see cref="JSValueScope.Current" />) |
| 50 | + /// when the exception was thrown, or null if there was no scope for the thread. |
| 51 | + /// </summary> |
| 52 | + public JSValueScope? CurrentScope { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the scope of the value (<see cref="JSValue.Scope" />) that was being accessed when |
| 56 | + /// the exception was thrown, or null if a static operation was attempted. |
| 57 | + /// </summary> |
| 58 | + public JSValueScope? TargetScope { get; } |
| 59 | + |
| 60 | + private static string GetMessage(JSValueScope? currentScope, JSValueScope? targetScope) |
| 61 | + { |
| 62 | + int threadId = Environment.CurrentManagedThreadId; |
| 63 | + string? threadName = Thread.CurrentThread.Name; |
| 64 | + string threadDescription = string.IsNullOrEmpty(threadName) ? |
| 65 | + $"#{threadId}" : $"#{threadId} \"{threadName}\""; |
| 66 | + |
| 67 | + if (targetScope == null) |
| 68 | + { |
| 69 | + // If the target scope is null, then this was an attempt to access either a static |
| 70 | + // operation or a JS reference (which has an environment but no scope). |
| 71 | + if (currentScope != null) |
| 72 | + { |
| 73 | + // In that case if the current scope is NOT null this exception |
| 74 | + // shouldn't be thrown. |
| 75 | + throw new ArgumentException("Current scope must be null if target scope is null."); |
| 76 | + } |
| 77 | + |
| 78 | + return $"There is no active JS value scope.\nCurrent thread: {threadDescription}. " + |
| 79 | + $"Consider using the synchronization context to switch to the JS thread."; |
| 80 | + } |
| 81 | + |
| 82 | + return "The JS value scope cannot be accessed from the current thread.\n" + |
| 83 | + $"The scope of type {targetScope.ScopeType} was created on thread" + |
| 84 | + $"#{targetScope.ThreadId} and is being accessed from {threadDescription}. " + |
| 85 | + $"Consider using the synchronization context to switch to the JS thread."; |
| 86 | + } |
| 87 | +} |
0 commit comments