Function calling method, device, electronic device and storage medium

Through the message forwarding mechanism of Objective-C, unimplemented function call messages are forwarded to exception handling classes, and functions are dynamically added, which solves the problem of function call crashes during APP running, achieves the effect of automatically catching exceptions and reducing crashes, and is suitable for APPs and systems in Objective-C language.

CN112052054BActive Publication Date: 2024-08-30BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202010910550.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-02
Publication Date
2024-08-30
Estimated Expiration
2040-09-02

AI Technical Summary

Technical Problem

During the running of the APP, when the function is called, the object does not implement the specified function, resulting in a crash problem. Especially in the Objective-C language, dynamic substitution and weak typing lead to frequent crashes, which is difficult to effectively solve with existing technology.

Method used

Through the message forwarding mechanism of Objective-C, resolveInstanceMethod and forwardingTargetForSelector are used to forward unimplemented function call messages to the exception handling class, and dynamically add functions to implement the call to reduce crashes.

Benefits of technology

It realizes the automatic capture of exceptions during function calls without the APP developers being aware of it, reducing program crashes. It has a wide range of processing, and can handle both custom classes and related crashes caused by system classes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112052054B
    Figure CN112052054B
Patent Text Reader

Abstract

The embodiments of the present application provide a function calling method, device, electronic device and storage medium, which forwards the call request of an unimplemented function through a message forwarding mechanism, forwards the call message of the function to an exception handling class using forwardingTargetForSelector, and dynamically adds functions using the exception handling class to implement the function call, and can absorb the function calls that are not defined by the original calling class, reducing the crash when calling the function. By utilizing the existing message forwarding mechanism process of Objective‑C, the existing code is slightly modified, and the APP developers are unaware of it. It can automatically capture exceptions in the function calling process without being processed by the APP developers, reducing program crashes. It can not only handle related exception crashes caused by custom classes, but also handle related crashes caused by system classes, with a wide range of processing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a function calling method, device, electronic device and storage medium. Background Art

[0002] With the development of computer technology, especially the emergence of smart phones, the number of APPs (Applications) has increased rapidly, and the requirements for APP performance indicators have become increasingly stringent. Among various indicators, the crash rate indicator is particularly important, because crashes seriously affect APP stability and user experience, and thus affect user reputation. Frequent APP crashes will cause user loss, which will affect the corporate image of operators. Therefore, security protection is needed in the code to reduce the crash rate, but in actual work, even if you are careful during the development process, bugs (vulnerabilities) are inevitable, which will cause APP crashes.

[0003] The inventor found in the research that in the prior art, when the APP is running, it will call different objects through dynamic replacement or forwarding, and it may not know who the real calling object is, and it is also not known whether the object implements the called function, which will cause the APP to crash. For example, the input parameter of the function is NSDictionary (dictionary), but the function received during the runtime may be NSArray (array). If a function implemented in NSDictionary but not in NSArray is called, it will cause a crash. Summary of the invention

[0004] The purpose of the embodiments of the present application is to provide a function calling method, device, electronic device and storage medium to reduce the crash during the function calling process. The specific technical solution is as follows:

[0005] In a first aspect, an embodiment of the present application provides a function calling method, the method comprising:

[0006] When an object based on the Objective-C language calls a specified function, if the specified function is not implemented in the object and the base class, the message forwarding mechanism process is entered;

[0007] After entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the specified function;

[0008] If the resolveInstanceMethod returns a message indicating that the specified function call fails, forwardingTargetForSelector is used to forward the call message of the specified function to the exception handling class, so that the exception handling class determines whether the specified function is implemented in the exception handling class after receiving the call message of the specified function;

[0009] In the case where the specified function is not implemented in the exception handling class, adding the specified function using the exception handling class;

[0010] The specified function is called using the exception handling class.

[0011] In a possible implementation, if the resolveInstance Method returns a message indicating that the call of the specified function fails, forwarding the call message of the specified function to the exception handling class using forwardingTargetFor Selector, so that the exception handling class determines whether the specified function is implemented in the exception handling class after receiving the call message of the specified function, including:

[0012] If the resolveInstanceMethod returns a message indicating that the call of the specified function fails, determining whether the call message of the specified function is forwarded through the forwardingTargetForSelector of the object;

