FunctionInstance.Dynamic.cs 6.9 KB

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