Disclosure of Invention
The invention provides a method and a device for supporting multi-language script execution, which are used for solving the problem of supporting the script execution containing multiple languages.
The method for supporting the execution of the multi-language script provided by the invention comprises the following steps:
generating a copy of the context data prior to executing the script; wherein the script is written in at least two languages, the context data being shared by at least two scripting language engines;
and matching the script language engine corresponding to the language of the script for the script according to the language of the script so as to compile and execute.
Further, the method of the present invention further comprises:
before executing a script, logically packaging part of the script into a function;
compiling the function to generate a class file when compiling the function;
caching the instance of the class file to the context data.
Further, the method of the present invention further comprises:
running the class file using a class loader; wherein each of the class files corresponds to one of the class loaders.
Further, the method of the present invention, wherein the step of matching the script language engine corresponding to the language of the script for the script according to the language of the script to perform compiling execution further includes:
obtaining a variable from the context data query;
if the variable is not obtained by querying the context data, querying the cached instance of the class file to obtain the variable;
and if the variable is not acquired by querying the cached instance of the class file, the variable is acquired by querying from an open source framework of the project.
Further, the method of the present invention, wherein the step of matching the script language engine corresponding to the language of the script for the script according to the language of the script to perform compiling execution further includes:
and if the variable is not inquired in the open source framework of the project, inquiring the variable through copying the context data.
Further, the method of the present invention further comprises:
stacking the copy of the context data before each execution of the script;
after each execution of the script, popping the copy of the context data.
The device for supporting the multi-language script execution provided by the invention comprises the following components:
a copy module to generate a copy of the context data prior to executing the script; wherein the script is written in at least two languages, and the context data is shared by at least two scripting language engines;
and the compiling and executing module is used for matching the script language engine corresponding to the language of the script for the script according to the language of the script so as to carry out compiling and executing.
Further, the apparatus of the present invention further includes:
a cache module to:
before executing the script, logically packaging part of the script into a function;
compiling the function to generate a class file when compiling the function;
caching an instance of the class file to the context data.
Further, the apparatus of the present invention further includes:
the class loader is used for operating the class file; wherein each of the class files corresponds to one of the class loaders.
Further, in the apparatus of the present invention, the compiling execution module is further configured to:
obtaining a variable from the context data query;
if the variable is not obtained by querying the context data, querying the cached instance of the class file to obtain the variable;
and if the cached instance of the class file is not queried and the variable is obtained, querying and obtaining the variable from an open source framework of the project.
Further, in the apparatus of the present invention, the compiling execution module is further configured to:
and if the variable is not inquired in the open source framework of the project, inquiring the variable through copying the context data.
Further, the apparatus of the present invention further comprises:
a management stack module to:
stacking the copy of the context data before each execution of the script;
popping the copy of the context data off the stack after each execution of the script.
The present invention also provides a storage medium storing computer program instructions for performing a method according to the present invention.
The present invention also provides a computing device comprising: a memory for storing computer program instructions and a processor for executing the computer program instructions, wherein the computer program instructions, when executed by the processor, trigger the computing device to perform the method of the invention.
The method and the device for supporting the execution of the multi-language script can support the integration of multiple languages in a project in a lightweight way, and the languages can be mutually called.
Detailed Description
The present invention is described in further detail below with reference to the attached drawing figures.
Fig. 1 is a flowchart illustrating a method for supporting multi-language script execution according to an embodiment of the present invention, and as shown in fig. 1, the method for supporting multi-language script execution according to an embodiment of the present invention includes:
step S101, before executing the script, generating a copy of the context data; wherein the script is written in at least two languages and the context data is shared by at least two scripting language engines.
And step S102, matching the script language engine corresponding to the language of the script for the script according to the language of the script so as to compile and execute.
Among the multiple languages contained in the script, the script may contain a static programming language or a dynamic programming language. I.e., a script, in which a part of the code is implemented by a static programming language and the other part of the code is implemented by a dynamic programming language. Dynamic programming languages are different relative to static programming languages. The dynamic programming language is compiled and executed at runtime. Static programming languages are typed before running, e.g., in the compilation phase. The engineering of the software takes a static language as an engineering framework main body, and the stability of the architecture can be ensured. However, when some business logic scenes with high requirements on flexibility are processed, the static language needs to be modified, tested, compiled and online every time, and the iteration speed is low. Therefore, in the face of a service requirement with a high iteration speed, a dynamic script realized by adopting a dynamic programming language is more adaptive, for example, the dynamic script is more adaptive under a service logic scene which needs to be dynamically judged and adjusted during execution. And dynamic scripts can provide easier grammatical sugars to shorten script content. If the main body of the project is supported by a static language in one project and changeable business logic is executed by a dynamic script, the iteration speed can be greatly increased on the premise of ensuring the stability of the architecture.
The context data can be shared by at least two scripting language engines, so that the situation that developers need to manually intervene in the introduction and embedding points of scripts is avoided, calling among the scripts can be realized without embedding hooks in codes of the scripts in advance, and the method is also suitable for scenes with concurrency or affairs.
In addition, the different script language engines share one context data, and a copy of the context data is generated each time the script is executed, so that the data can be ensured not to be polluted. Even if the execution of the script is wrong, the variable of the context data is wrong, the data can be automatically recovered from the copy of the context data in the execution process to be continuously executed, and the fault tolerance rate in the execution of the multi-language script is improved.
The scripting language engine may include a dynamic scripting engine and a static scripting engine, and compiles and executes codes implemented by a dynamic programming language and codes implemented by a static programming language in the script, respectively. During script execution, when the code part realized by the static programming language is executed, the static script engine is called to compile and execute the code part. When the code part realized by the dynamic programming language is executed in sequence in the execution process, the dynamic script engine is switched to compile and execute the code part.
The method for supporting the execution of the multi-language script can simultaneously support a static language and a dynamic language, greatly improve the iteration speed on the premise of ensuring the stability of the architecture and ensure that data cannot be polluted in the script execution process.
Fig. 2 is a flowchart illustrating a method for supporting multi-language script execution according to a second embodiment of the present invention, as shown in fig. 2, the method for supporting multi-language script execution according to the second embodiment of the present invention includes:
step S201, packaging part of the logic of the script into a function.
Each script can package a segment of independent logic contained in the script into a function, and the segment of independent logic can be written in one language. Each script may include several independent logics, and each logic is a part of the script. The logics can be written in different languages respectively, namely, the script is formed by independent logics of all parts into a whole, and the languages of all logics are different. The packed function can be compiled into a class file (class file) through step S205 at the time of compiling, and an instance of the class file is generated and cached in the context data for use when the script is executed.
Wherein the script is written in at least two languages. The script may be dynamically compiled at run-time into a class file (class file). The script may include a static programming language or a dynamic programming language among a plurality of languages. I.e., a script, in which a part of the code is implemented by a static programming language and the other part of the code is implemented by a dynamic programming language. Dynamic programming languages are different relative to static programming languages. The dynamic programming language is compiled and executed at runtime. Static programming languages are typed before running, e.g., in the compilation phase. The engineering of the software takes a static language as an engineering framework main body, and the stability of the architecture can be ensured. However, when some business logic scenarios with high requirements on flexibility are processed, the static language needs to be modified, tested, compiled and online every time, and the iteration speed is low. Therefore, in the face of a service requirement with high iteration speed, the dynamic script realized by adopting the dynamic programming language is more adaptive, for example, the dynamic script is more adaptive under a service logic scene which needs to be dynamically judged and adjusted during execution. And dynamic scripts can provide easier grammatical sugars to shorten script content. If the main body of the project is supported by a static language in one project and changeable business logic is executed by a dynamic script, the iteration speed can be greatly increased on the premise of ensuring the stability of the architecture.
In step S202, a copy of the context data is generated.
Wherein the context data is shared by at least two scripting language engines. Context data can be shared by at least two scripting language engines, so that the situation that developers need to manually intervene in the introduction and embedding points of scripts is avoided, calling among the scripts can be realized without embedding hooks in codes of the scripts in advance, and the method is also suitable for scenes with concurrency or affairs. By sharing context data by different scripting language engines and generating a copy of the context data each time a script is executed, it is ensured that the data is not contaminated.
Step S203, before the script is executed each time, the copy of the context data is stacked.
Each time the script is executed, a copy of the context data is generated and put on the stack, and after the script is executed, the copy of the context data is put off the stack through the step S206, so that the self-tuning of the script can be supported, and the independence of the context data can be ensured. Even if the execution of the script is wrong, the variable of the context data is wrong, the data can be automatically recovered from the copy of the context data in the execution process to be continuously executed, and the fault tolerance rate in the execution of the multi-language script is improved.
And step S204, matching the script language engine corresponding to the language of the script for the script according to the language of the script.
The scripting language engine may include a dynamic scripting engine and a static scripting engine, and compiles and executes codes realized by a dynamic programming language and codes realized by a static programming language in the script respectively. During script execution, when a code part realized by a static programming language is executed, a static script engine is called to compile and execute the code part. When the code part realized by the dynamic programming language is executed in sequence in the execution process, the dynamic script engine is switched to compile and execute the code part.
And step S205, compiling and executing the script by using the script language engine obtained by matching.
In step S205, when compiling the script, the steps a1 to a2 are performed as follows:
a1. compiling the function to generate a class file when the function is compiled;
a2. caching the instance of the class file to the context data.
When the generated instance of the class file is cached in the context data, variables necessary for execution can be conveniently inquired and obtained from the cached instance in the context data during script execution. If the execution has no problem, the instance is not required to be saved, and the instance is required to be deleted after the execution is finished every time for the execution efficiency, so the instance is only cached in the context data.
In step S205, when the variable needs to be acquired during the execution process, the following steps b1 to b4 are performed:
b1. obtaining a variable from the context data query;
b2. if the variable is not obtained by querying the context data, querying the cached instance of the class file to obtain the variable;
b3. if the variable is not obtained by querying the cached instance of the class file, querying the open source framework of the project to obtain the variable;
b4. and if the variable is not inquired in an open source framework (Spring) of the project, inquiring the variable through the copy of the context data.
Wherein, step b4 is an optional step, when no variable is queried in the open source framework of the project, the execution of the script can be ended and an error can be reported. However, considering that the copy of the context data independently stores the data during the script execution, if no variable is queried in the open source framework, the query can be continued through the copy of the context data. By rewriting the logic of the query variables in the script language engines to conform to the query sequence from b1 to b3 and clarifying the variable acquisition rules, the sharing of the same context data among different script language engines can be realized.
Step S206, after each script execution, popping the copy of the context data.
After the script is executed, the context data is copied out of the stack, so that self-tuning of the script can be supported, and independence of the context data is guaranteed.
Step S207, the class loader is used for running the class files.
Wherein each of the class files corresponds to one of the class loaders. Because the script content can be changed and replaced at any time, each compiled class file (class file) is independently operated in a class loader (class loader), so that mutual interference caused by execution of various logics can be avoided.
The method for supporting the execution of the multi-language script in the second embodiment of the invention can simultaneously support the static language and the dynamic language, greatly improve the iteration speed on the premise of ensuring the stability of the architecture and ensure that data cannot be polluted in the script execution process.
Fig. 3 is a schematic structural diagram of a device for supporting multi-language script execution according to a third embodiment of the present invention, and as shown in fig. 3, the device for supporting multi-language script execution according to the third embodiment of the present invention includes: a copy module 31 and a compile execution module 32.
A copy module 31 for generating a copy of the context data before executing the script; wherein the script is written in at least two languages, and the context data is shared by at least two scripting language engines;
and the compiling and executing module 32 is used for matching the script language engine corresponding to the language of the script for the script according to the language of the script so as to carry out compiling and executing.
The apparatus according to the third embodiment of the present invention is an implementation apparatus of the method according to the first embodiment of the present invention, and the specific principle thereof is the same as that of the method according to the first embodiment of the present invention, and is not described herein again.
Fig. 4 is a schematic structural diagram of a device for supporting multiple language script execution according to a fourth embodiment of the present invention, as shown in fig. 4, the device for supporting multiple language script execution according to the fourth embodiment of the present invention includes: a copy module 31, a compilation execution module 32, a cache module 41, a class loader 42, and a management stack module 43.
Wherein, the copying module 31 is configured to generate a copy of the context data before executing the script; wherein the script is written in at least two languages and the context data is shared by at least two scripting language engines.
The compiling and executing module 32 is configured to match the script with the script language engine corresponding to the language of the script according to the language of the script to perform compiling and executing.
Wherein the compiling execution module 32 is further configured to:
obtaining a variable from the context data query;
if the variable is not obtained by querying the context data, querying the cached instance of the class file to obtain the variable;
and if the cached instance of the class file is not queried and the variable is obtained, querying and obtaining the variable from an open source framework of the project.
Wherein the compiling execution module 32 is further configured to:
and if the variable is not inquired in the open source framework of the project, inquiring the variable through copying the context data.
Wherein, the cache module 41 is configured to:
before executing the script, logically packaging part of the script into a function;
compiling the function to generate a class file when the function is compiled;
caching an instance of the class file to the context data.
Wherein, the class loader 42 is used for running the class file. Wherein each of the class files corresponds to one of the class loaders.
Wherein, the management stack module 43 is configured to:
stacking the copy of the context data before each execution of the script;
popping the copy of the context data off the stack after each execution of the script.
The apparatus described in the fourth embodiment of the present invention is an apparatus for implementing the method described in the second embodiment of the present invention, and the specific principle is the same as that of the method described in the second embodiment of the present invention, which is not described herein again.
In one embodiment of the present invention, there is also provided a computing device comprising: a memory for storing computer program instructions and a processor for executing the computer program instructions, wherein the computer program instructions, when executed by the processor, trigger the computing device to perform the method of embodiments one and two of the present invention.
In one exemplary configuration of the invention, the computing devices each include one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
In an embodiment of the present invention, a storage medium is further provided, where the storage medium stores computer program instructions, and the computer program instructions are executed according to the methods described in the first embodiment and the second embodiment of the present invention.
Computer-readable storage media include permanent and non-permanent, removable and non-removable media and may implement information storage by any method or technology. The information may be computer readable instructions, data structures, programmed devices, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Disks (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium, which can be used to store information and which can be accessed by a computing device.
It should be noted that the present invention may be implemented in software and/or in a combination of software and hardware, for example, as an Application Specific Integrated Circuit (ASIC), a general purpose computer or any other similar hardware device. In some embodiments, the software program of the present invention may be executed by a processor to implement the above steps or functions. Also, the software programs (including associated data structures) of the present invention can be stored in a computer readable recording medium, such as RAM memory, magnetic or optical drive or diskette and the like. Further, some of the steps or functions of the present invention may be implemented in hardware, for example, as circuitry that cooperates with the processor to perform various steps or functions.
It will be evident to those skilled in the art that the invention is not limited to the details of the foregoing illustrative embodiments, and that the present invention may be embodied in other specific forms without departing from the spirit or essential attributes thereof. The present embodiments are therefore to be considered in all respects as illustrative and not restrictive, the scope of the invention being indicated by the appended claims rather than by the foregoing description, and all changes which come within the meaning and range of equivalency of the claims are therefore intended to be embraced therein. Any reference sign in a claim should not be construed as limiting the claim concerned. Furthermore, it is obvious that the word "comprising" does not exclude other elements or steps, and the singular does not exclude the plural. A plurality of units or means recited in the apparatus claims may also be implemented by one unit or means in software or hardware. The terms first, second, etc. are used to denote names, but not any particular order.