| | | 1 | | using Elsa.Expressions.JavaScript.Helpers; |
| | | 2 | | using Elsa.Expressions.JavaScript.Options; |
| | | 3 | | using Jint; |
| | | 4 | | using Jint.Runtime.Interop; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Extends <see cref="Engine"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class EngineExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Register the specified type <c>T</c> with the engine. |
| | | 17 | | /// </summary> |
| | 66 | 18 | | public static void RegisterType<T>(this Engine engine) => engine.RegisterType(typeof(T)); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Register the specified type with the engine, under its type name. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// <para> |
| | | 25 | | /// Types whose name cannot be written as a JavaScript identifier are skipped. A constructed generic type such |
| | | 26 | | /// as <c>IDictionary<string, object></c> is named <c>IDictionary`2</c> and an array type such as |
| | | 27 | | /// <c>byte[]</c> is named <c>Byte[]</c>. Registered, those would only be reachable through bracket notation — |
| | | 28 | | /// <c>globalThis['Byte[]']</c> — and every constructed generic type of the same arity would claim the same |
| | | 29 | | /// global, so the last one registered would silently win. |
| | | 30 | | /// </para> |
| | | 31 | | /// <para> |
| | | 32 | | /// A name that is already taken is left alone. Registering the same type twice is therefore a no-op, and a |
| | | 33 | | /// global installed before this method is called is never replaced. Built-in type globals are registered |
| | | 34 | | /// lazily through <see cref="EngineOptionsExtensions.RegisterType(Jint.Options, Type)"/> while the engine is |
| | | 35 | | /// being constructed, so host callbacks that run afterwards can replace them deliberately. |
| | | 36 | | /// </para> |
| | | 37 | | /// </remarks> |
| | | 38 | | public static void RegisterType(this Engine engine, Type type) |
| | | 39 | | { |
| | 66 | 40 | | var name = type.Name; |
| | | 41 | | |
| | 66 | 42 | | if (!IsUsableAsIdentifier(name)) |
| | 0 | 43 | | return; |
| | | 44 | | |
| | | 45 | | // Leave any value that the host or JavaScript already installed under this name untouched. |
| | 66 | 46 | | if (engine.Global.HasOwnProperty(name)) |
| | 43 | 47 | | return; |
| | | 48 | | |
| | 23 | 49 | | engine.SetValue(name, TypeReference.CreateTypeReference(engine, type)); |
| | 23 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static bool IsUsableAsIdentifier(string name) |
| | | 53 | | { |
| | 66 | 54 | | if (string.IsNullOrEmpty(name) || (!char.IsLetter(name[0]) && name[0] != '_' && name[0] != '$')) |
| | 0 | 55 | | return false; |
| | | 56 | | |
| | 2040 | 57 | | foreach (var c in name) |
| | | 58 | | { |
| | 954 | 59 | | if (!char.IsLetterOrDigit(c) && c != '_' && c != '$') |
| | 0 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | 66 | 63 | | return true; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal static void SyncVariablesContainer(this Engine engine, IOptions<JintOptions> options, string name, object? |
| | | 67 | | { |
| | 6 | 68 | | if (options.Value.DisableWrappers || options.Value.DisableVariableCopying) |
| | 0 | 69 | | return; |
| | | 70 | | |
| | | 71 | | // To ensure both variable accessor syntaxes work, we need to update the variables container in the engine as we |
| | 6 | 72 | | var variablesContainer = (IDictionary<string, object?>)engine.GetValue("variables").ToObject()!; |
| | 6 | 73 | | variablesContainer[name] = ObjectConverterHelper.ProcessVariableValue(engine, value); |
| | 6 | 74 | | engine.SetValue("variables", variablesContainer); |
| | 6 | 75 | | } |
| | | 76 | | } |