Function calling method and device and electronic equipment

By storing the correspondence between preset index and function pointer in the WASM module, the problem that the Native module cannot directly call external functions is solved, and calls to other program module functions are realized, and the range of callable functions is expanded.

CN120029695APending Publication Date: 2025-05-23HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311586244.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-11-23
Publication Date
2025-05-23

AI Technical Summary

Technical Problem

In the prior art, the Native module cannot directly call external functions through the WASM module because the function pointer and index of these functions are not stored in the WASM module.

Method used

By storing the correspondence between the preset index and the function pointer in the second program module, the first program module can obtain the index in the preset index and send a message to indicate the execution of the objective function. The second program module executes the objective function according to the index, thereby calling the function to be called.

Benefits of technology

It implements that the Native module or other program modules can call functions other than the WASM module, extending the scope of functions that the program module can call.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029695A_ABST
    Figure CN120029695A_ABST
Patent Text Reader

Abstract

The invention discloses a function calling method and device and electronic equipment, relates to the technical field of computers, and can enable a program module to call functions in other program modules. According to the specific scheme, a first index in preset indexes can be obtained through a first program module, the first index is used for indicating a function pointer of a first function, and the first function is used for calling a to-be-called function. Afterwards, a first message can be sent to the second program module through the first program module, the first message is used for indicating execution of the target function, the target function is used for calling the first function, and the first message comprises the first index. Then, in response to the first message, the target function can be executed through a second program module according to the first index, and a corresponding relation between the first index and a function pointer of the first function is stored in the second program module.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] With the development of science and technology, the number of programming languages ​​has gradually increased, and developers can use multiple programming languages ​​to develop applications. When the application is running, function pointers pointing to functions can be passed as parameters between different programming languages ​​to implement the call of function interfaces between different programming languages.

[0003] Taking the WebAssembly (WASM) module and Native module in the application as an example, the WASM module stores the function pointer of each function in the WASM module, and each function pointer corresponds to an index. The Native module can pass the index corresponding to the function pointer of the function in the WASM module to the WASM module, so that the WASM module can call the function pointer corresponding to the index through the index, thereby calling the function inside the WASM module.

[0004] However, in the above technical solution, the Native module can only call the functions in the WASM module through the function interface call or parameter passing. If the Native module calls a function outside the WASM module (such as function A) through the WASM module, since the function pointer and index of function A are not stored in the WASM module, the WASM module cannot call the function A through parameter passing. Therefore, in the function interface call between program modules, how to ensure that the program module can call the functions in other program modules has become a technical problem that needs to be solved urgently. Summary of the invention

[0005] The present application provides a function calling method, device and electronic device, so that a program module can call functions in other program modules.

[0006] In the first aspect, the present application provides a function calling method, which can be applied to an electronic device, in which a first program module and a second program module are deployed, and the second program module stores the correspondence between a preset index and a function pointer of a preset function, and the second program module is used to call the corresponding function pointer through the preset index. In this method, the first index in the preset index can be obtained through the first program module, and the first index is used to indicate the function pointer of the first function, and the first function is used to call the function to be called. Afterwards, a first message can be sent to the second program module through the first program module, and the first message is used to indicate the execution of a target function, and the target function is used to call the first function, and the first message includes a first index. Then, in response to the first message, the target function can be executed according to the first index through the second program module, and the correspondence between the first index and the function pointer of the first function is stored in the second program module.

[0007] Based on the above technical solution, the first program module can instruct the second program module to execute the target function. In addition, the first program module can send the first index to the second program module, because the second program module stores the correspondence between the first index and the function pointer of the first function. In the process of the second program module executing the target function, the second program module can call the function to be called through the function pointer of the first function. In this way, the second program module can call the function to be called without the function pointer and index of the function to be called. In this way, the second program module can not only call the functions within the second program module, but also call the functions in other program modules, which expands the functions that can be called by the second program module.

[0008] In combination with the first aspect, in a possible design, the first program module includes: a first import declaration, the first import declaration is used to indicate the import of a second function, the second function is used to obtain a first index, and the second program module includes: a second function, a first export declaration, the first export declaration is used to indicate the export of the second function. The method may also include: the second function may be exported by calling the first export declaration by the second program module. The second function may be imported by calling the first import declaration by the first program module.

[0009] In this way, after the second program module exports the second function and the first program module imports the second function, the first program module can directly call the second function.

[0010] In combination with the first aspect, in another possible design manner, the above-mentioned method of "obtaining a first index in a preset index through a first program module" may include: calling a second function through the first program module to obtain the first index.

[0011] In this way, the first program module can directly call the second function to obtain the first index, so that the first program module can trigger the second program module to call the first function through the first index, and then call the function to be called.

[0012] In combination with the first aspect, in another possible design, the first program module further includes: a third function, a second export declaration, the first function is specifically used to call the function to be called through the third function, the second export declaration is used to export the second export declaration is used to indicate the export of the third function, and the second program module further includes: a second import declaration, the second import declaration is used to indicate the import of the third function. The method may also include: calling the second export declaration through the first program module to export the third function. Calling the second import declaration through the second program module to import the third function.

[0013] In this way, after the first program module exports the third function and the second program module imports the third function, the second program module can call the third function.

[0014] In combination with the first aspect, in another possible design method, the first program module also includes: a function to be called, and the above-mentioned method of "executing the target function according to the first index through the second program module" may include: determining the function pointer of the first function according to the first index through the second program module, and calling the first function to trigger the call of the third function. Sending a second message to the first program module through the second program module, the second message is used to indicate the execution of the third function. In response to the second message, the third function is executed through the first program module to trigger the call of the function to be called and obtain the execution result of the function to be called. Sending the execution result of the function to be called to the second program module through the first program module. Executing the target function through the second program module according to the execution result of the function to be called.

[0015] It is understandable that after the second program module determines the function pointer of the first function according to the first index, the first function can be called to trigger the call of the third function. Since the first function can call the function to be called through the third function, when the first program module executes the third function, the function to be called can be called to obtain and send the execution result of the function to be called to the second program module. In this way, the second program module can call the function to be called to obtain the execution result of the function to be called and execute the target function without the need to use the function pointer and function index of the function to be called.

[0016] In combination with the first aspect, in another possible design, the method may further include: storing, through the first program module, a correspondence between a second index and a function pointer of the function to be called, wherein the second index is used to indicate the function pointer of the function to be called.

[0017] It can be understood that the first program module stores the correspondence between the second index and the function pointer of the to-be-called function, so that each program module can call the function pointer of the to-be-called function through the second index, thereby completing the calling of the function pointer.

[0018] In combination with the first aspect, in another possible design, the method may further include: obtaining a second index through a second program module, wherein the second message includes the second index.

[0019] It is understandable that the second message includes the second index, so that the first program module can determine the function pointer of the function to be called according to the second index. In this way, it is convenient for the first program module to execute the function pointer of the function to be called and obtain the execution result of the function to be called.

[0020] In combination with the first aspect, in another possible design, the function pointer information of the first function is the same as the function pointer information of the function to be called, and the function pointer information includes: parameter type and return value type.

[0021] It is understandable that the function pointer information of the first function includes the function pointer information of the function to be called, so that the first function can pass parameters to the function to be called, thereby ensuring that the first function can normally call the function to be called.

[0022] In combination with the first aspect, in another possible design, the first program module further includes: a function to be called, a third export declaration, the third export declaration is used to indicate the export of the function to be called, and the second program module further includes: a third import declaration, the third import declaration is used to indicate the import of the function to be called. The method further includes: exporting the function to be called by calling the third export declaration through the first program module. Importing the function to be called by calling the third import declaration through the second program module.

[0023] In this way, after the first program module exports the function to be called and the second program module imports the function to be called, the second program module can directly call the function to be called.

[0024] In combination with the first aspect, in another possible design method, the above-mentioned method of "executing the target function according to the first index through the second program module" may include: determining the function pointer of the first function according to the first index through the second program module, and calling the first function to trigger the call of the function to be called. Sending a third message to the first program module through the second program module, the third message is used to indicate the execution of the function to be called. In response to the third message, the function to be called is executed through the first program module to obtain the execution result of the function to be called. Sending the execution result of the function to be called to the second program module through the first program module. Executing the target function through the second program module according to the execution result of the function to be called.

[0025] It is understandable that after the second program module determines the function pointer of the first function according to the first index, the first function can be called to trigger the call of the function to be called. After that, the first program module executes the function to be called to obtain and send the execution result of the function to be called to the second program module. In this way, the second program module can call the function to be called to obtain the execution result of the function to be called and execute the target function without the need to use the function pointer and function index of the function to be called.

