Code communication method and related equipment

By obtaining and converting function variable types and values ​​in the Node.js environment, combining Rust language as a transit, calling functions in the dynamic link library, the problem that Node.js cannot directly call the underlying resources is solved, and efficient underlying file calls are achieved and development costs are reduced.

CN119718475APending Publication Date: 2025-03-28TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311283046.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-09-28
Publication Date
2025-03-28

AI Technical Summary

Technical Problem

Due to the nature of its upper-level programming language, Node.js lacks the ability to directly call the underlying resources of the computer, which leads to the need to communicate with other languages ​​(such as C and C++) during development. Existing methods such as using C++ expansion or FFI technology are costly and inconvenient to use.

Method used

In the Node.js environment, by obtaining the variable types and values ​​of the function to be called, using the predefined type correspondence, converting it to the format in the Rust environment, determining the function signature, and calling functions in the dynamic link library in the Rust environment.

Benefits of technology

It realizes the ability to efficiently call the system's underlying files in the Node.js environment, without the need for developers to learn and compile underlying languages ​​such as C++, reducing the cost of using the Node.js environment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119718475A_ABST
    Figure CN119718475A_ABST
Patent Text Reader

Abstract

The invention discloses a code communication method and related equipment. The embodiment of the invention can be applied to the field of computer codes. The method comprises the following steps: acquiring a variable type and a variable value of a function to be called in a Node.js environment; converting the variable type and the variable numerical value format in the Node.js environment into a variable type and a variable numerical value format in a Rust environment based on a predefined type corresponding relation; determining a function signature of a to-be-called function according to a variable type and a variable value in the Rust environment; and calling a to-be-called function of a file in a dynamic link library based on the function signature in the Rust environment. According to the method and the device, the capability of calling the system underlying file in the Node.js environment can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a code communication method and related equipment. Background Art

[0002] Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which allows JavaScript code to run on the server side. Node.js provides a set of APIs (Application Programming Interface) that allow JavaScript code to access the underlying functions of the operating system, such as the file system, network, process, etc. Node.js is efficient, lightweight, and scalable, and is suitable for developing high-concurrency, real-time, and scalable network applications.

[0003] Since Node.js is an upper-level programming language, its ability to call the underlying resources of the system is limited. Therefore, Node.js does not have the ability to call the underlying resources of the computer. When using Node.js for development, it is necessary to communicate with the code written in other languages ​​(such as C language, C++ language). Generally, there are two communication methods. One is to use C++ to write an extension for Node.js, and then call the source code or dynamic library written in other languages ​​in the code. This method requires developers to use C++ language for additional writing, and the cost of use is relatively high; the other is to use FFI technology to directly introduce dynamic link libraries written in other languages ​​into Node. This method does not have a universal function calling method, and requires developers to rewrite the function signature for each underlying function, and the cost of use is also relatively high.

[0004] Among them, FFI (Foreign Function Interface) is a programming technology that allows codes in different languages ​​to call each other. FFI allows code in one language to call functions or libraries in another language, thereby expanding the functionality of the language. There are many ways to implement FFI, such as using the dynamic link library provided by the operating system, using the RPC (Remote Procedure Call) protocol, etc. Summary of the invention

[0005] The embodiments of the present application provide a code communication method and related equipment. The related equipment may include a code communication device, an electronic device, a computer-readable storage medium and a computer program product, which can improve the ability of the Node.js environment to call the underlying system files.

[0006] The present application provides a code communication method, including:

[0007] In the Node.js environment, get the variable type and variable value of the function to be called;

[0008] Based on a predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment;

[0009] Determine the function signature of the function to be called according to the variable type and variable value in the Rust environment;

[0010] In the Rust environment, a to-be-called function of a file in a dynamic link library is called based on the function signature.

[0011] Furthermore, after obtaining the variable type and variable value of the function to be called in the Node.js environment, the method further includes:

[0012] The general parameter information of the function call is obtained in the Rust environment, and the general parameter information is compiled into the function call information in the Node.js environment.

[0013] Furthermore, before obtaining the variable type and variable value of the function in the Node.js environment, the method further includes:

