CN115982784B - Method and device for calling function by WebAsssembly module - Google Patents

Method and device for calling function by WebAsssembly module Download PDF

Info

Publication number
CN115982784B
CN115982784B CN202310278575.9A CN202310278575A CN115982784B CN 115982784 B CN115982784 B CN 115982784B CN 202310278575 A CN202310278575 A CN 202310278575A CN 115982784 B CN115982784 B CN 115982784B
Authority
CN
China
Prior art keywords
module
function
webassembly
index
name
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202310278575.9A
Other languages
Chinese (zh)
Other versions
CN115982784A (en
Inventor
刘静亚
路倩
张章
邹重人
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ziguang Tongxin Microelectronics Co Ltd
Original Assignee
Ziguang Tongxin Microelectronics Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ziguang Tongxin Microelectronics Co Ltd filed Critical Ziguang Tongxin Microelectronics Co Ltd
Priority to CN202310278575.9A priority Critical patent/CN115982784B/en
Publication of CN115982784A publication Critical patent/CN115982784A/en
Application granted granted Critical
Publication of CN115982784B publication Critical patent/CN115982784B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Landscapes

  • Devices For Executing Special Programs (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a method and a device for calling functions by using WebAssemblem modules, which are applied to electronic equipment, wherein the electronic equipment comprises resource-limited equipment, a plurality of WebAssemblem modules are arranged on the electronic equipment, and each WebAssemblem module is provided with a module ID and a plurality of function indexes before the WebAssemblem modules are arranged on the electronic equipment; then, responding to a calling instruction, and determining a target WebAssemble module according to the module ID; and then, determining an objective function in the objective WebAssembly module by using the function index, and deriving the objective function for other modules to call.

Description

Method and device for calling function by WebAsssembly module
Technical Field
The invention relates to the technical field of computers, in particular to a method and a device for calling functions by a WebAssembly module.
Background
A resource-constrained device (Resource Constrained Device) generally refers to an electronic device that has limited power supply, limited computing power, and limited storage capacity. Such as Smart cards (Smart Card) and security elements (secure elements), the internal hardware parts of which typically comprise: a central processing unit (Central Processing Unit, CPU for short), a random access Memory (Random Access Memory, RAM for short), a Read-Only Memory (ROM for short), an encryption/decryption engine, an input/output interface, a security sensor, and the like. These devices typically run on-chip operating systems (Chip Operating System, COS), which can communicate with external interface devices through input/output interfaces, receive commands from the external interface devices, process the commands internally, send command responses to the external interface devices, and provide the interface devices with secure access and control functions such as secure storage of private information, secure execution of important programs, user authentication, and the like.
Early on-chip operating system programs of the resource-constrained devices are solidified in a chip and cannot be modified after being released, so that the application range is greatly limited, and along with the advent of programming technology, the technology is also used on resource-constrained devices, for example, a CPU smart card platform developed by a solar microsystem, and the platform comprises an operating environment, a virtual machine, a CPU smart card API (Application Programming Interface, application program interface) and the like, so that the CPU smart card application can be run. After the CPU smart card is issued, the CPU smart card application may be loaded onto another CPU smart card or an existing application on the CPU smart card may be modified, and a plurality of CPU smart card applications may coexist independently. The application downloaded after the release of the CPU smart card can refer to the functions of other applications, such as calling the functions of other applications, but the calling is unidirectional, and the cross-calling cannot be realized.
Disclosure of Invention
The invention provides a method and a device for calling functions by a WebAssemble module, which solve the problem that program modules cannot be mutually and crossly called.
In a first aspect, the present invention provides a method for calling a function by using a WebAssembly module, which is applied to an electronic device, where a plurality of WebAssembly modules are installed on the electronic device, and the method includes:
Before the WebAssembly modules are installed on the electronic equipment, each WebAssembly module is provided with a module ID and a plurality of function indexes; the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
responding to a call instruction, and determining a target WebAssembly module according to the module ID;
determining an objective function in the objective WebAssemble module by using the function index, and leading out the objective function for calling; the target function can be called in another WebAsssembly module.
Optionally, modifying a standard WebAssembly by using a converter to obtain WebAssembly modules, wherein each WebAssembly module is provided with a module ID, and a plurality of function indexes include:
inputting a standard WebAssemblem module to the converter, wherein the standard WebAssemblem module is provided with a module name, a function name and a function signature index;
in the converter, replacing a module name in the standard WebAsssembly module with the module ID, and replacing the function name and the function signature index with the function index to obtain the WebAsssembly module;
The module ID and the function indexes are numbers, the function indexes are extracted from the output section of the WebAssembly module, and function byte codes can be found through the function indexes.
Optionally, each WebAssembly module is provided with a module ID, and the several function indexes further include:
verifying the corresponding relation between the module ID and the WebAssemble module;
if the module ID is correct, updating the module ID according to the module name; and/or the number of the groups of groups,
verifying whether the corresponding relation between the function index and the function in the WebAssemble module is correct;
if the function is correct, updating the function index according to the function name and the function signature index.
Optionally, before the WebAssembly module is mounted to the electronic device, the method further includes:
compiling the standard WebAssembly module and sending the standard WebAssembly module to the converter;
the converter changes the module name, the function name and the function signature index of the standard WebAssembly module according to a mask file and an export file;
the mask file comprises the WebAssemble module, a module address table and pre-installation information, wherein the module address table records a module address and the module ID;
The export file records the module name, the module ID, a WebAsssembly module corresponding to the module ID, the function name, the function signature index, a plurality of function indexes and functions corresponding to the function indexes.
Optionally, the determining, in response to the call instruction, the target WebAssembly module according to the module ID includes:
when the calling instruction is executed by a virtual machine on the electronic equipment, the virtual machine determines the module ID and the function index according to the calling instruction;
and determining the target WebAsssembly module according to the module ID and the export file.
Optionally, the determining the objective function in the objective WebAssembly module by using the function index, and deriving the objective function to call includes:
determining an objective function in the objective WebAssemble module according to the function index;
and exporting the target function, and calling in another WebAsssembly module.
Optionally, the determining the target function in the target WebAssembly module by using the function index, and deriving the target function to call further includes:
if the number of the called objective functions is multiple, deriving an order according to the ordering of the module IDs;
And exporting the objective function according to the export sequence.
Optionally, the method further comprises:
constructing a module address table on the electronic equipment; the module address table records the module address of each WebAssembly module and the module ID, and the module address and the module ID establish an association relation in the module address table.
In a second aspect, the present application provides a device for calling a function by using a WebAssembly module, which is applied to an electronic device, and the electronic device is provided with a plurality of WebAssembly modules, and the device includes:
the setting unit is used for setting a module ID and a plurality of function indexes in each WebAssemblem module before the WebAssemblem module is installed on the electronic equipment;
the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
the determining unit is used for responding to the calling instruction and determining a target WebAsssembly module according to the module ID;
the export unit is used for determining an objective function in the objective WebAssembly module by using the function index and exporting the objective function for calling;
The target function can be called in another WebAsssembly module.
In a third aspect, the present application provides a computer readable storage medium comprising execution instructions which, when executed by a processor of an electronic device, perform the method according to any of the first aspects.
In a fourth aspect, the present application provides an electronic device comprising a processor;
and a memory storing an execution instruction, a WebAssembly module, a module ID table, and a module address table;
an installer for installing the WebAssembly module onto the resource-constrained device;
a virtual machine for interpreting execution of WebAssembly bytecode;
a converter for converting the WebAssembly module;
the virtual machine is configured to read the WebAssembly byte code from the memory, and execute the instruction to implement the method for calling a function by the WebAssembly module according to any one of the first aspect.
The application provides a method for calling functions by using WebAssemblem modules, which is applied to electronic equipment, wherein the electronic equipment is provided with a plurality of WebAssemblem modules, and each WebAssemblem module is provided with a module ID and a plurality of function indexes before the WebAssemblem modules are installed on the electronic equipment; then, responding to a calling instruction, and determining a target WebAssemble module according to the module ID; and then, determining an objective function in the objective WebAssembly module by using the function index, and deriving the objective function for calling. In the application, the WebAssemblem module can call other WebAssemblem module functions according to the module ID and a plurality of function indexes, and the functions can be called among a plurality of WebAssemblem modules.
Further effects of the above-described non-conventional preferred embodiments will be described below in connection with the detailed description.
Drawings
In order to more clearly illustrate the embodiments of the invention or the prior art solutions, the drawings which are used in the description of the embodiments or the prior art will be briefly described below, it being obvious that the drawings in the description below are only some of the embodiments described in the present invention, and that other drawings can be obtained according to these drawings without inventive faculty for a person skilled in the art.
FIG. 1 is a flowchart of a method for invoking a function by a WebAssemblem module according to one embodiment of the invention;
FIG. 2 is a flowchart illustrating the steps of step S01 according to an embodiment of the present invention;
FIG. 3 is a flowchart illustrating the steps of step S02 according to an embodiment of the present invention;
FIG. 4 is a flowchart illustrating a step S03 according to an embodiment of the present invention;
FIG. 5 is a flowchart showing another step of step S03 according to an embodiment of the present invention;
FIG. 6 is a block diagram of a device for invoking a function by a WebAssemblem module according to an embodiment of the present invention;
fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the technical solutions of the present invention will be clearly and completely described below with reference to specific embodiments and corresponding drawings. It will be apparent that the described embodiments are only some, but not all, embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
A resource-constrained device (Resource Constrained Device) generally refers to an electronic device that has limited power supply, limited computing power, and limited storage capacity. Early on-chip operating system programs of the resource-constrained devices are solidified in a chip and cannot be modified after being released, so that the application range is greatly limited, and along with the advent of programming technology, the technology is also used on resource-constrained devices, such as a CPU smart card platform developed by a solar microsystem, and the platform comprises an operating environment, a virtual machine, a CPU smart card API and the like and can run CPU smart card applications. After the CPU smart card is issued, the CPU smart card application may be loaded onto another CPU smart card or an existing application on the CPU smart card may be modified, and a plurality of CPU smart card applications may coexist independently. The application downloaded after the release of the CPU smart card can refer to the functions of other applications, such as calling the functions of other applications, but the calling is unidirectional, and the cross-calling cannot be realized. That is, the program data packet file (CAP) to be installed can only refer to the already installed packets in the resource-constrained device, if the two CAPs to be downloaded have cross references, that is, the CAP1 calls the function in the CAP2, and the CAP2 also calls the function in the CAP1, then the two CAP packets cannot be installed, which greatly limits the flexibility of application writing.
In view of the above, the present invention provides a method for calling functions by using WebAssembly modules, which is applied to an electronic device, and the electronic device is provided with a plurality of WebAssembly modules. The electronic device may be a resource-constrained device, and as shown in fig. 1, a specific embodiment of a method for calling a function by using a WebAssembly module provided by the present invention is shown. In this embodiment, the method includes step S01, step S02, and step S03.
Step S01: before the WebAssembly modules are installed on the electronic equipment, each WebAssembly module is provided with a module ID and a plurality of function indexes; the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
step S02: responding to a call instruction, and determining a target WebAssembly module according to the module ID;
step S03: determining an objective function in the objective WebAssemble module by using the function index, and leading out the objective function for calling; the target function can be called in another WebAsssembly module.
It may be known that, before the WebAssembly module is not provided with a module ID and a plurality of function indexes, the existing WebAssembly module includes a module name, a function name and a function signature index, and since the WebAssembly module includes a module name, a function name and a function signature index, more memory is occupied, each WebAssembly module is provided with a module ID, a plurality of function indexes, a deletion module name, a function name and a function signature index, so that the WebAssembly module occupies less memory space.
In the prior art, webAssembly is executable byte code at the browser end, and the main problem to be solved is browser performance. The compiler can compile source codes written in high-level languages such as C, C ++, go, rust and the like into a WebAssemble module, and the WebAssemble module can run at a high speed in a browser, so that the native execution efficiency can be achieved. WebAssemblely bytecodes are assembly languages based on a general-purpose operating system, rather than physical machine languages, so WebAssemblely provides a quick, extensible and safe way to run the same code on all computers, and realizes one-time compiling and running everywhere. If the WebAssemblem byte codes and the virtual machines are cut and transplanted to the resource-restricted equipment, the WebAssemblem module can be downloaded to the released equipment to run.
In the prior art, the WebAssembly module is in a binary format with small volume and quick loading, and aims to fully play the hardware capability to achieve the original execution efficiency, and the WebAssembly module is in a brand new format with portability, small volume and quick loading, wherein the format comprises an identification number ID and a segment. The WebAssembly module main body mainly comprises a plurality of segments, the segment data comprises all information of the module segments, and each segment corresponds to an identification number ID. For example, when the identification number ID is 0, it is a Custom segment (Custom), which is mainly used for storing data such as debug information; when the identification number ID is 1, the Type field (Type) stores a function signature of the import function and the function inside the module. The function signature comprises the input parameter type, sequence and number of the function, and the return value type, sequence and number; when the ID of the identification number is 2, a plurality of pieces of Import function information are stored in the Import section (Import), and each piece of Import information records the module name, the function name and the function signature information index of the Import function; when the ID of the identification number is 3, the identification number is a Function section (Function), a Function signature index value of a Function in the Function section storage module is stored, and the Function signature index points to a Function signature of the type section; when the ID of the identification number is 4, the identification number is a Table segment (Table), the Table segment is a storage object reference, the function of a function pointer can be realized through the Table segment, the function pointer can be imported from an external host, and meanwhile, the function pointer can also be exported to an external host environment; when the identification number ID is 5, the Memory segment (Memory) is the running dynamic data of the stored program, and can be imported from an external host and exported to an external host environment; when the identification number ID is 6, the Global segment (Global) is used for storing all variable values; when the identification number ID is 7, the Export section (Export) is used to store Export information of all Export member variables and functions of the module, and each Export information exports a member variable or function, where the Export information includes a name of an Export member, a type of the Export member and an index of the Export member. If the derived function comprises the function name and the function index of the derived function, signature information and function byte codes of the derived function can be found according to the function index; when the identification number ID is 8, the starting segment (Start) is used for designating the index value of the module initialization function; when the ID of the identification number is 9, the identification number is an element segment (Elem) and is used for storing an index value of the function; when the identification number ID is 10, the code segment is used for storing instruction codes of functions, wherein the function call instruction gives indexes of the functions. If the function call instruction calls other module functions, the index points to the Import information of a certain Import function of the Import section, and the running environment needs to find the called function according to the Import information when running; if the function call instruction calls the function of the module, the index points to the function of the module; when the identification number ID is 11, the Data segment (Data) is used for storing static Data of the initialized memory.
It will be appreciated that the module name is an ASC string and that the target WebAssembly module can be determined from a plurality of WebAssembly modules. Similarly, the function name is an ASC string, and the objective function can be determined in the objective WebAsssembly module. The function signature index may point to a location of a type segment in the WebAssembly module that stores signature information of the import function, the signature information including an input parameter type, an input parameter order, and an input parameter number of the objective function, a function return value type, a function return value order, and a function return value number.
Before the WebAsssembly module sets a module ID and a plurality of function indexes, the WebAsssemly module virtual machine cross-module call function principle: the WebAssemble module can realize the function of calling other modules, in the import section of the calling module, a plurality of pieces of import information are contained, each piece of import information contains one piece of imported function information, the name of the module of the WebAssemble module where the import function is located, the name of the imported function, the signature information index of the imported function are contained, the signature information of the imported function can be found in the type section of the module according to the signature information index of the imported function, the function signature information contains the input parameter type, sequence and number of the function, and the return value type, sequence and number. When the WebAssembly virtual machine executes module byte codes, if functions of other modules are found to be called, the target WebAssembly module is found according to the module name by utilizing the imported function information in the module import section, then the target function is found in the export section of the target WebAssembly module according to the function name, the target function index is taken out after the target function is found, signature information of the target function is found in the type section of the target WebAssembly module according to the function index, the signature information is compared with the target function signature information indicated in the type section of the target WebAssembly module, and the virtual machine loads the target WebAssembly module after the comparison is consistent and finds the target function according to the target function index, so that the target function byte codes are executed. The function calling process can determine the function link relation and the call matching degree only when the function calling process is operated, the searching process is complicated, and the execution efficiency is low if the function calling process is applied to the module limited equipment.
Because the module ID is the unique identifier of the WebAssemblem module, the function index is the unique identifier of the function in the WebAssemblem module, namely, the target WebAssemblem module can be found through the module ID, the target function needing to be called is arranged in the target WebAssemblem module, after the target WebAssemblem module is found, the function index can be used for determining the target function in the target WebAssemblem module, the target function is derived, and the target function is applied to the other WebAssemblem module. The WebAssemble module can call other WebAssemble module functions according to the module ID and a plurality of function indexes, so that the efficiency of function call is improved, and functions can be called among a plurality of WebAssemble modules.
Fig. 1 shows only a basic embodiment of the method according to the invention, on the basis of which certain optimizations and developments are made, but other preferred embodiments of the method can also be obtained.
In an embodiment, the converter reforms a standard WebAssembly to obtain WebAssembly modules, as shown in fig. 2, in step S01, each WebAssembly module is provided with a module ID, and a plurality of function indexes include:
step S011: inputting a standard WebAssemblem module to the converter, wherein the standard WebAssemblem module is provided with a module name, a function name and a function signature index;
Step S012: in the converter, a module name in the standard WebAssembly module is replaced by the module ID, and the function name and the function signature index are replaced by the function index to obtain the WebAssembly module.
The module ID and the function index are numbers, the function index is extracted from an output section of the WebAssembly module, and function byte codes can be found through the function index.
It will be appreciated that the transducer may be integrated with the resource constrained device or may be provided as a separate device external to the resource constrained device.
And the multi-module function call is linked through the converter, so that the link relation of the WebAssemblem module is ensured to be kept unchanged logically, the link work when the electronic equipment installs the WebAssemblem module is reduced, and the module ID and the function index are numbers, so that the storage of the WebAssemblem module can be reduced. A plurality of source code groups realized by a high-level language are compiled into a plurality of WebAssemble modules, and then the WebAssemble modules are secondarily linked by using a converter, so that the cross-calling link relation of the WebAssemble modules is simplified, and the cross-calling WebAssemble modules can be installed and used on resource-limited equipment.
In an embodiment, each WebAssembly module is provided with a module ID, and the several function indexes further include:
step S013: verifying the corresponding relation between the module ID and the WebAssemble module;
step S0131: if the module ID is correct, updating the module ID according to the module name; and/or the number of the groups of groups,
step S014: verifying whether the corresponding relation between the function index and the function in the WebAssemble module is correct;
step S0141: if the function is correct, updating the function index according to the function name and the function signature index.
The verification step can ensure that the module ID and/or the function index in the WebAssemble module are correct, facilitate later function call, and ensure the accuracy of function call.
In an embodiment, before the WebAssembly module is mounted to the electronic device, the method further comprises:
step S10: compiling the standard WebAssembly module and sending the standard WebAssembly module to the converter;
step S20: the converter changes the module name, the function name and the function signature index of the standard WebAssembly module according to a mask file and an export file;
The mask file comprises the WebAssemble module, a module address table and pre-installation information, wherein the module address table records a module address and the module ID;
the export file records the module name, the module ID, a WebAsssembly module corresponding to the module ID, the function name, the function signature index, a plurality of function indexes and functions corresponding to the function indexes.
Through the converter, the WebAssemble module is set, so that a function is called in the WebAssemble module later, the target WebAssemble module and the target function can be found by using the module ID and the function index, and the calling speed is faster and more accurate. The content of the export file record is convenient for the converter to reform and link the standard WebAssemble which is subsequently installed on the resource-constrained device, so that other WebAssemble modules which are subsequently downloaded can call functions in the WebAssemble modules which are already installed on the resource-constrained device.
When the WebAssembly module is not installed on the electronic equipment, the WebAssembly module is a preinstallation module, and the preinstallation module is a WebAssembly module which is compiled and converted in advance and is filled on the resource-limited equipment together with a WebAssembly running environment, a virtual machine and an installer as firmware. The pre-installation module includes an API (application program interface) module and other application modules that need to be installed on the resource-constrained device prior to release. The preinstalled module is compiled into a plurality of WebAssemble modules, and then is set and linked by a converter to generate a mask file and an export file, wherein the mask file is compiled by a local compiler to form firmware together with a module installer, an operating environment, a virtual machine and other functional modules which are realized by the local language of the resource-constrained device, and is filled into the resource-constrained device.
When setting and linking N WebAssemble modules, the converter distributes module IDs for the N modules according to the conversion sequence,i.e. the module ID of the mth module is ID m ,ID m =m, where m= {0,1, …, N-1}, to set/reform the function import information of the import section of the WebAssembly module. The converter searches the WebAssemble module according to the module name and the function name of each piece of imported function information, if the found WebAssemble module is a preinstalled module, the converter takes out the signature information of the exported function from an exported file generated when the converter converts the preinstalled module to be compared, takes out the module ID and the function index to replace the module name and the function name of the piece of imported function information after the comparison is consistent, and then deletes the function signature information in the piece of imported function information; if the found target WebAssemblem module is the WebAssemblem module set by the converter, the converter finds the corresponding target WebAssemblem module according to the module name, finds the corresponding target function according to the function name from the export section of the target WebAssemblem module, takes out the function index, finds the function signature information according to the function index for comparison, uses the module ID for finding the target WebAssemblem module to replace the module name of the imported function information after comparison is consistent, uses the found function index to replace the imported function name, and then deletes the function signature information in the imported function information.
In one embodiment, as shown in fig. 3, step S02, in response to a call instruction, determines a target WebAssembly module according to the module ID, including step S021 and step S022.
Step S021: the call instruction is sent to the converter, and the converter determines the module ID and the function index according to the call instruction;
step S022: and determining the target WebAsssembly module according to the module ID and the export file.
When the WebAssembly module needs to call a function, an instruction is obtained according to the imported information, the call instruction is sent to the converter, the call instruction comprises information of the module ID and the function index, and the converter can determine the module ID and the function index according to the call instruction. The export file comprises the module ID, a WebAsssembly module corresponding to the module ID, the function index and a function corresponding to the function index. The WebAsssembly module corresponding to the module ID can be found in the export file according to the module ID, and the WebAsssemly module is the target WebAsssemly module.
In one embodiment, as shown in fig. 4, in step S03, the function index is used to determine an objective function in the objective WebAssembly module, and the objective function is derived to be called, which includes step S031 and step S032.
Step S031: determining an objective function in the objective WebAssemble module according to the function index;
step S032: and exporting the objective function, and carrying out calling on the other WebAsssembly module.
Likewise, according to the function index, a function corresponding to the function index can be found in the export file, and the function is the target function.
In an embodiment, as shown in fig. 5, in step S03, the function index is used to determine an objective function in the objective WebAssembly module, and the objective function is derived to be called, and further includes step S033 and step S034.
Step S033: if the number of the called objective functions is multiple, deriving an order according to the ordering of the module IDs;
step S034: and exporting the objective function according to the export sequence.
And according to the order export sequence of the module IDs, and then according to the export sequence, the objective function is exported, so that the objective function can be exported orderly, and the problem that the export program is blocked due to disorder of the export program of the objective function is avoided.
In an embodiment, the method for calling the function by the WebAssembly module further includes: constructing a module address table on the electronic equipment; the module address table records the module address of each WebAssembly module in the resource-constrained device and the module ID, and the module address and the module ID establish an association relationship in the module address table.
Because the module address table records the module address of each WebAssembly module and the module ID, the association relationship between the module address and the module ID is established in the module address table, after the module ID of the WebAssembly module where the calling function is located is determined, the target WebAssembly module can be determined according to the module ID, and the target WebAssembly module can be found according to the module address of the target WebAssembly module. However, the association relationship between the module address and the module ID is established in the module address table, so that the above steps can be omitted, the module address of the target WebAssembly module is directly determined by the module ID, the target WebAssembly module is found according to the module address, and the target function is called in the target WebAssembly module, so that the speed of function call can be improved.
And after the WebAsssembly module is processed by the converter, the WebAsssemly module is installed to the resource-constrained device. If a plurality of WebAssemblem modules are required to be installed on the resource-constrained device, each WebAssemblem module is downloaded to the resource-constrained device successively according to the order of the module IDs of the WebAssemblem modules from small to large. The resource-constrained device stores each WebAssemble module into a storage space according to the downloading sequence, and allocates module address IDs for the N installed WebAssemble modules, and the resource-constrained device receives the ID 0 To ID N-1 The allocation is performed. Wherein ID 0 To ID N-1 Either continuous or discontinuous. The resource-restricted device uses the module addresses of the N WebAssemble modules according to the ID 0 ~ID N-1 Sequentially filling the module address table. The installer replaces the module ID allocated by the converter in the export section of all the WebAssemble modules with the module ID' allocated by itself. Replacing module IDs in import information of all m-th referenced WebAssemblem modules in export sections of the downloaded N WebAssemblem modules with IDs m I.e. ID in import information referring to the mth module function m =ID m M= {0,1, …, N-1}. Thus, the logical order and the reference relation of the function cross-module call can be kept unchanged, and an installer does not need to link the function cross-module call.
When the WebAssemble virtual machine executes the module byte codes, if functions of other modules are required to be called, the imported function information in the import section of the WebAssemble module is taken out, the module ID in the imported information is utilized to look up a table to obtain a target WebAssemble module address, the target WebAssemble module is quickly found according to the module address, the target function is quickly found by utilizing the function index in the imported information, and then the target function byte codes are executed.
When the module ID and the function indexes are set, the converter compares the target function signature information recorded in the type section in the calling module with the target function signature information recorded in the type section in the target WebAssemble module, the WebAssemble module is downloaded to the resource-limited equipment, signature information comparison is not needed when the virtual machine calls the function, and the cross-module function call is accelerated.
In one embodiment, the pre-installed source code comprises an API source code set and a pre-installed application source code set, the API source code providing interface functions for pre-installed applications and post-download applications, the pre-installed applications being applications installed on the device prior to release of the resource-constrained device, the pre-installed applications being implemented in a high-level language. The API source code and the preinstalled application can be implemented in different high-level languages and can be compiled into a WebAssemblem module through a WebAssemblem compiler. The converter is responsible for extracting an export section of the input WebAssemble module, distributing a module ID for the pre-installed module to generate an export file, and transforming the import section of the input WebAssemble module to generate a mask file.
Illustratively, the APIs and pre-installed applications may be implemented using high-level language code, forming several source code sets, which may be referenced to each other, and which may be implemented using different high-level languages. The set of source codes may then be compiled using a WebAssembly compiler to generate a pre-installed WebAssembly module. Then, if the WebAssemble module is installed in an intervening manner, the converter reads in the WebAssemble module, links and converts the modules, verifies the introduced function matching, and generates an export file and a mask file. The mask file is then compiled, using a native compiler, via the native compiler, along with the module installer, running environment, virtual machine and other functional module source code written in the native language to form resource constrained device firmware.
In an embodiment, the functions to be implemented are implemented using high-level language coding, and the API functions provided by the system may be referenced to form source code sets. Then, the WebAssemblem compiler is used for compiling the source code groups into WebAssemblem module files, and the WebAssemblem modules can be called unidirectionally or cross-called. The converter reads in the export file, links and converts a plurality of WebAssembly modules, verifies the introduced function matching, and generates a download file which can be downloaded to and run on the resource-constrained device. Then, several download files are converted into several download instruction streams, and downloaded to the resource-constrained device, and the installation instruction is sent to the resource-constrained device, and the download module is installed. The resource-limited device receives a module downloading instruction of the downloader, stores the downloading module into a memory of the resource-limited device, allocates a module ID' for the downloading module, updates the module ID allocated by the external device in an importing section of the downloading module, stores the address of the downloading module into a module address table, stores the module ID into a module ID table, and then executes an installation instruction of the downloader to complete instantiation of the downloading module.
FIG. 6 shows an embodiment of the apparatus for invoking a function by the WebAssemblem module according to the present invention. The apparatus described in this embodiment is a physical apparatus for performing the method described in fig. 1. The technical solution is essentially identical to the above embodiment, and the corresponding description in the above embodiment is also applicable to this embodiment. The device in this embodiment includes:
the setting unit is used for setting a module ID and a plurality of function indexes in each WebAssemblem module before the WebAssemblem module is installed on the electronic equipment;
the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
the determining unit is used for responding to the calling instruction and determining a target WebAsssembly module according to the module ID;
the export unit is used for determining an objective function in the objective WebAssembly module by using the function index and exporting the objective function for calling;
the target function can be called in another WebAsssembly module.
In one embodiment, the converter reads a piece of function import information of the WebAssembly module import section, where the piece of function import information includes a module name of a WebAssembly module where the objective function is located, a function name of the objective function, and a function signature index of the objective function, and finds a function signature of the objective function in the type section of the WebAssembly module according to the objective function signature index. Then, the converter finds the imported module according to the module name of the import function, acquires the module ID of the imported module, reads the module ID from the export file if the imported module is a preinstalled module, and uses the ID temporarily assigned by the converter if the imported WebAssemble module is another WebAssemble module downloaded later. Then, the converter reads the export section imported into the WebAssembly module, the export section containing export function information of the WebAssembly module. The converter traverses the exported function information of the exported segment imported into the WebAsssembly module according to the function name of the target function, finds the item of target function information with the same function name, and fetches the function index. Then, the converter finds the function signature index of the derived function in the function segment of the target WebAssembly module according to the fetched function index. Then, the converter finds the function signature information of the derived function in the type segment of the target WebAssembly module according to the function signature index of the target function. Then, the converter reports errors and exits the conversion process when the function signature information of the imported WebAssemblem module is inconsistent with the function signature information of the target WebAssemblem module, and when the function signature information of the imported WebAssemblem module is inconsistent with the function signature information of the target WebAssemblem module, the converter reforms the imported section of the WebAssemblem module, replaces the module name of the imported function of the imported section with the module ID, replaces the function name of the imported function of the imported section with the target function index, and deletes the function signature index of the imported function.
Additionally, based on the embodiment shown in fig. 6, optionally, the setting unit further includes:
the input unit is used for inputting a standard WebAssemble module to the converter, wherein the standard WebAssemble module is provided with a module name, a function name and a function signature index;
the replacing unit is a converter, in the converter, a module name in the standard WebAssemblem module is replaced by the module ID, and the function name and the function signature index are replaced by the function index to obtain the WebAssemblem module;
the module ID and the function index are numbers, the function index is extracted from an output section of the WebAssembly module, and function byte codes can be found through the function index.
Optionally, the setting unit further includes a verification unit, where the verification unit is configured to:
verifying the corresponding relation between the module ID and the WebAssemble module;
if the module ID is correct, updating the module ID according to the module name; and/or the number of the groups of groups,
verifying whether the corresponding relation between the function index and the function in the WebAssemble module is correct;
if the function is correct, updating the function index according to the function name and the function signature index.
The device for calling the function by the WebAssemblem module further comprises a compiling unit, wherein the compiling unit is used for compiling the standard WebAssemblem module and sending the standard WebAssemblem module to the converter;
the converter is used for replacing the module name, the function name and the function signature index of the standard WebAssemble module to obtain a mask file and the export file;
the mask file comprises the WebAssemble module, a module address table and pre-installation information, wherein the module address table records a module address and the module ID;
the export file records the module name, the module ID, a WebAsssembly module corresponding to the module ID, the function name, the function signature index, the function index and a function corresponding to the function index.
Optionally, the determining unit is configured to:
the call instruction is sent to the converter, and the converter determines the module ID and the function index according to the call instruction;
and determining the target WebAsssembly module according to the module ID and the export file.
Optionally, the deriving unit is configured to:
Determining an objective function in the objective WebAsssembly module according to the function index and the export file;
and exporting the target function, and calling in another WebAsssembly module.
Optionally, the deriving unit is further configured to:
if the number of the called objective functions is multiple, deriving an order according to the ordering of the module IDs;
and exporting the objective function according to the export sequence.
Optionally, the device for calling the function by the WebAssembly module further includes:
the table building unit is used for building a module address table on the electronic equipment; the module address table records the module address of each WebAssembly module and the module ID, and the module address and the module ID establish an association relation in the module address table.
Fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present invention. At the hardware level, the electronic device comprises a processor, optionally an internal bus, a network interface, a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory (non-volatile Memory), such as at least 1 disk Memory. Of course, the electronic device may also include hardware required for other services.
Further, the memory may also store data such as WebAssembly module, module ID table, module address table, and the like.
The electronic device further includes:
an installer for installing the WebAssembly module onto the resource-constrained device;
a virtual machine for interpreting execution of WebAssembly bytecode;
a converter for converting the WebAssembly module;
the virtual machine is configured to read the WebAssembly byte code from the memory, and the virtual machine may be a processor.
The processor, network interface, and memory may be interconnected by an internal bus, which may be an ISA (Industry Standard Architecture ) bus, a PCI (Peripheral Component Interconnect, peripheral component interconnect standard) bus, or EISA (Extended Industry Standard Architecture ) bus, among others. The buses may be classified as address buses, data buses, control buses, etc. For ease of illustration, only one bi-directional arrow is shown in FIG. 7, but not only one bus or type of bus.
And the memory is used for storing the execution instruction. In particular, a computer program that executes instructions may be executed. The memory may include memory and non-volatile storage and provide the processor with instructions and data for execution.
In one possible implementation manner, the processor reads the corresponding execution instruction from the nonvolatile memory to the memory and then runs the execution instruction, and may also acquire the corresponding execution instruction from other devices, so as to form a device for calling the function by the WebAssembly module on a logic level. The processor executes the execution instructions stored in the memory to implement the method for calling the function by the WebAssembly module provided in any embodiment of the present invention.
The method executed by the WebAssembly module function calling device provided in the embodiment of fig. 6 of the present invention may be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or by instructions in the form of software. The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but also digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), field programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. The disclosed methods, steps, and logic blocks in the embodiments of the present invention may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The steps of the method disclosed in connection with the embodiments of the present invention may be embodied directly in the execution of a hardware decoding processor, or in the execution of a combination of hardware and software modules in a decoding processor. The software modules may be located in a random access memory, flash memory, read only memory, programmable read only memory, or electrically erasable programmable memory, registers, etc. as well known in the art. The storage medium is located in a memory, and the processor reads the information in the memory and, in combination with its hardware, performs the steps of the above method.
The embodiment of the invention also provides a computer readable storage medium, which stores execution instructions, and when the stored execution instructions are executed by a processor of electronic equipment, the electronic equipment can be enabled to execute the method for calling the function by the WebAssembly module provided in any embodiment of the invention, and the method is specifically used for executing the method shown in any of figures 1-5.
The electronic device described in the foregoing embodiments may be a computer.
It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as a method or a computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or a combination of software and hardware aspects.
The embodiments of the present invention are described in a progressive manner, and the same and similar parts of the embodiments are all referred to each other, and each embodiment is mainly described in the differences from the other embodiments. In particular, for the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments in part.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article or apparatus that comprises the element.
The foregoing is merely exemplary of the present invention and is not intended to limit the present invention. Various modifications and variations of the present invention will be apparent to those skilled in the art. Any modification, equivalent replacement, improvement, etc. which come within the spirit and principles of the invention are to be included in the scope of the claims of the present invention.

Claims (9)

1. A method for calling functions by using WebAssembly modules, which is applied to electronic equipment, wherein the electronic equipment is provided with a plurality of WebAssembly modules, and the method is characterized by comprising the following steps:
before the WebAssembly modules are installed on the electronic equipment, each WebAssembly module is provided with a module ID and a plurality of function indexes; the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
responding to a call instruction, and determining a target WebAssembly module according to the module ID;
determining an objective function in the objective WebAssemble module by using the function index, and leading out the objective function for calling; wherein the objective function can be called in another WebAssembly module;
modifying a standard WebAssemblem by using a converter to obtain WebAssemblem modules, wherein each WebAssemblem module is provided with a module ID, and a plurality of function indexes comprise: inputting a standard WebAssemblem module to the converter, wherein the standard WebAssemblem module is provided with a module name, a function name and a function signature index; in the converter, replacing a module name in the standard WebAsssembly module with the module ID, and replacing the function name and the function signature index with the function index to obtain the WebAsssembly module; the module ID and the function indexes are numbers, the function indexes are extracted from the output section of the WebAssembly module, and function byte codes can be found through the function indexes;
Compiling the standard WebAssembly module and sending the standard WebAssembly module to the converter before the WebAssembly module is mounted on the electronic device; the converter changes the module name, the function name and the function signature index of the standard WebAssembly module according to a mask file and an export file; the mask file comprises the WebAssemble module, a module address table and pre-installation information, wherein the module address table records a module address and the module ID; the export file records the module name, the module ID, a WebAsssembly module corresponding to the module ID, the function name, the function signature index, a plurality of function indexes and functions corresponding to the function indexes.
2. The method for calling functions by WebAssembly module according to claim 1, wherein each WebAssembly module is provided with a module ID, and the plurality of function indexes further comprise:
verifying the corresponding relation between the module ID and the WebAssemble module;
if the module ID is correct, updating the module ID according to the module name; and/or the number of the groups of groups,
Verifying whether the corresponding relation between the function index and the function in the WebAssemble module is correct;
if the function is correct, updating the function index according to the function name and the function signature index.
3. The method for calling a function by a WebAssembly module according to claim 1, wherein the determining the target WebAssembly module according to the module ID in response to the call instruction includes:
when the calling instruction is executed by a virtual machine on the electronic equipment, the virtual machine determines the module ID and the function index according to the calling instruction;
and determining the target WebAsssembly module according to the module ID and the export file.
4. The method for calling a function by a WebAssembly module of claim 3, wherein determining an objective function within the objective WebAssembly module with the function index and deriving the objective function for calling comprises:
determining an objective function in the objective WebAssemble module according to the function index;
and exporting the target function, and calling in another WebAsssembly module.
5. The method for calling a function by a WebAssembly module of claim 3, wherein determining an objective function within the objective WebAssembly module with the function index and deriving the objective function for calling further comprises:
If the number of the called objective functions is multiple, deriving an order according to the ordering of the module IDs;
and exporting the objective function according to the export sequence.
6. The method for a WebAssembly module to call a function of claim 1, wherein the method further comprises:
constructing a module address table on the electronic equipment; the module address table records the module address of each WebAssembly module and the module ID, and the module address and the module ID establish an association relation in the module address table.
7. A WebAssembly module function calling device applied to an electronic device, wherein a plurality of WebAssembly modules are installed on the electronic device, and the device is characterized by comprising:
the setting unit is used for setting a module ID and a plurality of function indexes in each WebAssemblem module before the WebAssemblem module is installed on the electronic equipment;
the module ID is a unique identifier of the WebAssemble module, and the function index is a unique identifier of a function in the WebAssemble module;
the determining unit is used for responding to the calling instruction and determining a target WebAsssembly module according to the module ID;
The export unit is used for determining an objective function in the objective WebAssembly module by using the function index and exporting the objective function for calling;
the target function can be called in another WebAsssembly module;
modifying a standard WebAssemblem by using a converter to obtain WebAssemblem modules, wherein each WebAssemblem module is provided with a module ID, and a plurality of function indexes comprise: inputting a standard WebAssemblem module to the converter, wherein the standard WebAssemblem module is provided with a module name, a function name and a function signature index; in the converter, replacing a module name in the standard WebAsssembly module with the module ID, and replacing the function name and the function signature index with the function index to obtain the WebAsssembly module; the module ID and the function indexes are numbers, the function indexes are extracted from the output section of the WebAssembly module, and function byte codes can be found through the function indexes;
compiling the standard WebAssembly module and sending the standard WebAssembly module to the converter before the WebAssembly module is mounted on the electronic device; the converter changes the module name, the function name and the function signature index of the standard WebAssembly module according to a mask file and an export file; the mask file comprises the WebAssemble module, a module address table and pre-installation information, wherein the module address table records a module address and the module ID; the export file records the module name, the module ID, a WebAsssembly module corresponding to the module ID, the function name, the function signature index, a plurality of function indexes and functions corresponding to the function indexes.
8. A computer readable storage medium, wherein the computer readable storage medium stores a computer program for executing the method of invoking a function by the WebAssembly module of any of claims 1 to 6.
9. An electronic device, the electronic device comprising:
a processor;
the memory is used for storing the processor executable instructions, the WebAssembly module, the module ID table and the module address table;
an installer for installing the WebAssembly module onto the resource-constrained device;
a virtual machine for interpreting execution of WebAssembly bytecode;
a converter for converting the WebAssembly module;
the virtual machine is configured to read the WebAssembly byte code from the memory, and execute the instruction to implement a method for invoking a function by using the WebAssembly module according to any one of claims 1 to 6.
CN202310278575.9A 2023-03-21 2023-03-21 Method and device for calling function by WebAsssembly module Active CN115982784B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310278575.9A CN115982784B (en) 2023-03-21 2023-03-21 Method and device for calling function by WebAsssembly module

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310278575.9A CN115982784B (en) 2023-03-21 2023-03-21 Method and device for calling function by WebAsssembly module

Publications (2)

Publication Number Publication Date
CN115982784A CN115982784A (en) 2023-04-18
CN115982784B true CN115982784B (en) 2023-09-12

Family

ID=85976528

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310278575.9A Active CN115982784B (en) 2023-03-21 2023-03-21 Method and device for calling function by WebAsssembly module

Country Status (1)

Country Link
CN (1) CN115982784B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116680014B (en) * 2023-08-01 2023-11-14 北京中电华大电子设计有限责任公司 Data processing method and device
CN116680015B (en) * 2023-08-03 2023-09-26 北京中电华大电子设计有限责任公司 Function calling method, function calling device, electronic equipment and readable storage medium
CN117008971B (en) * 2023-09-27 2023-12-29 北京中电华大电子设计有限责任公司 Conversion method and device of WebAsssembly instruction set

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111177712A (en) * 2019-12-31 2020-05-19 杭州趣链科技有限公司 WebAssembly model execution engine based on TEE
CN113867848A (en) * 2020-06-30 2021-12-31 腾讯科技(深圳)有限公司 Method, device and equipment for calling graphic interface and readable storage medium
CN113986402A (en) * 2021-10-28 2022-01-28 上海米哈游璃月科技有限公司 Function calling method and device, electronic equipment and storage medium
CN114661300A (en) * 2022-04-11 2022-06-24 北京万昇智能科技有限公司 Method and device for compiling PLC program code into WebAssembly code
CN115017048A (en) * 2022-06-13 2022-09-06 深圳前海微众银行股份有限公司 Method and device for determining test seeds

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10783082B2 (en) * 2019-08-30 2020-09-22 Alibaba Group Holding Limited Deploying a smart contract

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111177712A (en) * 2019-12-31 2020-05-19 杭州趣链科技有限公司 WebAssembly model execution engine based on TEE
CN113867848A (en) * 2020-06-30 2021-12-31 腾讯科技(深圳)有限公司 Method, device and equipment for calling graphic interface and readable storage medium
CN113986402A (en) * 2021-10-28 2022-01-28 上海米哈游璃月科技有限公司 Function calling method and device, electronic equipment and storage medium
CN114661300A (en) * 2022-04-11 2022-06-24 北京万昇智能科技有限公司 Method and device for compiling PLC program code into WebAssembly code
CN115017048A (en) * 2022-06-13 2022-09-06 深圳前海微众银行股份有限公司 Method and device for determining test seeds

Also Published As

Publication number Publication date
CN115982784A (en) 2023-04-18

Similar Documents

Publication Publication Date Title
CN115982784B (en) Method and device for calling function by WebAsssembly module
CN109976761B (en) Software development kit generation method and device and terminal equipment
US7506175B2 (en) File language verification
US9891900B2 (en) Generation of specialized methods based on generic methods and type parameterizations
KR100713740B1 (en) Object-oriented instruction set for resource-constrained devices
KR100713739B1 (en) Token-based linking
JP3689368B2 (en) Method of loading an application into a multi-application embedded system with data processing resources, corresponding system and execution method
CN109614165B (en) Multi-version parallel operation method and device for COM (component object model) component
CN110675256A (en) Method and device for deploying and executing intelligent contracts
CN111381938B (en) Method and system for executing intelligent contracts in block chain based on basic instruction set
KR20000052759A (en) Using a high level programming language with a microcontroller
US8818967B2 (en) Method for compressing identifiers
CN112256249A (en) Method and equipment for expanding Android system function and computer storage medium
CN116466995A (en) Instruction based on compound instruction and operand optimization method and device thereof
US8805801B2 (en) Method for compressing a .net file
CN115469894A (en) Application program installation control method, device, equipment and storage medium
Lancia et al. Java card virtual machine compromising from a bytecode verified applet
CN114942779A (en) Patch implementation method and device, embedded system and storage medium
CN116700840B (en) File execution method, device, electronic equipment and readable storage medium
CN113672238B (en) Operation method, device and equipment based on real-time operating system modularized management
WO2002091173A2 (en) Frameworks for efficient representation of string objects in java programming environments
CN111966443B (en) Smart card and working method thereof
CN113535566B (en) Android application verification method, device, equipment and storage medium
US20240192936A1 (en) Systems and methods for creating an extended smart card application file from multiple smart card application files
CN116700841B (en) Method and device for calling native API (application program interface)

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant