1# Naming Conventions for Ark Bytecode Functions 2 3## Overview 4This topic describes the naming conventions for the strings pointed to by the **name_off** field in [methods](arkts-bytecode-file-format.md#method) of bytecode files. These conventions apply to Ark bytecode files of version 12.0.4.0 or later. 5## Entry Function 6Function executed during module loading. The function name is fixed at **func_main_0**. 7## Non-Entry Function 8The naming structure for other functions in the bytecode file is as follows: 9```ts 10##prefix#original_function_name 11``` 12The following sections describe the prefix and original function name in detail. 13### Prefix 14The prefix includes information about the scope where the function is defined. It consists of the following parts: 15* Scope tag 16* Scope name 17* Renaming index (if applicable) 18 19The prefix structure is as follows: 20```ts 21<Scope tag 1><Scope name 1>[<Renaming index>]<Scope tag 2><Scope name 2><[Renaming index]>...<Scope tag n><Scope name n>[<Renaming index >]<Scope tag n+1> 22``` 23Here, angle brackets (< >) are delimiters for readability and are not part of the actual prefix, and square brackets ([ ]) indicates optional elements. [\<Renaming index>] is only included if there are duplicate scope names and can be empty otherwise. The last scope tag corresponds to the function itself. 24#### Scope Tag 25Scope tags indicate the type of scope. The table below lists the scopes and their corresponding tags. Other scopes are not included in function names. 26| Scope| Scope Tag| Description| 27| --- | --- | --- | 28| class | `~` | Scope defined by the **class** keywords.| 29| Instance function| `>` | Scope defined by the instance member functions in a class.| 30| Static function| `<` | Scope defined by the static functions in a class.| 31| Constructor function| `=` | Scope defined by the constructors in a class.| 32| Ordinary function| `*` | Scope 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 36Scope names indicate the name used to define the scope in the source code. If the scope is anonymous, it is an empty string. To reduce the bytecode size, the Ark compiler optimizes longer scope names, representing them as @ followed by a hexadecimal number. This hexadecimal number is 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 a [LiteralArray](arkts-bytecode-file-format.md#literalarray) storing the 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", where "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#### Renaming Index 45If the source code contains entities with the same name in the same scope, a renaming index is appended to the duplicate names. The renaming index is represented as ^ followed by a hexadecimal number. For the first occurrence, no index is added (the renaming index is empty), and subsequent occurrences start 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 renaming index. 55} 56``` 57### Original Function Name 58The original function name represents the name of the function in the source code. For anonymous functions, it is an empty string. Similarly, if there are duplicate function names in the same scope, a renaming index is appended to the duplicate names, including anonymous functions. 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 Cases 671. If an anonymous function is assigned to a variable, 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 assigned to a literal property, the following cases are possible: 72* If the property name does not contain a slash (/\) or a period (.), the original function name is the property name. 73 ```ts 74 let B = { 75 b : () => {} // The original function name is "b". 76 } 77 ``` 78* If the property name contains a slash (/\) or a period (.), the original function name follows the naming convention for anonymous functions to avoid 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 (_) in function names to avoid ambiguity.** 87## Examples 88```ts 89namespace A { // The function name of the 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 the function 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 the static function s in bytecode is "#&A~B<#s". 95 } 96 enum E { // The function name of the enum in bytecode is "#&A %#E". 97 98 } 99} 100``` 101```ts 102namespace LongNamespaceName { // The function name of the namespace in bytecode is "#&#LongNamespaceName". 103 class LongClassName { // The function name of the constructor function in bytecode is "#&@1~@0=#LongClassName". 104 longFunctionName() { // The function name of the instance function in bytecode is "#&@1~@0>#longFunctionName". 105 } 106 longFunctionName() { // The function name of the function in bytecode is "#&@1~@0>#longFunctionName^1". 107 function inSecondFunction() {} // The function name of the function in bytecode is "#&@1~@0>@2^1*#inSecondFunction". 108 } 109 } 110} 111``` 112