ScriptFunctionInstance.cs 5.0 KB

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