Code conversion method and device

By identifying the original object and generating the corresponding class, the problem of low efficiency and high maintenance cost of prototype-based programming language code is solved, and it is realized to convert it into efficient and low maintenance cost-based class-based programming language code.

CN118733034APending Publication Date: 2024-10-01HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310382210.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-03-31
Publication Date
2024-10-01

AI Technical Summary

Technical Problem

Existing prototype-based programming language code has inefficient execution and high maintenance costs, and lacks an effective method to convert it into class-based programming language code.

Method used

By identifying the original object, judging whether it meets the class generation conditions, generating the corresponding class, and defining methods and implementation features in the class, such as dynamic field addition and prototype chain reading, the prototype-based programming language code is converted into a class-based one. Programming language code.

Benefits of technology

It realizes the conversion of prototype-based programming language code into efficient, low-maintenance cost class-based programming language code, improving execution efficiency and reducing maintenance costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118733034A_ABST
    Figure CN118733034A_ABST
Patent Text Reader

Abstract

The invention discloses a code conversion method and device, and belongs to the technical field of computers. The method comprises the steps that an object in a first code is recognized, and the first code is a code of a programming language based on a prototype; under the condition that it is determined that the object meets the class generation condition, a class corresponding to the object is generated in a second code, and the second code is a code of a programming language based on a prototype; and in the class corresponding to the object, defining the method defined in the object. By adopting the method and the device, the code of the programming language based on the prototype can be converted into the code of the programming language based on the class.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a method and device for switching a software system. Background Art

[0002] With the continuous development of computer technology, more and more programming languages ​​are available to technicians. At present, these programming languages ​​are mainly divided into prototype-based programming languages ​​and class-based programming languages.

[0003] Prototype-based programming languages ​​are widely used due to their advantages such as easy writing and deployment. However, prototype-based programming languages ​​have low execution efficiency and high maintenance costs, while class-based programming languages ​​have higher execution efficiency and lower maintenance costs. Therefore, there is an urgent need for a method to convert the code of a prototype-based programming language into the code of a class-based programming language. Summary of the invention

[0004] The present disclosure provides a method and device for code conversion, which can convert the code of a prototype-based programming language into the code of a class-based programming language. The corresponding technical solution is as follows:

[0005] In a first aspect, a method for code conversion is provided, the method comprising: identifying an original object in a first code, wherein the first code is a code of a prototype-based programming language. When it is determined that the original object meets a class generation condition, generating a class corresponding to the original object in a second code, wherein the second code is a code of a prototype-based programming language. In the class corresponding to the object, defining a method defined in the object.

[0006] In the solution provided by the present disclosure, an original object in a prototype-based programming language code is identified, and if the original object meets the class generation condition, a class corresponding to the original object is generated in the class-based programming language code, and the method defined in the object is implemented in the class corresponding to the object. In this way, the code of the prototype-based programming language is converted into the code of the class-based programming language.

[0007] In a possible implementation, the class generation condition may include:

[0008] If it is determined that a method is defined in the original object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0009] If a method is defined in the original object, it means that the original object is a complex object, so its corresponding class can be generated in the second code.

[0010] In a possible implementation, the class generation condition may include:

[0011] If it is determined that the original object has a prototype object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0012] If the original object has a prototype object, it means that the original object is a complex object, so its corresponding class can be generated in the second code.

[0013] In a possible implementation, the class generation condition may include:

[0014] If it is determined that the original object is a prototype object of other original objects in the first code, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0015] If the original object has a prototype object, it means that the original object is a complex object, so its corresponding class can be generated in the second code.

[0016] In a possible implementation, in order to realize the feature of dynamically adding fields to objects in a prototype-based programming language, in the solution provided by the present disclosure, this feature can be realized in the code of a class-based programming language in the form of a Map. Accordingly, the processing can be as follows:

[0017] In the class corresponding to the original object, create a mapping table Map, and use Map to store the fields in the original object.

[0018] In a possible implementation, in the class corresponding to the original object, an acquisition function may be defined to read the fields in the Map corresponding to the original object.

[0019] In a possible implementation, in order to implement the feature of reading fields according to the prototype chain in a prototype-based programming language, in the solution provided by the present disclosure, this feature can be implemented through the above-mentioned acquisition function. Accordingly, the processing can be as follows:

[0020] When it is recognized that the first code includes the first original object and the second original object, and the second original object is the prototype object of the first original object, when reading a field in the class object corresponding to the first original object, the acquisition function defined in the class corresponding to the first original object is called, and the first Map in the class corresponding to the first original object (that is, the Map of the class object corresponding to the first original object) reads the first field; if the first field is not read in the first Map, the acquisition function defined in the class corresponding to the second original object is called, and the first field is read from the second Map in the class corresponding to the second original object.

[0021] In one possible implementation, in order to realize the feature of dynamically adding fields to objects in a prototype-based programming language, in the solution provided by the present disclosure, a setting function is defined in the class corresponding to the original object to add or modify fields in the Map.

