ScriptFunctionInstance.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, null, null, null), false);
  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. throw new JavaScriptException(result.Value);
  86. }
  87. if (result.Type == Completion.Return)
  88. {
  89. return result.Value;
  90. }
  91. }
  92. finally
  93. {
  94. Engine.LeaveExecutionContext();
  95. }
  96. return Undefined.Instance;
  97. }
  98. }
  99. /// <summary>
  100. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  101. /// </summary>
  102. /// <param name="arguments"></param>
  103. /// <returns></returns>
  104. public ObjectInstance Construct(JsValue[] arguments)
  105. {
  106. var proto = Get("prototype").TryCast<ObjectInstance>();
  107. var obj = new ObjectInstance(Engine);
  108. obj.Extensible = true;
  109. obj.Prototype = proto ?? Engine.Object.PrototypeObject;
  110. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  111. if (result != null)
  112. {
  113. return result;
  114. }
  115. return obj;
  116. }
  117. public ObjectInstance PrototypeObject { get; private set; }
  118. }
  119. }