[0026] In combination with the first aspect, in another possible design manner, the first program module is a Native module, and the second program module is a WASM module.

[0027] In combination with the first aspect, in another possible design, the first module is composed of program code in a first programming language, and the second program module is composed of program code in a second programming language, wherein the first programming language is the same as the second programming language; or the first programming language is different from the second programming language.

[0028] In a second aspect, the present application provides a function calling device, in which a first program module and a second program module are deployed, the second program module stores a correspondence between a preset index and a function pointer of a preset function, and the second program module is used to call the corresponding function pointer through the preset index. The function calling device includes:

[0029] The acquisition unit is used to acquire a first index in a preset index through a first program module, the first index is used to indicate a function pointer of a first function, and the first function is used to call a function to be called. The sending unit is used to send a first message to a second program module through the first program module, the first message is used to indicate the execution of a target function, the target function is used to call the first function, and the first message includes a first index. The processing unit is used to execute the target function according to the first index through the second program module in response to the first message, and the second program module stores a correspondence between the first index and the function pointer of the first function.

[0030] In conjunction with the second aspect, in a possible design, the first program module includes: a first import declaration, the first import declaration is used to indicate the import of the second function, the second function is used to obtain the first index, and the second program module includes: the second function, a first export declaration, the first export declaration is used to indicate the export of the second function. The processing unit is further used to call the first export declaration through the second program module to export the second function. The processing unit is further used to call the first import declaration through the first program module to import the second function.

[0031] In combination with the second aspect, in another possible design manner, the acquisition unit is specifically configured to acquire the first index by calling the second function through the first program module.

[0032] In combination with the second aspect, in another possible design mode, the first program module further includes: a third function, a second export declaration, the first function is specifically used to call the function to be called through the third function, the second export declaration is used to export the second export declaration is used to indicate the export of the third function, and the second program module further includes: a second import declaration, the second import declaration is used to indicate the import of the third function. The processing unit is further used to call the second export declaration through the first program module to export the third function. The processing unit is further used to call the second import declaration through the second program module to import the third function.

[0033] In combination with the second aspect, in another possible design method, the first program module also includes: a function to be called. The processing unit is also used to determine the function pointer of the first function according to the first index through the second program module, and call the first function to trigger the call of the third function. The sending unit is also used to send a second message to the first program module through the second program module, and the second message is used to indicate the execution of the third function. The processing unit is also used to execute the third function through the first program module in response to the second message to trigger the call of the function to be called and obtain the execution result of the function to be called. The sending unit is also used to send the execution result of the function to be called to the second program module through the first program module. The processing unit is also used to execute the target function according to the execution result of the function to be called through the second program module.

[0034] In combination with the second aspect, in another possible design, the processing unit is further used to store the correspondence between the second index and the function pointer of the function to be called through the first program module, and the second index is used to indicate the function pointer of the function to be called.

[0035] In conjunction with the second aspect, in another possible design, the sending unit is further configured to obtain a second index through a second program module, wherein the second message includes the second index.

[0036] In combination with the second aspect, in another possible design, the function pointer information of the first function is the same as the function pointer information of the function to be called, and the function pointer information includes: parameter type and return value type.

[0037] In combination with the second aspect, in another possible design, the first program module further includes: a function to be called, a third export declaration, the third export declaration is used to indicate the export of the function to be called, and the second program module further includes: a third import declaration, the third import declaration is used to indicate the import of the function to be called. The processing unit is further used to call the third export declaration through the first program module to export the function to be called. The processing unit is further used to call the third import declaration through the second program module to import the function to be called.

[0038] In combination with the second aspect, in another possible design method, the processing unit is also used to determine the function pointer of the first function according to the first index through the second program module, and call the first function to trigger the call of the function to be called. The sending unit is also used to send a third message to the first program module through the second program module, and the third message is used to indicate the execution of the function to be called. The processing unit is also used to execute the function to be called through the first program module in response to the third message to obtain the execution result of the function to be called. The sending unit is also used to send the execution result of the function to be called to the second program module through the first program module. The processing unit is also used to execute the target function according to the execution result of the function to be called through the second program module.

[0039] In combination with the second aspect, in another possible design manner, the first program module is a Native module, and the second program module is a WASM module.

[0040] In conjunction with the second aspect, in another possible design, the first module is composed of program codes in a first programming language, and the second program module is composed of program codes in a second programming language, wherein the first programming language is the same as the second programming language; or the first programming language is different from the second programming language.

[0041] In a third aspect, the present application provides an electronic device, comprising: a memory and one or more processors, wherein the memory is coupled to the processor; the memory is used to store computer program code, and the computer program code includes computer instructions; when the computer instructions are executed by the one or more processors, the electronic device executes the method described in the first aspect and any possible design method thereof.

[0042] In a fourth aspect, the present application provides a chip system, which is applied to an electronic device. The chip system includes one or more interface circuits and one or more processors. The interface circuit and the processor are interconnected by a line. The interface circuit is used to receive a signal from a memory of the electronic device and send the signal to the processor, where the signal includes a computer instruction stored in the memory. When the processor executes the computer instruction, the electronic device executes the method described in the first aspect and any possible design thereof.

[0043] In a fifth aspect, the present application provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method described in the first aspect and any possible design thereof.

[0044] In a sixth aspect, the present application provides a computer program product, which, when executed on a computer, enables the computer to execute the method described in the first aspect and any possible design thereof.

[0045] It can be understood that the beneficial effects that can be achieved by the function calling device described in the second aspect and any possible design thereof, the electronic device described in the third aspect and any possible design thereof, the chip system described in the fourth aspect, the computer-readable storage medium described in the fifth aspect, and the computer program product described in the sixth aspect can be referred to the beneficial effects in the first aspect and any possible design thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] Figure 1 A schematic diagram of an example of parameter transfer provided in an embodiment of the present application;

[0047] Figure 2 A schematic diagram of an example of program module interaction provided in an embodiment of the present application;

[0048] Figure 3 A schematic diagram of another example of program module interaction provided in an embodiment of the present application;

[0049] Figure 4 A flow chart of a function calling method provided in an embodiment of the present application;

[0050] Figure 5 Another function calling method flow chart provided in the embodiment of the present application;

[0051] Figure 6 Another function calling method flow chart provided in the embodiment of the present application;

[0052] Figure 7 Another function calling method flow chart provided in the embodiment of the present application;

[0053] Figure 8 Another function calling method flow chart provided in the embodiment of the present application;

[0054] Fig. 9 Another function calling method flow chart provided in the embodiment of the present application;

[0055] Fig.10 A schematic diagram of another system architecture provided for an embodiment of the present application;

[0056] Fig.11 A flow chart of a function table initialization method provided in an embodiment of the present application;

[0057] Fig.12Another function calling method flow chart provided in the embodiment of the present application;

[0058] Fig.13 Another function calling method flow chart provided in the embodiment of the present application;

[0059] Fig.14 A schematic diagram of the structural composition of a chip system provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0061] In this application, the character " / " generally indicates that the objects before and after are in an "or" relationship. For example, A / B can be understood as A or B.

[0062] The terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this embodiment, unless otherwise specified, "plurality" means two or more.

[0063] In addition, the terms "including" and "having" and any variations thereof mentioned in the description of this application are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or modules is not limited to the listed steps or modules, but may optionally include other steps or modules that are not listed, or may optionally include other steps or modules that are inherent to these processes, methods, products or devices.

[0064] In addition, in the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present concepts in a specific way.

[0065] In order to facilitate understanding of the technical solution of the present application, before introducing the function calling method of the embodiment of the present application in detail, the professional terms mentioned in the embodiment of the present application are first introduced.

[0066] 1. Programming language: It is a formal language used to define computer programs. It is a standardized communication skill used to issue instructions to computers. Every action and step taken by a computer device is executed according to a program that has been written in a computer language. A program is a collection of instructions to be executed by a computer. The basic unit of a program can be called a function. Different functions can correspond to different execution functions, and a complete task can be completed by calling different functions. Optionally, a programming language can also be called a computer language. In general, programming languages ​​can be divided into machine languages, assembly languages, and high-level languages. Specifically, programming languages ​​can include but are not limited to: C language, C++ language, Rust language, Java language, Python language, and WebAssembly language.

[0067] 2. Function pointer (FP): A function pointer is a pointer variable that points to a function (such as the function in the above computer language). The function can be a fixed program segment, or a subroutine, which can achieve fixed functions. A common pointer variable points to an integer, character, or array variable, while a function pointer points to a function. A function pointer can be called like a function and can also be passed as a parameter.

[0068] 3. Foreign function interface (FFI): A foreign function interface is a mechanism that allows a program written in one programming language to call a function written in another programming language.

