Skip to content

Commit 7c38b44

Browse files
authored
Merge pull request #1077 from NativeScript/bektchiev/release-native-counterpart
fix(api): Correct argument check in __releaseNativeCounterpart
2 parents 212ee64 + cd87a9c commit 7c38b44

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/NativeScript/GlobalObject.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static EncodedJSValue JSC_HOST_CALL releaseNativeCounterpart(ExecState* execStat
133133
}
134134

135135
auto arg0 = execState->argument(0);
136-
auto wrapper = jsCast<ObjCWrapperObject*>(arg0);
136+
auto wrapper = jsDynamicCast<ObjCWrapperObject*>(execState->vm(), arg0);
137137
if (!wrapper) {
138138
auto scope = DECLARE_THROW_SCOPE(execState->vm());
139139
WTF::String message("Argument is an object which is not a native wrapper.");

tests/TestRunner/app/ApiTests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,32 @@ describe(module.id, function () {
475475
const output = TNSGetOutput();
476476
expect(output).toBe("TNSAllocLog initTNSAllocLog dealloc");
477477
});
478+
479+
it("throws when object is not a native wrapper", function () {
480+
const expectedError = /argument.*not a native wrapper/i;
481+
expect(() => __releaseNativeCounterpart(0)).toThrowError(expectedError);
482+
expect(() => __releaseNativeCounterpart("")).toThrowError(expectedError);
483+
expect(() => __releaseNativeCounterpart([])).toThrowError(expectedError);
484+
expect(() => __releaseNativeCounterpart({})).toThrowError(expectedError);
485+
expect(() => __releaseNativeCounterpart(null)).toThrowError(expectedError);
486+
expect(() => __releaseNativeCounterpart(undefined)).toThrowError(expectedError);
487+
});
488+
489+
it("sets object to nil", function () {
490+
var arr = NSArray.arrayWithArray([1,2,3]);
491+
expect(arr.count).toBe(3);
492+
__releaseNativeCounterpart(arr);
493+
494+
expect(arr.toString()).toBe(null);
495+
expect(typeof arr).toBe(typeof {});
496+
497+
// Extract from [Working with nil](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW22):
498+
// If you expect a return value from a message sent to nil, the return value will be
499+
// nil for object return types, 0 for numeric types, and NO for BOOL types. Returned
500+
// structures have all members initialized to zero.
501+
expect(arr.count).toBe(0);
502+
});
503+
478504
});
479505
describe("async", function () {
480506
it("should work", function (done) {

0 commit comments

Comments
 (0)