2
0

FunctionConstructor.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Environments;
  7. namespace Jint.Native.Function
  8. {
  9. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  10. {
  11. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  12. private static readonly JsString _functionName = new JsString("Function");
  13. private FunctionInstance _throwTypeError;
  14. private static readonly char[] ArgumentNameSeparator = new[] { ',' };
  15. private FunctionConstructor(Engine engine)
  16. : base(engine, _functionName)
  17. {
  18. }
  19. public static FunctionConstructor CreateFunctionConstructor(Engine engine)
  20. {
  21. var obj = new FunctionConstructor(engine)
  22. {
  23. PrototypeObject = FunctionPrototype.CreatePrototypeObject(engine)
  24. };
  25. // The initial value of Function.prototype is the standard built-in Function prototype object
  26. // The value of the [[Prototype]] internal property of the Function constructor is the standard built-in Function prototype object
  27. obj._prototype = obj.PrototypeObject;
  28. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  29. obj._length = new PropertyDescriptor(JsNumber.One, PropertyFlag.Configurable);
  30. return obj;
  31. }
  32. public FunctionPrototype PrototypeObject { get; private set; }
  33. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  34. {
  35. return Construct(arguments, thisObject);
  36. }
  37. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  38. {
  39. var argCount = arguments.Length;
  40. string p = "";
  41. string body = "";
  42. if (argCount == 1)
  43. {
  44. body = TypeConverter.ToString(arguments[0]);
  45. }
  46. else if (argCount > 1)
  47. {
  48. var firstArg = arguments[0];
  49. p = TypeConverter.ToString(firstArg);
  50. for (var k = 1; k < argCount - 1; k++)
  51. {
  52. var nextArg = arguments[k];
  53. p += "," + TypeConverter.ToString(nextArg);
  54. }
  55. body = TypeConverter.ToString(arguments[argCount-1]);
  56. }
  57. IFunction function;
  58. try
  59. {
  60. var functionExpression = "function f(" + p + ") { " + body + "}";
  61. var parser = new JavaScriptParser(functionExpression, ParserOptions);
  62. function = (IFunction) parser.ParseScript().Body[0];
  63. }
  64. catch (ParserException)
  65. {
  66. return ExceptionHelper.ThrowSyntaxError<ObjectInstance>(_engine);
  67. }
  68. var functionObject = new ScriptFunctionInstance(
  69. Engine,
  70. function,
  71. _engine.GlobalEnvironment,
  72. function.Strict);
  73. return functionObject;
  74. }
  75. /// <summary>
  76. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  77. /// </summary>
  78. /// <param name="functionDeclaration"></param>
  79. /// <returns></returns>
  80. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration, LexicalEnvironment env)
  81. {
  82. var functionObject = new ScriptFunctionInstance(
  83. Engine,
  84. functionDeclaration,
  85. env,
  86. functionDeclaration.Strict || _engine._isStrict);
  87. return functionObject;
  88. }
  89. public FunctionInstance ThrowTypeError
  90. {
  91. get
  92. {
  93. if (!ReferenceEquals(_throwTypeError, null))
  94. {
  95. return _throwTypeError;
  96. }
  97. _throwTypeError = new ThrowTypeError(Engine);
  98. return _throwTypeError;
  99. }
  100. }
  101. public object Apply(JsValue thisObject, JsValue[] arguments)
  102. {
  103. if (arguments.Length != 2)
  104. {
  105. ExceptionHelper.ThrowArgumentException("Apply has to be called with two arguments.");
  106. }
  107. var func = thisObject.TryCast<ICallable>();
  108. var thisArg = arguments[0];
  109. var argArray = arguments[1];
  110. if (func is null)
  111. {
  112. return ExceptionHelper.ThrowTypeError<object>(Engine);
  113. }
  114. if (argArray.IsNullOrUndefined())
  115. {
  116. return func.Call(thisArg, Arguments.Empty);
  117. }
  118. var argArrayObj = argArray.TryCast<ObjectInstance>();
  119. if (ReferenceEquals(argArrayObj, null))
  120. {
  121. ExceptionHelper.ThrowTypeError(Engine);
  122. }
  123. var len = argArrayObj.Get(CommonProperties.Length, argArrayObj);
  124. var n = TypeConverter.ToUint32(len);
  125. var argList = new JsValue[n];
  126. for (var index = 0; index < n; index++)
  127. {
  128. var indexName = TypeConverter.ToString(index);
  129. var nextArg = argArrayObj.Get(JsString.Create(indexName), argArrayObj);
  130. argList[index] = nextArg;
  131. }
  132. return func.Call(thisArg, argList);
  133. }
  134. }
  135. }