[0069] 4. WebAssembly, which can be referred to as WASM, is a binary instruction format for a stack-based virtual machine. It is a low-level language similar to assembly language and can be executed in browsers or other WebAssembly runtime (WebAssemblyRuntime) systems.

[0070] 5. WebAssembly Runtime, which can be referred to as WASM Runtime, is an environment for executing WebAssembly modules. WASM Runtime is responsible for loading, parsing, compiling and executing WASM modules, enabling WASM modules to run in browsers or other runtime environments that support WebAssembly. WASM modules can be understood as program modules developed based on WASM. WASM modules can share resources such as memory and threads with JavaScript virtual machines. WASM modules can usually be used with JavaScript to achieve some functions. In other words, WASM modules can be seen as a reinforcement of JavaScript, making up for the shortcomings of JavaScript in execution efficiency.

[0071] With the development of science and technology, the number of programming languages ​​has gradually increased, and developers can use multiple programming languages ​​to develop applications. When the application is running, function pointers can be passed as parameters between different programming languages ​​to implement the call of function interfaces between different programming languages. At present, most common FFI calls are mutual calls within programming languages ​​such as C, C++, Rust, Java, and Python. For example, when embedding C language code into a Python application compiled with the Python programming language, the function pointer of the function written in C language can be passed as a parameter to the function in the Python application, thereby implementing the function of Python code calling the function written in C language.

[0072] However, in the WASM module, other program modules cannot pass function pointers directly to the WASM module, but need to pass the index of the function pointer. The WASM module finds the function pointer through the index and calls the function. The WASM module stores the function pointer of each function in the WASM module, and each function pointer corresponds to an index. Among them, the function pointer can be stored in a function table (such as the name of the function table can be indirect_table). When other program modules pass parameters to the WASM module, if the function pointer needs to be passed, the index corresponding to the function pointer can be passed. The WASM module can indirectly call the function pointer through the index of the function pointer. In this way, the WASM module can make function calls with the host environment or other WASM modules through the index of the function pointer.

[0073] It should be understood that other program modules in the embodiments themselves are modules other than WASM modules. Hereinafter, the other program module is taken as a Native module as an example. The Native module is a program module for interacting with programs developed using native development languages (such as Objective-C, Java, etc.).

[0074] Exemplarily, as shown in Table 1, it shows the correspondence between function pointers and indexes in the WASM module. When the Native module needs to pass function pointer 1, it can pass the index "1". When the Native module needs to pass function pointer 2, it can pass the index "2". When the Native module needs to pass function pointer 3, it can pass the index "3". When the Native module needs to pass function pointer 4, it can pass the index "4".

[0075] Table 1

[0076] Index Function Table 1 Function pointer 1 2 Function pointer 2 3 Function pointer 3 4 Function pointer 4

[0077] It should be understood that Table 1 is an exemplary table, and the embodiments of the present application do not limit the correspondence between function pointers and indexes (such as the number, representation form, and function pointers of function pointers and indexes).

[0078] Currently, WASM Runtime provides support for FFI between Native modules and WASM modules. The Native module can obtain the functions exported by the WASM module through WASM Runtime. The Native module can also write its own functions and then import the functions written by itself into the WASM module through the interfaces provided by WASM Runtime. For the functions exported by the WASM module, the Native module can also pass parameters and call them through the interfaces provided by WASM Runtime. For example, the Native module can obtain the index of the function pointer of the function exported by the WASM module and pass the index to the WASM module so that the WASM module can indirectly call the function pointer through the index of the function pointer, thereby calling the function pointed to by the function pointer in the WASM module.

[0079] Optionally, since the parameter data types in the WASM module are limited, passing parameters to the WASM module or the WASM module passing parameters out needs to comply with the data types of the WASM module. Exemplarily, such as Figure 1As shown, the parameter data types included in the WASM module may include: simple data types, ordinary pointers, structures, and function pointers. Among them, parameters of simple data types (such as I32, I64, F32, F64) can be passed directly. Parameters of ordinary pointer type need to be converted to the base address offset in the WASM module, and then passed as parameters. Parameters of structure type need to first obtain the structure address, then calculate the base address offset in the WASM module, and then pass the offset as a parameter. For function pointers as parameters, it is necessary to pass the index value of the function pointer corresponding to the WASM internal function table (which can be referred to as the table index) as a parameter.

[0080] but, Figure 1 The parameters passed in need to be stored in the WASM module before they can be recognized by the WASM module. In this way, if the Native module calls a function outside the WASM module (such as function A) through the WASM module, the WASM module cannot call the function A because the function pointer and index of function A are not stored in the WASM module. In this way, if other modules cannot call functions outside the WASM module through the WASM module, it may cause the application to run abnormally. In addition, the application program interface (Application Program Interface, API) for operating the WASM internal function table in the WASM Runtime is limited, and the get operation (the operation of obtaining the index of the function pointer) and the set operation (the operation of setting the index of the function pointer) cannot be performed, that is, other modules cannot directly obtain the index value through the get operation, nor can they set the index value for the function pointer in the WASM internal function table through the set operation. In addition, for the assignment of more complex structures (that is, a structure includes multiple members, and the corresponding value is set for each member), the parameters are passed after the assignment, for example, the members of the structure contain function pointers, and the index value of the function pointer in the structure cannot be set on the Native side. In addition, the current WASM module cannot change the capacity of its internal function table, that is, it cannot accommodate new function pointers and indexes, which will cause other modules to call limited functions inside the WASM module, affecting the expansion of application functions.

[0081] Alternatively, the Native module can export the above function A, and the WASM module can import the function A into the WASM module. In this way, the WASM module can call the function A. However, if the WASM module needs to call functions in multiple Native modules, the WASM module needs to import the functions multiple times, which is a cumbersome process.

[0082] To this end, an embodiment of the present application provides a function calling method. The method can be applied to an electronic device deployed with a first program module and a second program module, and the first program module can obtain a first index for indicating a function pointer of a first function, and the first function is used to call a function to be called. Afterwards, the first program module can send a first message to the second program module, and the first message is used to indicate the execution of a target function, and the target function is used to call the first function, and the first message includes a first index. Afterwards, the second program module can execute the target function according to the first index. Among them, the second program module stores a correspondence between the first index and the function pointer of the first function, and the second program module can call the function to be called through the function pointer of the first function. In this way, the second program module can call the function to be called without using the function pointer and index of the function to be called, so that the second program module can call functions in other program modules, expanding the functions that can be called by the second program module.

[0083] In an exemplary embodiment, the first program module may be a Native module, a Hybrid module, a Bridged module, etc. The second program module may be a WASM module.

[0084] In the embodiment of the present application, the first program module may be composed of program codes in a first programming language, and the second program module may be composed of program codes in a second programming language.

[0085] In a possible design, the first programming language is the same as the second programming language. Exemplarily, the first program module may be composed of C language, and the second program module may be composed of C language. Alternatively, the first program module may be composed of C++ language, and the second program module may be composed of C++ language. Alternatively, the first program module may be composed of Python language, and the second program module may be composed of Python language.

[0086] In another possible design, the first programming language is different from the second programming language. Exemplarily, the first program module can be composed of Python language, and the second program module can be composed of C language. Alternatively, the first program module can be composed of C++ language, and the second program module can be composed of Python language.

[0087] Among them, the first program module and the second program module can be program modules in the application. The application (such as the first application) in the embodiment of the present application can be an embedded application (i.e., a system application of the electronic device) installed in the electronic device or a downloadable application. Among them, the embedded application is an application provided as part of the implementation of an electronic device (such as a mobile phone). A downloadable application is an application that can provide its own Internet protocol multimedia subsystem (internet protocol multimedia subsystem, IMS) connection, and the downloadable application can be an application pre-installed in the terminal or a third-party application that can be downloaded and installed in the terminal by the user.

[0088] For example, the electronic device in the embodiments of the present application may be a tablet computer, a mobile phone, a desktop, a laptop, a handheld computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a cellular phone, a personal digital assistant (PDA), an augmented reality (AR) / virtual reality (VR) device, a vehicle-mounted device, etc. Alternatively, the electronic device may be a server, and the embodiments of the present application do not impose any special restrictions on the specific form of the electronic device.

[0089] The execution subject of the method for function call provided in the present application may be a device for function call, and the device may be an electronic device. At the same time, the device may also be a central processing unit (CPU) of the electronic device, or the device may also be a calling module (such as a first program module and a second program module) for function call in the electronic device. In the embodiment of the present application, the method for function call performed by the first program module and the second program module is taken as an example to illustrate the method for function call provided in the embodiment of the present application.