[0013] If the calling message of the specified function is not forwarded through the forwardingTargetForSelector of the object, the calling message of the specified function is forwarded to the exception handling class using the forwardingTargetForSelector in the NSObject class, so that after receiving the calling message of the specified function, the exception handling class determines whether the specified function is implemented in the exception handling class.

[0014] In a possible implementation, if the call message of the specified function is not forwarded by the forwardingTargetForSelector of the object, forwarding the call message of the specified function to the exception handling class by using the forwardingTargetForSelector in the NSObject class, so that the exception handling class determines whether the specified function is implemented in the exception handling class after receiving the call message of the specified function, including:

[0015] If the calling message of the specified function is not forwarded through the forwardingTargetForSelector of the object, use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established;

[0016] If the exception handling class has not been established, establish the exception handling class;

[0017] The calling message of the designated function is forwarded to the exception handling class by using forwardingTargetForSelector in the NSObject class, so that the exception handling class can determine whether the designated function is implemented in the exception handling class after receiving the calling message of the designated function.

[0018] In a possible implementation, after calling the specified function using the exception handling class, the method further includes:

[0019] Determine whether the object corresponds to Debug mode;

[0020] If the object corresponds to the Debug mode, a prompt message is generated and displayed, wherein the prompt message includes the calling class information and function information corresponding to the specified function.

[0021] In a second aspect, an embodiment of the present application provides a function calling device, the device comprising:

[0022] A forwarding mechanism startup module, used for, when an object based on the Objective-C language calls a specified function, if the specified function is not implemented in the object and the base class, entering the message forwarding mechanism process;

[0023] A first forwarding module, used to forward the call message of the specified function by using resolveInstanceMethod after entering the message forwarding mechanism process;

[0024] A second forwarding module is used for forwarding the call message of the specified function to the exception handling class by using forwardingTargetForSelector if the resolveInstanceMethod returns a message indicating that the call of the specified function fails, so that the exception handling class can determine whether the specified function is implemented in the exception handling class after receiving the call message of the specified function;

[0025] A function adding module, used for adding the specified function by using the exception handling class when the specified function is not implemented in the exception handling class;

[0026] A function calling module is used to call the specified function using the exception handling class.

[0027] In a possible implementation manner, the second forwarding module includes:

[0028] A message forwarding submodule, used for determining whether the calling message of the specified function is forwarded through the forwardingTargetForSelector of the object if the resolveInstanceMethod returns a message indicating that the calling of the specified function fails;

[0029] The exception handling class calls a submodule, which is used to forward the call message of the specified function to the exception handling class using the forwardingTargetForSelector in the NSObject class if the call message of the specified function is not forwarded through the forwardingTargetForSelector of the object, so that the exception handling class can determine whether the specified function is implemented in the exception handling class after receiving the call message of the specified function.

[0030] In a possible implementation, the exception handling class calls a submodule, specifically for:

[0031] If the calling message of the specified function is not forwarded through the forwardingTargetForSelector of the object, use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established;

[0032] If the exception handling class has not been established, establish the exception handling class;

[0033] The calling message of the designated function is forwarded to the exception handling class by using forwardingTargetForSelector in the NSObject class, so that the exception handling class can determine whether the designated function is implemented in the exception handling class after receiving the calling message of the designated function.

[0034] In a possible implementation, the device further includes:

[0035] A mode determination module, used to determine whether the object corresponds to the Debug mode;

[0036] The prompt information display module is used to generate and display prompt information if the object corresponds to the Debug mode, wherein the prompt information includes the calling class information and function information corresponding to the specified function.

[0037] In a third aspect, an embodiment of the present application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;

[0038] Memory, used to store computer programs;

[0039] A processor is used to implement the function calling method described in any one of the above claims when executing a program stored in a memory.

[0040] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the computer program implements any of the above-mentioned function calling methods.

[0041] In a fifth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any of the function calling methods described above.