[0022] In a possible implementation, when it is necessary to add a field in the class object corresponding to the original object, a setting function defined in the class corresponding to the original object is called to add the second field in the Map in the class corresponding to the original object.

[0023] A class object is an instance of a class, which can be obtained by instantiating the class. The class object includes all the contents defined in the class.

[0024] In a possible implementation, in order to realize the features of the prototype object in a prototype-based programming language, in the solution provided by the present disclosure, a prototype setting function is defined in the class corresponding to the original object to set the prototype object of the class object corresponding to the original object. The prototype object of the class object corresponding to the original object is the class object of the prototype object of the original object.

[0025] In one possible implementation, when it is determined that the original object does not meet the class generation conditions, it means that the original object only contains some fields and is a simple object. In the solution provided in the present disclosure, a Map corresponding to the original object is created in the second code, and the fields in the original object are written into the Map corresponding to the original object.

[0026] In a second aspect, a device for code conversion is provided, the device comprising:

[0027] an identification module, configured to identify an original object in a first code, wherein the first code is a code in a prototype-based programming language;

[0028] A generation module, configured to generate a class corresponding to the original object in a second code when it is determined that the original object satisfies a class generation condition, wherein the second code is a code of a prototype-based programming language;

[0029] A definition module is used to define the method defined in the object in the class corresponding to the object.

[0030] In a possible implementation, the generating module is used to:

[0031] If it is determined that a method is defined in the original object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0032] In a possible implementation, the generating module is used to:

[0033] If it is determined that the original object has a prototype object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0034] In a possible implementation, the generating module is used to:

[0035] If it is determined that the original object is a prototype object of other original objects in the first code, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0036] In a possible implementation, the generating module is further used to:

[0037] In the class corresponding to the original object, create a mapping table Map;

[0038] The fields in the original object are stored through the Map.

[0039] In a possible implementation, the definition module is further used to:

[0040] In the class corresponding to the original object, an acquisition function is defined, wherein the acquisition function is used to read the field in the Map.

[0041] In a possible implementation, the identification module is used to:

[0042] Identify a first primitive object and a second primitive object in the first code, wherein the second primitive object is a prototype object of the first primitive object;

[0043] The device also includes a calling module, which is used to:

[0044] Calling an acquisition function defined in a class corresponding to the first original object, and reading a first field from a first Map in a class corresponding to the first original object; if the first field is not read in the first Map, calling an acquisition function defined in a class corresponding to the second original object, and reading the first field from a second Map in a class corresponding to the second original object.

[0045] In a possible implementation, the definition module is further used to:

[0046] In the class corresponding to the original object, a setting function is defined, wherein the setting function is used to add or modify a field in the Map.

[0047] In a possible implementation manner, the device further includes a calling module, configured to:

[0048] The setting function defined in the class corresponding to the original object is called, and the second field is added to the Map in the class corresponding to the original object.

[0049] In a possible implementation, the definition module is further used to:

[0050] In the class corresponding to the original object, a prototype setting function is defined, wherein the prototype setting function is used to set the prototype object of the class object corresponding to the original object, and the class object corresponding to the prototype object is an instance of the class corresponding to the prototype object.

[0051] In a possible implementation, the generating module is further used to:

[0052] When it is determined that the original object does not meet the class generation condition, creating a Map corresponding to the original object in the second code;

[0053] Write the fields in the original object into the Map corresponding to the original object.

[0054] In a third aspect, a computing device cluster is provided, which includes at least one computing device, each computing device including a processor and a memory, wherein the processor of at least one computing device is used to execute instructions stored in the memory of at least one computing device, so that the computing device cluster performs the method as described in the first aspect or in any achievable manner of the first aspect.

[0055] In a fourth aspect, a computer program product comprising instructions is provided. When the instructions are executed by a computing device cluster, the computing device cluster executes the method as described in the first aspect or in any implementable manner of the first aspect.

[0056] In a fifth aspect, a computer-readable storage medium is provided, comprising computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster can execute the method as described in the first aspect or in any achievable manner of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0057] Figure 1 is a schematic diagram of a code conversion provided by an embodiment of the present disclosure;

[0058] Figure 2 is a schematic diagram of a code conversion provided by an embodiment of the present disclosure;

[0059] Figure 3 is a schematic diagram of a code conversion provided by an embodiment of the present disclosure;

[0060] Figure 4 is a schematic diagram of a code conversion provided by an embodiment of the present disclosure;

[0061] Figure 5 is a schematic diagram of a code conversion provided by an embodiment of the present disclosure;

[0062] Figure 6 is a schematic diagram of a device structure for code conversion provided by an embodiment of the present disclosure;

[0063] Figure 7 is a schematic diagram of a computing device provided by an embodiment of the present disclosure;

[0064] Figure 8 is a schematic diagram of a computing device cluster provided by an embodiment of the present disclosure;

