ScriptFunctionInstance.cs 5.6 KB

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