[0090] like Figure 2 As shown, the first program module is a Native module and the second program module is a WASM module. The Native program is the core program of the developer. After the Native program is started, the Native module can be obtained. In order to realize some functions, the Native module can call the functions exported in the WASM module through the WASM runtime. The Native module can rely on the operating system and has strong interactivity.

[0091] WASM modules need to be loaded and compiled by WASM Runtime. In addition, the functions exported by WASM modules need to receive the index of the function pointer as an input parameter. Among them, WASM modules can run in WASM Runtime.

[0092] Optionally, the Native module, WASM module, and WASM runtime may be located in an application in the electronic device.

[0093] In an embodiment of the present application, when the Native program is started, the WASM Runtime can be started to load and compile the WASM module. After that, the Native module can pass parameters to the compiled WASM module through the WASM runtime, call the function exported in the WASM module, and obtain the result of calling the function. Then, the WASM module can return the result to the WASM Runtime through the WASM runtime, and return it to the Native module.

[0094] It should be noted that Native modules, WASM modules, and WASM runtimes can run in the memory of electronic devices.

[0095] For example, Figure 3 As shown, it shows the process when the first program module and the second program module perform FFI interaction. During FFI interaction, the WASM runtime can load, parse, and compile the second program module (such as a WASM module). The second program module includes multiple export functions (such as export function 1, export function 2, and export function 3). The first program module can call the export function in the second program module through the function pointer parameter passing mechanism (i.e., the function calling method introduced in this application) to complete the call to the function pointer outside the second program module.

[0096] The function calling method provided in the embodiment of the present application is described in detail below in conjunction with the drawings in the specification.

[0097] The present application embodiment provides a method for calling a function, such as Figure 4 As shown, the function calling method may include:

[0098] S401: A first program module obtains a first index in a preset index.

[0099] The first index is used to indicate the function pointer of the first function, and the first index is the index of the function pointer of the first function. The first function is used to call the function to be called, and the function to be called can be a function outside the second program module. Optionally, the first function can indirectly or directly call the function to be called. Specifically, the process of the first function indirectly or directly calling the function to be called can refer to the following Figure 5and Figure 6 The introduction in the corresponding embodiments will not be repeated here.

[0100] In a possible design, the first index may be data of a simple data type.

[0101] Exemplarily, the first index may be integer data (such as I32 or I64). Alternatively, the first index may be floating point data (such as single-precision floating point F32 or double-precision floating point F64). The following describes the present application by taking the first index as integer data as an example.

[0102] It can be understood that when the second program module is a WASM module, since the WASM module supports direct parameter passing of simple data types, it can be ensured that the WASM module can efficiently identify the first index, thereby improving the efficiency of determining the function pointer.

[0103] In one possible implementation, the first program module may send a first index request to the second program module, and the first index request is used to request to obtain the first index. Correspondingly, the second program module may receive the first index request from the first program module and send the first index to the first program module. The second program module stores functions and a correspondence between indexes and function pointers of functions (which may be referred to as a first correspondence), the preset index includes the first index, and the functions stored in the second program module include the first function.

[0104] In the embodiment of the present application, the function pointer information of the first function includes the function pointer information of the function to be called, and the function pointer information includes: parameter type and return value type. That is, the parameter type of the first function includes the parameter type of the function to be called, and the return value type of the first function includes the return value type of the function to be called.

[0105] It should be understood that the function (or function pointer) may include multiple parameters, and the function may also have multiple parameter types. In the embodiment of the present application, the parameter type of the first function includes the parameter type of the function to be called, which means that all parameter types of the function to be called are a subset of all parameter types of the first function, or all parameter types of the function to be called are the same as all parameter types of the first function. The return value type of the first function includes the return value type of the function to be called, which means that the return value type of the first function is the same as the return value type of the function to be called.

[0106] Exemplarily, if the parameter type of the function to be called includes two integer parameters, and the return value type is an integer parameter, then the parameter type of the first function may include: at least two integer parameters, for example, three integer parameters, two integer parameters and one floating point parameter. The return value type of the first function is an integer parameter.

[0107] It can be understood that the function pointer information of the first function includes the function pointer information of the function to be called, so that the second program module can call the first function to pass parameters to the function to be called, and the first program module executes the function to be called according to the function pointer information, thereby ensuring that the first function can call the function to be called normally.

[0108] In yet another example, the first program module may call the second function to obtain the first index.

[0109] The second function is deployed in the second program module. The second function can be used to obtain the first index, such as obtaining the first index through a get operation. The return value of the second function can be the first index.

[0110] Optionally, before executing S401, the second function may be compiled in the second program module and imported into the first program module. Specifically, the second function may be exported from the second program module to the first program module through an export declaration or an import declaration.

[0111] For example, the first program module may include: a first import declaration, the first import declaration is used to indicate the import of the second function. The second program module includes: a second function, a first export declaration, the first export declaration is used to indicate the export of the second function. Before the first program module obtains the first index, the second program module may call the first export declaration to export the second function. Afterwards, the first program module may call the first import declaration to import the second function. In this way, after the second program module exports the second function and the first program module imports the second function, the first program module can directly call the second function to obtain the first index.

[0112] Exemplarily, the first program module may call the second function and send a fourth message to the second program module, the fourth message being used to instruct to obtain the first index. Afterwards, the second program module may execute the second function in response to the fourth message and obtain the function pointer of the first function. Afterwards, the second program module may determine the first index according to the first corresponding relationship.

[0113] For example, as shown in Code 1, it shows the second function.

[0114] export_fp export_new_func(){return wasm_new_func} Code 1.

[0115] Wherein, export_new_func is the function name of the second function, and wasm_new_func is the function pointer of the first function. The first program module can obtain the index of wasm_new_func by executing int fp_param=export_new_func().

[0116] In this way, the first program module can directly call the second function to obtain the first index, so that the first program module can trigger the second program module to call the first function through the first index, and then call the function to be called.

[0117] S402: The first program module sends a first message to the second program module. Correspondingly, the second program module can receive the first message from the first program module.

[0118] The first message is used to instruct the execution of the target function, and the target function is used to call the first function. The first message includes a first index and may also include other parameters, such as parameters required by the function to be called.

[0119] In a possible implementation, the first program module may call a target function and send a first message to the second program module, wherein the target function is deployed in the second program module and the first index is a parameter passed to the first function.

[0120] Exemplarily, as shown in Code 2, it shows the objective function.

[0121] wasm_export(a, b, fp_param) code 2.

[0122] Among them, wasm_export is the function name of the target function, a, b, fp_param are all parameters passed from the first program module to the target function, such as fp_param can be the first index, that is, the first index.

[0123] In some embodiments, the second program module may further include: a fourth export declaration, the fourth export declaration is used to indicate the export of the target function, and the first program module may further include: a fourth import declaration, the fourth import declaration is used to indicate the import of the target function. Before the first program module calls the target function, the second program module may call the fourth export declaration to export the target function. Afterwards, the first program module may call the fourth import declaration to import the target function.

[0124] In this way, after the second program module exports the target function and the first program module imports the target function, the first program module can call the target function.

[0125] S403: In response to the first message, the second program module executes the target function according to the first index.

[0126] The second program module stores a correspondence between an index and a function pointer of a function. The second program module can search for the correspondence based on the first index to obtain the function pointer of the first function, and then obtain the first function based on the function pointer of the first function, execute the first function, and call the corresponding function pointer through the preset index.

[0127] Exemplarily, in conjunction with Table 1, if the first index is "1", the second program module can call function pointer 1. Alternatively, if the first index is "2", the second program module can call function pointer 2.

[0128] In a possible implementation, in response to the first message, the second program module may determine the function pointer of the first function according to the first index to call the first function. Afterwards, the second program module may execute the first function to call the function to be called and obtain the execution result of the function to be called, where the execution result of the function to be called is the result of executing the function to be called. Then, the second program module may execute the target function according to the execution result of the function to be called.

[0129] Optionally, the objective function can also be used to achieve a target function. The embodiment of the present application does not limit the target function. For example, the target function can be an output function, a sending function, a display function, etc.

[0130] For example, assuming that the function of the target function is to pop up information, and the function of the function to be called is a summation function, after executing the target function, the electronic device can pop up the summation result obtained by the function to be called.

[0131] Based on the above technical solution, based on the above technical solution, the first program module can instruct the second program module to execute the target function. In addition, the first program module can send the first index to the second program module, because the second program module stores the correspondence between the first index and the function pointer of the first function. In the process of the second program module executing the target function, the second program module can call the function to be called through the function pointer of the first function. In this way, the second program module can call the function to be called without the function pointer and index of the function to be called. In this way, the second program module can not only call the functions in the second program module, but also call the functions in other program modules, which expands the functions that can be called by the second program module.

