LexicalEnvironment.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Runtime.CompilerServices;
  2. using Jint.Native;
  3. using Jint.Native.Object;
  4. using Jint.Runtime.References;
  5. namespace Jint.Runtime.Environments
  6. {
  7. /// <summary>
  8. /// Represents a Liexical Environment (a.k.a Scope)
  9. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2
  10. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.2
  11. /// </summary>
  12. public sealed class LexicalEnvironment
  13. {
  14. private readonly Engine _engine;
  15. private readonly EnvironmentRecord _record;
  16. private readonly LexicalEnvironment _outer;
  17. public LexicalEnvironment(Engine engine, EnvironmentRecord record, LexicalEnvironment outer)
  18. {
  19. _engine = engine;
  20. _record = record;
  21. _outer = outer;
  22. }
  23. public EnvironmentRecord Record
  24. {
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. get { return _record; }
  27. }
  28. public LexicalEnvironment Outer => _outer;
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public static Reference GetIdentifierReference(LexicalEnvironment lex, string name, bool strict)
  31. {
  32. // optimize for common case where result is in one of the nearest scopes
  33. if (lex._record.HasBinding(name))
  34. {
  35. return lex._engine.ReferencePool.Rent(lex._record, name, strict);
  36. }
  37. if (lex._outer == null)
  38. {
  39. return new Reference(Undefined.Instance, name, strict);
  40. }
  41. return GetIdentifierReferenceLooping(lex._outer, name, strict);
  42. }
  43. private static Reference GetIdentifierReferenceLooping(LexicalEnvironment lex, string name, bool strict)
  44. {
  45. while (true)
  46. {
  47. if (lex.Record.HasBinding(name))
  48. {
  49. return lex._engine.ReferencePool.Rent(lex._record, name, strict);
  50. }
  51. if (lex._outer == null)
  52. {
  53. return lex._engine.ReferencePool.Rent(Undefined.Instance, name, strict);
  54. }
  55. lex = lex._outer;
  56. }
  57. }
  58. public static LexicalEnvironment NewDeclarativeEnvironment(Engine engine, LexicalEnvironment outer = null)
  59. {
  60. return new LexicalEnvironment(engine, new DeclarativeEnvironmentRecord(engine), outer);
  61. }
  62. public static LexicalEnvironment NewObjectEnvironment(Engine engine, ObjectInstance objectInstance, LexicalEnvironment outer, bool provideThis)
  63. {
  64. return new LexicalEnvironment(engine, new ObjectEnvironmentRecord(engine, objectInstance, provideThis), outer);
  65. }
  66. }
  67. }