[0014] A variable type in the Rust environment is selected, and a corresponding relationship between the variable type in the Rust environment and the variable type in the Node.js environment is determined.

[0015] Further, based on the predefined type correspondence, the variable type and the variable value format in the Node.js environment are converted into the variable type and the variable value format in the Rust environment, including:

[0016] Based on a predefined type correspondence, determine the variable type in the Rust environment corresponding to the variable type in the Node.js environment;

[0017] When the type of the variable value in the Node.js environment corresponds to the type of the variable in the Rust environment, the variable value in the Rust environment and the format of the variable value in the Rust environment are determined according to the variable value in the Node.js environment.

[0018] Further, according to the variable type and variable value in the Rust environment, the function signature of the function to be called is determined, including

[0019] Convert the generic parameter type and the generic return value type in the generic parameter information into the variable type in the Rust environment;

[0020] Convert the general parameter value in the general parameter information into the variable value in the Rust environment according to the format of the variable value in the Rust environment;

[0021] According to the converted general parameter information, the function signature of the function to be called is determined.

[0022] Further, in the Rust environment, calling a to-be-called function in a file in a dynamic link library based on the function signature includes:

[0023] According to the function signature, obtain the variable type and variable value in the Rust environment;

[0024] Convert the variable type and variable value in the Rust environment to a general type variable in the C language environment;

[0025] According to the general type variables in the C language environment, the function to be called in the file in the dynamic link library is called.

[0026] Furthermore, the converting of the variable type and variable value in the Rust environment into a general type variable in the C language environment includes:

[0027] Create a general type variable pointer of the C language environment, the pointer points to the variable in the Rust environment;

[0028] The general type variable pointer is passed into the C language environment, and the variable pointed to by the general type variable pointer is obtained in the C language environment;

[0029] The variable pointed to by the general type variable pointer is converted into a corresponding variable type in the C language environment.

[0030] Accordingly, an embodiment of the present application provides a code communication device, including:

[0031] The acquisition unit is used to obtain the variable type and variable value of the function to be called in the Node.js environment;

[0032] A conversion unit, configured to convert the variable type and the variable value format in the Node.js environment into the variable type and the variable value format in the Rust environment based on a predefined type correspondence relationship;

[0033] A determination unit, used to determine a function signature of a to-be-called function according to a variable type and a variable value in the Rust environment;

[0034] A calling unit is used to call a to-be-called function in a file in a dynamic link library based on the function signature in the Rust environment.

[0035] An electronic device provided in an embodiment of the present application includes a processor and a memory, wherein the memory stores a plurality of instructions, and the processor loads the instructions to execute the steps in the code communication method provided in the embodiment of the present application.

[0036] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the steps in the code communication method provided in the embodiment of the present application are implemented.

[0037] In addition, an embodiment of the present application also provides a computer program product, including a computer program or instructions, which, when executed by a processor, implements the steps in the code communication method provided in the embodiment of the present application.

[0038] The embodiment of the present application provides a code communication method and related equipment, which can obtain the variable type and variable value of the function to be called in the Node.js environment; based on the predefined type correspondence, convert the variable type and variable value format in the Node.js environment to the variable type and variable value format in the Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; in the Rust environment, call the function to be called in the file in the dynamic link library based on the function signature. This application uses the Rust language as a transfer party to call codes developed in languages ​​such as c and c++ in the Node.js environment. The ability to call the underlying system files in the Node.js environment is realized, without the need for developers to learn and compile underlying languages ​​such as C++, reducing the cost of using the Node.js environment. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0040] Figure 1 It is a scenario diagram of the code communication method provided in an embodiment of the present application;

[0041] Figure 2 is a first flow chart of the code communication method provided in an embodiment of the present application;

[0042] Figure 3 is a second flow chart of the code communication method provided in an embodiment of the present application;

[0043] Figure 4is a schematic diagram of the structure of a code communication device provided in an embodiment of the present application;

[0044] Figure 5 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

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

[0046] The embodiment of the present application provides a code communication method and related equipment, and the related equipment may include a code communication device, an electronic device, a computer-readable storage medium, and a computer program product. The code communication device may be integrated in an electronic device, and the electronic device may be a terminal or a server.