[0065] Fig. 9 It is a schematic diagram of a computing device cluster provided by an embodiment of the present disclosure. DETAILED DESCRIPTION

[0066] In order to facilitate the understanding of the embodiments provided by the present disclosure, some nouns or terms involved in the embodiments are explained below.

[0067] Prototype-based programming:

[0068] Prototype-based programming is a form of object-oriented programming. In prototype-based programming, behavior reuse (commonly known as inheritance in class-based programming) is achieved by copying an already existing prototype object. Prototype-based programming is generally considered to be classless, prototype-oriented, or instance-based programming. In prototype-based programming, methods are defined in objects, and fields in objects are not fixed and can be added dynamically. Programming languages ​​based on prototype programming are generally referred to as prototype-based programming languages. Exemplary, prototype-based programming languages ​​include JavaScript (a lightweight, interpreted or just-in-time compiled programming language with function priority), Lua (a lightweight scripting language), etc.

[0069] Class-based programming:

[0070] Class-based programming is a way of object-oriented programming. When designing a program, the category of the object is emphasized. In class-based programming, an object must be created based on a class, which is also an important difference between class-based programming and prototype-based programming. Objects created based on a class are regarded as instances of the class, also called class objects. The process of creating objects based on a class can be called instantiation of the class. Different instances of the same class are usually only allowed to have differences in state. Therefore, programs based on class programming have higher stability and higher security. In class-based programming, methods are defined in classes, and fields in class objects are usually fixed and cannot be added dynamically. Programming languages ​​based on class programming are usually called class-based programming languages. Exemplary, class-based programming languages ​​include C++ (a lightweight, interpreted or just-in-time compiled programming language with function priority), Java, etc.

[0071] Inheritance:

[0072] Inheritance is a concept in object-oriented programming. Inheritance allows a subclass to have various properties and methods of the parent class without having to write the same code again. When a subclass inherits the parent class, it can redefine some properties and override some methods, that is, overwrite the original properties and methods of the parent class, so that it has different functions from the parent class.

[0073] Scripting language:

[0074] Scripting languages ​​are computer programming languages ​​created to shorten the traditional "write, compile, link, run" process. Early scripting languages ​​were often called batch languages ​​or job control languages. A script is usually interpreted and run rather than compiled. Scripting languages ​​are usually simple, easy to learn, and easy to use, with the goal of allowing programmers to quickly complete program writing. The prototype-based programming language mentioned in this disclosure is a scripting language.

[0075] The present disclosure provides a method for code conversion, which can be implemented by a computing device, which can be a terminal device or a server. The terminal device can be a desktop computer, a laptop computer, etc. The computing device can be deployed in a single unit or in a cluster.

[0076] See also Figure 1 , which exemplarily shows an implementation scenario of the present disclosure, such as Figure 1 As shown, the prototype-based programming language is used as the source language and the class-based programming language is used as the target language. The code conversion method provided by the present disclosure can be applied to an offline scenario, and the computing device translates the source language code into the target language code offline. In addition, in this implementation scenario, the fields in the object in the source language code can be added dynamically, and the type is not fixed, while the methods in the object in the source language code cannot be added dynamically, and the prototype object of the object will not change.

[0077] For ease of description, in the following embodiments of the present disclosure, the source language is a prototype-based programming language, and the target language is a class-based programming language. In addition, in the embodiments provided by the present disclosure, code conversion can also be referred to as code translation, and correspondingly, converting the code of the source language into the code of the target language can also be referred to as translating the code of the source language into the code of the target language.

[0078] In the code conversion method provided by the present disclosure, a computing device identifies an object in a code written in a source language, and if the object meets the class generation condition, a class corresponding to the object is generated in the code written in a target language, and the method defined in the object is implemented in the class corresponding to the object. In this way, the code of a prototype-based programming language is converted into the code of a class-based programming language.

[0079] The code conversion method provided by the present disclosure is described below in conjunction with the accompanying drawings. Figure 2 , the processing flow of the method may include the following steps:

[0080] Step 201: Identify the original object in the first code.

[0081] The first code is a code to be translated and is based on a prototype programming language.

[0082] In an implementation, a computing device obtains a first code.

[0083] Exemplarily, the target device stores the first code, and upon receiving a code sending instruction sent by a user or a code acquisition request sent by a computing device, the target device can send the full first code to the computing device. The target device may be a server or a terminal device.

[0084] After acquiring the first code, the computing device performs a static scan on the first code to identify each object in the first code. In order to distinguish the objects from the converted class objects, in the embodiment of the present disclosure, the objects identified in the first code are referred to as original objects.

[0085] Step 202: For each original object in the first code, determine whether the original object meets the class generation condition.

[0086] In implementation, for each original object identified in the first code, the computing device determines whether the original object satisfies the class generation condition. Exemplarily, the computing device determines that the original object satisfies the class generation condition when determining that the original object satisfies any of the following conditions:

[0087] Condition 1: The original object defines the method.

