FLeafNode.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FLeafNode.h"
  17. #import "FEmptyNode.h"
  18. #import "FChildrenNode.h"
  19. #import "FConstants.h"
  20. #import "FImmutableSortedDictionary.h"
  21. #import "FUtilities.h"
  22. #import "FStringUtilities.h"
  23. #import "FSnapshotUtilities.h"
  24. @interface FLeafNode ()
  25. @property (nonatomic, strong) id<FNode> priorityNode;
  26. @property (nonatomic, strong) NSString *lazyHash;
  27. @end
  28. @implementation FLeafNode
  29. @synthesize value;
  30. @synthesize priorityNode;
  31. - (id)initWithValue:(id)aValue {
  32. self = [super init];
  33. if (self) {
  34. self.value = aValue;
  35. self.priorityNode = [FEmptyNode emptyNode];
  36. }
  37. return self;
  38. }
  39. - (id)initWithValue:(id)aValue withPriority:(id<FNode>)aPriority {
  40. self = [super init];
  41. if (self) {
  42. self.value = aValue;
  43. [FSnapshotUtilities validatePriorityNode:aPriority];
  44. self.priorityNode = aPriority;
  45. }
  46. return self;
  47. }
  48. #pragma mark -
  49. #pragma mark FNode methods
  50. - (BOOL) isLeafNode {
  51. return YES;
  52. }
  53. - (id<FNode>) getPriority {
  54. return self.priorityNode;
  55. }
  56. - (id<FNode>) updatePriority:(id<FNode>)aPriority {
  57. return [[FLeafNode alloc] initWithValue:self.value withPriority:aPriority];
  58. }
  59. - (id<FNode>) getImmediateChild:(NSString *) childName {
  60. if ([childName isEqualToString:@".priority"]) {
  61. return self.priorityNode;
  62. } else {
  63. return [FEmptyNode emptyNode];
  64. }
  65. }
  66. - (id<FNode>) getChild:(FPath *)path {
  67. if (path.getFront == nil) {
  68. return self;
  69. } else if ([[path getFront] isEqualToString:@".priority"]) {
  70. return [self getPriority];
  71. } else {
  72. return [FEmptyNode emptyNode];
  73. }
  74. }
  75. - (BOOL)hasChild:(NSString *)childName {
  76. return [childName isEqualToString:@".priority"] && ![self getPriority].isEmpty;
  77. }
  78. - (NSString *)predecessorChildKey:(NSString *)childKey
  79. {
  80. return nil;
  81. }
  82. - (id<FNode>) updateImmediateChild:(NSString *)childName withNewChild:(id<FNode>)newChildNode {
  83. if ([childName isEqualToString:@".priority"]) {
  84. return [self updatePriority:newChildNode];
  85. } else if (newChildNode.isEmpty) {
  86. return self;
  87. } else {
  88. FChildrenNode* childrenNode = [[FChildrenNode alloc] init];
  89. childrenNode = [childrenNode updateImmediateChild:childName withNewChild:newChildNode];
  90. childrenNode = [childrenNode updatePriority:self.priorityNode];
  91. return childrenNode;
  92. }
  93. }
  94. - (id<FNode>) updateChild:(FPath *)path withNewChild:(id<FNode>)newChildNode {
  95. NSString* front = [path getFront];
  96. if(front == nil) {
  97. return newChildNode;
  98. } else if (newChildNode.isEmpty && ![front isEqualToString:@".priority"]) {
  99. return self;
  100. } else {
  101. NSAssert(![front isEqualToString:@".priority"] || path.length == 1, @".priority must be the last token in a path.");
  102. return [self updateImmediateChild:front withNewChild:
  103. [[FEmptyNode emptyNode] updateChild:[path popFront] withNewChild:newChildNode]];
  104. }
  105. }
  106. - (id) val {
  107. return [self valForExport:NO];
  108. }
  109. - (id) valForExport:(BOOL)exp {
  110. if(exp && !self.getPriority.isEmpty) {
  111. return @{kPayloadValue : self.value,
  112. kPayloadPriority : [[self getPriority] val]};
  113. }
  114. else {
  115. return self.value;
  116. }
  117. }
  118. - (BOOL)isEqual:(id <FNode>)other {
  119. if(other == self) {
  120. return YES;
  121. } else if (other.isLeafNode) {
  122. FLeafNode *otherLeaf = other;
  123. if ([FUtilities getJavascriptType:self.value] != [FUtilities getJavascriptType:otherLeaf.value]) {
  124. return NO;
  125. }
  126. return [otherLeaf.value isEqual:self.value] && [otherLeaf.priorityNode isEqual:self.priorityNode];
  127. } else {
  128. return NO;
  129. }
  130. }
  131. - (NSUInteger)hash {
  132. return [self.value hash] * 17 + self.priorityNode.hash;
  133. }
  134. - (id <FNode>)withIndex:(id <FIndex>)index {
  135. return self;
  136. }
  137. - (BOOL)isIndexed:(id <FIndex>)index {
  138. return YES;
  139. }
  140. - (BOOL) isEmpty {
  141. return NO;
  142. }
  143. - (int) numChildren {
  144. return 0;
  145. }
  146. - (void) enumerateChildrenUsingBlock:(void (^)(NSString *, id<FNode>, BOOL *))block
  147. {
  148. // Nothing to iterate over
  149. }
  150. - (void) enumerateChildrenReverse:(BOOL)reverse usingBlock:(void (^)(NSString *, id<FNode>, BOOL *))block
  151. {
  152. // Nothing to iterate over
  153. }
  154. - (NSEnumerator *)childEnumerator
  155. {
  156. // Nothing to iterate over
  157. return [@[] objectEnumerator];
  158. }
  159. - (NSString *) dataHash {
  160. if (self.lazyHash == nil) {
  161. NSMutableString *toHash = [[NSMutableString alloc] init];
  162. [FSnapshotUtilities appendHashRepresentationForLeafNode:self toString:toHash hashVersion:FDataHashVersionV1];
  163. self.lazyHash = [FStringUtilities base64EncodedSha1:toHash];
  164. }
  165. return self.lazyHash;
  166. }
  167. - (NSComparisonResult)compare:(id <FNode>)other {
  168. if (other == [FEmptyNode emptyNode]) {
  169. return NSOrderedDescending;
  170. } else if ([other isKindOfClass:[FChildrenNode class]]) {
  171. return NSOrderedAscending;
  172. } else {
  173. NSAssert(other.isLeafNode, @"Compared against unknown type of node.");
  174. return [self compareToLeafNode:(FLeafNode*)other];
  175. }
  176. }
  177. + (NSArray*) valueTypeOrder {
  178. static NSArray* valueOrder = nil;
  179. static dispatch_once_t once;
  180. dispatch_once(&once, ^{
  181. valueOrder = @[kJavaScriptObject, kJavaScriptBoolean, kJavaScriptNumber, kJavaScriptString];
  182. });
  183. return valueOrder;
  184. }
  185. - (NSComparisonResult) compareToLeafNode:(FLeafNode*)other {
  186. NSString* thisLeafType = [FUtilities getJavascriptType:self.value];
  187. NSString* otherLeafType = [FUtilities getJavascriptType:other.value];
  188. NSUInteger thisIndex = [[FLeafNode valueTypeOrder] indexOfObject:thisLeafType];
  189. NSUInteger otherIndex = [[FLeafNode valueTypeOrder] indexOfObject:otherLeafType];
  190. assert(thisIndex >= 0 && otherIndex >= 0);
  191. if (otherIndex == thisIndex) {
  192. // Same type. Compare values.
  193. if (thisLeafType == kJavaScriptObject) {
  194. // Deferred value nodes are all equal, but we should also never get to this point...
  195. return NSOrderedSame;
  196. } else if (thisLeafType == kJavaScriptString) {
  197. return [self.value compare:other.value options:NSLiteralSearch];
  198. } else {
  199. return [self.value compare:other.value];
  200. }
  201. } else {
  202. return thisIndex > otherIndex ? NSOrderedDescending : NSOrderedAscending;
  203. }
  204. }
  205. - (NSString *) description {
  206. return [[self valForExport:YES] description];
  207. }
  208. - (void) forEachChildDo:(fbt_bool_nsstring_node)action {
  209. // There are no children, so there is nothing to do.
  210. return;
  211. }
  212. @end