Permalink
Browse files

net45 support. (#530)

  • Loading branch information...
ViceIce authored and sebastienros committed Sep 10, 2018
1 parent 6353bb4 commit 7dbdd67c53b1d27bbc6bfa0e42bcd086074a4e6a
@@ -1,13 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFrameworks>netcoreapp2.1;net452</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Runtime\Scripts\*.*;Parser\Scripts\*.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Jint\Jint.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" Condition=" '$(TargetFramework)' == 'net452' " />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
@@ -16,4 +19,4 @@
<PackageReference Include="xunit.runner.console" Version="2.4.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
</Project>
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
@@ -33,7 +34,7 @@ public ReplaceCultureAttribute(string currentCulture, string currentUICulture)
UICulture = new CultureInfo(currentUICulture);
}
#if NET451
#if NET451 || NET452
/// <summary>
/// The <see cref="Thread.CurrentCulture"/> for the test. Defaults to en-GB.
/// </summary>
@@ -54,7 +55,7 @@ public ReplaceCultureAttribute(string currentCulture, string currentUICulture)
#endif
public CultureInfo Culture { get; }
#if NET451
#if NET451 || NET452
/// <summary>
/// The <see cref="Thread.CurrentUICulture"/> for the test. Defaults to en-US.
/// </summary>
@@ -70,7 +71,7 @@ public override void Before(MethodInfo methodUnderTest)
_originalCulture = CultureInfo.CurrentCulture;
_originalUICulture = CultureInfo.CurrentUICulture;
#if NET451
#if NET451 || NET452
Thread.CurrentThread.CurrentCulture = Culture;
Thread.CurrentThread.CurrentUICulture = UICulture;
#else
@@ -82,7 +83,7 @@ public override void Before(MethodInfo methodUnderTest)
public override void After(MethodInfo methodUnderTest)
{
#if NET451
#if NET451 || NET452
Thread.CurrentThread.CurrentCulture = _originalCulture;
Thread.CurrentThread.CurrentUICulture = _originalUICulture;
#else
@@ -647,13 +647,23 @@ public void ShouldThrowStatementCountOverflow()
);
}
#if NET452
[Fact]
public void ShouldThrowMemoryLimitExceeded()
{
Assert.Throws<PlatformNotSupportedException>(
() => new Engine(cfg => cfg.LimitMemory(2048)).Execute("a=[]; while(true){ a.push(0); }")
);
}
#else
[Fact]
public void ShouldThrowMemoryLimitExceeded()
{
Assert.Throws<MemoryLimitExceededException>(
() => new Engine(cfg => cfg.LimitMemory(2048)).Execute("a=[]; while(true){ a.push(0); }")
);
}
#endif
[Fact]
public void ShouldThrowTimeout()
View
@@ -0,0 +1,18 @@
namespace System
{
internal static class ArrayExt
{
private static class EmptyArray<T>
{
public static readonly T[] Value;
static EmptyArray()
{
EmptyArray<T>.Value = new T[0];
}
}
public static T[] Empty<T>() => EmptyArray<T>.Value;
}
}
View
@@ -86,8 +86,42 @@ public sealed class Engine
{ typeof(System.Text.RegularExpressions.Regex), (Engine engine, object v) => engine.RegExp.Construct((System.Text.RegularExpressions.Regex)v, "") }
};
internal readonly Dictionary<(Type, string), Func<Engine, object, PropertyDescriptor>> ClrPropertyDescriptorFactories =
new Dictionary<(Type, string), Func<Engine, object, PropertyDescriptor>>();
internal struct ClrPropertyDescriptorFactoriesKey : IEquatable<ClrPropertyDescriptorFactoriesKey>
{
public ClrPropertyDescriptorFactoriesKey(Type type, string propertyName)
{
Type = type;
PropertyName = propertyName;
}
internal readonly Type Type;
internal readonly string PropertyName;
public bool Equals(ClrPropertyDescriptorFactoriesKey other)
{
return Type == other.Type && PropertyName == other.PropertyName;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is ClrPropertyDescriptorFactoriesKey other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return (Type.GetHashCode() * 397) ^ PropertyName.GetHashCode();
}
}
}
internal readonly Dictionary<ClrPropertyDescriptorFactoriesKey, Func<Engine, object, PropertyDescriptor>> ClrPropertyDescriptorFactories =
new Dictionary<ClrPropertyDescriptorFactoriesKey, Func<Engine, object, PropertyDescriptor>>();
internal JintCallStack CallStack = new JintCallStack();
@@ -209,7 +243,7 @@ public Engine(Action<Options> options)
_argumentsInstancePool = new ArgumentsInstancePool(this);
_jsValueArrayPool = new JsValueArrayPool();
Eval = new EvalFunctionInstance(this, System.Array.Empty<string>(), LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
Eval = new EvalFunctionInstance(this, System.ArrayExt.Empty<string>(), LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
Global.FastAddProperty("eval", Eval, true, false, true);
_statements = new StatementInterpreter(this);
View
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NeutralLanguage>en-US</NeutralLanguage>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<AssemblyOriginatorKeyFile>Jint.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Esprima" Version="1.0.0-beta-1036" />
<PackageReference Include="Esprima" Version="1.0.0-beta-1049" />
</ItemGroup>
</Project>
</Project>
@@ -106,12 +106,12 @@ public ArrayInstance Construct(int capacity)
{
ExceptionHelper.ThrowArgumentException("invalid array length", nameof(capacity));
}
return Construct(System.Array.Empty<JsValue>(), (uint) capacity);
return Construct(System.ArrayExt.Empty<JsValue>(), (uint) capacity);
}
public ArrayInstance Construct(uint capacity)
{
return Construct(System.Array.Empty<JsValue>(), capacity);
return Construct(System.ArrayExt.Empty<JsValue>(), capacity);
}
public ArrayInstance Construct(JsValue[] arguments, uint capacity)
@@ -26,7 +26,7 @@ public ArrayInstance(Engine engine, uint capacity = 0) : base(engine, objectClas
{
if (capacity < MaxDenseArrayLength)
{
_dense = capacity > 0 ? new PropertyDescriptor[capacity] : System.Array.Empty<PropertyDescriptor>();
_dense = capacity > 0 ? new PropertyDescriptor[capacity] : System.ArrayExt.Empty<PropertyDescriptor>();
}
else
{
@@ -39,7 +39,7 @@ public ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, o
int length = 0;
if (items == null || items.Length == 0)
{
_dense = System.Array.Empty<PropertyDescriptor>();
_dense = System.ArrayExt.Empty<PropertyDescriptor>();
length = 0;
}
else
@@ -469,7 +469,7 @@ private JsValue Splice(JsValue thisObj, JsValue[] arguments)
}
a.SetLength(actualDeleteCount);
var items = System.Array.Empty<JsValue>();
var items = System.ArrayExt.Empty<JsValue>();
if (arguments.Length > 2)
{
items = new JsValue[arguments.Length - 2];
@@ -7,7 +7,7 @@ namespace Jint.Native.Function
public sealed class BindFunctionInstance : FunctionInstance, IConstructor
{
public BindFunctionInstance(Engine engine)
: base(engine, "bind", System.Array.Empty<string>(), null, false)
: base(engine, "bind", System.ArrayExt.Empty<string>(), null, false)
{
}
@@ -66,7 +66,7 @@ private static string[] GetParameterNames(IFunction functionDeclaration)
if (count == 0)
{
return System.Array.Empty<string>();
return System.ArrayExt.Empty<string>();
}
var names = new string[count];
@@ -6,7 +6,7 @@ namespace Jint.Native.Function
public sealed class ThrowTypeError : FunctionInstance
{
public ThrowTypeError(Engine engine)
: base(engine, "throwTypeError", System.Array.Empty<string>(), engine.GlobalEnvironment, false)
: base(engine, "throwTypeError", System.ArrayExt.Empty<string>(), engine.GlobalEnvironment, false)
{
DefineOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden), false);
Extensible = false;
@@ -446,7 +446,7 @@ private string Encode(string uriString, HashSet<char> unescapedUriSet)
v = (c - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000;
}
byte[] octets = System.Array.Empty<byte>();
byte[] octets = System.ArrayExt.Empty<byte>();
if (v >= 0 && v <= 0x007F)
{
@@ -231,14 +231,14 @@ public ObjectWrapper(ObjectInstance target)
public ObjectInstance Next()
{
return (ObjectInstance) _callable.Call(_target, System.Array.Empty<JsValue>());
return (ObjectInstance) _callable.Call(_target, Arguments.Empty);
}
public void Return()
{
if (_target.TryGetValue("return", out var func))
{
((ICallable) func).Call(_target, System.Array.Empty<JsValue>());
((ICallable) func).Call(_target, Arguments.Empty);
}
}
}
@@ -50,8 +50,19 @@ internal CachedPower(ulong significand, short binaryExponent, short decimalExpon
}
}
internal readonly struct GetCachedPowerResult
{
public GetCachedPowerResult(short decimalExponent, DiyFp cMk)
{
this.decimalExponent = decimalExponent;
this.cMk = cMk;
}
internal readonly short decimalExponent;
internal readonly DiyFp cMk;
}
internal static (short, DiyFp) GetCachedPower(int e, int alpha, int gamma)
internal static GetCachedPowerResult GetCachedPower(int e, int alpha, int gamma)
{
const int kQ = DiyFp.KSignificandSize;
double k = System.Math.Ceiling((alpha - e + kQ - 1) * Kd1Log210);
@@ -60,7 +71,7 @@ internal static (short, DiyFp) GetCachedPower(int e, int alpha, int gamma)
var cMk = new DiyFp(cachedPower.Significand, cachedPower.BinaryExponent);
Debug.Assert((alpha <= cMk.E + e) && (cMk.E + e <= gamma));
return (cachedPower.DecimalExponent, cMk);
return new GetCachedPowerResult(cachedPower.DecimalExponent, cMk);
}
// Code below is converted from GRISU_CACHE_NAME(8) in file "powers-ten.h"
@@ -101,10 +101,22 @@ private static bool IsSpecial(long d64)
return (d64 & KExponentMask) == KExponentMask;
}
internal readonly struct NormalizedBoundariesResult
{
public NormalizedBoundariesResult(DiyFp minus, DiyFp plus)
{
Minus = minus;
Plus = plus;
}
internal readonly DiyFp Minus;
internal readonly DiyFp Plus;
}
// Returns the two boundaries of first argument.
// The bigger boundary (m_plus) is normalized. The lower boundary has the same
// exponent as m_plus.
internal static (DiyFp, DiyFp) NormalizedBoundaries(long d64)
internal static NormalizedBoundariesResult NormalizedBoundaries(long d64)
{
DiyFp v = AsDiyFp(d64);
bool significandIsZero = (v.F == KHiddenBit);
@@ -125,7 +137,7 @@ internal static (DiyFp, DiyFp) NormalizedBoundaries(long d64)
mMinus = new DiyFp((v.F << 1) - 1, v.E - 1);
}
mMinus = new DiyFp(mMinus.F << (mMinus.E - mPlus.E), mPlus.E);
return (mMinus, mPlus);
return new NormalizedBoundariesResult(mMinus, mPlus);
}
private const int KSignificandSize = 52; // Excludes the hidden bit.
@@ -470,13 +470,19 @@ private static bool Grisu3(double v, FastDtoaBuilder buffer)
// closest floating-point neighbors. Any number strictly between
// boundary_minus and boundary_plus will round to v when convert to a double.
// Grisu3 will never output representations that lie exactly on a boundary.
(DiyFp boundaryMinus, DiyFp boundaryPlus) = DoubleHelper.NormalizedBoundaries(bits);
var boundaries = DoubleHelper.NormalizedBoundaries(bits);
var boundaryMinus = boundaries.Minus;
var boundaryPlus = boundaries.Plus;
Debug.Assert(boundaryPlus.E == w.E);
var (mk, tenMk) = CachedPowers.GetCachedPower(
var result = CachedPowers.GetCachedPower(
w.E + DiyFp.KSignificandSize,
MinimalTargetExponent, MaximalTargetExponent);
var mk = result.decimalExponent;
var tenMk = result.cMk;
Debug.Assert(MinimalTargetExponent <= w.E + tenMk.E +
DiyFp.KSignificandSize &&
MaximalTargetExponent >= w.E + tenMk.E +
@@ -41,7 +41,7 @@ public JsValue[] RentArray(int size)
{
if (size == 0)
{
return Array.Empty<JsValue>();
return ArrayExt.Empty<JsValue>();
}
if (size == 1)
{
@@ -6,7 +6,7 @@ namespace Jint.Runtime
{
public static class Arguments
{
public static readonly JsValue[] Empty = Array.Empty<JsValue>();
public static readonly JsValue[] Empty = ArrayExt.Empty<JsValue>();
public static JsValue[] From(params JsValue[] o)
{
@@ -38,7 +38,7 @@ public static JsValue[] Skip(this JsValue[] args, int count)
var newLength = args.Length - count;
if (newLength <= 0)
{
return Array.Empty<JsValue>();
return ArrayExt.Empty<JsValue>();
}
var array = new JsValue[newLength];
@@ -188,7 +188,7 @@ public override string[] GetAllBindingNames()
size += _dictionary.Count;
}
var keys = size > 0 ? new string[size] : Array.Empty<string>();
var keys = size > 0 ? new string[size] : ArrayExt.Empty<string>();
int n = 0;
if (_set)
{
Oops, something went wrong.

0 comments on commit 7dbdd67

Please sign in to comment.