/
ScriptFunctionInstance.cs
154 lines (134 loc) · 5.56 KB
/
ScriptFunctionInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Esprima.Ast;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Environments;
namespace Jint.Native.Function
{
/// <summary>
///
/// </summary>
public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
{
private readonly IFunction _functionDeclaration;
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
/// </summary>
/// <param name="engine"></param>
/// <param name="functionDeclaration"></param>
/// <param name="scope"></param>
/// <param name="strict"></param>
public ScriptFunctionInstance(Engine engine, IFunction functionDeclaration, LexicalEnvironment scope, bool strict)
: base(engine, GetParameterNames(functionDeclaration), scope, strict)
{
_functionDeclaration = functionDeclaration;
Engine = engine;
Extensible = true;
Prototype = engine.Function.PrototypeObject;
DefineOwnProperty("length", new PropertyDescriptor(JsValue.FromInt(FormalParameters.Length), false, false, false ), false);
var proto = engine.Object.Construct(Arguments.Empty);
proto.DefineOwnProperty("constructor", new PropertyDescriptor(this, true, false, true), false);
DefineOwnProperty("prototype", new PropertyDescriptor(proto, true, false, false ), false);
if (_functionDeclaration.Id != null)
{
DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, null, null, null), false);
}
if (strict)
{
var thrower = engine.Function.ThrowTypeError;
DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);
}
}
[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>
/// <param name="thisArg"></param>
/// <param name="arguments"></param>
/// <returns></returns>
public override JsValue Call(JsValue thisArg, JsValue[] arguments)
{
using (new StrictModeScope(Strict, true))
{
// setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
JsValue thisBinding;
if (StrictModeScope.IsStrictModeCode)
{
thisBinding = thisArg;
}
else if (thisArg == Undefined.Instance || thisArg == Null.Instance)
{
thisBinding = Engine.Global;
}
else if (!thisArg.IsObject())
{
thisBinding = TypeConverter.ToObject(Engine, thisArg);
}
else
{
thisBinding = thisArg;
}
var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Scope);
Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
try
{
Engine.DeclarationBindingInstantiation(
DeclarationBindingType.FunctionCode,
_functionDeclaration.HoistingScope.FunctionDeclarations,
_functionDeclaration.HoistingScope.VariableDeclarations,
this,
arguments);
var result = Engine.ExecuteStatement(_functionDeclaration.Body);
if (result.Type == Completion.Throw)
{
JavaScriptException ex = new JavaScriptException(result.GetValueOrDefault())
.SetCallstack(Engine, result.Location);
throw ex;
}
if (result.Type == Completion.Return)
{
return result.GetValueOrDefault();
}
}
finally
{
Engine.LeaveExecutionContext();
}
return Undefined.Instance;
}
}
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
/// </summary>
/// <param name="arguments"></param>
/// <returns></returns>
public ObjectInstance Construct(JsValue[] arguments)
{
var proto = Get("prototype").TryCast<ObjectInstance>();
var obj = new ObjectInstance(Engine);
obj.Extensible = true;
obj.Prototype = proto ?? Engine.Object.PrototypeObject;
var result = Call(obj, arguments).TryCast<ObjectInstance>();
if (result != null)
{
return result;
}
return obj;
}
public ObjectInstance PrototypeObject { get; private set; }
}
}