FunctionInstance.Dynamic.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Environments;
  5. using Jint.Runtime.Interpreter;
  6. using Environment = Jint.Runtime.Environments.Environment;
  7. namespace Jint.Native.Function;
  8. #pragma warning disable MA0049
  9. public partial class Function
  10. #pragma warning restore MA0049
  11. {
  12. private static readonly JsString _functionNameAnonymous = new JsString("anonymous");
  13. /// <summary>
  14. /// https://tc39.es/ecma262/#sec-createdynamicfunction
  15. /// </summary>
  16. internal Function CreateDynamicFunction(
  17. ObjectInstance constructor,
  18. JsValue newTarget,
  19. FunctionKind kind,
  20. JsCallArguments arguments)
  21. {
  22. // TODO var callerContext = _engine.GetExecutionContext(1);
  23. var callerContext = _engine.ExecutionContext;
  24. var callerRealm = callerContext.Realm;
  25. var calleeRealm = _engine.ExecutionContext.Realm;
  26. _engine._host.EnsureCanCompileStrings(callerRealm, calleeRealm);
  27. if (newTarget.IsUndefined())
  28. {
  29. newTarget = constructor;
  30. }
  31. Func<Intrinsics, ObjectInstance>? fallbackProto = null;
  32. switch (kind)
  33. {
  34. case FunctionKind.Normal:
  35. fallbackProto = static intrinsics => intrinsics.Function.PrototypeObject;
  36. break;
  37. case FunctionKind.Async:
  38. fallbackProto = static intrinsics => intrinsics.AsyncFunction.PrototypeObject;
  39. break;
  40. case FunctionKind.Generator:
  41. fallbackProto = static intrinsics => intrinsics.GeneratorFunction.PrototypeObject;
  42. break;
  43. case FunctionKind.AsyncGenerator:
  44. fallbackProto = static intrinsics => intrinsics.AsyncGeneratorFunction.PrototypeObject;
  45. break;
  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. functionExpression = "async function* f(){}";
  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. functionExpression = "async function* f(";
  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. var parserOptions = _engine.GetActiveParserOptions();
  134. if (!parserOptions.AllowReturnOutsideFunction)
  135. {
  136. parserOptions = parserOptions with { AllowReturnOutsideFunction = true };
  137. }
  138. Parser parser = new(parserOptions);
  139. function = (IFunction) parser.ParseScriptGuarded(callerRealm, functionExpression, strict: _engine._isStrict).Body[0];
  140. }
  141. catch (ParseErrorException ex)
  142. {
  143. ExceptionHelper.ThrowSyntaxError(_engine.ExecutionContext.Realm, ex.Message);
  144. }
  145. var proto = GetPrototypeFromConstructor(newTarget, fallbackProto);
  146. var realmF = _realm;
  147. var scope = realmF.GlobalEnv;
  148. PrivateEnvironment? privateEnv = null;
  149. var definition = new JintFunctionDefinition(function);
  150. Function F = OrdinaryFunctionCreate(proto, definition, function.IsStrict() ? FunctionThisMode.Strict : FunctionThisMode.Global, scope, privateEnv);
  151. F.SetFunctionName(_functionNameAnonymous, force: true);
  152. if (kind == FunctionKind.Generator)
  153. {
  154. var prototype = OrdinaryObjectCreate(_engine, _realm.Intrinsics.GeneratorFunction.PrototypeObject.PrototypeObject);
  155. F.DefinePropertyOrThrow(CommonProperties.Prototype, new PropertyDescriptor(prototype, PropertyFlag.Writable));
  156. }
  157. else if (kind == FunctionKind.AsyncGenerator)
  158. {
  159. var prototype = OrdinaryObjectCreate(_engine, _realm.Intrinsics.AsyncGeneratorFunction.PrototypeObject.PrototypeObject);
  160. F.DefinePropertyOrThrow(CommonProperties.Prototype, new PropertyDescriptor(prototype, PropertyFlag.Writable));
  161. }
  162. else if (kind == FunctionKind.Normal)
  163. {
  164. F.MakeConstructor();
  165. }
  166. return F;
  167. }
  168. /// <summary>
  169. /// https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
  170. /// </summary>
  171. internal ScriptFunction OrdinaryFunctionCreate(
  172. ObjectInstance functionPrototype,
  173. JintFunctionDefinition function,
  174. FunctionThisMode thisMode,
  175. Environment scope,
  176. PrivateEnvironment? privateScope)
  177. {
  178. return new ScriptFunction(
  179. _engine,
  180. function,
  181. scope,
  182. thisMode,
  183. functionPrototype) { _privateEnvironment = privateScope, _realm = _realm };
  184. }
  185. }