[0088] Condition 2: The original object has a prototype object.

[0089] Condition three: The original object is the prototype object of other original objects in the first code.

[0090] With respect to the above condition 1, if there is a statement function() in the code corresponding to the original object, it is determined that a method is defined in the original object.

[0091] For the above conditions 2 and 3, if there is a prototype object setting statement in the code corresponding to the original object, it is determined that the prototype object has a prototype object. Exemplarily, if there is a statement index=base in the code corresponding to the original object, it is determined that the original object has a prototype object, and the original object base in the first code is the prototype object of the original object. Correspondingly, the original object base can also be determined to be the prototype object of other original objects.

[0092] Step 203: If it is determined that the original object meets the class generation condition, a class corresponding to the original object is generated in the second code, the method defined in the original object is implemented in the class corresponding to the original object, and the class is instantiated to generate a corresponding class object.

[0093] The second code is a code in the target programming language.

[0094] In implementation, when it is determined that the original object meets the class generation condition, a class corresponding to the original object is generated in the second code.

[0095] In the class corresponding to the original object, the methods defined in the original object (also called the functions defined in the original object), get functions, set functions, and prototype setting (setProto) functions are implemented, and a mapping table (Map) is constructed in the class.

[0096] In addition, a function unique to the source language may be defined in the original object, so an equivalent function of the function needs to be generated in the class corresponding to the original object. The equivalent function of the function is a function implemented in the target language and having the same function as the function. For example, taking Lua as the source language, a search (index) function unique to Lua may be defined in the original object.

[0097] The above-mentioned Map is used to store the fields in the original object. Exemplarily, the Map may be a hash map (HashMap);

[0098] The above get function is used to read the fields in the above Map;

[0099] The above set function is used to add or modify fields in the above Map;

[0100] The above setProto function is used to set the prototype object of the class object corresponding to the original object.

[0101] The following is an example to illustrate this step:

[0102] like Figure 3As shown, in the first code of the source language, there are two original objects, object A and object B, respectively, where object B is the prototype object of object A. Function A is defined in object A, and function A is defined in object B. In addition, assuming that the source language is Lua, a reading function indexing() can also be defined in object A and object B. When generating a class, an interface can be defined, which can be named proto. The proto interface declares the following functions:

[0103] get function, such as Figure 3 get(key: string): Any in the function takes a string as input and outputs any type.

[0104] set function, such as Figure 3 In set(key: string, value: Any), the key input to this function is a string and the value is of any type.

[0105] setProto function, such as Figure 3 setProto(proto:ProtoType) in .

[0106] The class generated based on object A is class A, and the class generated based on object B is class B. In class A, a Map needs to be constructed, such as Figure 3 In addition, class A also needs to implement the function A in object A, the index function indexing(), and the get function, set function, and setProto function declared in the interface proto. In class B, a Map needs to be constructed, such as Figure 3 In addition, class B also needs to implement the function B in object B, the function indexing(), and the get function, set function, and setProto function declared in the interface proto.

[0107] After the class corresponding to the original object is generated, the fields in the original object are written into the Map of the class corresponding to the original object. Then, the class is instantiated to generate the class object corresponding to the original object.

[0108] In addition, if there are multiple primitive objects belonging to the same prototype chain in the first code, when the class objects corresponding to these primitive objects are generated in the second code, the setProto function needs to be called to set the prototype objects corresponding to the primitive objects. Exemplarily, there are a first primitive object and a second primitive object in the first code, and the first primitive object is the prototype object of the second primitive object. When the second class object corresponding to the second primitive object is generated in the second code, the setProto function needs to be called to set the first class object corresponding to the second primitive object as the prototype object of the second class object.

[0109] In a possible implementation, when a field is read in the original object, it is read according to the prototype chain, and accordingly, an equivalent reading function also needs to be implemented in the second code.

[0110] For example, the first code includes three original objects, obj A, obj B and obj C, where obj B is the prototype object of objA, and obj C is the prototype object of obj B. Thus, obj A, obj B and obj C form a prototype chain. The following is an example of reading field C4 in obj A:

[0111] When reading field C4 in obj A, if field A is read in obj A, the value of field C4 is returned. If field C4 is not read in obj A, the prototype object of obj A is determined to be obj B, and then field C4 is read in obj B. If field C4 is read in obj B, the value of field C4 is returned. If field C4 is not read in obj B, the prototype object of objB is determined to be obj C, and then field C4 is read in obj C. If field C4 is read in obj C, the value of field C4 is returned. If field C4 is not read in obj C, it is determined whether the index function is defined in obj C. If the index function is defined in obj C, the index function is called and the corresponding value is returned. If the index function is not defined in obj C, it is determined whether the index function is defined in obj B. If the index function is defined in obj B, the index function is called and the corresponding value is returned. If the index function is not defined in obj B, determine whether the index function is defined in obj A. If the index function is defined in obj A, call the index function and return the corresponding value.

