FunctionInstance.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections.Generic;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Environments;
  6. namespace Jint.Native.Function
  7. {
  8. public abstract class FunctionInstance : ObjectInstance, ICallable
  9. {
  10. private const string PropertyNamePrototype = "prototype";
  11. private const int PropertyNamePrototypeLength = 9;
  12. private PropertyDescriptor _prototype;
  13. private const string PropertyNameLength = "length";
  14. private const int PropertyNameLengthLength = 6;
  15. private PropertyDescriptor _length;
  16. private readonly Engine _engine;
  17. protected FunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine)
  18. {
  19. _engine = engine;
  20. FormalParameters = parameters;
  21. Scope = scope;
  22. Strict = strict;
  23. }
  24. /// <summary>
  25. /// Executed when a function object is used as a function
  26. /// </summary>
  27. /// <param name="thisObject"></param>
  28. /// <param name="arguments"></param>
  29. /// <returns></returns>
  30. public abstract JsValue Call(JsValue thisObject, JsValue[] arguments);
  31. public LexicalEnvironment Scope { get; }
  32. public string[] FormalParameters { get; }
  33. public bool Strict { get; }
  34. public virtual bool HasInstance(JsValue v)
  35. {
  36. var vObj = v.TryCast<ObjectInstance>();
  37. if (vObj == null)
  38. {
  39. return false;
  40. }
  41. var po = Get("prototype");
  42. if (!po.IsObject())
  43. {
  44. throw new JavaScriptException(_engine.TypeError, string.Format("Function has non-object prototype '{0}' in instanceof check", TypeConverter.ToString(po)));
  45. }
  46. var o = po.AsObject();
  47. if (o == null)
  48. {
  49. throw new JavaScriptException(_engine.TypeError);
  50. }
  51. while (true)
  52. {
  53. vObj = vObj.Prototype;
  54. if (vObj == null)
  55. {
  56. return false;
  57. }
  58. if (vObj == o)
  59. {
  60. return true;
  61. }
  62. }
  63. }
  64. public override string Class => "Function";
  65. /// <summary>
  66. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.4
  67. /// </summary>
  68. /// <param name="propertyName"></param>
  69. /// <returns></returns>
  70. public override JsValue Get(string propertyName)
  71. {
  72. var v = base.Get(propertyName);
  73. if (propertyName.Length == 6
  74. && propertyName == "caller"
  75. && ((v.As<FunctionInstance>()?.Strict).GetValueOrDefault()))
  76. {
  77. throw new JavaScriptException(_engine.TypeError);
  78. }
  79. return v;
  80. }
  81. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  82. {
  83. if (_prototype != null)
  84. {
  85. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNamePrototype, _prototype);
  86. }
  87. if (_length != null)
  88. {
  89. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameLength, _length);
  90. }
  91. foreach (var entry in base.GetOwnProperties())
  92. {
  93. yield return entry;
  94. }
  95. }
  96. public override PropertyDescriptor GetOwnProperty(string propertyName)
  97. {
  98. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  99. {
  100. return _prototype ?? PropertyDescriptor.Undefined;
  101. }
  102. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  103. {
  104. return _length ?? PropertyDescriptor.Undefined;
  105. }
  106. return base.GetOwnProperty(propertyName);
  107. }
  108. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  109. {
  110. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  111. {
  112. _prototype = desc;
  113. }
  114. else if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  115. {
  116. _length = desc;
  117. }
  118. else
  119. {
  120. base.SetOwnProperty(propertyName, desc);
  121. }
  122. }
  123. public override bool HasOwnProperty(string propertyName)
  124. {
  125. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  126. {
  127. return _prototype != null;
  128. }
  129. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  130. {
  131. return _length != null;
  132. }
  133. return base.HasOwnProperty(propertyName);
  134. }
  135. public override void RemoveOwnProperty(string propertyName)
  136. {
  137. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  138. {
  139. _prototype = null;
  140. }
  141. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  142. {
  143. _prototype = null;
  144. }
  145. base.RemoveOwnProperty(propertyName);
  146. }
  147. }
  148. }