FunctionConstructor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. using Jint.Runtime.Interpreter;
  8. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-function-constructor
  12. /// </summary>
  13. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  14. {
  15. private static readonly ParserOptions ParserOptions = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  16. private static readonly JsString _functionName = new JsString("Function");
  17. private static readonly JsString _functionNameAnonymous = new JsString("anonymous");
  18. internal FunctionConstructor(
  19. Engine engine,
  20. Realm realm,
  21. ObjectPrototype objectPrototype)
  22. : base(engine, realm, _functionName)
  23. {
  24. PrototypeObject = new FunctionPrototype(engine, realm, objectPrototype);
  25. _prototype = PrototypeObject;
  26. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  27. _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
  28. }
  29. public FunctionPrototype PrototypeObject { get; }
  30. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  31. {
  32. return Construct(arguments, thisObject);
  33. }
  34. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
  35. /// <summary>
  36. /// https://tc39.es/ecma262/#sec-createdynamicfunction
  37. /// </summary>
  38. private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  39. {
  40. var argCount = arguments.Length;
  41. string p = "";
  42. string body = "";
  43. if (argCount == 1)
  44. {
  45. body = TypeConverter.ToString(arguments[0]);
  46. }
  47. else if (argCount > 1)
  48. {
  49. var firstArg = arguments[0];
  50. p = TypeConverter.ToString(firstArg);
  51. for (var k = 1; k < argCount - 1; k++)
  52. {
  53. var nextArg = arguments[k];
  54. p += "," + TypeConverter.ToString(nextArg);
  55. }
  56. body = TypeConverter.ToString(arguments[argCount-1]);
  57. }
  58. IFunction function = null;
  59. try
  60. {
  61. string functionExpression;
  62. if (argCount == 0)
  63. {
  64. functionExpression = "function f(){}";
  65. }
  66. else
  67. {
  68. functionExpression = "function f(";
  69. if (p.IndexOf('/') != -1)
  70. {
  71. // ensure comments don't screw up things
  72. functionExpression += "\n" + p + "\n";
  73. }
  74. else
  75. {
  76. functionExpression += p;
  77. }
  78. functionExpression += ")";
  79. if (body.IndexOf('/') != -1)
  80. {
  81. // ensure comments don't screw up things
  82. functionExpression += "{\n" + body + "\n}";
  83. }
  84. else
  85. {
  86. functionExpression += "{" + body + "}";
  87. }
  88. }
  89. var parser = new JavaScriptParser(functionExpression, ParserOptions);
  90. function = (IFunction) parser.ParseScript().Body[0];
  91. }
  92. catch (ParserException)
  93. {
  94. ExceptionHelper.ThrowSyntaxError(_realm);
  95. }
  96. // TODO generators etc, rewrite logic
  97. var proto = GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.Function.PrototypeObject);
  98. var functionObject = new ScriptFunctionInstance(
  99. Engine,
  100. function,
  101. _realm.GlobalEnv,
  102. function.Strict,
  103. proto)
  104. {
  105. _realm = _realm
  106. };
  107. functionObject.MakeConstructor();
  108. // the function is not actually a named function
  109. functionObject.SetFunctionName(_functionNameAnonymous, force: true);
  110. return functionObject;
  111. }
  112. /// <summary>
  113. /// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
  114. /// </summary>
  115. internal FunctionInstance InstantiateFunctionObject(JintFunctionDefinition functionDeclaration, EnvironmentRecord env)
  116. {
  117. var functionObject = new ScriptFunctionInstance(
  118. Engine,
  119. functionDeclaration,
  120. env,
  121. functionDeclaration.ThisMode)
  122. {
  123. _realm = _realm
  124. };
  125. functionObject.MakeConstructor();
  126. return functionObject;
  127. }
  128. }
  129. }