Skip to content

Commit c201b4c

Browse files
committed
fix: dispatch instance base selectors through super
1 parent 289e52c commit c201b4c

4 files changed

Lines changed: 50 additions & 58 deletions

File tree

NativeScript/ffi/shared/bridge/Install.mm

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,11 @@ function installSelectorGroups(target, groups, receiverIsClass) {
951951
}
952952
var selectorFunction =
953953
api.__makeSelectorGroupFunction(nativeClass, !!receiverIsClass, selectors);
954-
Object.defineProperty(target, name, {
955-
configurable: true,
956-
enumerable: false,
957-
writable: true,
958-
value: receiverIsClass
954+
Object.defineProperty(target, name, {
955+
configurable: true,
956+
enumerable: false,
957+
writable: true,
958+
value: receiverIsClass
959959
? (function(fn, memberName) {
960960
return function() {
961961
if (this && typeof this === 'object' && this.kind === 'object') {
@@ -972,7 +972,18 @@ function installSelectorGroups(target, groups, receiverIsClass) {
972972
return rememberInstanceClass(fn(...args));
973973
};
974974
})(selectorFunction, name)
975-
: selectorFunction
975+
: (function(fn, memberName) {
976+
return function() {
977+
if (this && typeof this === 'object' && this.kind === 'object') {
978+
var baseArgs = [nativeClass, this, memberName];
979+
for (var baseArgIndex = 0; baseArgIndex < arguments.length; baseArgIndex++) {
980+
baseArgs.push(arguments[baseArgIndex]);
981+
}
982+
return api.__invokeBase(...baseArgs);
983+
}
984+
return fn.apply(this, arguments);
985+
};
986+
})(selectorFunction, name)
976987
});
977988
}
978989
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const assert = require("assert");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const repoRoot = path.resolve(__dirname, "../../..");
6+
7+
for (const relativePath of [
8+
"NativeScript/ffi/shared/bridge/Install.mm",
9+
"packages/react-native/native-api/ffi/shared/bridge/Install.mm",
10+
]) {
11+
const source = fs.readFileSync(path.join(repoRoot, relativePath), "utf8");
12+
13+
const invokeBaseCount = source.match(/return api\.__invokeBase\(\.\.\.baseArgs\);/g)?.length ?? 0;
14+
assert(
15+
invokeBaseCount >= 2,
16+
`${relativePath}: class and instance selector wrappers should route native receivers through __invokeBase`,
17+
);
18+
assert(
19+
source.includes("return fn.apply(this, arguments);"),
20+
`${relativePath}: instance selector wrapper should preserve normal this-bound fallback dispatch`,
21+
);
22+
assert(
23+
source.includes("value: receiverIsClass") &&
24+
source.includes(": (function(fn, memberName) {") &&
25+
source.includes("var baseArgs = [nativeClass, this, memberName];"),
26+
`${relativePath}: instance selector wrapper should build base invocation arguments from nativeClass, receiver, and member name`,
27+
);
28+
}
29+
30+
console.log("runtime instance selector base dispatch tests passed");

test/runtime/fixtures/TNSTestNativeCallbacks.m

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -331,44 +331,20 @@ + (void)recordsPointer:(TNSSimpleStruct*)object {
331331
}
332332

333333
+ (void)apiNSMutableArrayMethods:(NSMutableArray*)object {
334-
NSLog(@"NS_MUTARRAY enter");
335-
NSLog(@"NS_MUTARRAY before addObject:b");
336334
[object addObject:@"b"];
337-
NSLog(@"NS_MUTARRAY after addObject:b");
338-
NSLog(@"NS_MUTARRAY before addObject:x");
339335
[object addObject:@"x"];
340-
NSLog(@"NS_MUTARRAY after addObject:x");
341-
NSLog(@"NS_MUTARRAY before addObject:c");
342336
[object addObject:@"c"];
343-
NSLog(@"NS_MUTARRAY after addObject:c");
344-
NSLog(@"NS_MUTARRAY before addObject:y");
345337
[object addObject:@"y"];
346-
NSLog(@"NS_MUTARRAY after addObject:y");
347-
NSLog(@"NS_MUTARRAY before addObject:z");
348338
[object addObject:@"z"];
349-
NSLog(@"NS_MUTARRAY after addObject:z");
350-
NSLog(@"NS_MUTARRAY before insertObject:a atIndex:0");
351339
[object insertObject:@"a" atIndex:0];
352-
NSLog(@"NS_MUTARRAY after insertObject:a atIndex:0");
353-
NSLog(@"NS_MUTARRAY before removeObjectAtIndex:2");
354340
[object removeObjectAtIndex:2];
355-
NSLog(@"NS_MUTARRAY after removeObjectAtIndex:2");
356-
NSLog(@"NS_MUTARRAY before removeLastObject");
357341
[object removeLastObject];
358-
NSLog(@"NS_MUTARRAY after removeLastObject");
359-
NSLog(@"NS_MUTARRAY before set object[3]");
360342
object[3] = @"d";
361-
NSLog(@"NS_MUTARRAY after set object[3]");
362-
NSLog(@"NS_MUTARRAY before count/hash");
363343
TNSLog([NSString stringWithFormat:@"%tu%tu", [object count], [object hash]]);
364-
NSLog(@"NS_MUTARRAY after count/hash");
365344

366-
NSLog(@"NS_MUTARRAY before enumerate");
367345
for (id x in object) {
368-
NSLog(@"NS_MUTARRAY enumerate value %@", x);
369346
TNSLog([NSString stringWithFormat:@"%@", x]);
370347
}
371-
NSLog(@"NS_MUTARRAY after enumerate");
372348
}
373349

374350
+ (void)apiSwizzle:(TNSSwizzleKlass*)object {

test/runtime/runner/app/tests/ApiTests.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,10 @@ describe(module.id, function () {
247247
});
248248

249249
it("NSMutableArrayMethods", function () {
250-
console.log("NS_MUTARRAY_JS spec enter");
251250
var JSMutableArray = NSMutableArray.extend({
252251
init: function () {
253-
console.log("NS_MUTARRAY_JS init enter");
254252
var self = NSMutableArray.prototype.init.apply(this, arguments);
255253
self._array = [];
256-
console.log("NS_MUTARRAY_JS init exit");
257254
return self;
258255
},
259256
// TODO
@@ -263,60 +260,38 @@ describe(module.id, function () {
263260
// NSMutableArray.prototype.dealloc.apply(this, arguments);
264261
// },
265262
insertObjectAtIndex: function (anObject, index) {
266-
console.log("NS_MUTARRAY_JS insertObjectAtIndex enter", anObject, index);
267263
this._array.splice(index, 0, anObject);
268-
console.log("NS_MUTARRAY_JS insertObjectAtIndex exit", this._array.length);
269264
},
270265
removeObjectAtIndex: function (index) {
271-
console.log("NS_MUTARRAY_JS removeObjectAtIndex enter", index);
272266
this._array.splice(index, 1);
273-
console.log("NS_MUTARRAY_JS removeObjectAtIndex exit", this._array.length);
274267
},
275268
addObject: function (anObject) {
276-
console.log("NS_MUTARRAY_JS addObject enter", anObject);
277269
this._array.push(anObject);
278-
console.log("NS_MUTARRAY_JS addObject exit", this._array.length);
279270
},
280271
removeLastObject: function () {
281-
console.log("NS_MUTARRAY_JS removeLastObject enter");
282272
this._array.pop();
283-
console.log("NS_MUTARRAY_JS removeLastObject exit", this._array.length);
284273
},
285274
replaceObjectAtIndexWithObject: function (index, anObject) {
286-
console.log("NS_MUTARRAY_JS replaceObjectAtIndexWithObject enter", index, anObject);
287275
this._array[index] = anObject;
288-
console.log("NS_MUTARRAY_JS replaceObjectAtIndexWithObject exit", this._array.length);
289276
},
290277
objectAtIndex: function (index) {
291-
var value = this._array[index];
292-
console.log("NS_MUTARRAY_JS objectAtIndex", index, value);
293-
return value;
278+
return this._array[index];
294279
},
295280
get count() {
296-
var count = this._array.length;
297-
console.log("NS_MUTARRAY_JS count", count);
298-
return count;
281+
return this._array.length;
299282
},
300283
get hash() {
301-
var hash = this.count;
302-
console.log("NS_MUTARRAY_JS hash", hash);
303-
return hash;
284+
return this.count;
304285
}
305286
}, {
306287
name: 'JSMutableArray'
307288
});
308-
console.log("NS_MUTARRAY_JS class ready");
309289

310290
(function () {
311-
console.log("NS_MUTARRAY_JS before new");
312291
var array = new JSMutableArray();
313-
console.log("NS_MUTARRAY_JS after new");
314-
console.log("NS_MUTARRAY_JS before native");
315292
TNSTestNativeCallbacks.apiNSMutableArrayMethods(array);
316-
console.log("NS_MUTARRAY_JS after native");
317293
}());
318294
gc();
319-
console.log("NS_MUTARRAY_JS after gc");
320295

321296
expect(TNSGetOutput()).toBe('44abcd');
322297
});

0 commit comments

Comments
 (0)