1# Naming Rules of Ark Bytecode Functions
2
3## Abstract
4This topic describes the naming rules of the string pointed by the **name_off** field of [Method](arkts-bytecode-file-format.md#method) in the bytecode file. These rules take effect since the Ark Bytecode in version **12.0.4.0**.
5## Entry Point Function
6Function that is executed when the module is loaded. The function name is fixed to **func_main_0**.
7## Non-entry Point Function
8The name structure of other functions in the bytecode file is as follows:
9```ts
10##Prefix#Original function name
11```
12The following sections will describe the prefix and original function name in detail.
13### Prefix
14The prefix contains the scope information when the function is defined. It consists of the following parts:
15* Scope Label
16* Scope Name
17* Duplicate Sequence Number
18
19The prefix structure is as follows:
20```ts
21<Scope Label 1><Scope Name 1>[<Duplicate Sequence Number>]<Scope Label 2><Scope Name 2><[Duplicate Sequence Number]>...<Scope Label n><Scope Name n>[<Duplicate Sequence Number >]<Scope Label n+1>
22```
23In this case, angle brackets (< >) are separators for easy reading and are excluded from the actual prefix; square brackets ([ ]) indicate that the value can be empty. [\<Duplicate Sequence Number>] is required only when duplicate scopes exist, which indicates that [\<Duplicate Sequence Number>] can be empty. The last scope label is the one corresponding to the function.
24#### Scope Label
25The scope label indicates the scope type. The following table lists the scopes and corresponding labels. Other scopes are not recorded in the function name.
26| Scope| Scope Label| Description|
27| --- | --- | --- |
28| Class| `~` | Scope defined by the **class** keywords.|
29| Instance function| `>` | Scope defined by the instance functions of a class|
30| Static function| `<` | Scope defined by the static functions of a class|
31| Constructor used to create a worker object.| `=` | Scope defined by the constructors of a class|
32| Common function| `*` | Scopes defined by all functions except the preceding types|
33| namespace/module | `&` | Scope defined by the **namespace** or **module** keywords|
34| enum | `%` | Scope defined by the **enum** keywords|
35#### Scope Name
36The name used to define the scope in the source code. If the value is anonymous, the value is an empty string. To reduce the bytecode size, the ArkCompiler optimizes long scope names. In this case, the scope names are displayed in the format of **@hexadecimal number**. This number indicates the index of the scope name in a string array. In the bytecode file, there is a [field](arkts-bytecode-file-format.md#field) named **ScopeNames** in the [Class](arkts-bytecode-file-format.md#class) corresponding to the source code. This **field** value points to an offset of [LiteralArray](arkts-bytecode-file-format.md#literalarray) which stores a string array. The hexadecimal number is the index of the scope name in this array. The original function name is not converted to an index.
37Example:
38```ts
39function longFuncName() {                  // The function name of longFuncName is "#*#longFuncName", in which "longFuncName" is the original function name and will not be converted to an index.
40    function A() { }                       // The function name of A is "#*@0*#A", where "@0" indicates the string whose index is 0 in the corresponding LiteralArray. In this case, the string is "longFuncName", which means that the original name of this function is "#*longFuncName*#A".
41    function B() { }                       // The function name of B is "#*@0*#B".
42}
43```
44#### Duplicate Sequence Number
45If an entity with the same name exists in the same scope within the source code, the entity name is suffixed with a duplicate sequence number. This number is written in the format of **^ hexadecimal number**. If duplicate names exist, the first one is not numbered, that is, the duplicate sequence number is empty. And the entities are numbered from the second one, starting from **1**.
46
47Example:
48```ts
49namespace A {
50    function bar() { }                      // The function name of bar is "#&A*#bar".
51}
52
53namespace A {
54    function foo() { }                      // The function name of foo is "#&A^1*#foo", where "^1" indicates the duplicate sequence number.
55}
56```
57### Original Function Name
58It indicates function name in the source code. For an anonymous function, the value is an empty string. Similarly, if a function with the same name exists in the same scope within the source code, the function name is followed by a duplicate sequence number, so does an anonymous function.
59
60```ts
61function foo() {}                           // The original function name is "foo".
62() => { }                                   // The original function name is "".
63() => { }                                   // The original function name is "^1".
64```
65
66#### Special Situations
671. If an anonymous function is stored in a variable when the function is defined, the original function name is the variable name. An example is as follows:
68    ```ts
69    let a = () => {}                            // The original function name is "a".
70    ```
712. If an anonymous function is defined in an object literal and stored in a literal attribute:
72* If the attribute name does not contain a **slash (\)** or a **period (.)**, the original function name is the attribute name.
73    ```ts
74    let B = {
75        b : () => {}                            // The original function name is "b".
76    }
77    ```
78* If the attribute name contains a **slash (\)** or a **period (.)**, the original function is named as an anonymous function to prevent ambiguity.
79    ```ts
80    let a = {
81        "a.b#c^2": () => {}                     // The original function name is "".
82        "x\\y#": () => {}                       // The original function name is "^1".
83    }
84    ```
85
86**You should avoid using characters other than letters, digits, and underscores (_) to name functions to avoid ambiguity.**
87## Example
88```ts
89namespace A {                               // The function name of namespace in bytecode is "#&#A".
90    class B {                               // The function name of the constructor in bytecode is "#&A~B=#B".
91        m() {                               // The function name of m in bytecode is "#&A~B>#m".
92            return () => {}                 // The function name of the anonymous function in bytecode is "#&A~B>m*#".
93        }
94        static s() {}                       // The function name of static function s in bytecode is "#&A~B<#s".
95    }
96    enum E {                                // The function name of enum in bytecode is "#&A %#E".
97
98    }
99}
100```
101```ts
102namespace LongNamespaceName {               // The function name of namespace in bytecode is "#&#LongNamespaceName".
103    class LongClassName {                   // The function name of the constructor in bytecode is "#&@1~@0=#LongClassName".
104        longFunctionName() {                // The function name of the instance function in the bytecode is "#&@1~@0>#longFunctionName".
105        }
106        longFunctionName() {                // The function name in bytecode is "#&@1~@0>#longFunctionName^1".
107            function inSecondFunction() {}  // The function name in bytecode is "#&@1~@0>@2^1*#inSecondFunction".
108        }
109    }
110}
111```
112