[0047] It is understandable that the code communication method of this embodiment can be executed on a terminal, can be executed on a server, or can be executed by both a terminal and a server. The above examples should not be construed as limiting the present application.

[0048] like Figure 1 As shown, a terminal and a server jointly execute a code communication method as an example. The code communication system provided in the embodiment of the present application includes a terminal 10 and a server 11, etc. The terminal 10 and the server 11 are connected via a network, such as a wired or wireless network connection, etc., wherein the code communication device can be integrated in the server.

[0049] Among them, the server 11 can be used to: obtain the variable type and variable value of the function to be called in the Node.js environment; based on the predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; in the Rust environment, call the function to be called in the file in the dynamic link library based on the function signature. Among them, the server 11 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. The code communication method or device disclosed in this application, wherein multiple servers can be composed of a blockchain, and the server is a node on the blockchain.

[0050] The terminal 10 may be used to: receive the instruction corresponding to the function to be called, and display it on the terminal page based on the instruction. The terminal 10 may include a mobile phone, an intelligent voice interaction device, an intelligent home appliance, a vehicle terminal, an aircraft, a tablet computer, a notebook computer, or a personal computer (PC). The terminal 10 may also be provided with a client, which may be an application client or a browser client, etc.

[0051] It should be noted that the description order of the following embodiments is not intended to limit the preferred order of the embodiments.

[0052] This embodiment will be described from the perspective of a code communication device, which can be integrated into an electronic device, such as a server or a terminal. This embodiment can be applied to various scenarios such as subscription account message sorting and content recommendation. Figure 2 As shown, the specific process of the code communication method can be as follows:

[0053] S201. Obtain the variable type and variable value of the function to be called in the Node.js environment.

[0054] Among them, Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which allows JavaScript code to run on the server side. Node.js provides a set of APIs (Application Programming Interface) that allow JavaScript code to access the underlying functions of the operating system, such as the file system, network, process, etc. Node.js is efficient, lightweight, and scalable, and is suitable for developing high-concurrency, real-time, and scalable network applications.

[0055] In some embodiments, before obtaining the variable type and variable value of the function in the Node.js environment, the following steps are also included:

[0056] A variable type in the Rust environment is selected, and a corresponding relationship between the variable type in the Rust environment and the variable type in the Node.js environment is determined.

[0057] Among them, Rust is a system-level programming language. The syntax of Rust is similar to C++, but it has stronger type safety and memory safety mechanisms.

[0058] The data types of different programming languages ​​are stored in different formats on the underlying machine. So when passing data from one language to another, you need to determine the type of the data. This way, the other language can successfully convert, for example, a numeric type in the js environment to a numeric type in the rust environment.

[0059] In the Rust environment, we first select some common variable types and define the correspondence between these variable types and the variable types in the Node.js environment, so as to realize code communication between the Node.js environment and the Rust environment.

[0060] S202. Based on a predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment.

[0061] Preferably, in some embodiments, the type declaration of the parameters and the type declaration of the return value of the function to be called can be obtained in the Node.js environment, so as to determine the variable type and variable value of the function to be called.

[0062] The parameter type declaration and the return value type declaration are input by the user in the Node.js environment, or calculated by the code in the Node.js environment.

[0063] To call a dynamic link library file (.so type file) in Rust, you can use the functions provided by libloading dependency. libloading dependency provides the loading and calling functions of dynamic link libraries, allowing Rust code to call functions in .so files.

[0064] In order to call a specific function, you can first define the function's signature. For example, you can define a puts function that accepts a string parameter and returns an integer value. In this case, the function's signature is defined, that is, the type of the function's input parameter and the type of the return value. This implements a simple call to the function provided by the .so file.

[0065] The code to call the puts function in the libc library is as follows:

[0066]

[0067] In practical applications, it is impossible to write a function signature for each function in advance in order to consider general scenarios. Therefore, this application proposes a new specification definition, so that in the case of uncertain number and type of parameters, according to the corresponding rules, the required data preparation is completed and the corresponding assembly instruction code is generated to complete the function call.