[0132] It should be noted that the embodiments of the present application do not limit the deployment location of the function to be called. For example, the function to be called can be deployed in the second program module. For another example, the function to be called can be deployed in the first program module.

[0133] In the case where the function to be called is deployed in the second program module, the second program module executing the first function to call the function to be called may include: in response to the first message, the second program module may determine the function pointer of the first function according to the first index included in the first message to call the first function. After that, the second program module may execute the first function to call the function to be called. Then, the second program module may execute the function to be called to obtain the execution result of the function to be called. Subsequently, the second program module may execute the target function according to the execution result of the function to be called.

[0134] In the case where the function to be called is deployed in the first program module, the second program module may indirectly or directly call the function to be called through the first function. The following introduces the process of Method 1: the second program module indirectly calls the function to be called through the first function.

[0135] In Method 1, the first program module further includes: a dispatch function and a second export declaration for indicating the export of the dispatch function, and the second program module further includes: a second import declaration for indicating the import of the dispatch function. The first function is specifically used to call the function to be called through the dispatch function.

[0136] In this application, the export declaration refers to the function name of the exported function. It should be understood that by exporting the function name of the function through the export declaration, the function can be exposed for use by other program modules. The import declaration refers to the function name of the imported function. It should be understood that by importing the function name of the function through the import declaration, the function can be directly called by the function name. For example, both the export declaration and the import declaration may be a piece of program code.

[0137] It should be understood that this application does not limit the naming of the dispatch function, and it can also be named otherwise, such as the third function, etc.

[0138] In the embodiments of this application, before the second program module executes the target function, the first program module may call the second export declaration to export the dispatch function. After that, the second program module may call the second import declaration to import the dispatch function. In some embodiments, after the second program module imports the dispatch function, the second program module may call the first function and call the function to be called through the dispatch function, and then execute the target function. Among them, the function pointer information of the dispatch function includes the function pointer information of the function to be called.

[0139] In a possible implementation manner, the second program module may import the dispatch function into the second program module through a preset import interface.

[0140] Exemplarily, the preset import interface may include but is not limited to the wasm_instance_new API. The WASM module can call the wasm_instance_new API through the WASM runtime, pass the dispatch function as a parameter to the wasm_instance_new API, and complete the import.

[0141] It should be noted that the WASM module has certain requirements for imported parameters or functions. If the function imported into the WASM module does not meet the requirements, the function import into the WASM module will fail.

[0142] In a possible implementation, the first program module may encapsulate the dispatch function to obtain an encapsulated dispatch function, wherein the encapsulated dispatch function satisfies a preset import specification, and the preset import specification includes: the function parameters are data types supported by WASM, and the function return value is a data type supported by other program modules.

[0143] Among them, the data types supported by WASM include: integer, floating point, vector, etc. The data types supported by other program modules include: string, Boolean, Null, integer, etc.

[0144] Exemplarily, in the encapsulated dispatch function, the parameter is of wasm_store_t type, and the return value is of wasm_extern_t type.

[0145] That is to say, in the embodiment of the present application, the dispatch function imported into the second program module is a dispatch function that meets the preset import specification after encapsulation. In this way, it can be ensured that the dispatch function is successfully imported into the WASM module.

[0146] The present application embodiment provides a function calling method, such as Figure 5 As shown, in the function calling method, S403 may include:

[0147] S501. The second program module determines the function pointer of the first function according to the first index and the first corresponding relationship, and calls the first function based on the function pointer of the first function to obtain the function name of the dispatch function, so as to return the function name of the dispatch function to the first program module, triggering the first program module to execute the dispatch function.

[0148] Among them, the relevant description of the first corresponding relationship can refer to the above and will not be repeated here.

[0149] Exemplarily, as shown in code three, it shows the first function.

[0150] int wasm_new_func(int a,int b){return dispatch(a,b)}Code three.

[0151] Among them, wasm_new_func is the function name (or function pointer) of the first function, a and b are parameters received by the first function, and the first function is executed based on the parameters a and b to obtain the return value of the first function. The return value of the first function is the function name (or function pointer) of the dispatch function.

[0152] S502: The second program module sends a second message to the first program module. Correspondingly, the first program module can receive the second message from the second program module.

[0153] The second message may be used to indicate calling or executing the dispatch function. The second message may include the function name of the dispatch function or a function pointer called the dispatch function.

[0154] Optionally, the second message may further include: parameters passed to the dispatch function. It should be understood that when the second program module passes the parameters to the dispatch function through the second message, the dispatch function may perform corresponding functions based on the parameters.

[0155] S503. In response to the second message, the first program module executes the dispatch function to call the function to be called through the dispatch function to obtain the execution result of the function to be called.

[0156] In a possible implementation, the dispatch function may include the function name of the function to be called. The return value of the dispatch function is the execution result of the function to be called. In response to the second message, the first program module may execute / call the dispatch function according to the function name of the dispatch function. Afterwards, the first program module may obtain the return value of the dispatch function, execute the function to be called based on the return value, and obtain the execution result of the function to be called.

[0157] Exemplarily, take the function to be called as the sum function add as an example, as shown in Code 4, which shows the dispatch function.

[0158] int dispatch(int a,int b){return add(a,b)}Code 4.

[0159] Among them, dispatch is the function name of the dispatch function, a and b are the parameters passed to the dispatch function, add is the function name of the function to be called, and the return value of the dispatch function is the execution result of the add function.

[0160] In another possible implementation, the second message may include an index corresponding to a function pointer of the function to be called, and the index corresponding to the function pointer of the function to be called is used to indicate the function pointer of the function to be called. The first program module may execute the function to be called according to the index corresponding to the function pointer of the function to be called to obtain an execution result of the function to be called.

[0161] In an embodiment of the present application, before the first program module sends the first message to the second program module (i.e., S402), the first program module can obtain the index corresponding to the function pointer of the function to be called and the function pointer of the function to be called, and store the correspondence between the index corresponding to the function pointer of the function to be called and the function pointer of the function to be called.

[0162] It should be understood that the present application does not limit the naming of the index corresponding to the function pointer of the function to be called, and it can also be named as the second index or others. The second index can be set by the developer. For example, the second index can be "1", "2", "0.1".

[0163] Optionally, the first program module may correspond the index corresponding to the function pointer of the to-be-called function and the function pointer of the to-be-called function to be stored in an external function table, where the external function table may be understood as a function table located outside the WASM module.

[0164] Optionally, the external function table can be a hash table. The key of the hash table is the index indicating the function pointer, and the value of the hash table is the function pointer. The key of the hash table can be set by the developer or use an auto-increment index. The auto-increment index can be understood as an index other than the index specified by the existing standard. The first program module can store the index corresponding to the function pointer of the function to be called and the function pointer of the function to be called in the hash table.

[0165] Optionally, before the second program module sends the second message to the first program module, the second program module may obtain the index corresponding to the function pointer of the to-be-called function, and send the index corresponding to the function pointer of the to-be-called function to the first program module. The second message may include the index corresponding to the function pointer of the to-be-called function.

[0166] In one possible design, the second program module locally stores the index corresponding to the function pointer of the function to be called, and the second program module can obtain the index corresponding to the function pointer of the function to be called from the stored data. Optionally, the developer can pre-set the index corresponding to the function pointer of the function to be called in the WASM module.

[0167] Exemplarily, as shown in Code 5, it shows the first function.

[0168] int wasm_new_func(int a,int b){int table_idx=1; return disp(table_idx,a,b)}Code five.

[0169] Among them, table_idx=1 is used to indicate that the index corresponding to the function pointer of the function to be called is "1".

[0170] In an embodiment of the present application, in response to the second message, the first program module can execute the dispatch function according to the function name of the dispatch function to trigger the call of the function to be called. Afterwards, the first program module can determine the function pointer of the function to be called according to the corresponding relationship between the index corresponding to the function pointer of the function to be called, the index corresponding to the function pointer of the function to be called and the function pointer of the function to be called, so as to call the function to be called. Then, the first program module can execute the function to be called and obtain the execution result of the function to be called.

[0171] Exemplarily, as shown in Code 6, it shows the dispatch function.

[0172] int disp(int table_idx,int a,int b){

[0173] function_pointer fp = (function_pointer) map [table_idx]; return fp (a, b)} Code six.

[0174] Among them, disp is used to represent the function name of the dispatch function, table_idx, a, and b are all used to represent the parameters received by the dispatch function, map is used to represent the name of the hash table, "1" is used to represent the index corresponding to the function pointer of the function to be called, and fp is used to represent the function pointer corresponding to "1" (that is, the function pointer of the function to be called).