[0112] Combine the following Figure 4 , the above reading function implemented in the second code is explained:

[0113] exist Figure 4In the first code, three original objects, obj A, obj B and obj C, are included, where obj B is the prototype object of obj A and obj C is the prototype object of obj B. obj A includes four fields, A1=va1, A2=va2, A3=va3 and A4=va4. obj B includes two fields, B1=vb1 and B2=vb2. obj C includes three fields, C1=vc1, C2=vc2 and C3=vc3. obj A includes the statement setmetatable(objA, {__index=objB}), which indicates that obj B is the prototype object of obj A. obj B includes the statement setmetatable(objB, {__index=objC}), which indicates that obj C is the prototype object of obj B. obj C includes the following index function:

[0114]

[0115] The index function returns the value vc4 when the key is C4.

[0116] exist Figure 4 In the example, classobjA is the class object corresponding to obj A, classobjB is the class object corresponding to obj B, and classobjC is the class object corresponding to obj C. The HashMap in classobjA stores four fields in objA, the HashMap in classobjB stores two fields in objB, and the HashMap in classobjC stores three fields in objC. The get function, set function, and setProto function declared in the proto interface are implemented in classobjA, classobjA, and classobjC. In addition, the index function is also implemented in classobjC.

[0117] When reading field C4 in classobjA, the get function in classobjA is called to read it. Field C4 is not read, and the prototype object of classobjA is determined to be classobjB. Then, the get function in classobjB is called to read field C4 in classobjB. Field C4 is not read in classobjB either, and the prototype object of classobjB is determined to be classobjC. Then, the get function in classobjC is called to read field C4 in classobjC. Field C4 is still not read in classobjC, so the index function in classobjC is called, and the value vc4 is returned.

[0118] In one possible implementation, if a new field is dynamically added to an original object in the first code after the first code is translated into the second code, the set function can be called in the class object corresponding to the original object in the second code to add the new field to the Map of the class object.

[0119] For example, Figure 5 As shown, the field B3=vb3 is dynamically added to objB, and the set function in classobjB is called to add the field B3=vb3 to the HashMap of classobjB.

[0120] Step 204: If it is determined that the original object does not meet the class generation condition, a target data structure object corresponding to the original object is generated in the second code.

[0121] In implementation, the target data structure object may be a Map object, such as a HashMap object. If the original object does not meet the class generation condition, a Map structure may be generated in the second code to store the fields in the original object.

[0122] The code conversion method provided by the present disclosure is described below through an example. The method can be implemented by a computing device. In this example, the source language is Lua and the target language is C++. The first code example of the source language is as follows:

[0123]

[0124]

[0125] In the above first code example, two original objects are included, namely the base table object (base) and the extended table object (derived), wherein the base table object is the prototype object of the extended table object, and it can also be called the metatable (setmetatable) of the base table object as the prototype object of the extended table object.

[0126] The basic table object stores multiple fields, such as the employee's name (Name), number (ID), and work location (Location). The basic table object defines a sendContent function, which is used to write the value of a field to the database. In addition, the basic table object also defines an index function. In this example, when the index function is called, it returns "cannot find item for key".

[0127] In the extended object, the base table object is set as its metatable, and then the extended table object inherits the data and methods in the base table object.

[0128] Both the base table object and the extended table object can dynamically add and change fields. In the above example, lines 18 and 19 simulate database access to add the Age field to the extended table object and the Level field to the extended table object. After the addition is completed, all fields in the extended table object and the base table object can be accessed through the extended table object.

[0129] When accessing a field (height) that is not present in either the base table object or the extended table object, the index function defined in the base table will be triggered, resulting in "cannot find item for key".

[0130] In the extended table object, you can call the sendContent function defined in the base table object to write the value of a field into the database. In the above example, line 27 of the code simulates this function, which writes the value of the level field into the database.

[0131] An example of converting the above first code example into a second C++ code using the code conversion method provided by the present disclosure is as follows:

[0132] Step 601: Add the header files of the functions used in C++ and the standard namespace std used in the second code. The example is as follows:

[0133]

[0134]

[0135] In this example, only the get function and the set function are declared in the proto interface. The setProto function can be defined in the class object.

[0136] Step 603: Define a field search function and a field setting function in the second code. An example is as follows:

[0137]

[0138] Step 604: In the first code, the basic table object is the prototype object of the extended table object, and the basic table object is defined as

[0139]

[0140]

[0141]

[0142] Step 606: Create a Map corresponding to the basic table object in the second code, and write the fields in the basic table object into the Map. An example is as follows:

[0143]

[0144] Step 607: Instantiate the class corresponding to the basic table object, and use the Map created in step 606 to generate the class object corresponding to the basic table object. The example is as follows:

[0145] 83.base bs(initialValue); / / Instantiate the class corresponding to the base table object, and use the above Map to generate the class object corresponding to the base table object

