FunctionConstructor.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 object Call(object thisObject, object[] arguments)
  33. {
  34. return Construct(arguments);
  35. }
  36. public ObjectInstance Construct(object[] arguments)
  37. {
  38. var argCount = arguments.Length;
  39. string p = "";
  40. string body = "";
  41. if (argCount == 1)
  42. {
  43. body = TypeConverter.ToString(arguments[0]);
  44. }
  45. else if (argCount > 1)
  46. {
  47. var firstArg = arguments[0];
  48. p = TypeConverter.ToString(firstArg);
  49. for (var k = 1; k < argCount - 1; k++)
  50. {
  51. var nextArg = arguments[k];
  52. p += "," + TypeConverter.ToString(nextArg);
  53. }
  54. body = TypeConverter.ToString(arguments[argCount-1]);
  55. }
  56. body = TypeConverter.ToString(body);
  57. // todo: ensure parsable as parameter list
  58. var parameters = p.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries);
  59. var parser = new JavaScriptParser();
  60. FunctionExpression function;
  61. try
  62. {
  63. var functionExpression = "function(" + p + ") { " + body + "}";
  64. function = parser.ParseFunctionExpression(functionExpression);
  65. }
  66. catch (ParserError)
  67. {
  68. throw new JavaScriptException(Engine.SyntaxError);
  69. }
  70. // todo: check if there is not a way to use the FunctionExpression directly instead of creating a FunctionDeclaration
  71. var functionObject = new ScriptFunctionInstance(
  72. Engine,
  73. new FunctionDeclaration
  74. {
  75. Type = SyntaxNodes.FunctionDeclaration,
  76. Body = new BlockStatement
  77. {
  78. Type = SyntaxNodes.BlockStatement,
  79. Body = new [] { function.Body }
  80. },
  81. Parameters = parameters.Select(x => new Identifier
  82. {
  83. Type = SyntaxNodes.Identifier,
  84. Name = x
  85. }).ToArray(),
  86. FunctionDeclarations = new List<FunctionDeclaration>(),
  87. VariableDeclarations = new List<VariableDeclaration>()
  88. },
  89. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  90. function.Strict
  91. ) { Extensible = true };
  92. return functionObject;
  93. }
  94. /// <summary>
  95. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  96. /// </summary>
  97. /// <param name="functionDeclaration"></param>
  98. /// <returns></returns>
  99. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration)
  100. {
  101. var functionObject = new ScriptFunctionInstance(
  102. Engine,
  103. functionDeclaration,
  104. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  105. functionDeclaration.Strict
  106. ) { Extensible = true };
  107. return functionObject;
  108. }
  109. private FunctionInstance _throwTypeError;
  110. public FunctionInstance ThrowTypeError
  111. {
  112. get
  113. {
  114. if (_throwTypeError != null)
  115. {
  116. return _throwTypeError;
  117. }
  118. _throwTypeError = new ThrowTypeError(Engine);
  119. return _throwTypeError;
  120. }
  121. }
  122. public object Apply(object thisObject, object[] arguments)
  123. {
  124. if (arguments.Length != 2)
  125. {
  126. throw new ArgumentException("Apply has to be called with two arguments.");
  127. }
  128. var func = thisObject as ICallable;
  129. var thisArg = arguments[0];
  130. var argArray = arguments[1];
  131. if (func == null)
  132. {
  133. throw new JavaScriptException(Engine.TypeError);
  134. }
  135. if (argArray == Null.Instance || argArray == Undefined.Instance)
  136. {
  137. return func.Call(thisArg, Arguments.Empty);
  138. }
  139. var argArrayObj = argArray as ObjectInstance;
  140. if (argArrayObj == null)
  141. {
  142. throw new JavaScriptException(Engine.TypeError);
  143. }
  144. var len = argArrayObj.Get("length");
  145. var n = TypeConverter.ToUint32(len);
  146. var argList = new List<object>();
  147. for (var index = 0; index < n; index++)
  148. {
  149. var indexName = index.ToString();
  150. var nextArg = argArrayObj.Get(indexName);
  151. argList.Add(nextArg);
  152. }
  153. return func.Call(thisArg, argList.ToArray());
  154. }
  155. }
  156. }