A code conversion method, device, computer equipment and storage medium
By converting the C++ backend code in the code developed by Nodejs into JavaScript frontend code and running it in the browser, the problem of inaccurate code conversion in the existing technology is solved, and the code operation with high accuracy in the browser is achieved.
Patent Information
- Application Number
- CN202111250749.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-26
- Publication Date
- 2025-04-22
- Estimated Expiration
- 2041-10-26
AI Technical Summary
When the existing technology converts the code module or application developed by Nodejs into code runnable by the browser, it is impossible to accurately obtain the values of dynamic variables in the JavaScript code, resulting in the converted code running results that are inconsistent with the original code, reducing the accuracy of the code running.
By obtaining the server's runtime code, including back-end code based on C++ syntax specifications and front-end code based on JavaScript syntax specifications, the preset code conversion tool is used to convert the back-end code into target subcode that matches the JavaScript syntax specifications, and based on the target subcode and front-end code, the conversion code is determined so that it can run in the browser.
It realizes the conversion of Nodejs-developed code into code that supports running in the browser, while retaining the function of front-end code, avoiding static analysis steps, and improving the accuracy of the conversion code.
Smart Images

Figure CN113971027B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of code conversion, and in particular to a code conversion method, apparatus, computer device and storage medium. Background Art
[0002] Nodejs is a cross-platform JavaScript runtime, including front-end JavaScript code and back-end C++ code. Code modules or applications developed using Nodejs can run in various operating systems corresponding to the server. However, since C++ code cannot be perfectly compatible in the browser, code modules or applications developed using Nodejs cannot run accurately in the browser.
[0003] In the prior art, in order to solve the above problem, a build tool (such as an abstract syntax tree) is usually used to convert the JavaScript code and C++ code corresponding to the code module into a code that can be run by the browser. However, since JavaScript code is a dynamic language, directly using a build tool to analyze it is a static analysis, which will result in the inability to obtain the accurate values of some dynamic variables in the JavaScript code, and further cause the running results of the converted code to be inconsistent with the running results corresponding to the original code module, thereby reducing the accuracy of code running. Summary of the invention
[0004] The embodiments of the present disclosure at least provide a code conversion method, apparatus, computer equipment and storage medium.
[0005] In a first aspect, an embodiment of the present disclosure provides a code conversion method, including:
[0006] Obtaining runtime code corresponding to a target operating environment in a server; the runtime code includes a backend subcode formed based on a first syntax specification and a frontend subcode formed based on a second syntax specification; the target operating environment is used to implement a call from the frontend subcode to the backend subcode;
[0007] Based on the second grammar specification, converting the back-end subcode into a target subcode matching the second grammar specification;
[0008] Based on the target sub-code and the front-end sub-code, a conversion code corresponding to the runtime code is determined; wherein the conversion code is used to run in a browser.
[0009] In a possible implementation, after determining the conversion code, the method further includes:
[0010] Obtaining the target file corresponding to the target subcode from the server;
[0011] The target file is stored in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the conversion code.
[0012] In a possible implementation manner, the converting the back-end subcode into a target subcode matching the second grammar specification based on the second grammar specification includes:
[0013] The back-end subcode is converted into a target subcode matching the second grammar specification by using a preset code conversion tool based on a conversion relationship between the second grammar specification and the first grammar specification.
[0014] In a possible implementation, determining the conversion code corresponding to the runtime code based on the target sub-code and the front-end sub-code includes:
[0015] A code execution environment corresponding to the runtime code is created, and the conversion code is determined based on the code execution environment, the target sub-code, and the front-end sub-code.
[0016] In a possible implementation, the creating a code execution environment corresponding to the runtime code includes:
[0017] Determine at least one application program interface API corresponding to the backend subcode, and for each of the APIs, create an independent operating environment corresponding to the API;
[0018] Based on the independent operating environment corresponding to each API, a code operating environment corresponding to the runtime code is created.
[0019] In a possible implementation manner, before converting the back-end subcode into a target subcode matching the second grammar specification based on the second grammar specification, the method further includes:
[0020] Determining a target variable corresponding to the runtime code;
[0021] The determining, based on the target sub-code and the front-end sub-code, a conversion code corresponding to the runtime code comprises:
[0022] Create a Require requirement module corresponding to the target variable;
[0023] Based on the target sub-code, the front-end sub-code and the Require requirement module, a conversion code corresponding to the runtime code is determined.
[0024] In a possible implementation, the first grammar specification includes a C++ grammar specification, and the second grammar specification includes a JavaScript grammar specification.
[0025] In a second aspect, the present disclosure also provides a code conversion device, including:
[0026] An acquisition module, used for acquiring runtime code corresponding to a target operating environment in a server; the runtime code includes a backend subcode formed based on a first syntax specification and a frontend subcode formed based on a second syntax specification; the target operating environment is used for implementing a call of a frontend subcode to a backend subcode;
[0027] a conversion module, configured to convert the rear-end subcode into a target subcode matching the second grammatical specification based on the second grammatical specification;
[0028] The first determination module is used to determine the conversion code corresponding to the runtime code based on the target sub-code and the front-end sub-code; wherein the conversion code is used to run in a browser.
[0029] In a possible implementation, the device further includes:
[0030] A storage module, used for obtaining a target file corresponding to the target sub-code from a server after determining the conversion code;
[0031] The target file is stored in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the conversion code.
[0032] In a possible implementation, the conversion module is configured to use a preset code conversion tool to convert the back-end subcode into a target subcode matching the second grammar specification based on a conversion relationship between the second grammar specification and the first grammar specification.
[0033] In a possible implementation, the first determination module is used to create a code execution environment corresponding to the runtime code, and determine the conversion code based on the code execution environment, the target sub-code and the front-end sub-code.
[0034] In a possible implementation manner, the first determination module is used to determine at least one application program interface API corresponding to the backend subcode, and for each of the APIs, create an independent operating environment corresponding to the API;
[0035] Based on the independent operating environment corresponding to each API, a code operating environment corresponding to the runtime code is created.
[0036] In a possible implementation manner, the device further includes a second determining module:
[0037] The second determination module is used to determine the target variable corresponding to the runtime code before converting the backend subcode into a target subcode matching the second grammar specification based on the second grammar specification;
[0038] The first determination module is used to create a Require requirement module corresponding to the target variable;
[0039] Based on the target sub-code, the front-end sub-code and the Require requirement module, a conversion code corresponding to the runtime code is determined.
[0040] In a third aspect, an optional implementation of the present disclosure further provides a computer device, a processor, and a memory, wherein the memory stores machine-readable instructions executable by the processor, and the processor is used to execute the machine-readable instructions stored in the memory, and when the machine-readable instructions are executed by the processor, the machine-readable instructions perform the steps of the above-mentioned first aspect, or any possible implementation of the first aspect.
[0041] In a fourth aspect, an optional implementation of the present disclosure further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed, the steps of the above-mentioned first aspect, or any possible implementation of the first aspect are executed.
[0042] For a description of the effects of the above-mentioned code conversion apparatus, computer device, and computer-readable storage medium, please refer to the description of the above-mentioned code conversion method, which will not be repeated here.
[0043] The code conversion method, apparatus, computer device and storage medium provided by the embodiments of the present disclosure can enable the converted target subcode to run in a browser by converting the backend subcode formed based on the first grammatical specification in the runtime code into the target subcode matching the second grammatical specification; and then obtain the conversion code based on the target subcode and the front-end subcode, which not only ensures that the runtime code is converted into the conversion code that supports running in the browser, but also can realize the retention of the front-end subcode, avoiding the static analysis step; it is beneficial to accurately obtain each dynamic variable corresponding to the front-end subcode during the code conversion process, thereby improving the accuracy of the converted code obtained by the conversion.
[0044] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following is a brief introduction to the drawings required for use in the embodiments. The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and are used together with the specification to illustrate the technical solutions of the present disclosure. It should be understood that the following drawings only illustrate certain embodiments of the present disclosure and should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can also be obtained based on these drawings without creative work.
[0046] Figure 1 A flow chart of a code conversion method provided by an embodiment of the present disclosure is shown;
[0047] Figure 2 A specific implementation flow chart of a code conversion provided by an embodiment of the present disclosure is shown;
[0048] Figure 3 A schematic diagram of a code conversion device provided by an embodiment of the present disclosure is shown;
[0049] Figure 4 A schematic diagram of the structure of a computer device provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical scheme and advantages of the embodiments of the present disclosure clearer, the technical scheme in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all of the embodiments. The components of the embodiments of the present disclosure generally described and shown here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure is not intended to limit the scope of the present disclosure claimed for protection, but merely represents the selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present disclosure.
[0051] In addition, the terms "first", "second", etc. in the description and claims of the embodiments of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein.
[0052] The "multiple or several" mentioned in this article refers to two or more. "And / or" describes the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the associated objects are in an "or" relationship.
[0053] Research has found that Nodejs, as a cross-platform javascript runtime, includes front-end JavaScript code and back-end C++ code. Code modules or applications developed using Nodejs can run in various operating systems corresponding to the server. However, since C++ code cannot be perfectly compatible in the browser, code modules or applications developed using Nodejs cannot run accurately in the browser.
[0054] In the prior art, in order to solve the above problem, a build tool (such as an abstract syntax tree) is usually used to convert the JavaScript code and C++ code corresponding to the code module into a code that can be run by the browser. However, since JavaScript code is a dynamic language, directly using a build tool to analyze it is a static analysis, which will result in the inability to obtain the accurate values of some dynamic variables in the JavaScript code, and further cause the running results of the converted code to be inconsistent with the running results corresponding to the original code module, thereby reducing the accuracy of code running.
[0055] Based on the above research, the present disclosure provides a code conversion method, apparatus, computer device and storage medium, which can enable the converted target subcode to run in a browser by converting the backend subcode formed based on the first syntax specification in the runtime code into the target subcode matching the second syntax specification; and then obtain the conversion code based on the target subcode and the front-end subcode, which not only ensures that the runtime code is converted into the conversion code that supports running in the browser, but also can realize the retention of the front-end subcode, avoiding the static analysis step; it is beneficial to accurately obtain each dynamic variable corresponding to the front-end subcode during the code conversion process, thereby improving the accuracy of the converted code obtained by the conversion.
[0056] The defects existing in the above solutions are the results obtained by the inventor after practice and careful research. Therefore, the discovery process of the above problems and the solutions proposed by the present disclosure for the above problems below should be the contributions made by the inventor to the present disclosure during the disclosure process.
[0057] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0058] It should be noted that the specific terms mentioned in the embodiments of the present disclosure include:
[0059] Node.js: It is a JavaScript runtime environment based on the Chrome V8 engine. Node.js and applications or modules built with Node.js can run in various operating systems, but cannot run completely on the browser side.
[0060] iframe: It is a tag of Hypertext Markup Language, which acts as a document within a document, or a floating frame. The iframe element can create an inline frame (i.e., an inline frame) that contains another document;
[0061] webworker: As a new feature of the browser, it has the function of creating a multi-threaded environment for JavaScript and can run in the browser;
[0062] Serviceworker: acts as a proxy server between the web application and the browser;
[0063] sharedworker: is a special webworker that can support communication functions for multiple browser contexts, such as multiple windows and iframes.
[0064] To facilitate understanding of the present embodiment, a code conversion method disclosed in the embodiment of the present disclosure is first introduced in detail. The executor of the code conversion method provided in the embodiment of the present disclosure is generally a computer device with certain computing capabilities. In some possible implementation methods, the code conversion method can be implemented by a processor calling computer-readable instructions stored in a memory.
[0065] The code conversion method provided by the embodiment of the present disclosure is described in detail below.
[0066] like Figure 1 As shown, a flowchart of a code conversion method provided by an embodiment of the present disclosure may include the following steps:
[0067] S101: Obtain runtime code corresponding to a target operating environment in a server; the runtime code includes a backend subcode formed based on a first syntax specification and a frontend subcode formed based on a second syntax specification; the target operating environment is used to implement a call from a frontend subcode to a backend subcode.
[0068] Here, the target operating environment may be an operating environment corresponding to Node.js, which is used to implement the call of the front-end subcode to the back-end subcode, that is, when the front-end subcode is executed in the target operating environment corresponding to the server, the front-end subcode may call the back-end subcode to obtain information. The server is the end that can run the runtime code corresponding to the target operating environment.
[0069] The runtime code may be the Node.js code developed by the developer. For example, the runtime code may be the code corresponding to the Node.js application developed by the developer, the code corresponding to the Node.js module, etc. Specifically, the runtime code may include back-end subcode formed based on the first syntax specification and front-end subcode formed based on the second syntax specification.
[0070] When the backend subcode is running on the backend, the backend can communicate with the operating system corresponding to the server. When the frontend subcode is running on the frontend, the frontend cannot directly communicate with the operating system corresponding to the server. Therefore, in the process of running the frontend subcode, if you need to obtain local files from the operating system, the frontend corresponding to the frontend subcode needs to communicate with the backend corresponding to the backend subcode, call the backend subcode to run on the backend, use the backend to obtain local files from the operating system and feed back to the frontend, so that the local files required to run the frontend subcode can be obtained to ensure the correct operation of the frontend subcode. In specific applications, the communication between the frontend and the backend can be carried out using internalBindings. Among them, internalBindings is a bridge used to connect the C++ end and the Javascript end in the Node standard library.
[0071] In one embodiment, the first syntax specification may include a C++ syntax specification, and the code formed based on the C++ syntax specification has the function of communicating with the operating system, and the second syntax specification includes a JavaScript syntax specification. Here, since the code formed based on the C++ syntax specification has the function of communicating with the operating system corresponding to the server, and the backend subcode in the Node.js code needs to communicate with the operating system, the backend subcode in the Node.js code can be a code formed based on the C++ syntax specification.
[0072] The browser does not support running the code formed based on the first syntax specification, so the runtime code corresponding to the target runtime environment in the server cannot be run in the browser.
[0073] In specific implementation, when there is a need to run the runtime code that can be run on the server in the browser, the runtime code can be obtained first, and then converted using subsequent steps to enable the converted runtime code to run in the browser.
[0074] S102: Based on the second grammar specification, convert the back-end subcode into a target subcode matching the second grammar specification.
[0075] Here, the target sub-code is a code obtained after converting the backend sub-code, has the same function as the backend sub-code, and supports running in a browser.
[0076] In specific implementation, after obtaining the runtime code, the back-end subcode in the runtime code can be determined, and then the conversion relationship between the second grammar specification and the first grammar specification can be determined. Based on the conversion relationship, the back-end subcode is converted into a target subcode that matches the second grammar specification, that is, the back-end subcode is converted into a target subcode formed based on the second grammar specification.
[0077] S103: Determine a conversion code corresponding to the runtime code based on the target sub-code and the front-end sub-code; wherein the conversion code is used to run in a browser.
[0078] Here, the conversion code is a code obtained by converting the runtime code, has the same function as the runtime code, and can be run in a browser.
[0079] During specific implementation, the target subcode and the front-end subcode may be merged, and the merged code may be used as the conversion code.
[0080] In this way, by converting the backend subcode formed based on the first syntax specification in the runtime code into a target subcode matching the second syntax specification, the converted target subcode can be run in the browser, and then the conversion code is obtained based on the target subcode and the frontend subcode. This not only ensures that the runtime code is converted into the conversion code that supports running in the browser, but also retains the frontend subcode, avoiding the static analysis step, which is beneficial to accurately obtain each dynamic variable corresponding to the frontend subcode during the code conversion process, thereby improving the accuracy of the conversion code obtained by the conversion.
[0081] In one embodiment, after the conversion code is determined, the target file corresponding to the target sub-code may also be obtained from the server.
[0082] Here, the server stores a target file corresponding to the target subcode, that is, a target file corresponding to the backend subcode; the target file is a local file of the server, which can be obtained by the backend corresponding to the backend subcode formed based on the first syntax specification, and is a file required for the operation of the unconverted runtime code. Specifically, the target file may include variable files, data files, etc., which are also files required for the operation of the target subcode and frontend subcode corresponding to the conversion code.
[0083] In specific implementation, since the conversion code needs to run in the browser, and the target file is not stored in the memory corresponding to the browser, in order to ensure that the conversion code can run accurately in the browser, it is necessary to first obtain the target file corresponding to the target sub-code from the server.
[0084] Furthermore, the target file may be stored in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the converted code.
[0085] Here, the internal file system is a system for implementing file storage, which can be applied in the browser. Specifically, the internal file system can be a file system obtained by using the memfs technology. Among them, memfs is equivalent to memory-filesystem, which simulates a disk operating system in memory. Because the speed of reading disk is much slower than reading memory, we can use the memory file system as a storage medium in scenarios where files need to be read and written frequently.
[0086] The file system obtained by using memfs technology has the same implementation as the proprietary API (Application Programming Interface) part of Node.js fs corresponding to the target subcode, that is, it has the same function as the proprietary API part of Node.js fs. Among them, Node.js fs is equivalent to Node.js file system, and the node.js file system corresponds to the proprietary API of node.js. The proprietary API of node.js can be used by the backend to call Node.js fs to obtain files.
[0087] In specific implementation, after obtaining the target file, the target file can be stored in the internal file system corresponding to the browser. In this way, the target file will be stored in the browser, and the browser can directly obtain the target file from the internal file system during the subsequent execution of the conversion code, thereby realizing the accurate execution of the conversion code.
[0088] In addition, the steps of obtaining the target file and storing the target file in the internal file system corresponding to the browser provided in the embodiment of the present disclosure are not limited to being executed after obtaining the conversion code, but can also be executed at any time after determining the back-end subcode, which is not limited here.
[0089] In one embodiment, for S102, a preset code conversion tool may be used to convert the back-end subcode into a target subcode matching the second grammar specification based on the conversion relationship between the second grammar specification and the first grammar specification.
[0090] Here, the preset code conversion tool can be any conversion tool with code conversion capability, which is not limited here. In specific implementation, after determining the back-end subcode, the back-end subcode can be input into the preset code conversion tool, and the preset code conversion tool is used to convert the back-end subcode into a target subcode matching the second grammar specification based on the conversion relationship between the second grammar specification and the first grammar specification.
[0091] Alternatively, in one implementation, after obtaining the backend subcode, the developer may manually convert it into the target subcode based on the conversion relationship between the second grammar specification and the first grammar specification.
[0092] In one embodiment, for S103, after obtaining the target sub-code, a code execution environment corresponding to the runtime code may be created, and a conversion code may be determined based on the code execution environment, the target sub-code, and the front-end sub-code.
[0093] Here, the code running environment is used to characterize the running process of the runtime code when it is running on the server. In specific implementation, the code running environment may include but is not limited to the following: generating global variables corresponding to the runtime code, and protecting the proprietary API of Node.js fs.
[0094] In specific implementation, after obtaining the target sub-code, the code running environment when the runtime code is running on the server can be determined. Specifically, the global variables corresponding to the runtime code and the proprietary API of Node.js fs can be determined; then, the global variables corresponding to the runtime code can be generated and the proprietary API of Node.js fs can be protected, thereby completing the creation of the code running environment. Afterwards, based on the code running environment, the target sub-code and the front-end sub-code can be merged to obtain the converted code.
[0095] In one embodiment, the step of protecting the proprietary API of Node.js fs in the code running environment corresponding to the creation of the runtime code can be implemented according to the following steps:
[0096] Step 1: determine at least one application program interface API corresponding to the backend subcode, and for each API, create an independent operating environment corresponding to the API.
[0097] Here, the application program interface API corresponding to the backend subcode may be a proprietary API of Node.js fs. The independent operating environment is an API operating environment that can be provided by the browser, and in the API operating environment provided by the browser, the API corresponding to the backend subcode can be independently operated. Specifically, the independent operating environment may be iframe, webworker, serviceworker, sharedworker, etc.
[0098] In specific implementation, after obtaining the backend subcode, the subcode can be parsed to determine each node module corresponding to the backend subcode and the proprietary API of Node.js fs required by each node module, thereby obtaining at least one application program interface API corresponding to the backend subcode.
[0099] Afterwards, for each API, the independent operating environment corresponding to the API when running on the server can be determined. Afterwards, the independent operating environment corresponding to the API when running on the server can be converted into an independent operating environment that can be provided by the browser, thereby completing the creation of the independent operating environment corresponding to the API.
[0100] In this way, based on the independent operating environment created, while ensuring that the API can run independently in the browser, it is also possible to protect the API corresponding to the backend subcode.
[0101] Step 2: Based on the independent runtime environment corresponding to each API, create a code runtime environment corresponding to the runtime code.
[0102] Here, the independent runtime environment corresponding to each created API can be used as the code runtime environment corresponding to the runtime code.
[0103] In one embodiment, before converting the backend subcode into the target subcode, the process further includes determining the target variable corresponding to the runtime code.
[0104] Here, the target variables corresponding to the runtime code are the variables included in the runtime code. During the running of the runtime code, the values corresponding to the target variables can change dynamically. Different target variables can correspond to different node modules.
[0105] During specific implementation, the runtime code may be parsed to determine various target variables included in the runtime code.
[0106] Furthermore, the conversion code corresponding to the runtime code can be determined based on the target variable, the target subcode and the front-end subcode. Specifically, for each target variable, a Require module corresponding to the target variable can be created, that is, a Require module of the node module corresponding to the target variable can be created to load the node module in the browser, and after the Require module corresponding to the target variable is created, the Require module can also be initialized.
[0107] The Require module may include module code, and the Require module may be used to load the node module corresponding to the conversion code. In addition, the step of creating the Require module corresponding to the target variable may be performed after the target variable is determined.
[0108] Afterwards, the module code, front-end subcode, and back-end subcode corresponding to the created Require requirement module can be merged to obtain the conversion code corresponding to the runtime code.
[0109] like Figure 2 As shown, a specific implementation flow chart of a code conversion provided by an embodiment of the present disclosure may include the following steps:
[0110] S201: Obtain the runtime code corresponding to the target runtime environment in the server.
[0111] The runtime code includes a backend subcode formed based on a first grammar specification and a frontend subcode formed based on a second grammar specification.
[0112] S202: Determine the target variable corresponding to the runtime code, and create a requirement module corresponding to the target variable.
[0113] During specific implementation, the runtime code may be parsed to determine various target variables included in the runtime code, and a Require requirement module corresponding to the target variable may be created.
[0114] S203: Using a preset code conversion tool, based on the conversion relationship between the second grammar specification and the first grammar specification, convert the back-end subcode into a target subcode matching the second grammar specification.
[0115] S204: Determine at least one application program interface API corresponding to the backend subcode, and create an independent operating environment corresponding to each API.
[0116] S205: Based on the independent running environment corresponding to each API, create a code running environment corresponding to the runtime code.
[0117] S206: Determine the conversion code corresponding to the runtime code based on the target sub-code, the front-end sub-code and the requirement module.
[0118] S207: Obtain a target file corresponding to the target sub-code from the server, and store the target file in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the converted code.
[0119] Here, there is no strict execution order between S207 and S202 to S206.
[0120] Regarding the specific implementation process of S201 to S207, reference may be made to the above embodiments, which will not be described in detail here.
[0121] Those skilled in the art will appreciate that, in the above method of specific implementation, the order in which the steps are written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of the steps should be determined by their functions and possible internal logic.
[0122] Based on the same inventive concept, a code conversion device corresponding to the code conversion method is also provided in the embodiment of the present disclosure. Since the principle of solving the problem by the device in the embodiment of the present disclosure is similar to the above-mentioned code conversion method in the embodiment of the present disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0123] like Figure 3 FIG. 1 is a schematic diagram of a code conversion device provided by an embodiment of the present disclosure, comprising:
[0124] The acquisition module 301 is used to acquire the runtime code corresponding to the target operating environment in the server; the runtime code includes the backend subcode formed based on the first syntax specification and the frontend subcode formed based on the second syntax specification; the target operating environment is used to implement the call of the frontend subcode to the backend subcode;
[0125] A conversion module 302, configured to convert the back-end subcode into a target subcode matching the second grammar specification based on the second grammar specification;
[0126] The first determination module 303 is used to determine the conversion code corresponding to the runtime code based on the target sub-code and the front-end sub-code; wherein the conversion code is used to run in a browser.
[0127] In a possible implementation, the device further includes:
[0128] The storage module 304 is used to obtain the target file corresponding to the target sub-code from the server after determining the conversion code;
[0129] The target file is stored in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the conversion code.
[0130] In a possible implementation, the conversion module 302 is configured to use a preset code conversion tool to convert the back-end subcode into a target subcode matching the second grammar specification based on a conversion relationship between the second grammar specification and the first grammar specification.
[0131] In a possible implementation, the first determination module 303 is used to create a code execution environment corresponding to the runtime code, and determine the conversion code based on the code execution environment, the target sub-code and the front-end sub-code.
[0132] In a possible implementation manner, the first determining module 303 is used to determine at least one application program interface API corresponding to the backend subcode, and for each of the APIs, create an independent operating environment corresponding to the API;
[0133] Based on the independent operating environment corresponding to each API, a code operating environment corresponding to the runtime code is created.
[0134] In a possible implementation manner, the device further includes a second determining module 305:
[0135] The second determination module 305 is used to determine the target variable corresponding to the runtime code before converting the back-end subcode into a target subcode matching the second grammar specification based on the second grammar specification;
[0136] The first determination module 303 is used to create a Require requirement module corresponding to the target variable;
[0137] Based on the target sub-code, the front-end sub-code and the Require requirement module, a conversion code corresponding to the runtime code is determined.
[0138] For descriptions of the processing flow of each module in the device and the interaction flow between each module, reference may be made to the relevant descriptions in the above method embodiment, which will not be described in detail here.
[0139] The present disclosure also provides a computer device, such as Figure 4 FIG. 1 is a schematic diagram of a computer device structure provided by an embodiment of the present disclosure, including:
[0140] A processor 41 and a memory 42; the memory 42 stores machine-readable instructions executable by the processor 41, and the processor 41 is used to execute the machine-readable instructions stored in the memory 42. When the machine-readable instructions are executed by the processor 41, the processor 41 performs the following steps: S101: Obtaining the runtime code corresponding to the target operating environment in the server; the runtime code includes a backend subcode formed based on a first grammatical specification and a frontend subcode formed based on a second grammatical specification; the target operating environment is used to implement the call of the frontend subcode to the backend subcode; S102: Based on the second grammatical specification, converting the backend subcode into a target subcode matching the second grammatical specification; and S103: Based on the target subcode and the frontend subcode, determining the conversion code corresponding to the runtime code; wherein the conversion code is used to run in a browser.
[0141] The above-mentioned memory 42 includes internal memory 421 and external memory 422; the memory 421 here is also called internal memory, which is used to temporarily store the calculation data in the processor 41 and the data exchanged with the external memory 422 such as the hard disk. The processor 41 exchanges data with the external memory 422 through the internal memory 421.
[0142] The specific execution process of the above instructions can refer to the steps of the code conversion method described in the embodiment of the present disclosure, which will not be repeated here.
[0143] The embodiment of the present disclosure also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the code conversion method described in the above method embodiment are executed. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0144] The computer program product of the code conversion method provided in the embodiment of the present disclosure includes a computer-readable storage medium storing program code, and the instructions included in the program code can be used to execute the steps of the code conversion method described in the above method embodiment. For details, please refer to the above method embodiment, which will not be repeated here.
[0145] The computer program product may be implemented in hardware, software or a combination thereof. In one optional embodiment, the computer program product is embodied as a computer storage medium, and in another optional embodiment, the computer program product is embodied as a software product, such as a software development kit (SDK) and the like.
[0146] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present disclosure, it should be understood that the disclosed device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the device or unit can be electrical, mechanical or other forms.
[0147] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0148] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0149] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0150] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The protection scope of the present disclosure is not limited thereto. Although the present disclosure is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present disclosure, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.
Claims
1. A code conversion method, characterized in that: include: Obtaining runtime code corresponding to the target operating environment in the server; the runtime code includes a backend subcode formed based on a first grammar specification and a frontend subcode formed based on a second grammar specification; The target operating environment is used to implement the calling of the backend subcode by the frontend subcode; Based on the second grammar specification, converting the back-end subcode into a target subcode matching the second grammar specification; Determine, based on the target sub-code and the front-end sub-code, a conversion code corresponding to the runtime code; wherein the conversion code is used to run in a browser; The front end corresponding to the front end subcode is configured to communicate with the back end corresponding to the back end subcode during operation; Before converting the rear-end subcode into a target subcode matching the second grammatical specification based on the second grammatical specification, the method further includes: Determine the target variable corresponding to the runtime code, and create a requirement module corresponding to the target variable; wherein the requirement module includes a module code for loading a node module corresponding to the conversion code; Wherein, determining the conversion code corresponding to the runtime code based on the target subcode and the front-end subcode includes: Determining a conversion code corresponding to the runtime code based on the target subcode, the front-end subcode, and a module code corresponding to the required module; The target variable includes various variables included in the runtime code; during the running of the runtime code, the value corresponding to the target variable changes dynamically.
2. The method according to claim 1, characterized in that: After determining the conversion code, the method further includes: Obtaining the target file corresponding to the target subcode from the server; The target file is stored in an internal file system corresponding to the browser, so that the browser can obtain the target file from the internal file system during the process of running the conversion code.
3. The method according to claim 1, characterized in that The converting the back-end subcode into a target subcode matching the second grammar specification based on the second grammar specification includes: The back-end subcode is converted into a target subcode matching the second grammar specification by using a preset code conversion tool based on a conversion relationship between the second grammar specification and the first grammar specification.
4. The method according to claim 1, characterized in that: The determining, based on the target sub-code and the front-end sub-code, a conversion code corresponding to the runtime code comprises: A code execution environment corresponding to the runtime code is created, and the conversion code is determined based on the code execution environment, the target sub-code, and the front-end sub-code.
5. The method according to claim 4, characterized in that The step of creating a code execution environment corresponding to the runtime code includes: Determine at least one application program interface API corresponding to the backend subcode, and for each of the APIs, create an independent operating environment corresponding to the API; Based on the independent operating environment corresponding to each API, a code operating environment corresponding to the runtime code is created.
6. The method according to claim 1, characterized in that Before converting the rear-end subcode into a target subcode matching the second grammatical specification based on the second grammatical specification, the method further includes: Determining a target variable corresponding to the runtime code; The determining, based on the target sub-code and the front-end sub-code, a conversion code corresponding to the runtime code comprises: Create a Require requirement module corresponding to the target variable; Based on the target sub-code, the front-end sub-code and the Require requirement module, a conversion code corresponding to the runtime code is determined.
7. The method according to claim 1, characterized in that The first grammar specification includes a C++ grammar specification, and the second grammar specification includes a JavaScript grammar specification.
8. A code conversion device, characterized in that: include: An acquisition module, used to acquire runtime code corresponding to the target operating environment in the server; the runtime code includes a backend subcode formed based on the first grammar specification and a frontend subcode formed based on the second grammar specification; The target operating environment is used to implement the calling of the backend subcode by the frontend subcode; a conversion module, configured to convert the rear-end subcode into a target subcode matching the second grammatical specification based on the second grammatical specification; A first determination module is used to determine the conversion code corresponding to the runtime code based on the target sub-code and the front-end sub-code; wherein the conversion code is used to run in a browser; The front end corresponding to the front end subcode is configured to communicate with the back end corresponding to the back end subcode during operation; Wherein, the code conversion device further includes a second determination module, Before converting the rear-end subcode into a target subcode matching the second grammatical specification based on the second grammatical specification, the second determining module is configured to: Determine the target variable corresponding to the runtime code, and create a requirement module corresponding to the target variable; wherein the requirement module includes a module code for loading a node module corresponding to the conversion code; Wherein, determining the conversion code corresponding to the runtime code based on the target subcode and the front-end subcode includes: Determining a conversion code corresponding to the runtime code based on the target subcode, the front-end subcode, and a module code corresponding to the required module; The target variable includes various variables included in the runtime code; during the running of the runtime code, the value corresponding to the target variable changes dynamically.
9. A computer device, characterized in that: include: A processor and a memory, wherein the memory stores machine-readable instructions executable by the processor, and the processor is used to execute the machine-readable instructions stored in the memory. When the machine-readable instructions are executed by the processor, the processor executes the steps of the code conversion 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 computer program. When the computer program is executed by a computer device, the computer device executes the steps of the code conversion method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method and device for dynamically providing JS-compatible contents and terminal equipment
CN108388454A
Programming language integration method, programming software system and electronic device
CN111651165A
Python code resource loading method and device
CN113391816A