Permalink
Browse files

Make ObjectInstance MruPropertyCache2 dictionary lazy (#454)

  • Loading branch information...
lahma authored and sebastienros committed Jan 2, 2018
1 parent aaf42cc commit 189af9f321507a6dc0a52a886427dc16b7109e8a
View
@@ -160,4 +160,4 @@ project.lock.json
.build
.idea
BenchmarkDotNet.Artifacts
BenchmarkDotNet.Artifacts*
View
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Esprima;
using Esprima.Ast;
using Jint.Native;
@@ -408,7 +407,7 @@ public Completion ExecuteStatement(Statement statement)
return _statements.ExecuteForInStatement(statement.As<ForInStatement>());
case Nodes.FunctionDeclaration:
return new Completion(Completion.Normal, null, null);
return Completion.Empty;
case Nodes.IfStatement:
return _statements.ExecuteIfStatement(statement.As<IfStatement>());
@@ -728,7 +727,12 @@ public JsValue Invoke(JsValue value, object thisObj, object[] arguments)
throw new ArgumentException("Can only invoke functions");
}
return callable.Call(JsValue.FromObject(this, thisObj), arguments.Select(x => JsValue.FromObject(this, x)).ToArray());
var items = new JsValue[arguments.Length];
for (int i = 0; i < arguments.Length; ++i)
{
items[i] = JsValue.FromObject(this, arguments[i]);
}
return callable.Call(JsValue.FromObject(this, thisObj), items);
}
/// <summary>
@@ -766,7 +770,12 @@ public JsValue GetValue(JsValue scope, string propertyName)
}
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.5
public void DeclarationBindingInstantiation(DeclarationBindingType declarationBindingType, IEnumerable<FunctionDeclaration> functionDeclarations, IEnumerable<VariableDeclaration> variableDeclarations, FunctionInstance functionInstance, JsValue[] arguments)
public void DeclarationBindingInstantiation(
DeclarationBindingType declarationBindingType,
IList<FunctionDeclaration> functionDeclarations,
IList<VariableDeclaration> variableDeclarations,
FunctionInstance functionInstance,
JsValue[] arguments)
{
var env = ExecutionContext.VariableEnvironment.Record;
bool configurableBindings = declarationBindingType == DeclarationBindingType.EvalCode;
@@ -776,8 +785,9 @@ public void DeclarationBindingInstantiation(DeclarationBindingType declarationBi
{
var argCount = arguments.Length;
var n = 0;
foreach (var argName in functionInstance.FormalParameters)
for (var i = 0; i < functionInstance.FormalParameters.Length; i++)
{
var argName = functionInstance.FormalParameters[i];
n++;
var v = n > argCount ? Undefined.Instance : arguments[n - 1];
var argAlreadyDeclared = env.HasBinding(argName);
@@ -790,8 +800,9 @@ public void DeclarationBindingInstantiation(DeclarationBindingType declarationBi
}
}
foreach (var f in functionDeclarations)
for (var i = 0; i < functionDeclarations.Count; i++)
{
var f = functionDeclarations[i];
var fn = f.Id.Name;
var fo = Function.CreateFunctionObject(f);
var funcAlreadyDeclared = env.HasBinding(fn);
@@ -808,12 +819,12 @@ public void DeclarationBindingInstantiation(DeclarationBindingType declarationBi
if (existingProp.Configurable.Value)
{
go.DefineOwnProperty(fn,
new PropertyDescriptor(
value: Undefined.Instance,
writable: true,
enumerable: true,
configurable: configurableBindings
), true);
new PropertyDescriptor(
value: Undefined.Instance,
writable: true,
enumerable: true,
configurable: configurableBindings
), true);
}
else
{
@@ -854,14 +865,19 @@ public void DeclarationBindingInstantiation(DeclarationBindingType declarationBi
}
// process all variable declarations in the current parser scope
foreach (var d in variableDeclarations.SelectMany(x => x.Declarations))
for (var i = 0; i < variableDeclarations.Count; i++)
{
var dn = d.Id.As<Identifier>().Name;
var varAlreadyDeclared = env.HasBinding(dn);
if (!varAlreadyDeclared)
var variableDeclaration = variableDeclarations[i];
var declarations = (List<VariableDeclarator>) variableDeclaration.Declarations;
foreach (var d in declarations)
{
env.CreateMutableBinding(dn, configurableBindings);
env.SetMutableBinding(dn, Undefined.Instance, strict);
var dn = d.Id.As<Identifier>().Name;
var varAlreadyDeclared = env.HasBinding(dn);
if (!varAlreadyDeclared)
{
env.CreateMutableBinding(dn, configurableBindings);
env.SetMutableBinding(dn, Undefined.Instance, strict);
}
}
}
}
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Jint.Native.Object;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
@@ -49,7 +47,7 @@ private JsValue Bind(JsValue thisObj, JsValue[] arguments)
var f = new BindFunctionInstance(Engine) {Extensible = true};
f.TargetFunction = thisObj;
f.BoundThis = thisArg;
f.BoundArgs = arguments.Skip(1).ToArray();
f.BoundArgs = arguments.Skip(1);
f.Prototype = Engine.Function.PrototypeObject;
var o = target as FunctionInstance;
@@ -126,7 +124,7 @@ public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
throw new JavaScriptException(Engine.TypeError);
}
return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1).ToArray());
return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
}
public override JsValue Call(JsValue thisObject, JsValue[] arguments)
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Esprima.Ast;
using Jint.Native.Object;
using Jint.Runtime;
@@ -22,7 +23,7 @@ public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
/// <param name="scope"></param>
/// <param name="strict"></param>
public ScriptFunctionInstance(Engine engine, IFunction functionDeclaration, LexicalEnvironment scope, bool strict)
: base(engine, functionDeclaration.Params.Select(x => x.As<Identifier>().Name).ToArray(), scope, strict)
: base(engine, GetParameterNames(functionDeclaration), scope, strict)
{
_functionDeclaration = functionDeclaration;
@@ -48,6 +49,20 @@ public ScriptFunctionInstance(Engine engine, IFunction functionDeclaration, Lexi
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string[] GetParameterNames(IFunction functionDeclaration)
{
var list = (List<INode>) functionDeclaration.Params;
var count = list.Count;
var names = new string[count];
for (var i = 0; i < count; ++i)
{
names[i] = ((Identifier) list[i]).Name;
}
return names;
}
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
/// </summary>
@@ -12,6 +12,8 @@ namespace Jint.Native.Number
/// </summary>
public sealed class NumberPrototype : NumberInstance
{
private static readonly char[] _numberSeparators = {'.', 'e'};
private NumberPrototype(Engine engine)
: base(engine)
{
@@ -154,7 +156,7 @@ private JsValue ToPrecision(JsValue thisObj, JsValue[] arguments)
// Get the number of decimals
string str = x.ToString("e23", CultureInfo.InvariantCulture);
int decimals = str.IndexOfAny(new [] { '.', 'e' });
int decimals = str.IndexOfAny(_numberSeparators);
decimals = decimals == -1 ? str.Length : decimals;
p -= decimals;
Oops, something went wrong.

0 comments on commit 189af9f

Please sign in to comment.