FunctionInstance.Dynamic.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Environments;
  6. using Jint.Runtime.Interpreter;
  7. using Environment = Jint.Runtime.Environments.Environment;
  8. namespace Jint.Native.Function;
  9. #pragma warning disable MA0049
  10. public partial class Function
  11. #pragma warning restore MA0049
  12. {
  13. private static readonly JsString _functionNameAnonymous = new JsString("anonymous");
  14. /// <summary>
  15. /// https://tc39.es/ecma262/#sec-createdynamicfunction
  16. /// </summary>
  17. internal Function CreateDynamicFunction(
  18. ObjectInstance constructor,
  19. JsValue newTarget,
  20. FunctionKind kind,
  21. JsValue[] arguments)
  22. {
  23. // TODO var callerContext = _engine.GetExecutionContext(1);
  24. var callerContext = _engine.ExecutionContext;
  25. var callerRealm = callerContext.Realm;
  26. var calleeRealm = _engine.ExecutionContext.Realm;
  27. _engine._host.EnsureCanCompileStrings(callerRealm, calleeRealm);
  28. if (newTarget.IsUndefined())
  29. {
  30. newTarget = constructor;
  31. }
  32. Func<Intrinsics, ObjectInstance>? fallbackProto = null;
  33. switch (kind)
  34. {
  35. case FunctionKind.Normal:
  36. fallbackProto = static intrinsics => intrinsics.Function.PrototypeObject;
  37. break;
  38. case FunctionKind.Async:
  39. fallbackProto = static intrinsics => intrinsics.AsyncFunction.PrototypeObject;
  40. break;
  41. case FunctionKind.Generator:
  42. case FunctionKind.AsyncGenerator:
  43. default:
  44. ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
  45. break;
  46. }
  47. var argCount = arguments.Length;
  48. var p = "";
  49. var body = "";
  50. if (argCount == 1)
  51. {
  52. body = TypeConverter.ToString(arguments[0]);
  53. }
  54. else if (argCount > 1)
  55. {
  56. var firstArg = arguments[0];
  57. p = TypeConverter.ToString(firstArg);
  58. for (var k = 1; k < argCount - 1; k++)
  59. {
  60. var nextArg = arguments[k];
  61. p += "," + TypeConverter.ToString(nextArg);
  62. }
  63. body = TypeConverter.ToString(arguments[argCount - 1]);
  64. }
  65. IFunction? function = null;
  66. try
  67. {
  68. string? functionExpression = null;
  69. if (argCount == 0)
  70. {
  71. switch (kind)
  72. {
  73. case FunctionKind.Normal:
  74. functionExpression = "function f(){}";
  75. break;
  76. case FunctionKind.Generator:
  77. functionExpression = "function* f(){}";
  78. break;
  79. case FunctionKind.Async:
  80. functionExpression = "async function f(){}";
  81. break;
  82. case FunctionKind.AsyncGenerator:
  83. ExceptionHelper.ThrowNotImplementedException("Async generators not implemented");
  84. break;
  85. default:
  86. ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
  87. break;
  88. }
  89. }
  90. else
  91. {
  92. switch (kind)
  93. {
  94. case FunctionKind.Normal:
  95. functionExpression = "function f(";
  96. break;
  97. case FunctionKind.Async:
  98. functionExpression = "async function f(";
  99. break;
  100. case FunctionKind.Generator:
  101. functionExpression = "function* f(";
  102. break;
  103. case FunctionKind.AsyncGenerator:
  104. ExceptionHelper.ThrowNotImplementedException("Async generators not implemented");
  105. break;
  106. default:
  107. ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
  108. break;
  109. }
  110. if (p.Contains('/'))
  111. {
  112. // ensure comments don't screw up things
  113. functionExpression += "\n" + p + "\n";
  114. }
  115. else
  116. {
  117. functionExpression += p;
  118. }
  119. functionExpression += ")";
  120. if (body.Contains('/'))
  121. {
  122. // ensure comments don't screw up things
  123. functionExpression += "{\n" + body + "\n}";
  124. }
  125. else
  126. {
  127. functionExpression += "{" + body + "}";
  128. }
  129. }
  130. JavaScriptParser parser = new(new ParserOptions
  131. {
  132. Tolerant = false,
  133. RegexTimeout = _engine.Options.Constraints.RegexTimeout
  134. });
  135. function = (IFunction) parser.ParseScript(functionExpression, source: null, _engine._isStrict).Body[0];
  136. }
  137. catch (ParserException ex)
  138. {
  139. ExceptionHelper.ThrowSyntaxError(_engine.ExecutionContext.Realm, ex.Message);
  140. }
  141. var proto = GetPrototypeFromConstructor(newTarget, fallbackProto);
  142. var realmF = _realm;
  143. var scope = realmF.GlobalEnv;
  144. PrivateEnvironment? privateEnv = null;
  145. var definition = new JintFunctionDefinition(function);
  146. Function F = OrdinaryFunctionCreate(proto, definition, function.Strict ? FunctionThisMode.Strict : FunctionThisMode.Global, scope, privateEnv);
  147. F.SetFunctionName(_functionNameAnonymous, force: true);
  148. if (kind == FunctionKind.Generator)
  149. {
  150. ExceptionHelper.ThrowNotImplementedException("generators not implemented");
  151. }
  152. else if (kind == FunctionKind.AsyncGenerator)
  153. {
  154. // TODO
  155. // Let prototype be ! OrdinaryObjectCreate(%AsyncGeneratorFunction.prototype.prototype%).
  156. // Perform DefinePropertyOrThrow(F, "prototype", PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  157. ExceptionHelper.ThrowNotImplementedException("async generators not implemented");
  158. }
  159. else if (kind == FunctionKind.Normal)
  160. {
  161. F.MakeConstructor();
  162. }
  163. return F;
  164. }
  165. /// <summary>
  166. /// https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
  167. /// </summary>
  168. internal ScriptFunction OrdinaryFunctionCreate(
  169. ObjectInstance functionPrototype,
  170. JintFunctionDefinition function,
  171. FunctionThisMode thisMode,
  172. Environment scope,
  173. PrivateEnvironment? privateScope)
  174. {
  175. return new ScriptFunction(
  176. _engine,
  177. function,
  178. scope,
  179. thisMode,
  180. functionPrototype) { _privateEnvironment = privateScope, _realm = _realm };
  181. }
  182. }