FunctionPrototype.cs 5.2 KB

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