[0146] Step 608: Instantiate the class corresponding to the extended table object, generate the class object corresponding to the extended table object, and call the setProto function defined in the class corresponding to the extended table object to set the prototype object of the class object corresponding to the basic table object as the prototype object of the class object corresponding to the extended table. The example is as follows:

[0147] 84.derived dr; / / Construct a class object corresponding to an empty extension table object

[0148] 85.dr.setProto(&bs); / / Call the setProto function defined in the class corresponding to the extended table object to set the prototype object of the class object corresponding to the basic table object to the prototype object of the class object corresponding to the extended table object

[0149] Step 609: When the Age field is dynamically added to the extended table object in the first code, the set function of the class object corresponding to the extended table object is called in the second code to write the Age field into the Map of the class object corresponding to the extended table object. The example is as follows:

[0150] 86.dr.set("Age",32); / / Call the set function defined in the class corresponding to the extended table object to add the Age field to the class object corresponding to the extended table object

[0151] Step 610: In the case where the Level field is dynamically added to the basic table object in the first code, the set function defined in the class corresponding to the basic table object is called in the second code to write the Level field into the Map of the class object corresponding to the basic table object. The example is as follows:

[0152] 87.bs.set("Level",string("Associate")); / / Call the set function defined in the class corresponding to the base table object to add the Level field to the class object corresponding to the base table object

[0153] Step 611: In the case where the Age field and the Level field are read in the extended table object in the first code, in the second code, in the class object corresponding to the extended table object, the get function defined in the class is called to read the value of the Age field and the value of the Level field. The example is as follows:

[0154] 88.any age = dr.get("Age"); / / Read the value of the Age field from the class object corresponding to the extended table object

[0155] 89.any level = dr.get("Level"); / / Read the value of the Level field from the class object corresponding to the extended table object, which ultimately comes from the basic table object

[0156] 90.cout<<"Age read from derived is"< <any_cast <int>(age) << endl; / / Print the value of the read Age field

[0157] 91. cout << "Level read from base is" << any_cast <string>(level) << endl; / / Print the value of the read Level field

[0158] Step 612, when reading the height field in the extended table object in the above first code, in the second code, in the class object corresponding to the extended table object, call the get function defined by this class to read the value of the height field. The example is as follows:

[0159] 92. any unexisted = dr.get("height"); / / Read the value of the height field from the class object corresponding to the extended table object. Since the field is not found in either the class object corresponding to the extended table object or the class object corresponding to the base table object, it will trigger the index function of the base table, and the return value is "cannot find item for key: height"

[0160] field, which will trigger the index function of the base table, and the return value is "cannot find item for key: height"

[0161] 93. cout << any_cast <string>(unexisted) << endl; / / Print the value of the obtained height field, which is

[0162] cannot find item for key: height

[0163] Step 613: When the sendContent function is called in the first code to write fields to the database, then in the second code, in the class object corresponding to the extended table object, call the sendContent function defined in this class. The example is as follows:

[0164] 94.dr.sendContent("Level"); / / Call the sendContent function defined in the class corresponding to the extended table object, and because of the inheritance relationship of the class, call the sendContent function defined in the class corresponding to the base table object

[0165] The following lists an example of converting the original object that does not meet the class conversion condition in the first code to the second code.

[0166] The example of the first code in the source language is as follows:

[0167]

[0168] In this first code example, there is an original object, which is a simple table object (simple). Multiple fields, such as Name, ID, and Location, are stored in the simple table object. In addition, in the above example, the Name field in the simple table object is also read.

[0169] Using the code conversion method provided by the present disclosure, the example of converting the above first code example to the second code in C++ is as follows:

[0170] Step 701: Add the header files of the functions used in C++ and the standard namespace std used in the second code. The example is as follows:

[0171]

[0172] Step 702: In the first code, if the simple table object does not define a method, has no prototype object, and is not the prototype object of other original objects, then in the second code, create a Map corresponding to the simple table object and write the fields in the simple table object into the created Map. The example is as follows:

[0173]

[0174] Step 703: When the first code reads the Name field of the simple table object, the second code implements reading the Name field in the Map of the created object. The example is as follows:

[0175]

[0176] Based on the same technical concept, the present disclosure also provides a device for code conversion, such as Figure 6 As shown, the device comprises:

[0177] An identification module 610, configured to identify an original object in a first code, wherein the first code is a code in a prototype-based programming language;

[0178] A generating module 620, configured to generate a class corresponding to the original object in a second code when it is determined that the original object satisfies a class generation condition, wherein the second code is a code of a prototype-based programming language;

[0179] The definition module 630 is used to define the method defined in the object in the class corresponding to the object.

[0180] In a possible implementation, the generating module 620 is configured to:

[0181] If it is determined that a method is defined in the original object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0182] In a possible implementation, the generating module 620 is configured to:

[0183] If it is determined that the original object has a prototype object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0184] In a possible implementation, the generating module is used to:

[0185] If it is determined that the original object is a prototype object of other original objects in the first code, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