[0042] The function calling method, device, electronic device and storage medium provided by the embodiment of the present application, when an object based on the Objective-C language calls a specified function, if the specified function is not implemented in the object and the base class, enter the message forwarding mechanism process; after entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the specified function; if resolveInstanceMethod returns a message indicating that the call of the specified function fails, use forwardingTargetForSelector to forward the call message of the specified function to the exception handling class, so that the exception handling class can determine whether the specified function is implemented in the exception handling class after receiving the call message of the specified function; if the specified function is not implemented in the exception handling class, use the exception handling class to add the specified function; use the exception handling class to call the specified function. The call request of the unimplemented function is forwarded through the message forwarding mechanism, the call message of the function is forwarded to the exception handling class using forwardingTargetForSelector, and the function is dynamically added using the exception handling class, so as to realize the call of the function, which can absorb the function call that is not defined by the original calling class and reduce the crash when calling the function. By utilizing the existing message forwarding mechanism process of Objective-C, the existing code is modified slightly, and the APP developers are unaware of it. It can automatically capture exceptions in the function call process without the APP developers handling them, thus reducing program crashes. It can not only handle related exception crashes generated by custom classes, but also handle related crashes generated by system classes, with a wide range of processing. Of course, implementing any product or method of this application does not necessarily require achieving all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art are briefly introduced below.

[0044] Figure 1 This is a first schematic diagram of the function calling method according to an embodiment of the present application;

[0045] Figure 2a A schematic diagram of a message forwarding mechanism based on Objective-C in the prior art;

[0046] Figure 2b Another flowchart of a message forwarding mechanism based on Objective-C in the prior art is shown;

[0047] Figure 3 for Figure 1 A schematic diagram of a specific implementation method of step S13 in the embodiment shown;

[0048] Figure 4 for Figure 3 A schematic diagram of a specific implementation method of step S132 in the embodiment shown;

[0049] Figure 5 A second schematic diagram of the function calling method according to an embodiment of the present application;

[0050] Figure 6 A third schematic diagram of the function calling method according to an embodiment of the present application;

[0051] Figure 7 A schematic diagram of a function calling device according to an embodiment of the present application;

[0052] Figure 8 A schematic diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0053] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application.

[0054] The inventor found in the research that if an unimplemented function is called during the running process of the APP, the APP will crash, especially for weakly typed languages ​​such as Objective-C (an object-oriented programming language that extends C). Objective-C is a dynamic and weakly typed language. The advantage of this language is its great flexibility. It can perform some dynamic replacement or forwarding operations at runtime to optimize the design. However, it also brings disadvantages. During compilation, it is unknown who the real calling object is, and it is also unknown whether the object implements the called function, so it crashes more frequently.

[0055] In order to reduce the crash of an APP or system written based on Objective-C when calling a function, an embodiment of the present application provides a function calling method, which includes:

[0056] When an object based on the Objective-C language calls a specified function, if the specified function is not implemented in the above object and its base class, the message forwarding mechanism process is entered;

[0057] After entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the above specified function;

[0058] If the resolveInstanceMethod returns a message indicating that the call of the specified function fails, forwardingTargetForSelector is used to forward the call message of the specified function to the exception handling class, so that the exception handling class can determine whether the specified function is implemented in the exception handling class after receiving the call message of the specified function.

[0059] If the specified function is not implemented in the exception handling class, add the specified function using the exception handling class;

[0060] Use the above exception handling class to call the above specified function.

[0061] In an embodiment of the present application, a message forwarding mechanism is used to forward call requests for unimplemented functions, forwardingTargetForSelector is used to forward the call message of the function to an exception handling class, and the exception handling class is used to dynamically add functions to implement the function call. This can absorb function calls that are not defined by the original calling class and reduce crashes when calling functions.

[0062] The following is a detailed description:

[0063] See also Figure 1 , Figure 1 This is a first flow chart of a function calling method according to an embodiment of the present application, the method comprising:

[0064] S11, when an object based on the Objective-C language calls a specified function, if the specified function is not implemented in the object and the base class, the message forwarding mechanism process is entered.

[0065] The function calling method of the embodiment of the present application can be implemented by an electronic device. Specifically, the electronic device can be a smart phone, a tablet computer, or a personal computer.

[0066] The function calling method of the embodiment of the present application is for an APP or system based on the Objective-C language. The specified function can be any related function. When an object written in the Objective-C language calls the specified function, if the specified function is not implemented in the object and the base class, the message forwarding mechanism process is performed. Optionally, it is possible to query whether the specified function exists in the function cache and function list of the object and the base class. If not, it is determined that the specified function is not implemented in the object and the base class.

[0067] S12, after entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the above specified function.

