FunctionConstructor.cs 6.0 KB

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