NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore

Home Page:http://docs.nativescript.org/runtimes/ios

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Object.defineProperty methods cannot be invoked from Objective-C

Mitko-Kerezov opened this issue · comments

Brief description

Methods declared inside a JavaScript class which are constructed using the following template:
(where MyClass is some JavaScript class extending some Objective-C class (with __extends))

Object.defineProperty(MyClass, "methodName", {
    get: function () {
        // code ...
    },
    set: function (value) {
           // code ...
    }
});

Are not accessible from Objective-C - when asked respondsToSelector the class answers with NO as if no Objective-C counterpart of said method is present at all.

Repro

I've devised a quick app in the playground to showcase the issue. The actual repro of the issue is located inside home.component.ts

Example

An example can be seen in this PR:
hypery2k/nativescript-urlhandler#81

When inside the running application from within Objective-C native code one tries to swizzle application:openURL:options: of the application delegate and uses the following code:

-(BOOL) swizzled_application:(UIApplication *)application
    openURL:(NSURL *)url
    options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    if ([self respondsToSelector:@selector(swizzled_application:openURL:options:)])
    {
        NSLog(@"Original method exists!!");
        return [self swizzled_application:application openURL:url options:options];
    }
    
    NSLog(@"Original method does NOT exist!!");
    return NO;
}

Running the code shows that the delegate does not respond to swizzled_application:openURL:options: when the method applicationOpenURLOptions is defined as Object.defineProperty. This leads to inability to invoke the JavaScript method from native code.
Whereas after the change in the proposed PR the method applicationOpenURLOptions is defined as delegate["applicationOpenURLOptions"] = function () { ... } then the delegate responds correctly to swizzled_application:openURL:options: and the JavaScript method can be invoked.

Yep I believe this is also the case for me. Thanks for the explanation 👍