FunctionPrototype.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using Jint.Collections;
  3. using Jint.Native.Array;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
  12. /// </summary>
  13. public sealed class FunctionPrototype : FunctionInstance
  14. {
  15. private static readonly JsString _functionName = new JsString("Function");
  16. private FunctionPrototype(Engine engine)
  17. : base(engine, _functionName, strict: false)
  18. {
  19. }
  20. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  21. {
  22. var obj = new FunctionPrototype(engine)
  23. {
  24. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  25. _prototype = engine.Object.PrototypeObject,
  26. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero
  27. };
  28. return obj;
  29. }
  30. protected override void Initialize()
  31. {
  32. var properties = new PropertyDictionary(5, checkExistingKeys: false)
  33. {
  34. ["constructor"] = new PropertyDescriptor(Engine.Function, PropertyFlag.NonEnumerable),
  35. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString), true, false, true),
  36. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 2), true, false, true),
  37. ["call"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "call", CallImpl, 1), true, false, true),
  38. ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "bind", Bind, 1), true, false, true)
  39. };
  40. SetProperties(properties);
  41. }
  42. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  43. {
  44. var target = thisObj.TryCast<ICallable>(x =>
  45. {
  46. ExceptionHelper.ThrowTypeError(Engine);
  47. });
  48. var func = thisObj as IConstructor;
  49. var thisArg = arguments.At(0);
  50. var f = new BindFunctionInstance(Engine)
  51. {
  52. TargetFunction = thisObj,
  53. BoundThis = thisArg,
  54. BoundArgs = arguments.Skip(1),
  55. _prototype = Engine.Function.PrototypeObject
  56. };
  57. if (target is FunctionInstance functionInstance)
  58. {
  59. var l = TypeConverter.ToNumber(functionInstance.Get(CommonProperties.Length, functionInstance)) - (arguments.Length - 1);
  60. f.SetOwnProperty(CommonProperties.Length, new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden));
  61. }
  62. else
  63. {
  64. f.SetOwnProperty(CommonProperties.Length, PropertyDescriptor.AllForbiddenDescriptor.NumberZero);
  65. }
  66. f.DefineOwnProperty(CommonProperties.Caller, _engine._getSetThrower);
  67. f.DefineOwnProperty(CommonProperties.Arguments, _engine._getSetThrower);
  68. return f;
  69. }
  70. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  71. {
  72. if (!(thisObj is FunctionInstance))
  73. {
  74. return ExceptionHelper.ThrowTypeError<FunctionInstance>(_engine, "Function object expected.");
  75. }
  76. return "function() {{ ... }}";
  77. }
  78. internal JsValue Apply(JsValue thisObject, JsValue[] arguments)
  79. {
  80. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  81. var thisArg = arguments.At(0);
  82. var argArray = arguments.At(1);
  83. if (argArray.IsNullOrUndefined())
  84. {
  85. return func.Call(thisArg, Arguments.Empty);
  86. }
  87. var argList = CreateListFromArrayLike(argArray);
  88. var result = func.Call(thisArg, argList);
  89. return result;
  90. }
  91. internal JsValue[] CreateListFromArrayLike(JsValue argArray, Types? elementTypes = null)
  92. {
  93. var argArrayObj = argArray as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine);
  94. var operations = ArrayOperations.For(argArrayObj);
  95. var allowedTypes = elementTypes ??
  96. Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object;
  97. var argList = operations.GetAll(allowedTypes);
  98. return argList;
  99. }
  100. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  101. {
  102. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  103. JsValue[] values = ArrayExt.Empty<JsValue>();
  104. if (arguments.Length > 1)
  105. {
  106. values = new JsValue[arguments.Length - 1];
  107. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  108. }
  109. var result = func.Call(arguments.At(0), values);
  110. return result;
  111. }
  112. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  113. {
  114. return Undefined;
  115. }
  116. }
  117. }