From 78836f2400350486f0b6b5f33d02f501379ae8d1 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 12 Aug 2020 16:21:45 +0200 Subject: [PATCH] Jint updated and tests fixed --- .../ContentWrapper/ContentDataObject.cs | 35 +++++++++++---- .../ContentWrapper/ContentDataProperty.cs | 2 +- .../ContentWrapper/ContentFieldObject.cs | 43 ++++++++++++++++--- .../Scripting/ContentWrapper/JsonMapper.cs | 6 ++- .../Scripting/Extensions/HttpJintExtension.cs | 9 ++-- .../Scripting/Internal/Parser.cs | 10 ++--- ...Squidex.Domain.Apps.Core.Operations.csproj | 2 +- .../CreateIdentityV2CommandMiddleware.cs | 1 - .../Squidex.Domain.Apps.Entities.csproj | 1 - .../Validators/NoValueValidatorTests.cs | 3 +- .../RequiredStringValidatorTests.cs | 3 +- 11 files changed, 84 insertions(+), 31 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataObject.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataObject.cs index 0945fd37b..f57eaca91 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataObject.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataObject.cs @@ -6,9 +6,11 @@ // ========================================================================== using System.Collections.Generic; +using System.Linq; using Jint; using Jint.Native; using Jint.Native.Object; +using Jint.Runtime; using Jint.Runtime.Descriptors; using Squidex.Domain.Apps.Core.Contents; using Squidex.Infrastructure; @@ -24,11 +26,11 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper private Dictionary fieldProperties; private bool isChanged; + public override bool Extensible => true; + public ContentDataObject(Engine engine, NamedContentData contentData) : base(engine) { - Extensible = true; - this.contentData = contentData; } @@ -68,23 +70,27 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper return isChanged; } - public override void RemoveOwnProperty(string propertyName) + public override void RemoveOwnProperty(JsValue property) { if (fieldsToDelete == null) { fieldsToDelete = new HashSet(); } + var propertyName = property.AsString(); + fieldsToDelete.Add(propertyName); fieldProperties?.Remove(propertyName); MarkChanged(); } - public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError) + public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc) { EnsurePropertiesInitialized(); + var propertyName = property.AsString(); + if (!fieldProperties.ContainsKey(propertyName)) { fieldProperties[propertyName] = new ContentDataProperty(this) { Value = desc.Value }; @@ -93,25 +99,38 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper return true; } - public override void Put(string propertyName, JsValue value, bool throwOnError) + public override bool Set(JsValue property, JsValue value, JsValue receiver) { EnsurePropertiesInitialized(); + var propertyName = property.AsString(); + fieldProperties.GetOrAdd(propertyName, this, (k, c) => new ContentDataProperty(c)).Value = value; + + return true; } - public override PropertyDescriptor GetOwnProperty(string propertyName) + public override PropertyDescriptor GetOwnProperty(JsValue property) { EnsurePropertiesInitialized(); + var propertyName = property.AsString(); + return fieldProperties.GetOrAdd(propertyName, this, (k, c) => new ContentDataProperty(c, new ContentFieldObject(c, new ContentFieldData(), false))); } - public override IEnumerable> GetOwnProperties() + public override IEnumerable> GetOwnProperties() + { + EnsurePropertiesInitialized(); + + return fieldProperties.Select(x => new KeyValuePair(x.Key, x.Value)); + } + + public override List GetOwnPropertyKeys(Types types = Types.String | Types.Symbol) { EnsurePropertiesInitialized(); - return fieldProperties; + return fieldProperties.Keys.Select(x => (JsValue)x).ToList(); } private void EnsurePropertiesInitialized() diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataProperty.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataProperty.cs index c7bf8455f..34b6ed511 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataProperty.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentDataProperty.cs @@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper foreach (var (key, propertyDescriptor) in obj.GetOwnProperties()) { - contentField.Put(key, propertyDescriptor.Value, true); + contentField.Set(key, propertyDescriptor.Value); } this.value = contentField; diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldObject.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldObject.cs index 9f86e3b02..f69a49fd9 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldObject.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldObject.cs @@ -6,7 +6,11 @@ // ========================================================================== using System.Collections.Generic; +using System.Linq; +using Jint; +using Jint.Native; using Jint.Native.Object; +using Jint.Runtime; using Jint.Runtime.Descriptors; using Squidex.Domain.Apps.Core.Contents; using Squidex.Infrastructure; @@ -28,12 +32,13 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper get { return fieldData; } } + public override bool Extensible => true; + public ContentFieldObject(ContentDataObject contentData, ContentFieldData? fieldData, bool isNew) : base(contentData.Engine) { - Extensible = true; - this.contentData = contentData; + this.fieldData = fieldData; if (isNew) @@ -80,23 +85,38 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper return isChanged; } - public override void RemoveOwnProperty(string propertyName) + public override void RemoveOwnProperty(JsValue property) { if (valuesToDelete == null) { valuesToDelete = new HashSet(); } + var propertyName = property.AsString(); + valuesToDelete.Add(propertyName); valueProperties?.Remove(propertyName); MarkChanged(); } - public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError) + public override bool Set(JsValue property, JsValue value, JsValue receiver) { EnsurePropertiesInitialized(); + var propertyName = property.AsString(); + + valueProperties.GetOrAdd(propertyName, k => new ContentFieldProperty(this)).Value = value; + + return true; + } + + public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc) + { + EnsurePropertiesInitialized(); + + var propertyName = property.AsString(); + if (!valueProperties.ContainsKey(propertyName)) { valueProperties[propertyName] = new ContentFieldProperty(this) { Value = desc.Value }; @@ -105,18 +125,27 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper return true; } - public override PropertyDescriptor GetOwnProperty(string propertyName) + public override PropertyDescriptor GetOwnProperty(JsValue property) { EnsurePropertiesInitialized(); + var propertyName = property.AsString(); + return valueProperties?.GetOrDefault(propertyName) ?? PropertyDescriptor.Undefined; } - public override IEnumerable> GetOwnProperties() + public override IEnumerable> GetOwnProperties() + { + EnsurePropertiesInitialized(); + + return valueProperties.Select(x => new KeyValuePair(x.Key, x.Value)); + } + + public override List GetOwnPropertyKeys(Types types = Types.String | Types.Symbol) { EnsurePropertiesInitialized(); - return valueProperties; + return valueProperties.Keys.Select(x => (JsValue)x).ToList(); } private void EnsurePropertiesInitialized() diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs index 791fa1a4f..db62a685a 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs @@ -62,6 +62,8 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper target.FastAddProperty(key, Map(value, engine), false, true, true); } + target.PreventExtensions(); + return target; } @@ -103,7 +105,7 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper var result = JsonValue.Array(); - for (var i = 0; i < arr.GetLength(); i++) + for (var i = 0; i < arr.Length; i++) { result.Add(Map(arr.Get(i.ToString()))); } @@ -119,7 +121,7 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper foreach (var (key, propertyDescriptor) in obj.GetOwnProperties()) { - result[key] = Map(propertyDescriptor.Value); + result[key.AsString()] = Map(propertyDescriptor.Value); } return result; diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Extensions/HttpJintExtension.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Extensions/HttpJintExtension.cs index ca9f9d16b..8aede99ac 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Extensions/HttpJintExtension.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Extensions/HttpJintExtension.cs @@ -8,6 +8,7 @@ using System; using System.Net.Http; using System.Threading.Tasks; +using Jint; using Jint.Native; using Jint.Native.Json; using Jint.Runtime; @@ -58,7 +59,7 @@ namespace Squidex.Domain.Apps.Core.Scripting.Extensions var responseObject = await ParseResponse(context, response); - context.Engine.ResetTimeoutTicks(); + context.Engine.ResetConstraints(); callback(responseObject); } @@ -86,9 +87,11 @@ namespace Squidex.Domain.Apps.Core.Scripting.Extensions { var value = TypeConverter.ToString(property.Value); - if (!string.IsNullOrWhiteSpace(key)) + var keyString = key.AsString(); + + if (!string.IsNullOrWhiteSpace(keyString)) { - request.Headers.TryAddWithoutValidation(key, value ?? string.Empty); + request.Headers.TryAddWithoutValidation(keyString, value ?? string.Empty); } } } diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs index b647c1fe5..b2d3087f5 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs @@ -30,20 +30,20 @@ namespace Squidex.Domain.Apps.Core.Scripting.Internal this.memoryCache = memoryCache; } - public Program Parse(string script) + public Script Parse(string script) { var key = Key(script); - if (!memoryCache.TryGetValue(key, out var program)) + if (!memoryCache.TryGetValue