ScriptFunctionInstance.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Linq;
  2. using Jint.Native.Object;
  3. using Jint.Parser;
  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 IFunctionDeclaration _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, IFunctionDeclaration functionDeclaration, LexicalEnvironment scope, bool strict)
  23. : base(engine, functionDeclaration.Parameters.Select(x => x.Name).ToArray(), scope, strict)
  24. {
  25. _functionDeclaration = functionDeclaration;
  26. Engine = engine;
  27. Extensible = true;
  28. Prototype = engine.Function.PrototypeObject;
  29. DefineOwnProperty("length", new PropertyDescriptor(new JsValue(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. /// <summary>
  45. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  46. /// </summary>
  47. /// <param name="thisArg"></param>
  48. /// <param name="arguments"></param>
  49. /// <returns></returns>
  50. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  51. {
  52. using (new StrictModeScope(Strict, true))
  53. {
  54. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  55. JsValue thisBinding;
  56. if (StrictModeScope.IsStrictModeCode)
  57. {
  58. thisBinding = thisArg;
  59. }
  60. else if (thisArg == Undefined.Instance || thisArg == Null.Instance)
  61. {
  62. thisBinding = Engine.Global;
  63. }
  64. else if (!thisArg.IsObject())
  65. {
  66. thisBinding = TypeConverter.ToObject(Engine, thisArg);
  67. }
  68. else
  69. {
  70. thisBinding = thisArg;
  71. }
  72. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Scope);
  73. Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  74. try
  75. {
  76. Engine.DeclarationBindingInstantiation(
  77. DeclarationBindingType.FunctionCode,
  78. _functionDeclaration.FunctionDeclarations,
  79. _functionDeclaration.VariableDeclarations,
  80. this,
  81. arguments);
  82. var result = Engine.ExecuteStatement(_functionDeclaration.Body);
  83. if (result.Type == Completion.Throw)
  84. {
  85. JavaScriptException ex = new JavaScriptException(result.GetValueOrDefault())
  86. .SetCallstack(Engine, result.Location);
  87. throw ex;
  88. }
  89. if (result.Type == Completion.Return)
  90. {
  91. return result.GetValueOrDefault();
  92. }
  93. }
  94. finally
  95. {
  96. Engine.LeaveExecutionContext();
  97. }
  98. return Undefined.Instance;
  99. }
  100. }
  101. /// <summary>
  102. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  103. /// </summary>
  104. /// <param name="arguments"></param>
  105. /// <returns></returns>
  106. public ObjectInstance Construct(JsValue[] arguments)
  107. {
  108. var proto = Get("prototype").TryCast<ObjectInstance>();
  109. var obj = new ObjectInstance(Engine);
  110. obj.Extensible = true;
  111. obj.Prototype = proto ?? Engine.Object.PrototypeObject;
  112. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  113. if (result != null)
  114. {
  115. return result;
  116. }
  117. return obj;
  118. }
  119. public ObjectInstance PrototypeObject { get; private set; }
  120. }
  121. }