[0175] It is understandable that the second message includes the index corresponding to the function pointer of the function to be called, so that the first program module can determine the function pointer of the function to be called according to the index corresponding to the function pointer of the function to be called. In this way, the first program module can execute the function pointer of the function to be called and obtain the execution result of the function to be called without the developer repeatedly inputting the function name of the function to be called.

[0176] S504: The first program module sends the execution result of the to-be-called function to the second program module.

[0177] In a possible implementation manner, the first program module may transfer the execution result of the to-be-called function to the target function.

[0178] Exemplarily, as shown in Code 7, it shows the function to be called.

[0179] int add(int a,int b){return a+b;}Code seven.

[0180] Among them, add is used to represent the function name or function pointer of the function to be called, a and b are used to represent the parameters received by the function to be called, and return a+b is used to represent the return of the sum of a and b.

[0181] Correspondingly, the second program module can receive the execution result of the to-be-called function from the first program module.

[0182] S505. The second program module executes the target function according to the execution result of the function to be called.

[0183] It is understandable that after the second program module determines the function pointer of the first function according to the first index, the dispatch function can be called to trigger the call of the dispatch function. Since the dispatch function can call the function to be called through the dispatch function, when the first program module executes the dispatch function, the function to be called can be called to obtain and send the execution result of the function to be called to the second program module. In this way, the second program module can call the function to be called to obtain the execution result of the function to be called and execute the target function without the function pointer and function index of the function to be called.

[0184] The following is an introduction to the process of method 1 with a specific example. Figure 6 As shown, the first program module may include: an imported target function, an imported second function, a dispatch function, and a function to be called; the second program module may include: an imported dispatch function, a dispatch function, a second function, and a target function.

[0185] S61. The first program module may call the imported second function, and the second program module executes the second function to obtain a first index.

[0186] S62. The second function may return the first index.

[0187] S63: The first program module may call the imported target function and pass the first index to the target function, and the second program module starts to execute the target function.

[0188] S64: The target function calls the function pointer of the first function according to the first index to call the dispatch function.

[0189] S65. The second program module executes the dispatch function, and the dispatch function calls the dispatch function.

[0190] S66: The first program module executes the dispatch function.

[0191] S67: The first program module calls the function to be called and obtains the execution result of the function to be called.

[0192] S68: The function to be called returns the execution result of the function to be called to the target function, and the second program module continues to execute the target function according to the execution result of the function to be called.

[0193] After introducing the first method of calling the function to be called (i.e., the second program module indirectly calls the function to be called through the dispatch function), the second method of calling the function to be called (i.e., the second program module can directly call the function to be called through the dispatch function) is introduced below.

[0194] In the second method, the first program module further includes: a function to be called, a third export declaration, the third export declaration is used to indicate the export of the function to be called, and the second program module further includes: a third import declaration, the third import declaration is used to indicate the import of the function to be called.

[0195] In the embodiment of the present application, before the second program module calls the target function, the first program module can call the third export declaration to export the function to be called. Afterwards, the second program module can call the third import declaration to import the function to be called.

[0196] In this way, after the first program module exports the function to be called and the second program module imports the function to be called, the second program module can directly call the function to be called.

[0197] In some embodiments, after the second program module imports the function to be called, the second program module may call the dispatch function, and call the function to be called through the dispatch function, thereby executing the target function.

[0198] The present application embodiment provides a function calling method, such as Figure 7 As shown, in the function calling method, S403 may include:

[0199] S701. The second program module determines the function pointer of the first function according to the first index and the first corresponding relationship, and calls the dispatch function based on the function pointer of the first function to return the function name of the dispatch function to the first program module, triggering the first program module to execute the function to be called.

[0200] Exemplarily, as shown in Code 8, a dispatch function is shown.

[0201] int dispatch(int a,int b){return add(a,b)}Code 8.

[0202] Among them, dispatch is the function name or function pointer of the dispatch function, a and b are the parameters received by the dispatch function, add is the function name of the function to be called, and the return value of the dispatch function is the execution result of the add function.

[0203] S702: The second program module sends a third message to the first program module.

[0204] The third message is used to instruct the execution of the function to be called. The third message may include the function name of the function to be called.

[0205] Optionally, the third message may further include: parameters passed to the function to be called.

[0206] Accordingly, the first program module may receive a third message from the second program module.

[0207] S703. In response to the third message, the first program module executes the function to be called and obtains an execution result of the function to be called.

[0208] S704: The first program module sends the execution result of the to-be-called function to the second program module.

[0209] It should be noted that, for the introduction of the first program module sending the execution result of the to-be-called function to the second program module, reference may be made to the introduction of S505 in the above embodiment, which will not be elaborated here.

[0210] Correspondingly, the second program module can receive the execution result of the to-be-called function from the first program module.

[0211] S705. The second program module executes the target function according to the execution result of the function to be called.

[0212] It is understandable that after the second program module determines the function pointer of the first function according to the first index, the dispatch function can be called to trigger the call of the function to be called. After that, the first program module executes the function to be called to obtain and send the execution result of the function to be called to the second program module. In this way, the second program module can call the function to be called to obtain the execution result of the function to be called and execute the target function without the function pointer and function index of the function to be called.

[0213] The following is an introduction to the process of method 2 with a specific example. Figure 8 As shown, the first program module may include: an imported target function, an imported second function, and a function to be called; the second program module may include: a dispatch function, an imported function to be called, and a target function.

[0214] S81. The first program module may call the imported second function, and the second program module executes the second function to obtain a first index.

[0215] S82. The second function may return the first index.

[0216] S83. The first program module may call the imported target function and pass the first index to the target function, and the second program module starts to execute the target function.

[0217] S84. The target function calls the function pointer of the first function according to the first index to call the dispatch function.

[0218] S85. The first program module executes the function to be called and obtains the execution result of the function to be called.

[0219] S86: The function to be called returns the execution result of the function to be called to the target function, and the second program module continues to execute the target function according to the execution result of the function to be called.

[0220] After introducing the first and second methods for calling the function to be called, the complete process of implementing the present application is introduced below, taking the second program module as a WASM module as an example.

[0221] like Fig. 9 As shown, the function calling method may include:

[0222] S901: The electronic device initializes an external function table.

[0223] The external function table is stored in the memory.

[0224] This step is performed after the program starts running. In this step, the developer can create an external function table in memory, such as a hash table (the key is used to represent the table index and the value is used to represent the function pointer). Then, the electronic device can initialize the external function table.

[0225] Optionally, the electronic device may set a preset capacity of the external function table.

[0226] In this way, by externally simulating the function table of the WASM module, there is no need to add or modify the function table inside the WASM module. Instead, a function table is created on the caller (such as the first program module) to store the function pointer, simulating the function table inside the WASM, and realizing the storage function when the FFI function pointer is passed. In addition, the preset capacity of the external function table can be pre-set on the caller, thereby increasing the storage data volume of the external function table and improving efficiency.

[0227] By presetting the preset capacity of the external function table, the storage data volume of the external function table can be increased and the efficiency can be improved.

[0228] S902. The electronic device creates a dispatch function and imports a WASM module.

[0229] The dispatch function is used to call an external function pointer, which is a function pointer outside the WASM module.

[0230] This step occurs before the FFI call (i.e. the WASM module calls the external function pointer). In this step, the dispatch function needs to be declared and implemented. The dispatch function can determine the corresponding external function pointer through the table index of the external function table as a parameter, and complete the call to the external function pointer. After implementation, the electronic device can rewrite the dispatch function into a function that meets the WASM import specification based on the interface provided by WASMRuntime, and then import the dispatch function into the WASM module through WASMRuntime.

[0231] Optionally, the electronic device may execute S902 after executing S901.

[0232] S903: The electronic device puts the function pointer into an external function table.

[0233] This step is performed after the external function table has been created and the dispatch function has been successfully imported into the WASM module. In this step, when an FFI call is about to occur, the electronic device can put the function pointer of the function to be called (the current technical solution requires the function pointer to be passed as a parameter) into the external function table, and specify the table index or use an auto-increment index. Ensure that the target index corresponding to the function pointer of the function to be called (that is, the index corresponding to the function pointer of the function to be called) can also be obtained in the WASM module, because the target index needs to be passed to the dispatch function in subsequent steps.

[0234] Optionally, when the FFI is called, the electronic device may execute S903.

[0235] S904. The electronic device obtains the function pointer of the wasm_func function exported by the WASM module, and replaces the function to be called with the input parameter.