[0068] First, you can define the complete parameter information of a function call in the Rust environment as the common parameter information of the function call.

[0069] In a specific embodiment, the following code can be used to define the complete parameter information of a function call in the Rust environment. The code is as follows:

[0070] / / Return value type

[0071] #[napi]

[0072] pub enum RetType{String,

[0073] I32,

[0074] / / Parameter type

[0075] #[napi]

[0076] pub enum Params Type{

[0077] String,

[0078] I32,

[0079] #[napi(object)]

[0080] struct FFIParams{

[0081] pub library:String, / / The path of the .so file to be opened

[0082] pub func_name:String, / / The name of the function to be called

[0083] pub ret_type:RetType, / / Define the type of the return value

[0084] pub params_type:Vec <paramstype>, / / Define the type of parameter

[0085] pub params_value:Vec <jsunknown>, / / Define the specific value of the parameter

[0086] }

[0087] Then compile the general parameter information of the function call into the type format in the Js environment. The specific code is as follows:

[0088] export const enum RetType{String=0,I32=1

[0089] export const enum Params Type{

[0090] String=0,

[0091] I32=1

[0092] export interface FfiParams{

[0093] library:string

[0094] funcName:string

[0095] retType:RetType

[0096] paramsType:Array <paramstype>

[0097] paramsValue:Array <unknown>

[0098] export function load(params:FfiParams):string I number

[0099] After determining how to represent variables in the rust environment in the js environment, an example of a function call is as follows:

[0100] const a=1

[0101] const b=100

[0102] equal(p.load({

[0103] library:". / libsum.so", / / path of so file funcName:'sum', / / name of the function to be called

[0104] retType:1, / / The return value type is a number

[0105] paramsType:[1,1], / / There are two parameters, the type is number and the number paramsValue:[a,b] / / The values ​​of the two parameters are 1 and 100 respectively. Use equal to represent the expectation that the calculation result of the function in the rust environment is consistent with the calculation result in the js environment.}), a+b) /

[0106] The general parameter information can be used to define the function's calling name, the function's calling path (i.e., the access path to the dynamic link library file), the parameter type, the return value type, and the parameter value.

[0107] In some specific embodiments, 0 can be used in the Nodejs environment to represent the current data, which we need to convert into the String string type in the Rust environment.

[0108] Among them, the code in the Rust environment is as follows:

[0109] #[napi]

[0110] pub enum ParamsType{String,

[0111] I32,

[0112] After compiling to the type file on the Node.js side, the code is as follows:

[0113] rust

[0114] #[napi]

[0115] pub enum ParamsType{

[0116] String I32,

[0117] export const enum ParamsType{

[0118] String=0,

[0119] I32=1

[0120] In some embodiments, based on a predefined type correspondence, converting the variable type and the variable value format in the Node.js environment into the variable type and the variable value format in the Rust environment includes the following steps:

[0121] Based on a predefined type correspondence, determine the variable type in the Rust environment corresponding to the variable type in the Node.js environment;

[0122] When the type of the variable value in the Node.js environment corresponds to the type of the variable in the Rust environment, the variable value in the Rust environment and the format of the variable value in the Rust environment are determined according to the variable value in the Node.js environment.

[0123] Preferably, a general function signature fn() can be defined to represent all function types. Use this general function signature to try to access the function method of this .so file. Then we convert the variable type in the Node.js environment to the variable type in the rust environment.

[0124] If the variable type defined in the Node.js environment is i32, which is a numeric type, it will be converted to the ffi_type_sint32 type, which is the numeric type in the rust environment.

[0125] If the variable type defined in the Node.js environment is String, it will be converted to the ffi_type_pointer type, which is the pointer type in the rust environment. Because String data is stored in the heap data structure in memory, it can be represented by a pointer type here.

[0126] After converting to the data type in the rust environment, the numerical value of the data in the Node.js environment is converted to the numerical format of the data in the rust environment.

[0127] On the rust side, we define the variable type as JsUnknow, which represents an unknown arbitrary type. We can force it to be converted to the type we expect when using it. For example, when the variable type determined by the Node.js environment is the i32 numeric type, we can force this unknown type to be converted to a numeric type. Use the coerce_to_number method to force the conversion. When the value of the variable matches the defined variable type, the forced conversion can succeed.

[0128] Similarly, when the variable type determined by the Node.js environment is a String string type, coerce_to_string can be used to force the unknown type to be converted to a string type.

[0129] Through the above two steps, we have successfully converted the variable types and variable values ​​in the js environment into the variable types and variable values ​​in the rust environment.

[0130] S203. Determine a function signature of the function to be called according to the variable type and variable value in the Rust environment.

[0131] In some embodiments, determining a function signature of a function to be called according to a variable type and a variable value in the Rust environment comprises the following steps:

[0132] Convert the generic parameter type and the generic return value type in the generic parameter information into the variable type in the Rust environment;

[0133] Convert the general parameter value in the general parameter information into the variable value in the Rust environment according to the format of the variable value in the Rust environment;

[0134] According to the converted general parameter information, the function signature of the function to be called is determined.

[0135] S204: In the Rust environment, calling a to-be-called function in a file in a dynamic link library based on the function signature.

[0136] The converted general parameter information can be used to call the underlying system method ffi_call to call the function method in a specific .so file.

[0137] In some embodiments, in the Rust environment, calling a to-be-called function of a file in a dynamic link library based on the function signature comprises the following steps:

[0138] According to the function signature, obtain the variable type and variable value in the Rust environment;

[0139] Convert the variable type and variable value in the Rust environment to a general type variable in the C language environment;

[0140] According to the general type variables in the C language environment, the function to be called in the file in the dynamic link library is called.

[0141] Among them, the universal type variable refers to the c_void type variable. We need to convert the rust variable type and variable value to the c_void type. This type means that it can represent any type of variable, similar to jsUnknow, but c_void is the type in the lowest level language, C. When the variable is used, the C language environment converts the c_void type to the type in the specific C language environment.

[0142] In some embodiments, converting the variable type and variable value in the Rust environment into a general type variable in the C language environment comprises the following steps:

[0143] Create a general type variable pointer of the C language environment, the pointer points to the variable in the Rust environment;

[0144] The general type variable pointer is passed into the C language environment, and the variable pointed to by the general type variable pointer is obtained in the C language environment;

[0145] The variable pointed to by the general type variable pointer is converted into a corresponding variable type in the C language environment.

[0146] According to the following code, a pointer of type c_void is directly created to point to a variable in the rust environment. This pointer is then passed to the c language environment. After the c language environment reads the variable corresponding to the pointer, it attempts to convert it to the corresponding type in the c language environment. The code is as follows:

[0147] Define function memory structure

[0148] let mut cif=ffi_cif{

[0149] nargs:params_type.len()as u32, / / number of parameters arg_types:arg_types.as_mut_ptr(), / / parameter type rtype:r_type, return value type};

[0150] Call, function in S0 file

[0151] ffi_call(

[0152] &mut cif, / / The memory structure defined above

[0153] Some(*func), / / The function corresponding to the general function signature obtained through lib.get above args_typesas*mut c_void, / / Parameter type arg_values.as_mut_ptr(), / / Parameter value )

[0155] As can be seen from the above, this embodiment can obtain the variable type and variable value of the function to be called in the Node.js environment; based on the predefined type correspondence, convert the variable type and variable value format in the Node.js environment to the variable type and variable value format in the Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; in the Rust environment, call the function to be called in the file in the dynamic link library based on the function signature. This application uses the Rust language as a transfer party to call codes developed in languages ​​such as c and c++ in the Node.js environment. The ability to call the underlying system files in the Node.js environment is realized, without the need for developers to learn and compile underlying languages ​​such as C++, reducing the cost of using the Node.js environment.

[0156] According to the method described in the previous embodiment, the following will be further described in detail by taking the example of the code communication device being specifically integrated into an electronic device. The embodiment of the present application provides a code communication method, such as Figure 3 As shown, the specific process of the code communication method can be as follows:

[0157] S301. The electronic device defines a function signature of a general .so file in a Rust environment.

[0158] Preferably, a general function signature fn() can be defined by the electronic device to represent all function types.

[0159] The general function signature can be used to define the function's calling name, the function's calling path (i.e., the access path to the dynamic link library file), the parameter type, the return value type, and the parameter value.

[0160] S302: The electronic device generates a corresponding code according to the universal function signature.

[0161] The electronic device uses this common function signature to try to access the function method of this .so file. Then the variable type in the Node.js environment is converted to the variable type in the rust environment.

[0162] If the variable type defined in the Node.js environment is i32, which is a numeric type, it will be converted to the ffi_type_sint32 type, which is the numeric type in the rust environment.

[0163] If the variable type defined in the Node.js environment is String, it will be converted to the ffi_type_pointer type, which is the pointer type in the rust environment. Because String data is stored in the heap data structure in memory, it can be represented by a pointer type here.

[0164] After converting to the data type in the rust environment, the numerical value of the data in the Node.js environment is converted to the numerical format of the data in the rust environment.

[0165] S303. The electronic device calls a dynamic link library .so type file in the Rust environment.

[0166] By directly creating a pointer of type c_void to point to a variable in the rust environment, this pointer is then passed to the c language environment, which reads the variable corresponding to the pointer and then tries to convert it to the corresponding type in the c language environment.

[0167] As can be seen from the above, this embodiment can obtain the variable type and variable value of the function to be called in the Node.js environment; based on the predefined type correspondence, convert the variable type and variable value format in the Node.js environment to the variable type and variable value format in the Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; in the Rust environment, call the function to be called in the file in the dynamic link library based on the function signature. This application uses the Rust language as a transfer party to call codes developed in languages ​​such as c and c++ in the Node.js environment. The ability to call the underlying system files in the Node.js environment is realized, without the need for developers to learn and compile underlying languages ​​such as C++, reducing the cost of using the Node.js environment.

[0168] In order to better implement the above method, the embodiment of the present application also provides a code communication device, such as Figure 4 As shown, the code communication device may include an acquisition unit 401, a conversion unit 402, a determination unit 403, and a calling unit 404, as follows:

[0169] The 401 acquisition unit is used to obtain the variable type and variable value of the function to be called in the Node.js environment.

[0170] Optionally, in some embodiments of the present application, the code communication device may further include a predefined unit as follows:

[0171] The predefinition unit is used to select the variable type in the Rust environment and determine the correspondence between the variable type in the Rust environment and the variable type in the Node.js environment.

[0172] Optionally, in some embodiments of the present application, the acquisition unit may further include a first acquisition subunit, a second acquisition subunit and a determination subunit, as follows:

[0173] A first acquisition subunit is used to acquire general parameter information of a function call in the Rust environment, and compile the general parameter information into function call information in the Node.js environment, wherein the function call information includes an access path of a dynamic link library file;

[0174] The second acquisition subunit is used to

[0175] Determine the subunits for

[0176] 402 conversion unit, used for converting the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment based on a predefined type correspondence.

[0177] Optionally, in some embodiments of the present application, the conversion unit may further include a corresponding subunit No. 2 conversion subunit, as follows:

[0178] A corresponding subunit, used for determining, based on a predefined type correspondence, a variable type in the Rust environment corresponding to a variable type in the Node.js environment;

[0179] A conversion subunit is used to determine the variable value in the Rust environment and the format of the variable value in the Rust environment according to the variable value in the Node.js environment when the type of the variable value in the Node.js environment corresponds to the variable type in the Rust environment.

[0180] 403 determination unit, used to determine the function signature of the to-be-called function according to the variable type and variable value in the Rust environment.

[0181] Optionally, in some embodiments of the present application, the determining unit may further include a first conversion subunit, a second conversion subunit and a determining subunit, as follows:

[0182] A first conversion subunit, configured to convert a generic parameter type and a generic return value type in the generic parameter information into a variable type in the Rust environment;

[0183] A second conversion subunit, configured to convert the general parameter value in the general parameter information into the variable value in the Rust environment according to the format of the variable value in the Rust environment;

[0184] A determination subunit is used to determine the function signature of the function to be called according to the converted general parameter information.

[0185] The calling unit 404 is used to call the to-be-called function in the file of the dynamic link library based on the function signature in the Rust environment.

[0186] Optionally, in some embodiments of the present application, the calling unit may further include a signature acquisition subunit, a variable conversion subunit and a calling subunit, as follows:

[0187] A signature acquisition subunit, used to obtain the variable type and variable value in the Rust environment according to the function signature;

[0188] A variable conversion subunit, used to convert the variable type and variable value in the Rust environment into a general type variable in the C language environment;

[0189] The calling subunit is used to call the to-be-called function of the file in the dynamic link library according to the general type variable in the C language environment.

[0190] As can be seen from the above, this embodiment can be used to obtain the variable type and variable value of the function to be called in the Node.js environment through an acquisition unit; through a conversion unit, based on a predefined type correspondence, the variable type and variable value format in the Node.js environment are converted into the variable type and variable value format in the Rust environment; through a determination unit, it is used to determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; through a calling unit, it is used to call the function to be called in the file in the dynamic link library in the Rust environment based on the function signature. This application uses the Rust language as a transfer party to call codes developed in languages ​​such as c and c++ in the Node.js environment. The ability to call the underlying system files in the Node.js environment is realized, without the need for developers to learn and compile underlying languages ​​such as C++, thereby reducing the cost of using the Node.js environment.

[0191] The present application also provides an electronic device, such as Figure 5 As shown, it shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present application, and the electronic device may be a terminal or a server, etc. Specifically:

[0192] The electronic device may include components such as a processor 501 with one or more processing cores, a memory 502 with one or more computer-readable storage media, a power supply 503, and an input unit 504. Those skilled in the art will appreciate that Figure 5 The electronic device structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0193] The processor 501 is the control center of the electronic device, and uses various interfaces and lines to connect various parts of the entire electronic device. By running or executing software programs and / or modules stored in the memory 502, and calling data stored in the memory 502, the processor 501 performs various functions of the electronic device and processes data. Optionally, the processor 501 may include one or more processing cores; preferably, the processor 501 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 501.

[0194] The memory 502 can be used to store software programs and modules. The processor 501 executes various functional applications and data processing by running the software programs and modules stored in the memory 502. The memory 502 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 502 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 502 may also include a memory controller to provide the processor 501 with access to the memory 502.

[0195] The electronic device also includes a power supply 503 for supplying power to each component. Preferably, the power supply 503 can be logically connected to the processor 501 through a power management system, so as to manage charging, discharging, power consumption and other functions through the power management system. The power supply 503 can also include one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, power status indicators and other arbitrary components.

[0196] The electronic device may further include an input unit 504, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal input related to user settings and function control.

[0197] Although not shown, the electronic device may further include a display unit, etc., which will not be described in detail herein. Specifically in this embodiment, the processor 501 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 502 according to the following instructions, and the processor 501 will run the application programs stored in the memory 502, thereby realizing various functions, as follows:

[0198] An embodiment of the present application provides a code communication method and related devices, which can obtain the variable type and variable value of a function to be called in a Node.js environment; based on a predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in a Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; and in the Rust environment, call the function to be called in a file in a dynamic link library based on the function signature.

[0199] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0200] As can be seen from the above, this embodiment can obtain the variable type and variable value of the function to be called in the Node.js environment; based on the predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; in the Rust environment, call the function to be called in the file in the dynamic link library based on the function signature. This application uses the Rust language as a transfer party to call codes developed in languages ​​such as c and c++ in the Node.js environment. The ability to call the underlying system files in the Node.js environment is realized, without the need for developers to learn and compile underlying languages ​​such as C++, reducing the cost of using the Node.js environment.

[0201] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.

[0202] To this end, an embodiment of the present application provides a computer-readable storage medium, in which a plurality of instructions are stored, and the instructions can be loaded by a processor to execute the steps in any code communication method provided in the embodiment of the present application. For example, the instructions can execute the following steps:

[0203] An embodiment of the present application provides a code communication method and related equipment, which can obtain the variable type and variable value of a function to be called in a Node.js environment; based on a predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in a Rust environment; determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; and in the Rust environment, call the function to be called in a file in a dynamic link library based on the function signature.

[0204] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0205] The computer-readable storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0206] Since the instructions stored in the computer-readable storage medium can execute the steps in any code communication method provided in the embodiments of the present application, the beneficial effects that can be achieved by any code communication method provided in the embodiments of the present application can be achieved. Please refer to the previous embodiments for details and will not be repeated here.

[0207] According to one aspect of the present application, a computer program product or a computer program is provided, the computer program product or the computer program comprising computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method provided in various optional implementations of the above-mentioned code communication aspect.

[0208] The above is a detailed introduction to a code communication method and related equipment provided in an embodiment of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for technical personnel in this field, according to the idea of ​​the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.< / unknown> < / paramstype> < / jsunknown> < / paramstype>

Claims

1. A code communication method, characterized in that: include: In the Node.js environment, get the variable type and variable value of the function to be called; Based on a predefined type correspondence, convert the variable type and variable value format in the Node.js environment into the variable type and variable value format in the Rust environment; Determine the function signature of the function to be called according to the variable type and variable value in the Rust environment; In the Rust environment, a to-be-called function of a file in a dynamic link library is called based on the function signature.

2. The code communication method according to claim 1, characterized in that: In the Node.js environment, after obtaining the variable type and variable value of the function to be called, the method further includes: The general parameter information of the function call is obtained in the Rust environment, and the general parameter information is compiled into the function call information in the Node.js environment.

3. The code communication method according to claim 2, characterized in that: Before obtaining the variable type and variable value of a function in the Node.js environment, the following steps are also included: A variable type in the Rust environment is selected, and a corresponding relationship between the variable type in the Rust environment and the variable type in the Node.js environment is determined.

4. The code communication method according to claim 3, characterized in that: Based on the predefined type correspondence, the variable type and the variable value format in the Node.js environment are converted into the variable type and the variable value format in the Rust environment, including: Based on a predefined type correspondence, determine the variable type in the Rust environment corresponding to the variable type in the Node.js environment; When the type of the variable value in the Node.js environment corresponds to the type of the variable in the Rust environment, the variable value in the Rust environment and the format of the variable value in the Rust environment are determined according to the variable value in the Node.js environment.

5. The code communication method according to claim 4, characterized in that: According to the variable types and variable values ​​in the Rust environment, determine the function signature of the function to be called, including Convert the generic parameter type and the generic return value type in the generic parameter information into the variable type in the Rust environment; Convert the general parameter value in the general parameter information into the variable value in the Rust environment according to the format of the variable value in the Rust environment; According to the converted general parameter information, the function signature of the function to be called is determined.

6. The code communication method according to claim 5, characterized in that: In the Rust environment, calling a function to be called in a file in a dynamic link library based on the function signature includes: According to the function signature, obtain the variable type and variable value in the Rust environment; Convert the variable type and variable value in the Rust environment to a general type variable in the C language environment; According to the general type variables in the C language environment, the function to be called in the file in the dynamic link library is called.

7. The code communication method according to claim 6, characterized in that: The converting the variable type and variable value in the Rust environment into a general type variable in the C language environment includes: Create a general type variable pointer of the C language environment, the pointer points to the variable in the Rust environment; The general type variable pointer is passed into the C language environment, and the variable pointed to by the general type variable pointer is obtained in the C language environment; The variable pointed to by the general type variable pointer is converted into a corresponding variable type in the C language environment.

8. A code communication device, characterized in that: include: The acquisition unit is used to obtain the variable type and variable value of the function to be called in the Node.js environment; A conversion unit, configured to convert the variable type and the variable value format in the Node.js environment into the variable type and the variable value format in the Rust environment based on a predefined type correspondence relationship; A determination unit, used to determine a function signature of a to-be-called function according to a variable type and a variable value in the Rust environment; A calling unit is used to call a to-be-called function in a file in a dynamic link library based on the function signature in the Rust environment.

9. An electronic device, characterized in that: It comprises a memory and a processor; the memory stores an application program, and the processor is used to run the application program in the memory to execute the operations in the code communication method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor to execute the steps in the code communication method according to any one of claims 1 to 7.

11. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed by a processor, the steps of the code communication method according to any one of claims 1 to 7 are implemented.