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