Skip to content

Commit

Permalink
Jint changes backported.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 1, 2020
1 parent 8d207ee commit 68b5b41
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
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;
Expand All @@ -24,11 +27,11 @@ public sealed class ContentDataObject : ObjectInstance
private Dictionary<string, PropertyDescriptor> fieldProperties;
private bool isChanged;

public override bool Extensible => true;

public ContentDataObject(Engine engine, NamedContentData contentData)
: base(engine)
{
Extensible = true;

this.contentData = contentData;
}

Expand Down Expand Up @@ -68,23 +71,27 @@ public bool TryUpdate(out NamedContentData result)
return isChanged;
}

public override void RemoveOwnProperty(string propertyName)
public override void RemoveOwnProperty(JsValue property)
{
if (fieldsToDelete == null)
{
fieldsToDelete = new HashSet<string>();
}

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 };
Expand All @@ -93,25 +100,43 @@ public override bool DefineOwnProperty(string propertyName, PropertyDescriptor d
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();

if (propertyName.Equals("toJSON", StringComparison.OrdinalIgnoreCase))
{
return PropertyDescriptor.Undefined;
}

return fieldProperties.GetOrAdd(propertyName, this, (k, c) => new ContentDataProperty(c, new ContentFieldObject(c, new ContentFieldData(), false)));
}

public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
{
EnsurePropertiesInitialized();

return fieldProperties.Select(x => new KeyValuePair<JsValue, PropertyDescriptor>(x.Key, x.Value));
}

public override List<JsValue> GetOwnPropertyKeys(Types types = Types.String | Types.Symbol)
{
EnsurePropertiesInitialized();

return fieldProperties;
return fieldProperties.Keys.Select(x => (JsValue)x).ToList();
}

private void EnsurePropertiesInitialized()
Expand All @@ -127,4 +152,4 @@ private void EnsurePropertiesInitialized()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override JsValue CustomValue

foreach (var (key, propertyDescriptor) in obj.GetOwnProperties())
{
contentField.Put(key, propertyDescriptor.Value, true);
contentField.Set(key, propertyDescriptor.Value);
}

this.value = contentField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
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;
Expand All @@ -19,7 +24,7 @@ public sealed class ContentFieldObject : ObjectInstance
{
private readonly ContentDataObject contentData;
private readonly ContentFieldData? fieldData;
private HashSet<string> valuesToDelete;
private HashSet<string>? valuesToDelete;
private Dictionary<string, PropertyDescriptor> valueProperties;
private bool isChanged;

Expand All @@ -28,12 +33,13 @@ public ContentFieldData? FieldData
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)
Expand Down Expand Up @@ -80,23 +86,35 @@ public bool TryUpdate(out ContentFieldData? result)
return isChanged;
}

public override void RemoveOwnProperty(string propertyName)
public override void RemoveOwnProperty(JsValue property)
{
if (valuesToDelete == null)
{
valuesToDelete = new HashSet<string>();
}
var propertyName = property.AsString();

valuesToDelete ??= new HashSet<string>();
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 };
Expand All @@ -105,18 +123,32 @@ public override bool DefineOwnProperty(string propertyName, PropertyDescriptor d
return true;
}

public override PropertyDescriptor GetOwnProperty(string propertyName)
public override PropertyDescriptor GetOwnProperty(JsValue property)
{
EnsurePropertiesInitialized();

var propertyName = property.AsString();

if (propertyName.Equals("toJSON", StringComparison.OrdinalIgnoreCase))
{
return PropertyDescriptor.Undefined;
}

return valueProperties?.GetOrDefault(propertyName) ?? PropertyDescriptor.Undefined;
}

public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
{
EnsurePropertiesInitialized();

return valueProperties.Select(x => new KeyValuePair<JsValue, PropertyDescriptor>(x.Key, x.Value));
}

public override List<JsValue> GetOwnPropertyKeys(Types types = Types.String | Types.Symbol)
{
EnsurePropertiesInitialized();

return valueProperties;
return valueProperties.Keys.Select(x => (JsValue)x).ToList();
}

private void EnsurePropertiesInitialized()
Expand All @@ -135,4 +167,4 @@ private void EnsurePropertiesInitialized()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ private static JsValue FromObject(JsonObject obj, Engine engine)
target.FastAddProperty(key, Map(value, engine), false, true, true);
}

target.PreventExtensions();

return target;
}

Expand Down Expand Up @@ -103,7 +105,7 @@ public static IJsonValue Map(JsValue? value)

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())));
}
Expand All @@ -119,7 +121,7 @@ public static IJsonValue Map(JsValue? value)

foreach (var (key, propertyDescriptor) in obj.GetOwnProperties())
{
result[key] = Map(propertyDescriptor.Value);
result[key.AsString()] = Map(propertyDescriptor.Value);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,16 +52,19 @@ private async Task GetJsonAsync(ExecutionContext context, string url, Action<JsV
{
using (var httpClient = httpClientFactory.CreateClient())
{
var request = CreateRequest(url, headers);
var response = await httpClient.SendAsync(request, context.CancellationToken);

response.EnsureSuccessStatusCode();
using (var request = CreateRequest(url, headers))
{
using (var response = await httpClient.SendAsync(request, context.CancellationToken))
{
response.EnsureSuccessStatusCode();

var responseObject = await ParseResponse(context, response);
var responseObject = await ParseResponse(context, response);

context.Engine.ResetTimeoutTicks();
context.Engine.ResetConstraints();

callback(responseObject);
callback(responseObject);
}
}
}
}
catch (Exception ex)
Expand All @@ -86,17 +90,19 @@ private static HttpRequestMessage CreateRequest(string url, JsValue? headers)
{
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);
}
}
}

return request;
}

private async Task<JsValue> ParseResponse(ExecutionContext context, HttpResponseMessage response)
private static async Task<JsValue> ParseResponse(ExecutionContext context, HttpResponseMessage response)
{
var responseString = await response.Content.ReadAsStringAsync();

Expand All @@ -110,4 +116,4 @@ private async Task<JsValue> ParseResponse(ExecutionContext context, HttpResponse
return jsonValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ internal sealed class Parser
private static readonly TimeSpan Expiration = TimeSpan.FromMinutes(10);
private static readonly ParserOptions DefaultParserOptions = new ParserOptions
{
AdaptRegexp = true, Tolerant = true, Loc = true
AdaptRegexp = true,
Tolerant = true,
Loc = true
};

private readonly IMemoryCache memoryCache;
Expand All @@ -30,25 +32,25 @@ public Parser(IMemoryCache memoryCache)
this.memoryCache = memoryCache;
}

public Program Parse(string script)
public Script Parse(string script)
{
var key = Key(script);

if (!memoryCache.TryGetValue<Program>(key, out var program))
if (!memoryCache.TryGetValue<Script>(key, out var compiledScript))
{
var parser = new JavaScriptParser(script, DefaultParserOptions);

program = parser.ParseProgram();
compiledScript = parser.ParseScript();

memoryCache.Set(key, program, Expiration);
memoryCache.Set(key, compiledScript, Expiration);
}

return program;
return compiledScript;
}

private static string Key(string script)
{
return $"SCRIPT_{script}";
}
}
}
}
Loading

0 comments on commit 68b5b41

Please sign in to comment.