From 056156251888896f6c0f29edf42c159763850568 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 5 Apr 2024 19:12:50 +0300 Subject: [PATCH] Upgrade to Jint 3.0.2 Remove obsolete STJ integration helpers --- src/OrchardCore.Build/Dependencies.props | 2 +- .../JavaScriptEngine.cs | 37 +----------- .../JsonValueConverter.cs | 56 ------------------- 3 files changed, 3 insertions(+), 92 deletions(-) delete mode 100644 src/OrchardCore/OrchardCore.Scripting.JavaScript/JsonValueConverter.cs diff --git a/src/OrchardCore.Build/Dependencies.props b/src/OrchardCore.Build/Dependencies.props index e3b7dd14b408..027601a202b7 100644 --- a/src/OrchardCore.Build/Dependencies.props +++ b/src/OrchardCore.Build/Dependencies.props @@ -26,7 +26,7 @@ - + diff --git a/src/OrchardCore/OrchardCore.Scripting.JavaScript/JavaScriptEngine.cs b/src/OrchardCore/OrchardCore.Scripting.JavaScript/JavaScriptEngine.cs index 8dd3421c99f9..5e40ce59e011 100644 --- a/src/OrchardCore/OrchardCore.Scripting.JavaScript/JavaScriptEngine.cs +++ b/src/OrchardCore/OrchardCore.Scripting.JavaScript/JavaScriptEngine.cs @@ -23,40 +23,7 @@ public JavaScriptEngine(IMemoryCache memoryCache) public IScriptingScope CreateScope(IEnumerable methods, IServiceProvider serviceProvider, IFileProvider fileProvider, string basePath) { - var engine = new Engine(options => - { - // Make JsonArray behave like JS array. - options.SetWrapObjectHandler(static (e, target, type) => - { - if (target is JsonArray) - { - var wrapped = new ObjectWrapper(e, target) - { - Prototype = e.Intrinsics.Array.PrototypeObject - }; - return wrapped; - } - - return new ObjectWrapper(e, target); - }); - - options.AddObjectConverter(); - - // We cannot access this[string] with anything else than JsonObject, otherwise itw will throw. - options.SetTypeResolver(new TypeResolver - { - MemberFilter = static info => - { - if (info.ReflectedType != typeof(JsonObject) && info.Name == "Item" && info is PropertyInfo p) - { - var parameters = p.GetIndexParameters(); - return parameters.Length != 1 || parameters[0].ParameterType != typeof(string); - } - - return true; - } - }); - }); + var engine = new Engine(); foreach (var method in methods) { @@ -78,7 +45,7 @@ static void ThrowInvalidScopeTypeException() ThrowInvalidScopeTypeException(); } - var parsedAst = _memoryCache.GetOrCreate(script, static entry => Engine.PrepareScript((string)entry.Key)); + var parsedAst = _memoryCache.GetOrCreate(script, static entry => Engine.PrepareScript((string) entry.Key)); var result = jsScope.Engine.Evaluate(parsedAst).ToObject(); diff --git a/src/OrchardCore/OrchardCore.Scripting.JavaScript/JsonValueConverter.cs b/src/OrchardCore/OrchardCore.Scripting.JavaScript/JsonValueConverter.cs deleted file mode 100644 index 5a59b5726337..000000000000 --- a/src/OrchardCore/OrchardCore.Scripting.JavaScript/JsonValueConverter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Nodes; -using Jint; -using Jint.Native; -using Jint.Runtime.Interop; - -namespace OrchardCore.Scripting.JavaScript; -public class JsonValueConverter : IObjectConverter -{ - public bool TryConvert(Engine engine, object value, out JsValue result) - { - if (value is JsonValue jsonValue) - { - var valueKind = jsonValue.GetValueKind(); - switch (valueKind) - { - case JsonValueKind.Object: - case JsonValueKind.Array: - result = JsValue.FromObject(engine, jsonValue); - break; - case JsonValueKind.String: - result = jsonValue.ToString(); - break; - case JsonValueKind.Number: - if (jsonValue.TryGetValue(out var doubleValue)) - { - result = JsNumber.Create(doubleValue); - } - else - { - result = JsValue.Undefined; - } - break; - case JsonValueKind.True: - result = JsBoolean.True; - break; - case JsonValueKind.False: - result = JsBoolean.False; - break; - case JsonValueKind.Undefined: - result = JsValue.Undefined; - break; - case JsonValueKind.Null: - result = JsValue.Null; - break; - default: - result = JsValue.Undefined; - break; - } - return true; - } - result = JsValue.Undefined; - return false; - - } -}