FunctionInstance.Dynamic.cs 7.3 KB

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