FunctionInstance.Dynamic.cs 6.9 KB

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