[0186] In a possible implementation, the generating module 620 is further configured to:

[0187] In the class corresponding to the original object, create a mapping table Map;

[0188] The fields in the original object are stored through the Map.

[0189] In a possible implementation, the definition module 630 is further configured to:

[0190] In the class corresponding to the original object, an acquisition function is defined, wherein the acquisition function is used to read the field in the Map.

[0191] In a possible implementation, the identification module 610 is configured to:

[0192] Identify a first primitive object and a second primitive object in the first code, wherein the second primitive object is a prototype object of the first primitive object;

[0193] The device also includes a calling module, which is used to:

[0194] Calling an acquisition function defined in a class corresponding to the first original object, and reading a first field from a first Map in a class corresponding to the first original object; if the first field is not read in the first Map, calling an acquisition function defined in a class corresponding to the second original object, and reading the first field from a second Map in a class corresponding to the second original object.

[0195] In a possible implementation, the definition module 630 is further configured to:

[0196] In the class corresponding to the original object, a setting function is defined, wherein the setting function is used to add or modify a field in the Map.

[0197] In a possible implementation manner, the device further includes a calling module, configured to:

[0198] The setting function defined in the class corresponding to the original object is called, and the second field is added to the Map in the class corresponding to the original object.

[0199] In a possible implementation, the definition module 630 is further configured to:

[0200] In the class corresponding to the original object, a prototype setting function is defined, wherein the prototype setting function is used to set the prototype object of the class object corresponding to the original object, and the class object corresponding to the prototype object is an instance of the class corresponding to the prototype object.

[0201] In a possible implementation, the generating module 620 is further configured to:

[0202] When it is determined that the original object does not meet the class generation condition, creating a Map corresponding to the original object in the second code;

[0203] Write the fields in the original object into the Map corresponding to the original object.

[0204] The code conversion device provided in the above embodiment only uses the division of the above functional modules as an example when performing cloud resource processing. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the computing device is divided into different functional modules to complete all or part of the functions described above. In addition, the code conversion device provided in the above embodiment and the code conversion method embodiment belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.

[0205] The present disclosure also provides a computing device 100. Figure 7 As shown, the computing device 100 includes: a bus 102, a processor 104, a memory 106, and a communication interface 108. The processor 104, the memory 106, and the communication interface 108 communicate with each other through the bus 102. The computing device 100 can be a server or a terminal device. It should be understood that the present disclosure does not limit the number of processors and memories in the computing device 100.

[0206] The bus 102 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 The bus 102 is represented by only one line, but it does not mean that there is only one bus or one type of bus. The bus 102 may include a path for transmitting information between various components of the computing device 100 (eg, the memory 106, the processor 104, and the communication interface 108).

[0207] The processor 104 may include any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0208] The memory 106 may include a volatile memory, such as a random access memory (RAM). The memory 106 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).

[0209] The memory 106 stores executable program codes, and the memory 106 executes the executable program codes to respectively implement the functions of the aforementioned identification module 610, generation module 620 and definition module 630, thereby implementing the code conversion method. That is, the memory 106 stores instructions for executing the code conversion method.

[0210] Alternatively, the memory 106 stores executable codes, and the processor 104 executes the executable codes to respectively implement the functions of the aforementioned code conversion device, thereby implementing the code conversion method. That is, the memory 106 stores instructions for executing the method for code conversion of cloud services.

[0211] The communication interface 108 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 100 and other devices or a communication network.

[0212] The present disclosure also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device may be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device may also be a terminal device such as a desktop computer, a laptop computer, or a smart phone.

[0213] like Figure 8 As shown, the computing device cluster includes at least one computing device 100. The memory 106 in one or more computing devices 100 in the computing device cluster may store the same instructions for executing the code conversion method.

[0214] In some possible implementations, the memory 106 of one or more computing devices 100 in the computing device cluster may also store partial instructions for executing the method for performing chaos exercises on cloud services. In other words, the combination of one or more computing devices 100 may jointly execute instructions for executing the method for performing chaos exercises on cloud services.

[0215] It should be noted that the memory 106 in different computing devices 100 in the computing device cluster may store different instructions, which are respectively used to execute part of the functions of the device for code conversion. That is, the instructions stored in the memory 106 in different computing devices 100 may implement the functions of one or more modules in the identification module 610, the generation module 620 and the definition module 630.

[0216] In some possible implementations, one or more computing devices in the computing device cluster may be connected via a network, which may be a wide area network or a local area network. Figure 7 A possible implementation is shown. Figure 7 As shown, two computing devices 100A and 100B are connected via a network. Specifically, the network is connected via a communication interface in each computing device. In this type of possible implementation, the memory 106 in the computing device 100A stores instructions for executing the functions of the identification module 610. At the same time, the memory 106 in the computing device 100B stores instructions for executing the functions of the generation module 620 and the definition module 630.

