FunctionConstructor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /// <summary>
  32. /// https://tc39.es/ecma262/#sec-createdynamicfunction
  33. /// </summary>
  34. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  35. {
  36. var argCount = arguments.Length;
  37. string p = "";
  38. string body = "";
  39. if (argCount == 1)
  40. {
  41. body = TypeConverter.ToString(arguments[0]);
  42. }
  43. else if (argCount > 1)
  44. {
  45. var firstArg = arguments[0];
  46. p = TypeConverter.ToString(firstArg);
  47. for (var k = 1; k < argCount - 1; k++)
  48. {
  49. var nextArg = arguments[k];
  50. p += "," + TypeConverter.ToString(nextArg);
  51. }
  52. body = TypeConverter.ToString(arguments[argCount-1]);
  53. }
  54. IFunction function = null;
  55. try
  56. {
  57. string functionExpression;
  58. if (argCount == 0)
  59. {
  60. functionExpression = "function f(){}";
  61. }
  62. else
  63. {
  64. functionExpression = "function f(";
  65. if (p.IndexOf('/') != -1)
  66. {
  67. // ensure comments don't screw up things
  68. functionExpression += "\n" + p + "\n";
  69. }
  70. else
  71. {
  72. functionExpression += p;
  73. }
  74. functionExpression += ")";
  75. if (body.IndexOf('/') != -1)
  76. {
  77. // ensure comments don't screw up things
  78. functionExpression += "{\n" + body + "\n}";
  79. }
  80. else
  81. {
  82. functionExpression += "{" + body + "}";
  83. }
  84. }
  85. var parser = new JavaScriptParser(functionExpression, ParserOptions);
  86. function = (IFunction) parser.ParseScript().Body[0];
  87. }
  88. catch (ParserException)
  89. {
  90. ExceptionHelper.ThrowSyntaxError(_realm);
  91. }
  92. // TODO generators etc, rewrite logic
  93. var proto = GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.Function.PrototypeObject);
  94. var functionObject = new ScriptFunctionInstance(
  95. Engine,
  96. function,
  97. _realm.GlobalEnv,
  98. function.Strict,
  99. proto)
  100. {
  101. _realm = _realm
  102. };
  103. functionObject.MakeConstructor();
  104. // the function is not actually a named function
  105. functionObject.SetFunctionName(_functionNameAnonymous, force: true);
  106. return functionObject;
  107. }
  108. /// <summary>
  109. /// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
  110. /// </summary>
  111. internal FunctionInstance InstantiateFunctionObject(JintFunctionDefinition functionDeclaration, EnvironmentRecord env)
  112. {
  113. var functionObject = new ScriptFunctionInstance(
  114. Engine,
  115. functionDeclaration,
  116. env,
  117. functionDeclaration.ThisMode)
  118. {
  119. _realm = _realm
  120. };
  121. functionObject.MakeConstructor();
  122. return functionObject;
  123. }
  124. }
  125. }