Detailed Description
The embodiment of the application provides a code processing method and device.
In order to make those skilled in the art better understand the technical solutions in the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Fig. 1 is a schematic flowchart of a code processing method provided in an embodiment of the present application, where the flowchart may be implemented by a program, and apparatuses that can carry the program include, but are not limited to, the following: personal computers, large and medium-sized computers, computer clusters, mobile phones, tablet computers, intelligent wearable equipment, vehicle machines and the like.
The process in fig. 1 may include the following steps:
s101: and obtaining a code to be processed.
In the embodiments of the present application, the code may be based on any one of programming languages. For example, JavaScript, JAVA, C + +, PHP, Python, and other programming languages. For convenience of description, the following embodiments are mainly described by taking JavaScript code as an example.
In the embodiment of the present application, the to-be-processed code is generally a code waiting to be packaged or modularized for a specified application scenario.
The code to be processed comprises one or more function bodies of the function and one or more function calls for the function. Under different application scenarios, the function body may not be changed, and the parameters used by the corresponding function calls may be different, for example, the parameter types are different, the parameter values are different, and the like.
S102: and determining each function call aiming at the specified function in the code to be processed and each branch condition in the specified function.
In the embodiment of the present application, the specified function may be any function whose function body is included in the code to be processed. In practical applications, for each function of which the function body is included in the code to be processed, each function of at least some of the functions may be respectively used as a designated function, and the flow in fig. 1 may be executed on each designated function.
In this embodiment of the present application, each function call specifically includes all function calls for a specified function in the code to be processed. The function calls may use different parameters, respectively.
For example, assume that the specified function is "clone (item)", some function call for the specified function may be "clone ()", another function call for the specified function may be "clone ('foo')", and so on. It can be seen that the parameter used by clone () is null (belonging to non-item), and the parameter used by clone ('foo') is the character string foo.
In the embodiment of the application, one or more branch conditions are contained in the specified function. The branch conditions specifically include all branch conditions in the specified function. Generally, branch conditions are contained in conditional statements such as "if", "else", and the like.
For example, assume that the function clone (item) is specified as follows:
it can be seen that the specified function of the above example contains two branch conditions in common. Branch 1 has a Branch condition of "! item ", the branching condition of the branch 2 is" typeof item ═ string ".
It should be noted that, in practical applications, the function body of the specified function may not be included in the code to be processed. However, in this case, the subsequent clipping process for the specified function may have an effect: the result of a function call to a specified function outside the code to be processed.
S103: and according to the branch condition and the parameters used by the function calls, cutting the branch codes corresponding to the branch condition in the specified function.
In the embodiment of the present application, as described above, the to-be-processed code is generally a code waiting to be packaged or modularized for a specified application scenario, and in the specified application scenario, when a function in the to-be-processed code is called, all branches in the function are not necessarily required to be used. Then, it can be considered that: in the specified application scenario, the branch codes which are not used are redundant, and can be cut out from the codes to be processed, and then the cut codes to be processed are packaged to reduce the size of the codes to be processed.
In the embodiment of the application, according to the branch condition and the parameters used by each function call, which branches are not needed to be used in the scene corresponding to the code to be processed can be determined, and then the code of the branch which is not used can be cut.
In addition, the present application does not limit the execution sequence of "determining each function call for the specified function in the code to be processed" and "determining each branch condition in the specified function" in step S102, and may execute simultaneously or execute one after another.
By the method of fig. 1, part of redundant codes (included in the to-be-processed codes) in the packed file can be eliminated, so that the volume of the packed file can be reduced, adverse effects on a user to obtain the packed file in the prior art can be reduced, and the execution efficiency of codes in the packed file can be improved, thereby solving the problems in the prior art partially or completely.
Based on the method of fig. 1, the examples of the present application also provide some specific embodiments of the method, and further embodiments, which are described below.
In the embodiment of the application, after the code to be processed is obtained, the subsequent steps can be realized by traversing the code to be processed, the code to be processed can also be converted into a data form which is easier to analyze, such as a syntax tree, and then the subsequent steps are realized according to the data obtained after the conversion. The syntax tree is used as an example below.
For step S102, the determining each function call for the specified function in the code to be processed specifically may include: generating a syntax tree of the code to be processed; and determining each function call aiming at the specified function in the code to be processed, parameters used by each function call and each branch condition in the specified function according to the syntax tree of the code to be processed.
The present application is not limited to how to generate the syntax tree, and can be implemented by using the existing technology. In the syntax tree, elements in codes such as functions and function calls can be represented as tree nodes, and the analysis and the operation are easy.
As mentioned above, the flow in fig. 1 may be executed for a plurality of functions in the code to be processed, so that all function calls and parameters used by the function calls in the code to be processed may be uniformly determined (and the data may be stored in a data structure such as a list for use).
Specifically, the determining, according to the syntax tree of the code to be processed, each function call for a specified function in the code to be processed and a parameter used by each function call may include: determining all function calls in the code to be processed according to the syntax tree of the code to be processed, and determining parameters used by all the function calls respectively; classifying all the determined function calls, wherein the function calls with the same function are classified into the same class; a type of function call for a specified function is determined among the classes, and parameters used by the type of function call are determined.
Further, the determining the branch conditions in the specified function may specifically include: determining a syntax tree of the specified function according to the syntax tree of the code to be processed; and determining each branch condition in the specified function according to the syntax tree of the specified function.
In this embodiment of the present application, for step S103, the performing, according to the branch condition and the parameter used by each function call, a clipping process on the branch code corresponding to the branch condition in the specified function specifically includes:
executing, for each of the branch conditions:
determining whether the parameters used by the function call can meet the branch conditions according to the parameters used by each function call in the function calls; if the parameters used by each function call cannot meet the branch condition, deleting the branch code corresponding to the branch condition.
For ease of understanding, the above example is used for illustration.
Assume that for the above specified function clone (item) in the pending code, there are two function calls for clone (item): clone (), clone ('foo'). It can be seen that clone () can conform to the branch condition "!of branch 1 of clone (item)! item ", a clone ('foo') may conform to the branching condition" typeof item ═ string '"of branch 2 of the clone (item), and therefore, neither branch's branch code is redundant and does not need to be clipped.
However, assuming that the clone (item) only calls clone () for the function of clone (item), the parameter used by clone () does not satisfy the branch condition "typeof item" of branch 2, and therefore, the branch code of branch 2 (i.e., the branch code corresponding to the branch condition of branch 2) is redundant and can be clipped. The function of the clipped clone (item) is as follows:
further, after the execution of each of the branch conditions, the following may be further performed: and if the executed specified function only contains one branch condition, deleting the code representing the branch condition. In this manner, code redundancy can be further eliminated.
The reason this can be done is: if the executed specified function only contains one branch condition, for any function call in the code to be processed aiming at the specified function, the parameter used by the function call must meet the branch condition, so that the code required to be executed when the branch condition is met is only required to be reserved, and the branch condition is not required to be reserved.
For ease of understanding, the above example is used for illustration.
The function body of the clipped clone (item) in the above example is further clipped, and the further clipping is as follows:
“function clone(item){
return item;
}”
since the function call of the to-be-processed code to the clone (item) only has the clone (), when the function call is executed, the "return item" can be directly executed without performing branch condition judgment.
Based on similar thinking, after each branch condition in the branch conditions is executed, the following steps can be executed: and for other branch conditions except the last branch condition in the executed specified function, if the branch codes corresponding to the other branch conditions all jump out of the specified function, deleting the code representing the last branch condition. In this manner, code redundancy may be further eliminated.
The reason this can be done is: for any function call in the to-be-processed code for the specified function, if the parameter used by the function call does not satisfy each branch condition before the last branch condition (if the parameter used by the function call satisfies the branch condition, the specified function is skipped, and subsequent codes in the specified function are not executed again), the last branch condition is necessarily satisfied.
The above description is provided for a code processing method provided in the embodiment of the present application, and based on the same inventive idea, the embodiment of the present application further provides a corresponding apparatus, as shown in fig. 2.
Fig. 2 is a schematic structural diagram of a code processing apparatus corresponding to fig. 1 according to an embodiment of the present application, where the apparatus may be located on an execution main body of the flow in fig. 1, and includes:
an obtaining module 201 for obtaining a code to be processed;
a determining module 202, configured to determine function calls for a specified function in the code to be processed, and branch conditions in the specified function;
and the processing module 203 performs clipping processing on the branch code corresponding to the branch condition in the specified function according to the branch condition and the parameter used by each function call.
Optionally, the determining module 202 determines each function call for the specified function in the code to be processed, and each branch condition in the specified function, specifically including:
the determining module 202 generates a syntax tree of the code to be processed, and determines each function call and parameters used by the function call for a specified function in the code to be processed, and each branch condition in the specified function according to the syntax tree of the code to be processed.
Optionally, the determining module 202 determines, according to the syntax tree of the code to be processed, each function call for the specified function in the code to be processed and parameters used by the function call, specifically including:
the determining module 202 determines all function calls in the code to be processed according to the syntax tree of the code to be processed, determines parameters used by the function calls respectively, classifies the determined function calls, classifies the function calls having the same function into the same class, determines a class of function calls for a specified function in each class, and determines parameters used by the class of function calls.
Optionally, the determining module 202 determines each branch condition in the specified function, which specifically includes:
the determining module 202 determines the syntax tree of the specified function according to the syntax tree of the code to be processed, and determines each branch condition in the specified function according to the syntax tree of the specified function.
Optionally, the processing module 203 performs, according to the branch condition and the parameter used by each function call, a clipping process on a branch code corresponding to the branch condition in the specified function, which specifically includes:
the processing module 203 performs, for each of the branch conditions:
determining whether the parameters used by the function call can meet the branch conditions according to the parameters used by each function call in the function calls;
if the parameters used by each function call cannot meet the branch condition, deleting the branch code corresponding to the branch condition.
Optionally, after the processing module 203 executes each branch condition in the branch conditions, if the executed specified function only includes one branch condition, the code indicating the branch condition is deleted.
Optionally, after the processing module 203 executes each of the branch conditions, for other branch conditions except for the last branch condition in the executed specified function, if all branch codes corresponding to the other branch conditions jump out of the specified function, the code representing the last branch condition is deleted.
Optionally, the code is JavaScript code.
The device and the method provided by the embodiment of the application are in one-to-one correspondence, so the device also has the beneficial technical effects similar to the corresponding method, and the beneficial technical effects of the method are explained in detail above, so the beneficial technical effects of the corresponding device are not described again.
In the 90 s of the 20 th century, improvements in a technology could clearly distinguish between improvements in hardware (e.g., improvements in circuit structures such as diodes, transistors, switches, etc.) and improvements in software (improvements in process flow). However, as technology advances, many of today's process flow improvements have been seen as direct improvements in hardware circuit architecture. Designers almost always obtain the corresponding hardware circuit structure by programming an improved method flow into the hardware circuit. Thus, it cannot be said that an improvement in the process flow cannot be realized by hardware physical modules. For example, a Programmable Logic Device (PLD), such as a Field Programmable Gate Array (FPGA), is an integrated circuit whose Logic functions are determined by programming the Device by a user. A digital system is "integrated" on a PLD by the designer's own programming without requiring the chip manufacturer to design and fabricate application-specific integrated circuit chips. Furthermore, nowadays, instead of manually making an Integrated Circuit chip, such Programming is often implemented by "logic compiler" software, which is similar to a software compiler used in program development and writing, but the original code before compiling is also written by a specific Programming Language, which is called Hardware Description Language (HDL), and HDL is not only one but many, such as abel (advanced Boolean Expression Language), ahdl (alternate Hardware Description Language), traffic, pl (core universal Programming Language), HDCal (jhdware Description Language), lang, Lola, HDL, laspam, hardward Description Language (vhr Description Language), vhal (Hardware Description Language), and vhigh-Language, which are currently used in most common. It will also be apparent to those skilled in the art that hardware circuitry that implements the logical method flows can be readily obtained by merely slightly programming the method flows into an integrated circuit using the hardware description languages described above.
The controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, a microprocessor or processor and a computer-readable medium storing computer-readable program code (e.g., software or firmware) executable by the (micro) processor, logic gates, switches, an Application Specific Integrated Circuit (ASIC), a programmable logic controller, and an embedded microcontroller, examples of which include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320, the memory controller may also be implemented as part of the control logic for the memory. Those skilled in the art will also appreciate that, in addition to implementing the controller as pure computer readable program code, the same functionality can be implemented by logically programming method steps such that the controller is in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers and the like. Such a controller may thus be considered a hardware component, and the means included therein for performing the various functions may also be considered as a structure within the hardware component. Or even means for performing the functions may be regarded as being both a software module for performing the method and a structure within a hardware component.
The systems, devices, modules or units illustrated in the above embodiments may be implemented by a computer chip or an entity, or by a product with certain functions. One typical implementation device is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
For convenience of description, the above devices are described as being divided into various units by function, and are described separately. Of course, the functionality of the units may be implemented in one or more software and/or hardware when implementing the present application.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In a typical configuration, a computing device includes 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.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, 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 Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The application may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The application may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only an example of the present application and is not intended to limit the present application. Various modifications and changes may occur to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.