FunctionConstructor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Linq;
  3. using Esprima;
  4. using Esprima.Ast;
  5. using Jint.Native.Object;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Environments;
  9. namespace Jint.Native.Function
  10. {
  11. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  12. {
  13. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  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.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  26. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  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. public ObjectInstance Construct(JsValue[] arguments)
  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 = parser.ParseProgram().Body.First().As<IFunction>();
  63. }
  64. catch (ParserException)
  65. {
  66. throw new JavaScriptException(Engine.SyntaxError);
  67. }
  68. var functionObject = new ScriptFunctionInstance(
  69. Engine,
  70. function,
  71. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  72. function.Strict
  73. ) { Extensible = true };
  74. return functionObject;
  75. }
  76. /// <summary>
  77. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  78. /// </summary>
  79. /// <param name="functionDeclaration"></param>
  80. /// <returns></returns>
  81. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration)
  82. {
  83. var functionObject = new ScriptFunctionInstance(
  84. Engine,
  85. functionDeclaration,
  86. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  87. functionDeclaration.Strict
  88. ) { Extensible = true };
  89. return functionObject;
  90. }
  91. private FunctionInstance _throwTypeError;
  92. private static readonly char[] ArgumentNameSeparator = new[] { ',' };
  93. public FunctionInstance ThrowTypeError
  94. {
  95. get
  96. {
  97. if (_throwTypeError != null)
  98. {
  99. return _throwTypeError;
  100. }
  101. _throwTypeError = new ThrowTypeError(Engine);
  102. return _throwTypeError;
  103. }
  104. }
  105. public object Apply(JsValue thisObject, JsValue[] arguments)
  106. {
  107. if (arguments.Length != 2)
  108. {
  109. throw new ArgumentException("Apply has to be called with two arguments.");
  110. }
  111. var func = thisObject.TryCast<ICallable>();
  112. var thisArg = arguments[0];
  113. var argArray = arguments[1];
  114. if (func == null)
  115. {
  116. throw new JavaScriptException(Engine.TypeError);
  117. }
  118. if (ReferenceEquals(argArray, Undefined) || ReferenceEquals(argArray, Undefined))
  119. {
  120. return func.Call(thisArg, Arguments.Empty);
  121. }
  122. var argArrayObj = argArray.TryCast<ObjectInstance>();
  123. if (argArrayObj == null)
  124. {
  125. throw new JavaScriptException(Engine.TypeError);
  126. }
  127. var len = argArrayObj.Get("length");
  128. var n = TypeConverter.ToUint32(len);
  129. var argList = new JsValue[n];
  130. for (var index = 0; index < n; index++)
  131. {
  132. var indexName = TypeConverter.ToString(index);
  133. var nextArg = argArrayObj.Get(indexName);
  134. argList[index] = nextArg;
  135. }
  136. return func.Call(thisArg, argList);
  137. }
  138. }
  139. }