FunctionConstructor.cs 5.4 KB

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