JavaScript automatic error correction method, system, equipment and medium
Through the JavaScript engine assembler, the JavaScript engine assembler detects and processes runtime errors, saves and restores the register values of the execution environment, solving the problem of error capture and processing of JavaScript runtime errors, and achieving the improvement of program continued execution and fault tolerance.
Patent Information
- Application Number
- CN202411568021.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2025-02-18
AI Technical Summary
The existing technology is difficult to effectively capture and handle JavaScript runtime errors, resulting in page stuck and user operations unresponsive.
When executing assembly code through the JavaScript engine assembler, it detects exceptions and saves the register value of the current execution environment, determines whether the exception is caught, restores the register value according to the exception type and returns an empty object or empty function implemented by the built-in code, or jumps to the original capture exception execution logic.
It implements the capture of JavaScript runtime errors and keeps the program executed, intercepts all errors thrown by the runtime, simplifies the processing of fault-tolerant logic, and improves the fault-tolerant capabilities of the JS engine.
Smart Images

Figure CN119473861A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of software development, and in particular to a method, system, device and medium for automatic error correction of JavaScript. Background Art
[0002] As an interpreted weakly typed scripting language, JavaScript is not only one of the core languages for web development, but has also been widely used in game development, mobile applications, machine learning and other fields in recent years. Due to the weak type characteristics of JavaScript, data types can be forced to change at will, making development more flexible and low-entry barriers, but it also brings a great risk of runtime errors. Due to the uncontrollability of developers, how to improve the fault tolerance of JavaScript engines has always been the focus of exploration in the industry. At present, there are generally three means of JavaScript (hereinafter referred to as JS) fault tolerance:
[0003] After downloading the JS file, use debugging tools (such as the inspect tool of edge / chrome browser) to detect syntax errors in the JS file. The fault-tolerant method is to add try catch logic to the error statement, replace the error statement, etc.
[0004] When parsing JS statements into AST (Abstract Syntax Tree), detect JS errors and tolerate them. The tolerance method is generally to skip the error line.
[0005] At runtime, according to the specific error type, the error location is found in different types of codes, and the return value is constructed for fault tolerance.
[0006] Several problems with the existing technology:
[0007] 1) Most fault-tolerant solutions solve syntax errors in JS code. In reality, most JS errors are thrown during execution. Errors thrown during execution are difficult to predict because they depend on JS semantics, business logic, etc. Once an error occurs, it will cause abnormal phenomena such as page freezing and no response when users operate the web page.
[0008] 2) The JS engine involves various forms of code during execution, such as stub code, built-in code, inline code, etc., and fault tolerance and return value construction are required in all codes. Code modification is difficult, fault tolerance is not comprehensive and prone to errors. There is a lack of a general technology to capture all JS exceptions and allow the program to continue execution.
[0009] In summary, there is an urgent need for a general technology to capture JavaScript runtime errors and allow the program to continue executing. Summary of the invention
[0010] In view of the above problems, the present disclosure provides a JavaScript automatic error correction method, system, device and medium, which are used to solve the problem of capturing JavaScript runtime errors and allowing the program to continue executing.
[0011] In a first aspect, a JavaScript automatic error correction method is provided, the method comprising:
[0012] When the JavaScript engine assembler executes the assembly code, it saves the register value of the current execution environment when an exception is detected;
[0013] By calling the preset runtime function, it is determined whether the current exception is caught. If the exception is not caught, the preset runtime function is called to determine the current exception type.
[0014] When the current exception type is an object call, the register values of the saved execution environment are restored and an empty object implemented with built-in code is returned;
[0015] When the current exception type is a function call, the register values of the saved execution environment are restored and the execution is jumped to the empty function implemented by the built-in code.
[0016] Furthermore, by calling the preset runtime function, it is determined whether the current exception has an exception capture, and then it also includes:
[0017] When an exception is caught, the register values of the saved execution environment are restored and the execution logic of the assembler's original caught exception is jumped to.
[0018] Furthermore, it also includes:
[0019] The exception information is reported and recorded through the preset runtime function for subsequent processing.
[0020] Furthermore, the register values of the current execution environment are saved, including:
[0021] The register values related to both the caller and the callee need to be saved.
[0022] Further, restoring the register values of the saved execution environment includes:
[0023] The register values relevant to both the caller and the callee need to be restored.
[0024] Furthermore, the preset runtime functions are called, including:
[0025] Use macro assembly to call the runtime function through the runtime function template name, and the return value of the function is saved in the register.
[0026] Further, built-in code, including:
[0027] Use macro assembly to access built-in code through the name of the code template.
[0028] In a second aspect, a JavaScript automatic error correction system includes: an exception detection module, a protection scene module, an exception capture judgment module, an exception type judgment module, an exception handling module and a recovery scene module;
[0029] The exception detection module is used by the JavaScript engine assembler to detect whether an exception is thrown when executing the assembly code;
[0030] The protection scene module is used to save the register value of the current execution environment when an exception is thrown;
[0031] The exception capture judgment module is used to determine whether the current exception has been captured by calling a preset runtime function;
[0032] The exception type judgment module is used to call the preset runtime function to judge the current exception type if the exception is not caught;
[0033] The exception handling module is used to return an empty object implemented by built-in code when the current exception type is an object call; when the current exception type is a function call, jump to the empty function implemented by the built-in code for execution;
[0034] The restore context module is used to restore the register values of the saved execution environment.
[0035] In a third aspect, an electronic device includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;
[0036] a memory storing a computer program;
[0037] The processor is used to implement the above-mentioned JavaScript automatic error correction method when executing the computer program stored in the memory.
[0038] In a fourth aspect, a computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the above-mentioned JavaScript automatic error correction method is implemented.
[0039] The present disclosure includes at least the following beneficial effects:
[0040] The present disclosure provides a JavaScript automatic error correction method and system by changing the assembler execution rules, which automatically corrects the errors that are not caught during the execution of the JS code, so that the JS logic can continue to execute; the present disclosure can intercept all errors thrown by the JS runtime and has strong versatility; the runtime error types are summarized into two categories: function errors and object errors, which simplifies the processing of fault-tolerant logic.
[0041] Other features and advantages of the present disclosure will be described in the following description, and partly become apparent from the description, or be understood by implementing the present disclosure. The purpose and other advantages of the present disclosure can be realized and obtained by the structures indicated in the description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0043] Figure 1 This is a schematic diagram of the fault tolerance method flow in an embodiment of the present disclosure;
[0044] Figure 2 This is a schematic diagram of the process principle of the JavaScript automatic error correction method in the embodiment of the present disclosure;
[0045] Figure 3 This is a schematic diagram of the structure of the JavaScript automatic error correction system in the embodiment of the present disclosure;
[0046] Figure 4 A schematic diagram of mapping error types and fault tolerance types according to an embodiment of the present disclosure;
[0047] Figure 5 A schematic diagram of the mapping of built-in code, corresponding JS semantics and pseudo-assembly calls in the embodiment of the present disclosure;
[0048] Figure 6 It is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0049] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution 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 part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0050] like Figure 1 As shown, a JavaScript automatic error correction method, the method comprising:
[0051] S101, when the JavaScript engine assembler executes the assembly code, when an exception is detected, the register value of the current execution environment is saved;
[0052] S102, determining whether the current exception is caught by calling a preset runtime function, and if the exception is not caught, calling a preset runtime function to determine the current exception type;
[0053] S103, when the current exception type is an object call, restoring the saved register value of the execution environment, and returning an empty object implemented by the built-in code;
[0054] S104, when the current exception type is a function call, the register value of the saved execution environment is restored, and the execution is jumped to the empty function implemented by the built-in code.
[0055] The specific implementation is as follows:
[0056] Since the JS engine ultimately executes assembly code when executing hot JS code (multiple executions) and non-hot JS code, all runtime error types are ultimately thrown in the assembler. Therefore, this paper uses the JS engine transition code rules in the assembler of the JS engine to provide a method and system for automatic error correction by changing the assembler execution rules when executing JS code. It mainly includes two parts: the preset code program and the assembler exception fault tolerance program.
[0057] The preset code program mainly provides support for the assembler exception fault-tolerant program, which is composed of JS engine transition code, including preset macro assembly, preset runtime code, and preset built-in code. Among them, macro assembly is a higher-level semantics combined using basic assembly, which is convenient for coding and understanding; runtime code and built-in code are code templates predefined by the JS engine, and the assembler can directly call them through the name of the code template. When the stack information of the current JS execution environment needs to be obtained, the runtime code is used to implement it, otherwise the built-in code is used to implement it. The preset macro assembly code mainly provides support for the exception detection module, the protection scene module, and the recovery scene module of the assembler exception fault-tolerant program; the preset runtime code mainly provides support for the exception capture judgment module, the exception type judgment module, and the exception information reporting module of the assembler exception fault-tolerant program; the preset built-in code mainly provides support for the exception handling module of the assembler exception fault-tolerant program.
[0058] The technical ideas are as follows:
[0059] When the abnormal detection module detects an abnormal error, it enters the protection scene module;
[0060] The protection scene module is used to save the register value of the current JS running environment and transfer it to the exception capture and judgment module;
[0061] The exception capture judgment module is used to determine whether the current exception has been captured by the JS exception capture logic. If the exception is captured, it will enter the recovery scene module and then enter the original logic execution; if the exception is not captured, it will enter the exception type judgment module;
[0062] The exception type judgment module is mainly used to judge the type of exception. Except for the function call error, which is classified as the function is undefined, all other errors are classified as the object is undefined. If the function is undefined, enter the recovery scene module and then enter the function undefined exception handling module; if the object is undefined, enter the recovery scene module and then enter the object undefined exception handling module;
[0063] The recovery scene module corresponds to the protection scene module and is used to restore the register value of the saved JS runtime environment;
[0064] The function undefined exception handling module is used to jump to the empty function implemented by the built-in code. Here, the empty function refers to the function type constructed in the code template, but the function body is empty;
[0065] The object undefined exception handling module is used to return an empty object implemented by built-in code. The empty object here refers to an object defined in the JS code but not assigned a value, and its typeof is undefined;
[0066] The assembler exception fault-tolerant program finally enters the exception information reporting module, which needs to reconstruct the error information, including the reconstructed error type, error line, error file and other information, and pass the error information from the JS engine process to the upper-level application, which then reports it to the network management for statistics.
[0067] like Figure 2 As shown, the specific implementation steps are as follows:
[0068] Step S1: The assembler of the JS engine is executing the assembly code. When an exception is detected, it goes to step S2. Exception throwing means interrupting the current assembly execution program and setting Exception information in the current JS execution stack;
[0069] Step S2: Protect the scene, that is, save the register values of the current execution environment. It should be noted here that the register values related to the JS caller and the JS callee need to be saved.
[0070] Go to step S3;
[0071] Step S3: Call the preset runtime function to determine whether the current exception is caught by try catch. If the exception is not caught, go to step S4; if the exception is caught, go to step S9. Here, the macro assembly calls the runtime function through the runtime function template name, and the return value of the function is saved in the register;
[0072] Step S4: Call the preset runtime function to determine whether the current exception type is a function. The exception types here are mainly divided into two categories, object call and function call. If it is an object call, go to step S5, if it is a function call, go to step S7. Here, the macro assembly calls the runtime function through the runtime function template name, and the return value of the function is saved in the register;
[0073] Step S5: restore the scene, that is, restore the register values of the saved execution environment. This corresponds to step 2 one by one. The register values related to the JS caller and the JS callee need to be restored. Go to step S6;
[0074] Step S6: Return an empty object implemented with built-in code. The macro assembler accesses the built-in code through the name of the code template. The empty object here can be regarded as an object defined but not assigned in the JS code, and its typeof is undefined. Go to step S11;
[0075] Step S7: restore the scene, same as step S5. Go to step S8;
[0076] Step S8: Jump to the empty function implemented by the built-in code for execution. The macro assembler accesses the built-in code through the name of the code template. Here, the empty function refers to the function type constructed in the code template, but the function body is empty. Go to step S11;
[0077] Step S9: restore the scene, same as step 5. Go to step S10;
[0078] Step S10: Jump to the original logic execution of the assembler, which refers to the execution logic of the original exception capture, and then go to step S11;
[0079] Step S11: abnormal information reporting, implemented by a preset runtime function, transmits information from the JS engine process to the application layer process through IPC communication, and the upper application process can inform the network management platform through broadcasting or other methods, and the network management platform will inform the developer and management platform after counting the error information. If JS is fault-tolerant, the error information needs to be reconstructed, including the reconstructed error type, error line, error file and other information.
[0080] like Figure 3As shown, a JavaScript automatic error correction system includes: an exception detection module, a scene protection module, a scene recovery module, an exception capture judgment module, an exception type judgment module, an exception information reporting module and an exception handling module;
[0081] The exception detection module is used by the JavaScript engine assembler to detect whether an exception is thrown when executing the assembly code;
[0082] The protection scene module is used to save the register value of the current execution environment when an exception is thrown;
[0083] A restore scene module is used to restore the register values of the saved execution environment;
[0084] The exception capture judgment module is used to determine whether the current exception has been captured by calling a preset runtime function;
[0085] The exception type judgment module is used to call the preset runtime function to judge the current exception type if the exception is not caught;
[0086] The exception information reporting module is used to transmit error information from the JS engine process to the upper application process through the in-process communication IPC;
[0087] The exception handling module is used to return an empty object implemented with built-in code when the current exception type is an object call; when the current exception type is a function call, jump to the empty function implemented with built-in code for execution.
[0088] The specific implementation is as follows:
[0089] Specifically, it includes two parts: assembler exception error correction program and preset code program.
[0090] The preset code program mainly includes preset macro assembly, preset runtime code, and preset built-in code. Among them, macro assembly is a higher-level semantic combination of basic assembly, which is convenient for coding and understanding; preset runtime code and preset built-in code are code templates predefined by the JS engine, and the assembler can directly call them through the name of the code template. When it is necessary to obtain the stack information of the current JS execution environment, use the preset runtime code to implement it, otherwise use the preset built-in code to implement it. Among them, the preset macro assembly provides support for the exception detection module, the protection scene module, and the recovery scene module; the preset runtime code provides support for the exception capture judgment module, the exception type judgment module, and the exception information reporting module; the preset built-in code provides support for the exception handling module.
[0091] The assembler exception fault-tolerant program mainly includes an exception detection module, a protection scene module, a recovery scene module, an exception capture judgment module, an exception type judgment module, an exception information reporting module and an exception handling module.
[0092] Exception detection module: It is completed by preset macro assembly language, where the exception refers to interrupting the current assembly execution program and setting exception information in the current JS execution stack.
[0093] Protect the scene module: It is completed by the preset macro assembly language, that is, saving the register values of the current JS execution environment. It should be noted here that the register values related to the JS caller and the JS callee need to be saved.
[0094] Restore scene module: It is completed by preset macro assembly language, corresponding to the function of protecting scene module, that is, restore the register value of the saved JS execution environment. The register values related to the JS caller (caller) and the JS callee (callee) need to be restored.
[0095] Exception capture judgment module: It is a set of predefined runtime function templates that provide support for JS code execution and are called by function template names in macro assembly. Its specific implementation is to traverse all stack frames (data structures that store function local variables and parameters) in the current JS execution environment and search for exception capture handles in the handle table.
[0096] Exception type judgment module: It is a set of predefined runtime function templates that provide support for JS code execution and are called by function template names in macro assembly. Its specific implementation is to classify different types of JS errors. Here, except for function calls, which are classified as function undefined types, other errors are classified as object undefined, such as Figure 4 shown.
[0097] Abnormal information reporting module: It is a set of predefined runtime function templates. If JS is fault-tolerant, the error information needs to be reconstructed to distinguish JS errors without automatic fault tolerance. The reported error information includes error type, error line, error file, etc. The error information is transmitted from the JS engine process to the upper application process through the in-process communication IPC. The upper application process can inform the network management platform through broadcasting, and the network management platform will inform the developers and management platform after counting the error information.
[0098] Exception handling module: It is a set of predefined built-in code templates. The built-in function is used here because it does not need to obtain the stack information of the current JS execution like the runtime code. The implementation is simpler and the assembler can directly access it through the name of the code template. It is specifically divided into function undefined exception handling module and object undefined exception handling module. The implementation scheme of the function undefined exception handling module is to jump to the empty function implemented by the built-in code for execution. Here the empty function refers to the function type constructed in the code template, but the function body is empty; the implementation scheme of the object undefined exception handling module is to return an empty object implemented by the built-in code. The empty object here can be regarded as an object defined in the JS code but not assigned a value, and its typeof is undefined. The correspondence between the built-in code, the corresponding JS semantics, and the pseudo-assembly call code name is as follows: Figure 5 shown.
[0099] The above embodiment provides a method and system for automatic error correction of JavaScript, and only uses the division of the above functional modules as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above.
[0100] like Figure 6 As shown, the present disclosure provides an electronic device, including a processor 601, a communication interface 602, a memory 603 and a communication bus 604, wherein the processor 601, the communication interface 602 and the memory 603 communicate with each other through the communication bus 604;
[0101] Memory 603, storing computer programs;
[0102] The processor 601 is configured to implement the above method when executing the computer program stored in the memory 603 .
[0103] The present disclosure provides a computer-readable storage medium storing a computer program, wherein the computer program implements the above method when executed by a processor.
[0104] The computer-readable storage medium may be included in the device / apparatus described in the above embodiment; or it may exist independently without being assembled into the device / apparatus. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.
[0105] According to an embodiment of the present disclosure, a computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.
[0106] Although the present disclosure has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein; and these modifications 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.
Claims
1. A JavaScript automatic error correction method, characterized in that: The method comprises: When the JavaScript engine assembler executes the assembly code, it saves the register value of the current execution environment when an exception is detected; By calling the preset runtime function, it is determined whether the current exception is caught. If the exception is not caught, the preset runtime function is called to determine the current exception type. When the current exception type is an object call, the register values of the saved execution environment are restored and an empty object implemented with built-in code is returned; When the current exception type is a function call, the register values of the saved execution environment are restored and the execution is jumped to the empty function implemented by the built-in code.
2. A JavaScript automatic error correction method according to claim 1, characterized in that: By calling the preset runtime function, it is determined whether the current exception has been caught, and then it also includes: When an exception is caught, the register values of the saved execution environment are restored and the execution logic of the assembler's original caught exception is jumped to.
3. A JavaScript automatic error correction method according to claim 1 or 2, characterized in that: Also includes: The exception information is reported and recorded through the preset runtime function for subsequent processing.
4. A JavaScript automatic error correction method according to claim 1, characterized in that: Save the register values of the current execution environment, including: The register values related to both the caller and the callee need to be saved.
5. A JavaScript automatic error correction method according to claim 1, characterized in that: Restore the register values of the saved execution environment, including: The register values relevant to both the caller and the callee need to be restored.
6. A JavaScript automatic error correction method according to claim 1, characterized in that: Calling pre-defined runtime functions, including: Use macro assembly to call the runtime function through the runtime function template name, and the return value of the function is saved in the register.
7. A JavaScript automatic error correction method according to claim 1, characterized in that: Built-in code, including: Use macro assembly to access built-in code through the name of the code template.
8. A JavaScript automatic error correction system, characterized in that: include: Anomaly detection module, scene protection module, anomaly capture and judgment module, anomaly type judgment module, anomaly handling module and scene recovery module; The exception detection module is used by the JavaScript engine assembler to detect whether an exception is thrown when executing the assembly code; The protection scene module is used to save the register value of the current execution environment when an exception is thrown; The exception capture judgment module is used to determine whether the current exception has been captured by calling a preset runtime function; The exception type judgment module is used to call the preset runtime function to judge the current exception type if the exception is not caught; The exception handling module is used to return an empty object implemented by built-in code when the current exception type is an object call; when the current exception type is a function call, jump to the empty function implemented by the built-in code for execution; The restore context module is used to restore the register values of the saved execution environment.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; a memory storing a computer program; A processor, for implementing a JavaScript automatic error correction method according to any one of claims 1 to 7 when executing a computer program stored in a memory.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, a JavaScript automatic error correction method according to any one of claims 1 to 7 is implemented.