[0068] The process of Objective-C's message forwarding mechanism can be as follows Figure 2a As shown, there are three chances to save the crash in this process, namely: resolveInstanceMethod, forwardingTargetForSelector and forwardInvocation. resolveInstanceMethod is used to provide an opportunity for the class to which the current object belongs to add its own implementation of the specified function to handle the crash; if resolveInstanceMethod returns yes, it means that the specified function has been added successfully. If it returns no, it means that the specified function has failed to be added and needs to be processed through other functions. forwardingTargetForSelector is used to provide an opportunity to forward to other objects for processing. forwardInvocation is used to provide an opportunity to forward messages to multiple objects in the form of NSInvocation. Specifically, the function calls in the message forwarding mechanism process can be as follows Figure 2b shown.

[0069] S13, if the above-mentioned resolveInstanceMethod returns a message indicating that the call of the above-mentioned specified function fails, the call message of the above-mentioned specified function is forwarded to the exception handling class using forwardingTargetForSelector, so that the above-mentioned exception handling class can determine whether the above-mentioned specified function is implemented in the above-mentioned exception handling class after receiving the call message of the above-mentioned specified function.

[0070] If the above resolveInstanceMethod returns a message indicating that the above specified function call failed, it means that the class to which the current object belongs fails to add the specified function, and forwardingTargetForSelector needs to be called to continue processing. forwardingTargetForSelector forwards the call message of the specified function to the exception handling class, so that the exception handling class can handle the call message of the specified function. The exception handling class can be established in advance or in real time, both of which are within the scope of protection of this application.

[0071] In a possible implementation, when the resolveInstanceMethod returns a message indicating that the specified function call has failed, and the exception handling class has not been established in the local system, it is necessary to first establish the exception handling class. Specifically, the declaration part and implementation part of the exception handling class can be directly created in the local system to establish the exception handling class. For example, the declaration part of the exception handling class can be created through the @interface and @end instructions, and the implementation part of the exception handling class can be created through the @implementation and @end instructions. After the exception handling class is established, it can be directly called in subsequent use.

[0072] S14, when the specified function is not implemented in the exception handling class, adding the specified function using the exception handling class.

[0073] If the specified function is not implemented in the exception handling class, add the specified function to the exception handling class. For example, you can use the runtime (memory management) related API (Application Programming Interface) to add the above specified function. The parameter of the forwardingTargetForSelector function is the Selector (SEL type) of the calling function. The exception handling class can use the runtime related API to determine whether the specified function is implemented. If not, you can add the corresponding function through the runtime's class_addMethod (Class_Nullable cls, SEL_Nonnull name, IMP_Nonnull imp, const char*_Nullable types) API. The structure of the Method in the runtime is as follows:

[0074]

[0075] From the above definition, it can be seen that Method consists of three parts. The function uses SEL to find the corresponding IMP to execute. For convenience, all Selectors passed by forwardingTargetForSelector can be mapped to the same function of the exception handling class for unified exception handling.

[0076] S15, using the above exception handling class to call the above specified function.

[0077] When the above-mentioned designated function is implemented in the exception handling class, the above-mentioned designated function is called by using the exception handling class, thereby implementing the calling of the designated function.

[0078] The embodiment of the present application implements forwardingTargetForSelector by overriding the function to forward the call message of the specified function to the exception handling class. As shown in FIG2 , there are three functions in the Objective-C message forwarding mechanism process that can implement message forwarding, namely, resolveInstanceMethod, forwardingTargetForSelector, and forwardInvocation.

[0079] If resolveInstanceMethod is overridden, an exception handling class needs to be added for each object that calls an unimplemented function, making the code more complicated, unable to achieve non-invasiveness, and inconvenient to handle system class situations. If forwardInvocation is overridden, a new NSInvocation object needs to be created. The cost of overriding is high, and the forwardInvocation function is often overridden by the multi-layer message forwarding selection mechanism. Overriding it again when the forwardInvocation function is overridden can easily lead to calling errors. Therefore, in this application, forwardingTargetForSelector is overridden, and forwardingTargetForSelector is used to forward the call message of the specified function to the exception handling class. The overriding change is small and the cost of the change is low.

[0080] In the embodiment of the present application, the call request of the unimplemented function is forwarded through the message forwarding mechanism, the call message of the function is forwarded to the exception handling class using forwardingTargetForSelector, and the function is dynamically added using the exception handling class to implement the function call, which can absorb the function call that is not defined by the original calling class and reduce the crash when calling the function. By using the existing message forwarding mechanism process of Objective-C, the existing code is slightly changed, and the APP developers are unaware. It can automatically capture the exception in the function call process without the APP developers handling it, reducing the program crash. It can not only handle the related exception crashes caused by custom classes, but also handle the related crashes caused by system classes (such as NSDictionary, NSArray, etc.), with a wide range of processing.

