JintForInStatement.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Generic;
  2. using Esprima.Ast;
  3. using Jint.Native;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interpreter.Expressions;
  6. using Jint.Runtime.References;
  7. namespace Jint.Runtime.Interpreter.Statements
  8. {
  9. /// <summary>
  10. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
  11. /// </summary>
  12. internal sealed class JintForInStatement : JintStatement<ForInStatement>
  13. {
  14. private readonly JintStatement _body;
  15. private readonly JintExpression _identifier;
  16. private readonly JintExpression _right;
  17. public JintForInStatement(Engine engine, ForInStatement statement) : base(engine, statement)
  18. {
  19. if (_statement.Left.Type == Nodes.VariableDeclaration)
  20. {
  21. _identifier = JintExpression.Build(engine, (Identifier) ((VariableDeclaration) _statement.Left).Declarations[0].Id);
  22. }
  23. else if (_statement.Left.Type == Nodes.MemberExpression)
  24. {
  25. _identifier = JintExpression.Build(engine, ((MemberExpression) _statement.Left));
  26. }
  27. else
  28. {
  29. _identifier = JintExpression.Build(engine, (Identifier) _statement.Left);
  30. }
  31. _body = Build(engine, _statement.Body);
  32. _right = JintExpression.Build(engine, statement.Right);
  33. }
  34. protected override Completion ExecuteInternal()
  35. {
  36. var varRef = _identifier.Evaluate() as Reference;
  37. var experValue = _right.GetValue();
  38. if (experValue.IsNullOrUndefined())
  39. {
  40. return new Completion(CompletionType.Normal, null, null, Location);
  41. }
  42. var obj = TypeConverter.ToObject(_engine, experValue);
  43. var v = Undefined.Instance;
  44. // keys are constructed using the prototype chain
  45. var cursor = obj;
  46. var processedKeys = new HashSet<string>();
  47. while (!ReferenceEquals(cursor, null))
  48. {
  49. var keys = _engine.Object.GetOwnPropertyNames(Undefined.Instance, Arguments.From(cursor)).AsArray();
  50. var length = keys.GetLength();
  51. for (uint i = 0; i < length; i++)
  52. {
  53. var p = keys.GetOwnProperty(i).Value.AsStringWithoutTypeCheck();
  54. if (processedKeys.Contains(p))
  55. {
  56. continue;
  57. }
  58. processedKeys.Add(p);
  59. // collection might be modified by inner statement
  60. if (cursor.GetOwnProperty(p) == PropertyDescriptor.Undefined)
  61. {
  62. continue;
  63. }
  64. var value = cursor.GetOwnProperty(p);
  65. if (!value.Enumerable)
  66. {
  67. continue;
  68. }
  69. _engine.PutValue(varRef, p);
  70. var stmt = _body.Execute();
  71. if (!ReferenceEquals(stmt.Value, null))
  72. {
  73. v = stmt.Value;
  74. }
  75. if (stmt.Type == CompletionType.Break)
  76. {
  77. return new Completion(CompletionType.Normal, v, null, Location);
  78. }
  79. if (stmt.Type != CompletionType.Continue)
  80. {
  81. if (stmt.Type != CompletionType.Normal)
  82. {
  83. return stmt;
  84. }
  85. }
  86. }
  87. cursor = cursor.Prototype;
  88. }
  89. return new Completion(CompletionType.Normal, v, null, Location);
  90. }
  91. }
  92. }