FunctionConstructor.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Linq;
  3. using Esprima;
  4. using Esprima.Ast;
  5. using Jint.Native.Object;
  6. using Jint.Native.String;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Descriptors.Specialized;
  9. using Jint.Runtime.Environments;
  10. namespace Jint.Native.Function
  11. {
  12. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  13. {
  14. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  15. private FunctionConstructor(Engine engine):base(engine, null, null, false)
  16. {
  17. }
  18. public static FunctionConstructor CreateFunctionConstructor(Engine engine)
  19. {
  20. var obj = new FunctionConstructor(engine);
  21. obj.Extensible = true;
  22. // The initial value of Function.prototype is the standard built-in Function prototype object
  23. obj.PrototypeObject = FunctionPrototype.CreatePrototypeObject(engine);
  24. // The value of the [[Prototype]] internal property of the Function constructor is the standard built-in Function prototype object
  25. obj.Prototype = obj.PrototypeObject;
  26. obj.SetOwnProperty("prototype", new AllForbiddenPropertyDescriptor(obj.PrototypeObject));
  27. obj.SetOwnProperty("length", new AllForbiddenPropertyDescriptor(1));
  28. return obj;
  29. }
  30. public void Configure()
  31. {
  32. }
  33. public FunctionPrototype PrototypeObject { get; private set; }
  34. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  35. {
  36. return Construct(arguments);
  37. }
  38. private string[] ParseArgumentNames(string parameterDeclaration)
  39. {
  40. if (string.IsNullOrWhiteSpace(parameterDeclaration))
  41. {
  42. return System.Array.Empty<string>();
  43. }
  44. string[] values = parameterDeclaration.Split(ArgumentNameSeparator, StringSplitOptions.RemoveEmptyEntries);
  45. var newValues = new string[values.Length];
  46. for (var i = 0; i < values.Length; i++)
  47. {
  48. newValues[i] = StringPrototype.TrimEx(values[i]);
  49. }
  50. return newValues;
  51. }
  52. public ObjectInstance Construct(JsValue[] arguments)
  53. {
  54. var argCount = arguments.Length;
  55. string p = "";
  56. string body = "";
  57. if (argCount == 1)
  58. {
  59. body = TypeConverter.ToString(arguments[0]);
  60. }
  61. else if (argCount > 1)
  62. {
  63. var firstArg = arguments[0];
  64. p = TypeConverter.ToString(firstArg);
  65. for (var k = 1; k < argCount - 1; k++)
  66. {
  67. var nextArg = arguments[k];
  68. p += "," + TypeConverter.ToString(nextArg);
  69. }
  70. body = TypeConverter.ToString(arguments[argCount-1]);
  71. }
  72. var parameters = this.ParseArgumentNames(p);
  73. IFunction function;
  74. try
  75. {
  76. var functionExpression = "function f(" + p + ") { " + body + "}";
  77. var parser = new JavaScriptParser(functionExpression, ParserOptions);
  78. function = parser.ParseProgram().Body.First().As<IFunction>();
  79. }
  80. catch (ParserException)
  81. {
  82. throw new JavaScriptException(Engine.SyntaxError);
  83. }
  84. var functionObject = new ScriptFunctionInstance(
  85. Engine,
  86. function,
  87. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  88. function.Strict
  89. ) { Extensible = true };
  90. return functionObject;
  91. }
  92. /// <summary>
  93. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  94. /// </summary>
  95. /// <param name="functionDeclaration"></param>
  96. /// <returns></returns>
  97. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration)
  98. {
  99. var functionObject = new ScriptFunctionInstance(
  100. Engine,
  101. functionDeclaration,
  102. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  103. functionDeclaration.Strict
  104. ) { Extensible = true };
  105. return functionObject;
  106. }
  107. private FunctionInstance _throwTypeError;
  108. private static readonly char[] ArgumentNameSeparator = new[] { ',' };
  109. public FunctionInstance ThrowTypeError
  110. {
  111. get
  112. {
  113. if (_throwTypeError != null)
  114. {
  115. return _throwTypeError;
  116. }
  117. _throwTypeError = new ThrowTypeError(Engine);
  118. return _throwTypeError;
  119. }
  120. }
  121. public object Apply(JsValue thisObject, JsValue[] arguments)
  122. {
  123. if (arguments.Length != 2)
  124. {
  125. throw new ArgumentException("Apply has to be called with two arguments.");
  126. }
  127. var func = thisObject.TryCast<ICallable>();
  128. var thisArg = arguments[0];
  129. var argArray = arguments[1];
  130. if (func == null)
  131. {
  132. throw new JavaScriptException(Engine.TypeError);
  133. }
  134. if (argArray == Null.Instance || argArray == Undefined.Instance)
  135. {
  136. return func.Call(thisArg, Arguments.Empty);
  137. }
  138. var argArrayObj = argArray.TryCast<ObjectInstance>();
  139. if (argArrayObj == null)
  140. {
  141. throw new JavaScriptException(Engine.TypeError);
  142. }
  143. var len = argArrayObj.Get("length");
  144. var n = TypeConverter.ToUint32(len);
  145. var argList = new JsValue[n];
  146. for (var index = 0; index < n; index++)
  147. {
  148. var indexName = TypeConverter.ToString(index);
  149. var nextArg = argArrayObj.Get(indexName);
  150. argList[index] = nextArg;
  151. }
  152. return func.Call(thisArg, argList);
  153. }
  154. }
  155. }