[0236] After S901-S903 are completed, execute at the FFI call. In this step, the electronic device can first obtain the function pointer exported from the WASM module (the function pointer of the wasm_func function, that is, the function pointer of the first function), and the function pointer of the wasm_func function can call the dispatch function imported into the WASM module. The signature of the function pointer of the wasm_func function (that is, the function pointer information) is consistent with the signature of the function pointer of the function to be called, which ensures that the result of the function pointer call of the wasm_func function is completely consistent with the function pointer of the function to be called. Then, the electronic device uses the obtained function pointer of the wasm_func function as the parameter of the target function to complete the input.

[0237] It can be understood that in the callback mechanism of the function pointer between the WASM module and the Native module, the call of the function pointer of the function to be called occurs on the caller (i.e., the first program module) through the import and export functions of the WASM module, and the dispatch function can be encapsulated and exported to pass the function pointer of the function to be called.

[0238] The following is an introduction to this application using the React Native framework as an example. In the open source React Native mobile terminal framework, the React Native mobile terminal framework can run on The React Native mobile terminal framework can use the WASM module (also called the WASM engine) to realize the communication between the upper JavaScript (JS) and the underlying C++. The WASM engine can accelerate the execution speed of the JS code, making the electronic device smoother and more fluent during the application startup and interaction stages.

[0239] like Fig.10 As shown, the React Native framework may include: front-end program code (such as code written using React), script files (such as JS Bundle), a first lightweight framework (such as JavaScript Interface (JSI) framework), a rendering module (such as Fabric), a second lightweight framework (such as Turbmodules), a layout engine (Yoga), a user interface (UserInterface, UI) module (such as Native UI), and a native module (such as Native Modules).

[0240] Among them, the Metro packaging tool can package the code written in React into a JS file (such as JS Bundle). The WASM engine is located between the script file and the first lightweight framework (not shown in the figure).

[0241] After React Native is started, the electronic device can start three threads, namely the native thread (NativeThread), JS thread (JS Thread) and UI thread (Shadow Thread). Among them, the JS thread is used to load the JS Bundle into the memory and compile it to obtain the Native module (not shown in the figure). The Native module can interact with the WASM engine. The native thread is used to load and execute the native module. The UI thread is used to render the front-end UI page. The interaction between the JS file and the native module can be completed through JSI, and the objects in the native module can be saved inside the JS file through JSI.

[0242] In the embodiment of the present application, the interaction between the Native module and the WASM engine can be achieved through steps 1 to 3.

[0243] Step 1: The electronic device constructs an external function table, which simulates the internal function table of the WASM module and is used to store function pointers.

[0244] This step is executed after the React Native program starts running. In the React Native underlying JSI code, a global variable, the external function table (such as an unordered hash table), is created. The key of the hash table is used to represent the table index value, the key is an integer, and the value is used to represent the function pointer, the value is a pointer type. Developers can preset the capacity of the external function table based on the program volume (such as the number of external function pointers).

[0245] For example, Fig.11 As shown, the electronic device can determine whether the memory meets the initialization requirements, such as whether the free memory size is greater than a preset memory threshold. If the memory meets the initialization requirements, the electronic device initializes the external function table. If the memory does not meet the initialization requirements, the external function table initialization fails and the program ends.

[0246] Step 2: The electronic device creates a dispatch function to call an external function pointer and imports the dispatch function into the WASM module.

[0247] This step is performed in the React Native underlying code. The creation of the dispatch function (the third function) is synchronized with step 1, but the import process is completed after step 1.

[0248] For example, Fig.12As shown, in step 2, the electronic device needs to declare and implement the dispatch function. The signature of the dispatch function has one more parameter than the signature of the external function pointer (i.e., the function pointer of the function to be called), which is an integer target index value. The target index value is the index value corresponding to the external function pointer. The target index value is used to find the corresponding function pointer of the function to be called in the external function table and complete the call. The specific implementation of the dispatch function is to determine the corresponding function pointer through the target index value, and then pass other parameters directly to the function pointer of the function to be called, and complete the call. After creating the dispatch function, it is also necessary to encapsulate the dispatch function (such as encapsulating the input parameters and return values ​​of the dispatch function) in combination with the interface provided by the WASM Runtime, because the variable types in the WASM module are fixed, and the imported functions that the WASM module can receive also need to comply with the specifications. Then after encapsulation, the electronic device can load and compile the WASM module. The electronic device determines whether the WASM module is successfully loaded and compiled. If the WASM module is successfully loaded and compiled, the electronic device imports the encapsulated dispatch function into the WASM module through the import application programming interface (Application Programming Interface, API) provided by the WASM Runtime. Then, the electronic device can instantiate the WASM module. If the WASM module is not successfully loaded and compiled, the program ends.

[0249] Step 3: The electronic device puts the function pointer into the external function table, obtains the function pointer exported by the WASM module (i.e., the function pointer of the first function), and replaces the function pointer of the function to be called with the target function.

[0250] This step is executed after the external function table has been created, the dispatch function has been successfully imported into the WASM module, and the WASM module has been successfully instantiated. This step is executed when the function pointer needs to be passed to the function pointer of the first function in React Native JSI.

[0251] like Fig.13As shown, the electronic device needs to declare an imported dispatch function in the WASM module, and at the same time declare and implement a first function and set the first function to the exported state (i.e., call the second function). The first function can call the imported dispatch function and pass the target index value to the imported dispatch function. Among them, the signature of the function pointer of the first function is the same as the signature of the function pointer of the function to be called, so that it can be ensured that the call behavior of the function pointer of the first function is exactly the same as the call behavior of the function pointer of the function to be called. The above steps all occur in the WASM module, and the subsequent steps occur in the React Native underlying code. The electronic device puts the function pointer of the function to be called into the external function table and assigns a table index to the function pointer of the function to be called (using an incrementing index or other types of indexes to ensure that the target index value can also be obtained within the WASM module, so that the WASM module can pass the target index value to the dispatch function in the subsequent steps). Then, the electronic device obtains the second function exported in the WASM module through the import API provided by the WASM Runtime and obtains the function pointer of the first function through the function name. Finally, the electronic device replaces the function pointer of the function to be called with the obtained function pointer of the first function as a parameter and puts it into the target function to complete the input parameter. In this way, when actually calling back the function pointer of the first function, the dispatch function will be called first through the dispatch function imported inside the WASM module. After that, the electronic device can determine the function pointer corresponding to the target index value in the external function table to complete the call.

[0252] The above mainly introduces the solution provided by the embodiments of the present application from the perspective of the electronic device (the first program module, the second program module). It can be understood that in order for the electronic device to implement the above functions, it includes the corresponding hardware structure and / or software module for executing each function. Those skilled in the art should easily realize that, combined with the steps of a function call method example described in the embodiments disclosed in the present application, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a certain function is executed in the way of hardware or software of the electronic device driving the hardware depends on the specific application and design constraint conditions of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but this implementation should not be considered to exceed the scope of the present application.

[0253] The embodiment of the present application can divide the function calling device into functional modules or functional units according to the above method example. For example, each functional module or functional unit can be divided according to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware or in the form of software functional modules or functional units. Among them, the division of modules or units in the embodiment of the present application is schematic, which is only a logical function division. There may be other division methods in actual implementation.

[0254] Some other embodiments of the present application provide an electronic device. The electronic device may include: a memory and one or more processors. The memory and the processor are coupled. The electronic device may also include a camera. Alternatively, the electronic device may be connected to an external camera. The memory is used to store computer program code, and the computer program code includes computer instructions. When the processor executes the computer instructions, the electronic device may execute the various functions or steps executed by the mobile phone in the above method embodiment.

[0255] The present application also provides a chip system, such as Fig.14 As shown, the chip system includes at least one processor 1401 and at least one interface circuit 1402. The processor 1401 and the interface circuit 1402 can be interconnected via lines. For example, the interface circuit 1402 can be used to receive signals from other devices (such as a memory of an electronic device). For another example, the interface circuit 1402 can be used to send signals to other devices (such as processor 1401). Exemplarily, the interface circuit 1402 can read instructions stored in the memory and send the instructions to the processor 1401. When the instructions are executed by the processor 1401, the electronic device can execute the various steps in the above embodiments. Of course, the chip system can also include other discrete devices, which are not specifically limited in the embodiments of the present application.

[0256] An embodiment of the present application also provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on the above-mentioned electronic device, the electronic device executes each function or step executed by the mobile phone in the above-mentioned method embodiment.

[0257] The embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute each function or step executed by the mobile phone in the above method embodiment.

[0258] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0259] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0260] The units described as separate components may or may not be physically separated, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.

[0261] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0262] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium, including several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.

[0263] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims

