forked from sebastienros/jint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionPrototype.cs
135 lines (111 loc) · 4.62 KB
/
FunctionPrototype.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
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
namespace Jint.Native.Function
{
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
/// </summary>
public sealed class FunctionPrototype : FunctionInstance
{
private FunctionPrototype(Engine engine) : base(engine, null, null, false)
{
}
public static FunctionPrototype CreatePrototypeObject(Engine engine)
{
var obj = new FunctionPrototype(engine);
obj.Extensible = true;
// The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
obj.Prototype = engine.Object.PrototypeObject;
obj.FastAddProperty("length", 0, false, false, false);
return obj;
}
public void Configure()
{
FastAddProperty("constructor", Engine.Function, true, false, true);
FastAddProperty("toString", new ClrFunctionInstance(Engine, ToString), true, false, true);
FastAddProperty("apply", new ClrFunctionInstance(Engine, Apply, 2), true, false, true);
FastAddProperty("call", new ClrFunctionInstance(Engine, CallImpl, 1), true, false, true);
FastAddProperty("bind", new ClrFunctionInstance(Engine, Bind, 1), true, false, true);
}
private JsValue Bind(JsValue thisObj, JsValue[] arguments)
{
var target = thisObj.TryCast<ICallable>(x =>
{
throw new JavaScriptException(Engine.TypeError);
});
var thisArg = arguments.At(0);
var f = new BindFunctionInstance(Engine) {Extensible = true};
f.TargetFunction = thisObj;
f.BoundThis = thisArg;
f.BoundArgs = arguments.Skip(1);
f.Prototype = Engine.Function.PrototypeObject;
var o = target as FunctionInstance;
if (o != null)
{
var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
f.FastAddProperty("length", System.Math.Max(l, 0), false, false, false);
}
else
{
f.FastAddProperty("length", 0, false, false, false);
}
var thrower = Engine.Function.ThrowTypeError;
f.DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
f.DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);
return f;
}
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
{
var func = thisObj.TryCast<FunctionInstance>();
if (func == null)
{
throw new JavaScriptException(Engine.TypeError, "Function object expected.");
}
return System.String.Format("function() {{ ... }}");
}
public JsValue Apply(JsValue thisObject, JsValue[] arguments)
{
var func = thisObject.TryCast<ICallable>();
var thisArg = arguments.At(0);
var argArray = arguments.At(1);
if (func == null)
{
throw new JavaScriptException(Engine.TypeError);
}
if (argArray == Null.Instance || argArray == Undefined.Instance)
{
return func.Call(thisArg, Arguments.Empty);
}
var argArrayObj = argArray.TryCast<ObjectInstance>();
if (argArrayObj == null)
{
throw new JavaScriptException(Engine.TypeError);
}
var len = argArrayObj.Get("length").AsNumber();
uint n = TypeConverter.ToUint32(len);
var argList = new JsValue[n];
for (int index = 0; index < n; index++)
{
string indexName = TypeConverter.ToString(index);
var nextArg = argArrayObj.Get(indexName);
argList[index] = nextArg;
}
return func.Call(thisArg, argList);
}
public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
{
var func = thisObject.TryCast<ICallable>();
if (func == null)
{
throw new JavaScriptException(Engine.TypeError);
}
return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
}
public override JsValue Call(JsValue thisObject, JsValue[] arguments)
{
return Undefined.Instance;
}
}
}