@@ -242,14 +242,26 @@ function nativeExtensionMethodsWithIndexedCollectionAliases(methods) {
242242 return methods;
243243 }
244244
245+ var hasObjectAtIndex =
246+ Object.prototype.hasOwnProperty.call(methods, 'objectAtIndex');
247+ var hasCount =
248+ Object.prototype.hasOwnProperty.call(methods, 'count');
249+ var hasSymbolIterator =
250+ typeof Symbol === 'function' && Symbol.iterator &&
251+ Object.prototype.hasOwnProperty.call(methods, Symbol.iterator);
245252 var needsObjectAtIndexedSubscript =
246- Object.prototype.hasOwnProperty.call(methods, 'objectAtIndex') &&
253+ hasObjectAtIndex &&
247254 !Object.prototype.hasOwnProperty.call(methods, 'objectAtIndexedSubscript');
248255 var needsSetObjectAtIndexedSubscript =
249256 Object.prototype.hasOwnProperty.call(methods, 'replaceObjectAtIndexWithObject') &&
250257 !Object.prototype.hasOwnProperty.call(methods, 'setObjectAtIndexedSubscript');
258+ var needsIndexedCollectionIterator =
259+ typeof Symbol === 'function' && Symbol.iterator &&
260+ hasObjectAtIndex && hasCount && !hasSymbolIterator;
251261
252- if (!needsObjectAtIndexedSubscript && !needsSetObjectAtIndexedSubscript) {
262+ if (!needsObjectAtIndexedSubscript &&
263+ !needsSetObjectAtIndexedSubscript &&
264+ !needsIndexedCollectionIterator) {
253265 return methods;
254266 }
255267
@@ -278,9 +290,57 @@ function nativeExtensionMethodsWithIndexedCollectionAliases(methods) {
278290 });
279291 }
280292
293+ if (needsIndexedCollectionIterator) {
294+ Object.defineProperty(prepared, Symbol.iterator, {
295+ configurable: true,
296+ enumerable: false,
297+ writable: true,
298+ value: function() {
299+ var receiver = this;
300+ var index = 0;
301+ return {
302+ next: function() {
303+ var countValue = receiver.count;
304+ var count = typeof countValue === 'function'
305+ ? countValue.call(receiver)
306+ : countValue;
307+ if (!(index < count)) {
308+ return { done: true };
309+ }
310+ return {
311+ value: receiver.objectAtIndex(index++),
312+ done: false
313+ };
314+ }
315+ };
316+ }
317+ });
318+ }
319+
281320 return prepared;
282321 }
283322
323+ function nativeExtensionMethodsHaveIterator(methods) {
324+ return typeof Symbol === 'function' && Symbol.iterator &&
325+ methods != null && typeof methods === 'object' &&
326+ Object.prototype.hasOwnProperty.call(methods, Symbol.iterator);
327+ }
328+
329+ function nativeExtensionOptionsWithIterator(options, methods) {
330+ var extendOptions = options || {};
331+ if (!nativeExtensionMethodsHaveIterator(methods)) {
332+ return extendOptions;
333+ }
334+ try {
335+ return Object.assign({}, extendOptions, {
336+ __hasIterator: true
337+ });
338+ } catch (_) {
339+ extendOptions.__hasIterator = true;
340+ return extendOptions;
341+ }
342+ }
343+
284344 Object.defineProperty(globalThis, '__nativeScriptCreateNativeApiIterator', {
285345 configurable: false,
286346 enumerable: false,
@@ -753,17 +813,8 @@ function rememberInstanceClass(instance) {
753813 throw new Error('extend() first parameter must be an object');
754814 }
755815 var extensionMethods = nativeExtensionMethodsWithIndexedCollectionAliases(methods);
756- var extendOptions = options || {};
757- if (typeof Symbol === 'function' &&
758- Object.prototype.hasOwnProperty.call(methods, Symbol.iterator)) {
759- try {
760- extendOptions = Object.assign({}, extendOptions, {
761- __hasIterator: true
762- });
763- } catch (_) {
764- extendOptions.__hasIterator = true;
765- }
766- }
816+ var extendOptions =
817+ nativeExtensionOptionsWithIterator(options, extensionMethods);
767818 var extendedNativeClass = api.__extendClass(nativeClass, extensionMethods, extendOptions);
768819 var extended = wrapNativeClass(extendedNativeClass);
769820 try {
@@ -1405,10 +1456,11 @@ function materializeTypeScriptNativeClass(constructor) {
14051456 options.exposedMethods = constructor.ObjCExposedMethods;
14061457 }
14071458
1408- var nativeBase = nativeClassLikeHandle(baseWrapper);
1409- var extensionMethods =
1410- nativeExtensionMethodsWithIndexedCollectionAliases(constructor.prototype || {});
1411- var nativeClass = api.__extendClass(nativeBase, extensionMethods, options);
1459+ var nativeBase = nativeClassLikeHandle(baseWrapper);
1460+ var extensionMethods =
1461+ nativeExtensionMethodsWithIndexedCollectionAliases(constructor.prototype || {});
1462+ options = nativeExtensionOptionsWithIterator(options, extensionMethods);
1463+ var nativeClass = api.__extendClass(nativeBase, extensionMethods, options);
14121464 var wrapper = wrapNativeClass(nativeClass);
14131465 state.wrapper = wrapper;
14141466
0 commit comments