[0081] In one possible implementation, see Figure 3 , if the resolveInstanceMethod returns a message indicating that the call of the specified function fails, forwardingTargetForSelector is used to forward the call message of the specified function to the exception handling class, so that the exception handling class determines whether the specified function is implemented in the exception handling class after receiving the call message of the specified function, including:

[0082] S131, if the resolveInstanceMethod returns a message indicating that the calling of the specified function fails, it is determined whether the calling message of the specified function is forwarded through the forwardingTargetForSelector of the object.

[0083] When resolveInstanceMethod returns a message indicating that the above specified function call failed, the first step is to determine whether the above object has overridden forwardingTargetForSelector to forward the call message. In some scenarios, there will be a situation where the object overrides forwardingTargetForSelector to forward the call message. In this case, it can be handled according to the object's own forwarding logic.

[0084] S132: If the calling message of the above-mentioned specified function is not forwarded through the forwardingTargetForSelector of the above-mentioned object, the calling message of the above-mentioned specified function is forwarded to the exception handling class using the forwardingTargetForSelector in the NSObject class, so that the above-mentioned exception handling class can determine whether the above-mentioned specified function is implemented in the above-mentioned exception handling class after receiving the calling message of the above-mentioned specified function.

[0085] Optionally, you can pre-add an NSObject category and override the forwardingTargetForSelector function in the NSObject category. Use the forwardingTargetForSelector function to forward the call message of the specified function to a local custom exception handling class for processing.

[0086] Since NSObject is the base class of all classes, and the functions in the category have a higher priority, when the class of the current object does not override forwardingTargetForSelector, the message forwarding mechanism will be called to the function in the category, so it can be automatically processed. Since NSObject is the base class of all classes, it covers both custom classes and system classes. Since it is implemented in the NSObject category, no changes are made to other classes, so it is not perceived during the development process.

[0087] Exception handling classes can be created in advance or in a lazy loading manner, optional, see Figure 4 , if the calling message of the specified function is not forwarded by the forwardingTargetForSelector of the object, the calling message of the specified function is forwarded to the exception handling class by using the forwardingTargetForSelector in the NSObject class, so that the exception handling class determines whether the specified function is implemented in the exception handling class after receiving the calling message of the specified function, including:

[0088] S1321, if the calling message of the above-mentioned specified function is not forwarded through the forwardingTargetForSelector of the above-mentioned object, use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established.

[0089] S1322: If the above exception handling class has not been established, establish the above exception handling class.

[0090] S1323, forwardingTargetForSelector in the NSObject class is used to forward the calling message of the specified function to the exception handling class, so that after receiving the calling message of the specified function, the exception handling class determines whether the specified function is implemented in the exception handling class.

[0091] In an embodiment of the present application, an exception handling class is created by lazy loading. When the above-mentioned function call exception does not occur, the exception handling class is not created, thereby saving memory overhead. When an exception occurs, the exception handling class is created, thereby reducing crashes.

[0092] In one possible implementation, see Figure 5 After calling the specified function using the exception handling class, the method further includes:

[0093] S16, determining whether the above object corresponds to the Debug mode.

[0094] There are usually two modes for developing programs: Debug mode and Release mode. In Debug mode, the compiler will record a lot of debugging information and can also add a lot of test code to facilitate developers to test and analyze and solve bugs when they occur.

[0095] S17, if the above object corresponds to the Debug mode, generate and display prompt information, wherein the above prompt information includes the calling class information and function information corresponding to the above designated function.

[0096] If the above object corresponds to the Debug mode, that is, the APP described by the object is developed in the Debug mode, a prompt message can be generated and displayed. Specifically, the prompt message can be expressed in the form of a prompt box, and the prompt message can include the call class information and function information corresponding to the specified function. For the Release mode, the prompt message can be sent to the server, etc.

[0097] In the embodiment of the present application, by displaying prompt information, it is convenient to display and count abnormal situations in the function call process.

[0098] The function calling method of the embodiment of the present application can also be as follows Figure 6 As shown, including:

[0099] S201, the specified function called by the target object is not implemented, and the message forwarding mechanism process is entered.

[0100] S202, determine whether resolveInstanceMethod returns YES. If resolveInstanceMethod returns YES, execute S203, if resolveInstanceMethod returns NO, execute S204.

