Disclosure of Invention
The application provides a binding method of a Java function and a C function, which comprises the following steps:
transferring a binding interface predefined for the target Java class to the target C file based on the reference; the binding interface comprises description information of a target Java function in the target Java class;
calling the binding interface in a target C file to acquire the description information of the target Java function;
and establishing a binding relation between the target Java function and the C function in the target C file based on the description information.
Optionally, before the establishing the binding relationship between the target Java function and the C function in the target C file based on the description information, the method further includes:
acquiring a Javascript execution code from a target html page;
performing syntax analysis on the Javascript execution code based on a preset syntax analyzer;
and generating a C function corresponding to the Javascript execution code in the C file.
Optionally, the method includes:
and after the binding relationship is established, calling the target Java function in the Javascript execution code so as to convert the target html page into a native page to be displayed locally.
Optionally, the transferring, to the target C file, the binding interface predefined for the target Java class based on the reference includes:
and calling a preset first binding function in the target Java class, and transmitting the reference address of the binding interface as a transmission parameter to a second binding function in the target C file.
Optionally, the binding interface includes a predefined interface function, and the description information of the target Java function is added to the interface function based on a Json format;
the calling the binding interface in the target C file to acquire the description information of the target Java function comprises:
calling the binding interface based on the reference address in the second binding function;
and analyzing the interface function of the binding interface to acquire the description information of the target Java function pre-added in the interface function.
Optionally, the syntax parser is a Duktape parsing engine.
The present application further provides a device for binding a Java function and a C function, the device including:
the transfer module is used for transferring a binding interface predefined for the target Java class to the target C file based on the reference; the binding interface comprises description information of a target Java function in the target Java class;
the first acquisition module is used for calling the binding interface in a target C file to acquire the description information of the target Java function;
and the binding module is used for establishing a binding relationship between the target Java function and the C function in the target C file based on the description information.
Optionally, the apparatus further comprises:
the second acquisition module is used for acquiring the Javascript execution code from the target html page;
the parsing module is used for parsing the Javascript execution code based on a preset syntax parser;
and the generating module is used for generating a C function corresponding to the Javascript execution code in the C file.
Optionally, the apparatus further comprises:
and the calling module is used for calling the target Java function in the Javascript execution code after the binding relationship is established so as to convert the target html page into a native page to be displayed locally.
Optionally, the transmission module is specifically configured to:
and calling a preset first binding function in the target Java class, and transmitting the reference address of the binding interface as a transmission parameter to a second binding function in the target C file.
Optionally, the binding interface includes a predefined interface function, and the description information of the target Java function is added to the interface function based on a Json format;
the first obtaining module is specifically configured to:
calling the binding interface based on the reference address in the second binding function;
and analyzing the interface function of the binding interface to acquire the description information of the target Java function pre-added in the interface function.
Optionally, the syntax parser is a Duktape parsing engine.
In the application, by defining a binding interface for a target Java class, adding description information of a target Java function to be bound in the binding interface, and transferring the binding interface to a target C file based on reference, then the binding interface is analyzed in the object C file to obtain the description information of the object Java function, and the binding relation between the target Java function and the C function in the target C file is established based on the acquired description information, so that when the binding relation between the target Java function and the C function is established, only a binding interface is defined for the target Java function and is transferred to the target C file based on the reference, the binding interface can be automatically analyzed in the target C file to realize the binding of the target Java function and the C function, and any modification is not required to be performed on the code of the C function, so that the complexity of establishing the binding relationship between the Java function and the C function can be reduced.
Detailed Description
In the development process of APP (Application), there is usually a need to call Java functions in Javascript scripts.
For example, when a developer wants to convert an html (hypertext markup language) page created based on a Javascript script into a native page supported by the APP (native page supported by the APP and using Java as an underlying programming language), the APP is required to be able to support calling a Java function in the Javascript script.
In order to meet the requirement, a Javascript script can be transcoded by a Javascript analysis engine, a C function corresponding to the Javascript script is generated in a C file of an APP, and then a binding relationship between a Java function to be called in a Java class and the C function is established. After the binding relationship is established, the Java function can be directly called in the Javascript, so that the html page can be converted into a native page to be displayed in the APP.
However, at present, when establishing a binding relationship between a Java function and a C function, a developer is usually required to respectively specify the Java function and the C function to be bound in a Java class and the C function of an APP, and then register a corresponding relationship with the Java function in a code of the C function to implement the binding by modifying a code of the C function. In the scheme, the Java functions to be bound in the Java class respectively correspond to the C functions in the C files, so that when the number of the Java functions and the C functions to be bound is large, developers need to modify codes of the C functions one by one, the code redundancy is large, and the operation is complex.
In view of the above, the present application provides a method for binding a Java function and a C function, which defines a binding interface for a target Java class, adds description information of a target Java function to be bound in the binding interface, transfers the binding interface to a target C file based on a reference, parses the binding interface in the target C file to obtain the description information of the target Java function, and establishes a binding relationship between the target Java function and the C function in the target C file based on the obtained description information, so that when establishing the binding relationship between the target Java function and the C function, the binding interface can be automatically parsed in the target C file to achieve the binding between the target Java function and the C function without performing any modification on a code of the C function, therefore, the complexity of establishing the binding relationship between the Java function and the C function can be reduced.
The present application is described below with reference to specific embodiments and specific application scenarios.
Referring to fig. 1, fig. 1 is a diagram illustrating a method for binding a Java function and a C function, applied to a client, according to an embodiment of the present application, where the method performs the following steps:
step 101, transmitting a binding interface predefined for a target Java class to a target C file based on reference; the binding interface comprises description information of a target Java function in the target Java class;
102, calling the binding interface in a target C file to acquire the description information of the target Java function;
step 103, establishing a binding relationship between the target Java function and the C function in the target C file based on the description information.
The client may include client software providing a specific function for a user; for example, the client software may be an APP installed on the user's smart terminal.
The Java class refers to a class file (source file with a suffix name of Java) in the source code of the APP, which takes the Java language as a programming language; the C file refers to a class file (source file with suffix name. C) in which C is a programming language in the source code of APP. The target Java function refers to a Java function which is specified in a target Java class by a developer based on actual service requirements and needs to establish a binding relationship with the C function in the target C file.
The Java function and the C function which need to establish the binding relationship can be functions which are similar in function or are completely equivalent in function, so that the same function can be realized in different programming language environments by mutual calling after the binding relationship between the Java function and the C function is established.
In this example, if it is required to call a Java function in a Javascript script, a html page created based on the Javascript script is converted into a native page supported by an APP and displayed in the APP, transcoding may be performed on the Javascript script in the html page by a Javascript parsing engine, and a target C function corresponding to the Javascript script is generated in a target C file, so that after a binding relationship between the target Java function and the target C function is established through the technical solutions shown in steps 101 to 103, transcoding is performed on the Javascript script in the html page by mutual call between the target Java function and the target C function, and the html page is converted into the native page supported by the APP and displayed in the APP.
Referring to fig. 2, fig. 2 is a flowchart illustrating a process of transcoding a Javascript script in an html page by a Javascript parsing engine to generate a corresponding C function in a target C file, in this example, and includes the following steps:
step 201, obtaining Javascript execution codes from a target html page;
in this example, the target html page may be an html page deployed at a server, and the APP may "pull down" the html page deployed at the server to the local through interaction with the server; for example, the html page may be actively requested from the server, or may be actively pushed by the server. The server can be a server facing the APP providing server, a server cluster or a cloud platform constructed based on the server cluster; for example, when the payment system is implemented, the APP may be a pay bank APP, the server may be a pay bank platform, and the html page may be a payment page to which the pay bank APP needs to jump after the user performs a specific payment operation in the page of the pay bank APP.
In the html page, Javascript execution codes can be embedded in advance. After the html page is pulled down to the local by the APP, due to incompatibility of programming languages, the APP generally cannot directly execute Javascript execution codes in the html page locally, and the html page is displayed locally. In this case, the APP may obtain the Javascript execution code embedded in the html page and save the Javascript execution code locally, and then perform a subsequent transcoding operation on the Javascript execution code to convert the html page into a native page supported by the APP.
Step 202, carrying out syntax analysis on the Javascript execution code based on a preset syntax analyzer;
in this example, the parser may be a Javascript parsing engine preset by APP.
Wherein, the Javascript parsing engine may be a Duktape parsing engine. The Duktape parsing engine is a mainstream Javascript embedded open source engine, and can be used as a grammar parser to execute grammar parsing on Javascript, and Javascript execution codes are embedded into a C file.
The APP can perform syntax analysis on the Javascript execution code acquired from the target html page through a Duktape analysis engine to analyze the syntax of the Javascript execution code.
Step 203, generating a C function corresponding to the Javascript execution code in the C file.
After the APP completes syntax analysis on the Javascript execution code through the Duktape analysis engine, a C function corresponding to the Javascript execution code can be generated in a memory of a target C file according to the analyzed syntax of the Javascript execution code, so that the aim of embedding the Javascript execution code in a target html page into the target C file can be fulfilled.
After the APP generates a C function corresponding to the Javascript execution code in the target html page in the target C file by using the Duktape parsing engine, the APP may start to establish a binding relationship between the target Java function and the C function in the target C file based on the technical solutions shown in steps 101 to 103.
In this example, before the APP starts to establish the binding relationship between the target Java function and the C function in the target C file, a developer of the APP may predefine a binding interface for a specified target Java class.
The binding interface is a Java interface defined for a target Java class by a developer. In the above structure of the binding interface, a method declared for the interface by a developer and description information of the method are generally included.
In this example, when a developer declares a method for the binding interface, the developer may define an interface function in the method in advance, where the interface function may be a Java function defined by the developer, and the Java function is used to carry description information of one or more target Java functions that need to be bound in a target Java class. After the developer defines the interface function in the method for binding the interface, the developer may add the description information of one or more Java functions to be bound in the target Java class to the interface function.
In an embodiment shown, when the developer adds the description information of the target Java function to be bound in the interface function, the description information may be added to the interface function in a Json format. The Json format is a popular data storage format, can store data in a large field form, and has the characteristic that the stored data is convenient to analyze. Therefore, the description information of the target Java function is stored in the interface function in a Json format, and the APP can analyze the stored description information later.
For example, a developer may define a binding interface for a target Java class by editing the following code:
Public interface Scriptable;
public String getScriptConfigJson();
{“count”:2,“array”:[{“JavaMethod”:“method1”},{“JavaMethod”:“method2”}],“name”:“JavaWindow”}
in the codes, JavaWindow is a target Java class, and Scriptable is a binding interface defined by a developer for the target Java class JavaWindow; the second line of code is a method included in the binding interface Scriptable, and in the method, getScriptConfigJson is an interface function defined by a developer. The third fourth line code is description information of the target Java function to be bound in the Json format added by the developer for the interface function getscript configjson, and the method1 and method2 in the description information are the target Java functions to be bound. In this example, after a developer defines a binding interface for a target Java class, the APP may transfer the binding interface to a target C file based on a reference by using a parameter transfer mechanism in a Java architecture, so that the binding interface may be called in the target C file to obtain the above-mentioned description information in the binding interface.
Based on a parameter transfer mechanism of a Java system, when the APP transfers the binding interface to the target C file based on the reference, the APP can transfer the reference address of the binding interface in the memory to the target C file as a transfer parameter.
In an illustrated embodiment, when the APP is started, a preset first binding function (Java function) may be called in the target Java class, and a reference address of the binding interface is used as a transfer parameter to be transferred to a second binding function (C function) in the target C file. The first binding function is a native function in the target Java class and is used for calling a second binding function in the target C file.
After the APP calls a preset first binding function in the target Java class and transmits the reference address of the binding interface as a transmission parameter to a second binding function in the target C file, the APP can call the binding interface in the target C file to obtain the description information of the target Java function to be bound.
The APP calls the binding interface in the target C file to acquire the description information of the target Java function, and the description information can be acquired through the second binding function.
In this case, since the reference address of the binding interface is already transferred to the local of the second binding function by the first binding function, the APP may call the second binding function in the target C file, call the binding interface in the second binding function based on the reference address of the binding interface, and execute the method stated in the binding interface in the second binding function, and obtain the description information of the target Java function to be bound in the Json format pre-added by the developer for the interface function by automatically parsing the interface function in the method.
For example, in implementation, the first binding function may be a bindJavaToC function in the target Java class, and the second binding function is a bind function in the target C file (determined by an existing binding mechanism of the Java system); the bindJavaToC function is a native function in the target Java class and is used for calling a bind function in the target C file.
When the APP transfers the binding interface to the target C file based on the reference, a bindJavaToC function can be called, and the reference address of the binding interface is taken as a transfer parameter and transferred to the bind function in the target C file.
Assuming that the target Java class is JavaWindow, the binding interface is Scriptable, and the interface function is getScriptConfigJson, after parameter transfer is completed, the APP may call a bind function in a target C file, execute a method declared in the binding interface, and analyze an interface function getScriptConfigJson in the method to obtain description information of a target Java function to be bound, which is pre-added by a developer for the interface function getScriptConfigJson.
In this example, after the APP obtains the description information of the target Java function that is pre-added by the developer for the interface function by analyzing the interface function, the APP may record, in the memory of the target C file, the corresponding relationship between the target Java function specified by the developer in the description information and the C function in the target C file based on the obtained description information, so as to complete the establishment of the binding relationship between the target Java function and the C function in the target C file.
For example, assume that the description information of the target Java function is:
{“count”:2,“array”:[{“JavaMethod”:“method1”},{“JavaMethod”:“method2”}],“name”:“JavaWindow”}
based on the above description information, method1 and method2 are target Java functions to be bound, and the APP may record the corresponding relationship between target Java functions method1 and method2 and the C function in the target C file in the memory of the target C file, and complete the establishment of the binding relationship between the C function in the target C file and the target Java functions method1 and method 2.
Therefore, by the method, when the binding relationship between the target Java function and the C function is established, developers only need to define the binding interface for the target Java function and add the description information of the target Java function in the binding interface, and the binding interface can be automatically analyzed in the target C file to realize the binding between the target Java function and the C function. In the whole binding process, developers do not need to perform any modification on codes of the C function, so that the redundancy of the codes can be reduced, and the complexity of establishing the binding relationship between the Java function and the C function is reduced.
In addition, it should be noted that:
in practical application, on one hand, when a target Java function which needs to establish a binding relationship with the C function is newly added to the target Java class, a developer can also update the target Java function in the description information, and after the APP is started, the above process can be repeatedly executed, and the binding interface is automatically analyzed in the target C file to realize the binding of the target Java function and the C function.
On the other hand, when a Java class is newly added in the APP and a Java function needing to establish a binding relationship with the C function exists in the Java class, a developer only needs to define a binding interface for the Java class and then update the version of the APP, so that the APP can also repeatedly execute the above processes after being started, and automatically analyze the binding interface in the target C file to realize the binding of the target Java function and the C function.
In this example, after the binding relationship between the target Java function and the C function in the target C file is established, the target Java function in the target Java class in the APP can be normally called with the C function in the target C file. Moreover, at this time, the C function of the target C file includes a C function corresponding to the Javascript script in the html page, which is generated in the target C file after the APP performs syntax analysis on the Javascript script in the html page through the Javascript analysis engine, so that the APP can call the target Java function in the Javascript script in the target html page through mutual call between the target Java function and the C function in the target C file.
In this case, the APP may call a target Java function in the Javascript script in the target html page to complete transcoding of the Javascript script, and convert the html page into a native page supported by the APP to be displayed in the APP.
In the above embodiment, by defining a binding interface for a target Java class, adding description information of a target Java function to be bound in the binding interface, transferring the binding interface to a target C file based on a reference by using an existing parameter transfer mechanism of a Java system, then automatically parsing the binding interface in the target C file to obtain the description information of the target Java function, and establishing a binding relationship between the target Java function and a C function in the target C file based on the obtained description information, it is realized that when establishing the binding relationship between the target Java function and the C function, the binding interface can be automatically parsed in the target C file to achieve the binding between the target Java function and the C function without performing any modification on a code of the C function by only defining the binding interface for the target Java function and transferring the binding interface to the target C file based on the reference, therefore, the complexity of establishing the binding relationship between the Java function and the C function can be reduced.
Corresponding to the method embodiment, the application also provides an embodiment of the device.
Referring to fig. 3, the present application provides a binding apparatus 30 for Java functions and C functions, which is applied to a client; referring to fig. 4, a hardware architecture related to a client of the binding apparatus 30 for bearing the Java function and the C function generally includes a CPU, a memory, a non-volatile memory, a network interface, an internal bus, and the like; taking a software implementation as an example, the binding apparatus 30 for Java functions and C functions can be generally understood as a computer program loaded in a memory, and a logic apparatus formed by combining software and hardware after a CPU runs, where the apparatus 30 includes:
a transfer module 301, configured to transfer a binding interface predefined for a target Java class to a target C file based on a reference; the binding interface comprises description information of a target Java function in the target Java class;
a first obtaining module 302, configured to call the binding interface in a target C file to obtain description information of the target Java function;
a binding module 303, configured to establish a binding relationship between the target Java function and the C function in the target C file based on the description information.
In this example, the device 30 further comprises:
a second obtaining module 304, configured to obtain a Javascript execution code from the target html page;
the parsing module 305 is configured to perform syntax parsing on the Javascript execution code based on a preset syntax parser;
a generating module 306, configured to generate a C function corresponding to the Javascript execution code in the C file.
In this example, the device 30 further comprises:
and the calling module 307 is configured to call the target Java function in the Javascript execution code after the binding relationship is established, so as to convert the target html page into a native page and display the native page locally.
In this example, the transmission module 301 is specifically configured to:
and calling a preset first binding function in the target Java class, and transmitting the reference address of the binding interface as a transmission parameter to a second binding function in the target C file.
In this example, the binding interface comprises a predefined interface function, and the description information of the target Java function is added to the interface function based on Json format;
the first obtaining module 302 is specifically configured to:
calling the binding interface based on the reference address in the second binding function;
and analyzing the interface function of the binding interface to acquire the description information of the target Java function pre-added in the interface function.
In this example, the parser is a Duktape parsing engine.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.
It will be understood that the present application is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the application is limited only by the appended claims.
The above description is only exemplary of the present application and should not be taken as limiting the present application, as any modification, equivalent replacement, or improvement made within the spirit and principle of the present application should be included in the scope of protection of the present application.