FunctionConstructor.cs 4.9 KB

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