[0101] After entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the specified function; determine whether resolveInstanceMethod returns YES or NO.

[0102] S203: Determine whether the message has been processed.

[0103] S204, determine whether the target object has overridden forwardingTargetForSelector to forward the message. If yes, execute S205, if not, execute S206.

[0104] S205: Process according to the forwarding logic of the target object.

[0105] S206, calling the forwardingTargetForSelector function in the NSObject class.

[0106] S207, determine whether the exception handling class has been created. If the exception handling class has not been created, execute S208; if the exception handling class has been created, execute S209.

[0107] S208, creating an exception handling class.

[0108] S209, determining whether the exception handling class has implemented the specified function. If the exception handling class has not implemented the specified function, execute S210; if the exception handling class has implemented the specified function, execute S211.

[0109] S210, adding the specified function.

[0110] S211, determine whether it is Debug mode. If yes, execute S212, if no, execute S213.

[0111] S212, a debugging prompt box pops up to indicate an error message.

[0112] S213, forwardingTargetForSelector returns an exception handling class.

[0113] In the embodiment of the present application, the call request of the unimplemented function is forwarded through the message forwarding mechanism, the call message of the function is forwarded to the exception handling class using forwardingTargetForSelector, and the function is dynamically added using the exception handling class to implement the function call, which can absorb the function call that is not defined by the original calling class and reduce the crash when calling the function. By using the existing message forwarding mechanism process of Objective-C, the existing code is slightly changed, and the APP developers are unaware. It can automatically capture the exception in the function call process without the APP developers handling it, reducing the program crash. It can not only handle the related exception crashes caused by custom classes, but also handle the related crashes caused by system classes (such as NSDictionary, NSArray, etc.), with a wide range of processing.

[0114] The present application also provides a function calling device, see Figure 7 , the device comprises:

[0115] The forwarding mechanism starting module 701 is used to enter the message forwarding mechanism process when an object based on the Objective-C language calls a specified function if the specified function is not implemented in the object and the base class;

[0116] The first forwarding module 702 is used to forward the call message of the above specified function by using resolveInstanceMethod after entering the message forwarding mechanism process;

[0117] The second forwarding module 703 is used for forwarding the calling message of the specified function to the exception handling class by using forwardingTargetForSelector if the resolveInstanceMethod returns a message indicating that the calling of the specified function fails, so that the exception handling class can determine whether the specified function is implemented in the exception handling class after receiving the calling message of the specified function;

[0118] A function adding module 704, used for adding the specified function by using the exception handling class when the specified function is not implemented in the exception handling class;

[0119] The function calling module 705 is used to call the above-mentioned specified function using the above-mentioned exception handling class.

[0120] In a possible implementation manner, the second forwarding module includes:

[0121] The message forwarding submodule is used to determine whether the calling message of the specified function is forwarded through the forwardingTargetForSelector of the object if the resolveInstanceMethod returns a message indicating that the calling of the specified function fails;

[0122] The exception handling class calls a submodule, which is used to forward the call message of the above-mentioned specified function to the exception handling class using the forwardingTargetForSelector in the NSObject class if the call message of the above-mentioned specified function is not forwarded through the forwardingTargetForSelector of the above-mentioned object, so that the above-mentioned exception handling class can determine whether the above-mentioned specified function is implemented in the above-mentioned exception handling class after receiving the call message of the above-mentioned specified function.

[0123] In a possible implementation, the above exception handling class calls a submodule, specifically for:

[0124] If the calling message of the above specified function is not forwarded through the forwardingTargetForSelector of the above object, use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established;

[0125] If the above exception handling class has not been established, establish the above exception handling class;

[0126] The calling message of the above-mentioned designated function is forwarded to the exception handling class by using forwardingTargetForSelector in the above-mentioned NSObject class, so that the above-mentioned exception handling class can determine whether the above-mentioned designated function is implemented in the above-mentioned exception handling class after receiving the calling message of the above-mentioned designated function.

[0127] In a possible implementation manner, the above device further includes:

[0128] A mode judgment module is used to judge whether the above object corresponds to the Debug mode;

[0129] The prompt information display module is used to generate and display prompt information if the above object corresponds to the Debug mode, wherein the above prompt information includes the calling class information and function information corresponding to the above specified function.

[0130] The present application also provides an electronic device, such as Figure 8As shown, it includes a processor 801, a communication interface 802, a memory 803 and a communication bus 804, wherein the processor 801, the communication interface 802, and the memory 803 communicate with each other through the communication bus 804.

