ScriptFunctionInstance.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Runtime.CompilerServices;
  2. using Esprima.Ast;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Environments;
  7. namespace Jint.Native.Function
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  13. {
  14. private readonly IFunction _functionDeclaration;
  15. /// <summary>
  16. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  17. /// </summary>
  18. /// <param name="engine"></param>
  19. /// <param name="functionDeclaration"></param>
  20. /// <param name="scope"></param>
  21. /// <param name="strict"></param>
  22. public ScriptFunctionInstance(Engine engine, IFunction functionDeclaration, LexicalEnvironment scope, bool strict)
  23. : base(engine, GetParameterNames(functionDeclaration), scope, strict)
  24. {
  25. _functionDeclaration = functionDeclaration;
  26. Engine = engine;
  27. Extensible = true;
  28. Prototype = engine.Function.PrototypeObject;
  29. DefineOwnProperty("length", new PropertyDescriptor(JsValue.FromInt(FormalParameters.Length), false, false, false ), false);
  30. var proto = engine.Object.Construct(Arguments.Empty);
  31. proto.DefineOwnProperty("constructor", new PropertyDescriptor(this, true, false, true), false);
  32. DefineOwnProperty("prototype", new PropertyDescriptor(proto, true, false, false ), false);
  33. if (_functionDeclaration.Id != null)
  34. {
  35. DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, null, null, null), false);
  36. }
  37. if (strict)
  38. {
  39. var thrower = engine.Function.ThrowTypeError;
  40. DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
  41. DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);
  42. }
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. private static string[] GetParameterNames(IFunction functionDeclaration)
  46. {
  47. var list = functionDeclaration.Params;
  48. var count = list.Count;
  49. var names = new string[count];
  50. for (var i = 0; i < count; ++i)
  51. {
  52. names[i] = ((Identifier) list[i]).Name;
  53. }
  54. return names;
  55. }
  56. /// <summary>
  57. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  58. /// </summary>
  59. /// <param name="thisArg"></param>
  60. /// <param name="arguments"></param>
  61. /// <returns></returns>
  62. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  63. {
  64. using (new StrictModeScope(Strict, true))
  65. {
  66. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  67. JsValue thisBinding;
  68. if (StrictModeScope.IsStrictModeCode)
  69. {
  70. thisBinding = thisArg;
  71. }
  72. else if (thisArg == Undefined.Instance || thisArg == Null.Instance)
  73. {
  74. thisBinding = Engine.Global;
  75. }
  76. else if (!thisArg.IsObject())
  77. {
  78. thisBinding = TypeConverter.ToObject(Engine, thisArg);
  79. }
  80. else
  81. {
  82. thisBinding = thisArg;
  83. }
  84. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Scope);
  85. Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  86. try
  87. {
  88. Engine.DeclarationBindingInstantiation(
  89. DeclarationBindingType.FunctionCode,
  90. _functionDeclaration.HoistingScope.FunctionDeclarations,
  91. _functionDeclaration.HoistingScope.VariableDeclarations,
  92. this,
  93. arguments);
  94. var result = Engine.ExecuteStatement(_functionDeclaration.Body);
  95. if (result.Type == Completion.Throw)
  96. {
  97. JavaScriptException ex = new JavaScriptException(result.GetValueOrDefault())
  98. .SetCallstack(Engine, result.Location);
  99. throw ex;
  100. }
  101. if (result.Type == Completion.Return)
  102. {
  103. return result.GetValueOrDefault();
  104. }
  105. }
  106. finally
  107. {
  108. Engine.LeaveExecutionContext();
  109. }
  110. return Undefined.Instance;
  111. }
  112. }
  113. /// <summary>
  114. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  115. /// </summary>
  116. /// <param name="arguments"></param>
  117. /// <returns></returns>
  118. public ObjectInstance Construct(JsValue[] arguments)
  119. {
  120. var proto = Get("prototype").TryCast<ObjectInstance>();
  121. var obj = new ObjectInstance(Engine);
  122. obj.Extensible = true;
  123. obj.Prototype = proto ?? Engine.Object.PrototypeObject;
  124. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  125. if (result != null)
  126. {
  127. return result;
  128. }
  129. return obj;
  130. }
  131. public ObjectInstance PrototypeObject { get; private set; }
  132. }
  133. }