[0217] Fig. 9 The connection method between the computing device clusters shown may be that considering that the method for performing chaos training on cloud services provided by the present disclosure requires a large amount of computing resources, it is considered to hand over the functions implemented by the identification module 610 to the computing device 100A for execution.

[0218] It should be understood that Fig. 9 The functions of the computing device 100A shown in FIG. 1 may also be completed by multiple computing devices 100. Similarly, the functions of the computing device 100B may also be completed by multiple computing devices 100.

[0219] The present disclosure also provides another computing device cluster. The connection relationship between the computing devices in the computing device cluster can be similar to that of Figure 8 and Fig. 9 The connection mode of the computing device cluster is different in that the memory 106 in one or more computing devices 100 in the computing device cluster may store the same instructions for performing the code conversion method.

[0220] In some possible implementations, the memory 106 of one or more computing devices 100 in the computing device cluster may also store partial instructions for executing the method of code conversion. In other words, the combination of one or more computing devices 100 can jointly execute the instructions for executing code conversion.

[0221] The present disclosure also provides a computer program product including instructions. The computer program product may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computing device, the at least one computing device is caused to perform a code conversion method.

[0222] The present disclosure also provides a computer-readable storage medium. The computer-readable storage medium may be any available medium that can be stored by a computing device or a data storage device such as a data center that includes one or more available media. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions that instruct the computing device to perform a code conversion method.

[0223] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention 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. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of the present invention.< / string> < / string> < / int>

Claims

1. A method of code conversion, It is characterized in that The method comprises: identifying a primitive object in a first code, wherein the first code is a code in a prototype-based programming language; In the case where it is determined that the original object satisfies the class generation condition, generating a class corresponding to the original object in a second code, wherein the second code is a code of a prototype-based programming language; In the class corresponding to the object, the method defined in the object is defined.

2. The method according to claim 1, It is characterized in that In the case where it is determined that the original object satisfies the class generation condition, generating a class corresponding to the original object in the second code includes: If it is determined that a method is defined in the original object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

3. The method according to claim 1, It is characterized in that In the case where it is determined that the original object satisfies the class generation condition, generating a class corresponding to the original object in the second code includes: If it is determined that the original object has a prototype object, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

4. The method according to claim 1, It is characterized in that In the case where it is determined that the original object satisfies the class generation condition, generating a class corresponding to the original object in the second code includes: If it is determined that the original object is a prototype object of other original objects in the first code, it is determined that the original object meets the class generation condition, and a class corresponding to the original object is generated in the second code.

5. The method according to any one of claims 1 to 4, It is characterized in that The method further comprises: In the class corresponding to the original object, create a mapping table Map; The fields in the original object are stored through the Map.

6. The method according to claim 5, It is characterized in that The method further comprises: In the class corresponding to the original object, an acquisition function is defined, wherein the acquisition function is used to read the field in the Map.

7. The method according to claim 6, It is characterized in that The identifying the original object in the first code includes: Identify a first primitive object and a second primitive object in the first code, wherein the second primitive object is a prototype object of the first primitive object; The method further comprises: Calling an acquisition function defined in a class corresponding to the first original object, and reading a first field from a first Map in a class corresponding to the first original object; if the first field is not read in the first Map, calling an acquisition function defined in a class corresponding to the second original object, and reading the first field from a second Map in a class corresponding to the second original object.

8. The method according to any one of claims 5 to 7, It is characterized in that The method further comprises: In the class corresponding to the original object, a setting function is defined, wherein the setting function is used to add or modify a field in the Map.

9. The method according to claim 8, It is characterized in that The method further comprises: The setting function defined in the class corresponding to the original object is called, and the second field is added to the Map in the class corresponding to the original object.

10. The method according to any one of claims 5 to 7, It is characterized in that The method further comprises: In the class corresponding to the original object, a prototype setting function is defined, wherein the prototype setting function is used to set the prototype object of the class object corresponding to the original object, and the class object corresponding to the prototype object is an instance of the class corresponding to the prototype object.

11. The method according to any one of claims 1 to 10, It is characterized in that The method further comprises: When it is determined that the original object meets the class generation condition, creating a Map corresponding to the original object in the second code; Write the fields in the original object into the Map corresponding to the original object.

12. A device for code conversion, It is characterized in that The device comprises: an identification module, configured to identify an original object in a first code, wherein the first code is a code in a prototype-based programming language; A generation module, configured to generate a class corresponding to the original object in a second code when it is determined that the original object satisfies a class generation condition, wherein the second code is a code of a prototype-based programming language; A definition module is used to define the method defined in the object in the class corresponding to the object.

13. A computing device cluster, It is characterized in that comprising at least one computing device, each computing device comprising a processor and a memory; The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the method according to any one of claims 1 to 10.

14. A computer program product comprising instructions, It is characterized in that When the instructions are executed by a computing device cluster, the computing device cluster executes the method according to any one of claims 1 to 10.

15. A computer-readable storage medium, It is characterized in that The method comprises computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster performs the method according to any one of claims 1 to 10.