[0131] Memory 803, used for storing computer programs;

[0132] The processor 801 is used to implement any of the above-mentioned function calling methods when executing the program stored in the memory 803.

[0133] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0134] The communication interface is used for communication between the above terminal and other devices.

[0135] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0136] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0137] In another embodiment provided in the present application, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the function calling method described in any one of the above embodiments is implemented.

[0138] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute the function calling method described in any one of the above embodiments.

[0139] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.

[0140] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0141] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the embodiments of the device, electronic device, storage medium and computer program product, since they are basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0142] The above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application are included in the protection scope of the present application.

Claims

1. A function calling method, characterized in that: The method comprises: When an object based on the Objective-C language calls a specified function, query whether the specified function exists in the function cache and function list of the object and the base class; if the specified function does not exist in the function cache and function list of the object and the base class, determine that the specified function is not implemented in the object and the base class; if the specified function is not implemented in the object and the base class, enter the message forwarding mechanism process; After entering the message forwarding mechanism process, use resolveInstanceMethod to forward the call message of the specified function; If the resolveInstanceMethod returns a message indicating that the specified function call failed, determine whether the call message of the specified function is forwarded through the forwardingTargetForSelector of the object; If the call message of the specified function is not forwarded through the forwardingTargetForSelector of the object, add the category of NSObject, overwrite forwardingTargetForSelector in the category of NSObject, and use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established; if the exception handling class has not been established, establish the exception handling class; use the forwardingTargetForSelector in the NSObject class to forward the call message of the specified function to the exception handling class, so that after receiving the call message of the specified function, the exception handling class determines whether the specified function is implemented in the exception handling class through the application program interface API related to the memory management runtime; If the specified function is not implemented in the exception handling class, add the specified function using the exception handling class through the class_addMethod API of the runtime; The specified function is called using the exception handling class.

2. The method according to claim 1, characterized in that: After calling the specified function using the exception handling class, the method further includes: Determine whether the object corresponds to Debug mode; If the object corresponds to the Debug mode, a prompt message is generated and displayed, wherein the prompt message includes the calling class information and function information corresponding to the specified function.

3. A function calling device, characterized in that: The device comprises: A forwarding mechanism startup module is used to query whether the specified function exists in the function cache and function list of the object and the base class when the object based on the Objective-C language calls the specified function; if the specified function does not exist in the function cache and function list of the object and the base class, determine that the specified function is not implemented in the object and the base class; if the specified function is not implemented in the object and the base class, enter the message forwarding mechanism process; A first forwarding module, used to forward the call message of the specified function by using resolveInstanceMethod after entering the message forwarding mechanism process; The second forwarding module is used to determine whether the call message of the specified function is forwarded through the forwardingTargetForSelector of the object if the resolveInstanceMethod returns a message indicating that the call of the specified function fails; if the call message of the specified function is not forwarded through the forwardingTargetForSelector of the object, add the category of NSObject, overwrite forwardingTargetForSelector in the category of NSObject, and use the forwardingTargetForSelector in the NSObject class to determine whether the exception handling class has been established; if the exception handling class has not been established, establish the exception handling class; use the forwardingTargetForSelector in the NSObject class to forward the call message of the specified function to the exception handling class, so that after receiving the call message of the specified function, the exception handling class determines whether the specified function is implemented in the exception handling class through the application program interface API related to the memory management runtime; A function adding module, used for adding the specified function by using the exception handling class through the class_addMethod API of the runtime when the specified function is not implemented in the exception handling class; A function calling module is used to call the specified function using the exception handling class.

4. The device according to claim 3, characterized in that The device also includes: A mode determination module, used to determine whether the object corresponds to the Debug mode; The prompt information display module is used to generate and display prompt information if the object corresponds to the Debug mode, wherein the prompt information includes the calling class information and function information corresponding to the specified function.

5. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1-2 when executing a program stored in a memory.

6. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1-2 are implemented.

Citation Information

Patent Citations

  • System running method and operation system

    CN107301095A

  • Message forwarding method, computer readable storage medium and electronic device

    CN108469993A

  • Method and device for preventing an application program from collapsing

    CN110083474A

  • Function calling method and device, electronic equipment and storage medium

    CN111240850A

  • Method and system for providing enhanced exception messages for exceptions thrown by virtual machines

    US20080244324A1