FunctionConstructor.cs 6.6 KB

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