FunctionConstructor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Environments;
  7. namespace Jint.Native.Function
  8. {
  9. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  10. {
  11. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  12. private static readonly JsString _functionName = new JsString("Function");
  13. private static readonly JsString _functionNameAnonymous = new JsString("anonymous");
  14. private FunctionConstructor(Engine engine)
  15. : base(engine, _functionName)
  16. {
  17. }
  18. public static FunctionConstructor CreateFunctionConstructor(Engine engine)
  19. {
  20. var obj = new FunctionConstructor(engine)
  21. {
  22. PrototypeObject = FunctionPrototype.CreatePrototypeObject(engine)
  23. };
  24. // The initial value of Function.prototype is the standard built-in Function prototype object
  25. // The value of the [[Prototype]] internal property of the Function constructor is the standard built-in Function prototype object
  26. obj._prototype = obj.PrototypeObject;
  27. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  28. obj._length = new PropertyDescriptor(JsNumber.One, PropertyFlag.Configurable);
  29. return obj;
  30. }
  31. public FunctionPrototype PrototypeObject { get; private set; }
  32. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  33. {
  34. return Construct(arguments, thisObject);
  35. }
  36. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  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. string functionExpression;
  60. if (argCount == 0)
  61. {
  62. functionExpression = "function f(){}";
  63. }
  64. else
  65. {
  66. functionExpression = "function f(";
  67. if (p.IndexOf('/') != -1)
  68. {
  69. // ensure comments don't screw up things
  70. functionExpression += "\n" + p + "\n";
  71. }
  72. else
  73. {
  74. functionExpression += p;
  75. }
  76. functionExpression += ")";
  77. if (body.IndexOf('/') != -1)
  78. {
  79. // ensure comments don't screw up things
  80. functionExpression += "{\n" + body + "\n}";
  81. }
  82. else
  83. {
  84. functionExpression += "{" + body + "}";
  85. }
  86. }
  87. var parser = new JavaScriptParser(functionExpression, ParserOptions);
  88. function = (IFunction) parser.ParseScript().Body[0];
  89. }
  90. catch (ParserException)
  91. {
  92. return ExceptionHelper.ThrowSyntaxError<ObjectInstance>(_engine);
  93. }
  94. var functionObject = new ScriptFunctionInstance(
  95. Engine,
  96. function,
  97. _engine.GlobalEnvironment,
  98. function.Strict);
  99. functionObject.MakeConstructor();
  100. // the function is not actually a named function
  101. functionObject.SetFunctionName(_functionNameAnonymous, force: true);
  102. return functionObject;
  103. }
  104. /// <summary>
  105. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  106. /// </summary>
  107. /// <param name="functionDeclaration"></param>
  108. /// <returns></returns>
  109. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration, LexicalEnvironment env)
  110. {
  111. var functionObject = new ScriptFunctionInstance(
  112. Engine,
  113. functionDeclaration,
  114. env,
  115. functionDeclaration.Strict || _engine._isStrict);
  116. functionObject.MakeConstructor();
  117. return functionObject;
  118. }
  119. }
  120. }