1. A function calling method, It is characterized in that The method is applied to an electronic device, wherein a first program module and a second program module are deployed in the electronic device, wherein the second program module stores a correspondence between a preset index and a function pointer of a preset function, and the second program module is used to call a corresponding function pointer through the preset index; the method comprises: Acquire a first index in the preset indexes through the first program module, where the first index is used to indicate a function pointer of a first function, and the first function is used to call a function to be called; Sending a first message to the second program module through the first program module, where the first message is used to instruct execution of a target function, the target function is used to call the first function, and the first message includes the first index; In response to the first message, the target function is executed according to the first index through the second program module, and the corresponding relationship between the first index and the function pointer of the first function is stored in the second program module.

2. The method according to claim 1, It is characterized in that The first program module includes: a first import declaration, the first import declaration is used to indicate the import of a second function, the second function is used to obtain the first index, and the second program module includes: a second function and a first export declaration, the first export declaration is used to indicate the export of the second function; Before acquiring the first index through the first program module, the method further includes: Calling the first export declaration through the second program module to export the second function; The first import statement is called by the first program module to import the second function.

3. The method according to claim 2, It is characterized in that The acquiring the first index in the preset indexes through the first program module includes: The first index is obtained by calling the second function through the first program module.

4. The method according to any one of claims 1 to 3, It is characterized in that The first program module further includes: a third function, a second export declaration, the first function is specifically used to call the to-be-called function through the third function, the second export declaration is used to export the second export declaration is used to indicate exporting the third function, the second program module further includes: a second import declaration, the second import declaration is used to indicate importing the third function; Before executing the target function by the second program module, the method further includes: Calling the second export declaration through the first program module to export the third function; The second import declaration is called by the second program module to import the third function.

5. The method according to claim 4, It is characterized in that The first program module further includes: the function to be called; and executing the target function according to the first index through the second program module includes: Determine, by the second program module, a function pointer of a first function according to the first index, and call the first function to trigger calling the third function; Sending a second message to the first program module through the second program module, where the second message is used to instruct execution of the third function; In response to the second message, executing the third function through the first program module to trigger calling the to-be-called function and obtain an execution result of the to-be-called function; Sending the execution result of the to-be-called function to the second program module through the first program module; The target function is executed according to the execution result of the to-be-called function through the second program module.

6. The method according to any one of claims 1 to 5, It is characterized in that Before sending the first message to the second program module through the first program module, the method further includes: The correspondence between the second index and the function pointer of the to-be-called function is stored through the first program module, and the second index is used to indicate the function pointer of the to-be-called function.

7. The method according to claim 6, It is characterized in that Before sending the second message to the first program module through the second program module, the method further includes: Acquire the second index through the second program module; The second message includes the second index.

8. The method according to any one of claims 1 to 7, It is characterized in that The function pointer information of the first function is the same as the function pointer information of the to-be-called function, and the function pointer information includes: parameter type and return value type.

9. The method according to any one of claims 1 to 3, It is characterized in that The first program module further includes: the function to be called, a third export declaration, the third export declaration is used to indicate the export of the function to be called, and the second program module further includes: a third import declaration, the third import declaration is used to indicate the import of the function to be called; Before calling the target function through the second program module, the method further includes: Calling the third export declaration through the first program module to export the function to be called; The third import declaration is called through the second program module to import the function to be called.

10. The method according to claim 9, It is characterized in that Executing the target function according to the first index by the second program module includes: Determine, by the second program module, a function pointer of a first function according to the first index, and call the first function to trigger calling of the to-be-called function; Sending a third message to the first program module through the second program module, wherein the third message is used to instruct execution of the to-be-called function; In response to the third message, executing the to-be-called function through the first program module to obtain an execution result of the to-be-called function; Sending the execution result of the to-be-called function to the second program module through the first program module; The target function is executed according to the execution result of the to-be-called function through the second program module.

11. The method according to any one of claims 1 to 10, It is characterized in that The first program module is a Native module, and the second program module is a WASM module.

12. The method according to any one of claims 1 to 11, It is characterized in that The first module is composed of program codes in a first programming language, and the second program module is composed of program codes in a second programming language; The first programming language is the same as the second programming language; or the first programming language is different from the second programming language.

13. A function calling device, It is characterized in that The device is deployed with a first program module and a second program module, wherein the second program module stores a correspondence between a preset index and a function pointer of a preset function, and the second program module is used to call the corresponding function pointer through the preset index; The device comprises: an acquisition unit, configured to acquire a first index in the preset indexes through the first program module, wherein the first index is used to indicate a function pointer of a first function, and the first function is used to call a function to be called; a sending unit, configured to send a first message to the second program module through the first program module, wherein the first message is used to instruct execution of a target function, the target function is used to call the first function, and the first message includes the first index; A processing unit is used to execute the target function according to the first index through the second program module in response to the first message, and the corresponding relationship between the first index and the function pointer of the first function is stored in the second program module.

14. The device according to claim 13, It is characterized in that The first program module includes: a first import declaration, the first import declaration is used to indicate the import of a second function, the second function is used to obtain the first index, and the second program module includes: a second function and a first export declaration, the first export declaration is used to indicate the export of the second function; The processing unit is further configured to call the first export declaration through the second program module to export the second function; The processing unit is further configured to call the first import declaration through the first program module to import the second function.

15. The device according to claim 14, It is characterized in that The acquisition unit is specifically configured to call the second function through the first program module to acquire the first index.

16. The device according to any one of claims 13 to 15, It is characterized in that The first program module further includes: a third function, a second export declaration, the first function is specifically used to call the to-be-called function through the third function, the second export declaration is used to export the second export declaration is used to indicate exporting the third function, the second program module further includes: a second import declaration, the second import declaration is used to indicate importing the third function; The processing unit is further configured to call the second export declaration through the first program module to export the third function; The processing unit is further configured to call the second import declaration through the second program module to import the third function.

17. The device according to claim 16, It is characterized in that The first program module also includes: the function to be called; The processing unit is further configured to determine, through the second program module, a function pointer of the first function according to the first index, and call the first function to trigger calling the third function; The sending unit is further configured to send a second message to the first program module through the second program module, wherein the second message is used to instruct execution of the third function; The processing unit is further configured to execute the third function through the first program module in response to the second message to trigger calling the to-be-called function and obtain an execution result of the to-be-called function; The sending unit is further used to send the execution result of the to-be-called function to the second program module through the first program module; The processing unit is further configured to execute the target function according to the execution result of the to-be-called function through the second program module.

18. The device according to any one of claims 13 to 17, It is characterized in that The processing unit is further used to store the correspondence between the second index and the function pointer of the to-be-called function through the first program module, wherein the second index is used to indicate the function pointer of the to-be-called function.

19. The device according to claim 18, It is characterized in that The sending unit is further configured to obtain the second index through the second program module; The second message includes the second index.

20. The device according to any one of claims 13 to 19, It is characterized in that The function pointer information of the first function is the same as the function pointer information of the to-be-called function, and the function pointer information includes: parameter type and return value type.

21. The device according to any one of claims 13 to 15, It is characterized in that The first program module further includes: the function to be called, a third export declaration, the third export declaration is used to indicate the export of the function to be called, and the second program module further includes: a third import declaration, the third import declaration is used to indicate the import of the function to be called; The processing unit is further configured to call the third export declaration through the first program module to export the function to be called; The processing unit is further configured to call the third import declaration through the second program module to import the function to be called.

22. The device according to claim 21, It is characterized in that The processing unit is further configured to determine a function pointer of a first function according to the first index through the second program module, and call the first function to trigger calling of the to-be-called function; The sending unit is further used to send a third message to the first program module through the second program module, wherein the third message is used to instruct execution of the to-be-called function; The processing unit is further configured to execute the to-be-called function through the first program module in response to the third message to obtain an execution result of the to-be-called function; The sending unit is further used to send the execution result of the to-be-called function to the second program module through the first program module; The processing unit is further configured to execute the target function according to the execution result of the to-be-called function through the second program module.

23. The device according to any one of claims 13 to 22, It is characterized in that The first program module is a Native module, and the second program module is a WASM module.

24. The device according to any one of claims 13 to 23, It is characterized in that The first module is composed of program codes in a first programming language, and the second program module is composed of program codes in a second programming language; The first programming language is the same as the second programming language; or the first programming language is different from the second programming language.

25. An electronic device, It is characterized in that The electronic device comprises: a memory and one or more processors; the memory is coupled to the processor, the memory is used to store computer program code, the computer program code comprises computer instructions, and when the computer instructions are executed by the one or more processors, the electronic device executes the method as described in any one of claims 1-12.

26. A computer-readable storage medium, It is characterized in that The method comprises computer instructions, which, when executed on an electronic device, cause the electronic device to execute the method as claimed in any one of claims 1 to 12.