# Native Functions

### Native Functions, Explained

Defined in `./src/types/types.d.ts`, a C0Native object represents a native function in the C0 Virtual Machine that can be called during the runtime.

```typescript
type C0Native = {
    numArgs: number;
    readonly functionType: C0NativeFuncType;
    readonly f: (hooks: ReactUIHook, mem: C0HeapAllocator, ...args: C0Value<C0TypeClass>[]) => 
        C0Value<C0TypeClass>;
};
```

Specifically, the property of `f` represents the code of native function that can be dynamically loaded during runtime.

The function header of `f` is defined as

```typescript
type f: (hooks: ReactUIHook, mem: C0HeapAllocator, ...args: C0Value<C0TypeClass>[]) => 
        C0Value<C0TypeClass>;
```

This means `f` will take a bunch of react hooks to update the react state (e.g. printout value), a memory allocator (to allocate or extract value from heap memory), and several (1 to more) C0Value arguments.

### Implementation Progress

In original C0VM, a bunch of native functions are implemented in C and connected to C0VM to support the system calls (e.g. receive user input, etc.).

The list of native functions is listed below:

> :white\_check\_mark: - Currently Support
>
> :hourglass: - Development in Progress, will be supported
>
> :x: - No Recent Plan for implementation

#### Command Line Arguments :hourglass:

| Native Functions     | Support?    |
| -------------------- | ----------- |
| NATIVE\_ARGS\_FLAG   | :hourglass: |
| NATIVE\_ARGS\_INT    | :hourglass: |
| NATIVE\_ARGS\_PARSE  | :hourglass: |
| NATIVE\_ARGS\_STRING | :hourglass: |

#### Standard I/O :white\_check\_mark:

| Native Functions  | Support?                                                                                    |
| ----------------- | ------------------------------------------------------------------------------------------- |
| NATIVE\_EOF       | ​​:white\_check\_mark: (Always return `true` since there's no file read in C0VM.ts)         |
| NATIVE\_FLUSH     | :white\_check\_mark: (Always return `true` since there's no "buffering" in React UI Update) |
| NATIVE\_PRINT     | :white\_check\_mark:                                                                        |
| NATIVE\_PRINTBOOL | :white\_check\_mark:                                                                        |
| NATIVE\_PRINTCHAR | :white\_check\_mark:                                                                        |
| NATIVE\_PRINTINT  | :white\_check\_mark:                                                                        |
| NATIVE\_PRINTLN   | :white\_check\_mark:                                                                        |
| NATIVE\_READLINE  | :white\_check\_mark:                                                                        |

#### Cursor :x:

> There are no recent plans to implement native functions in this category (`NATIVE_INDEX = [12, 54]`)

#### Double Arithmetic :x:

> We will not support this since there is no `double` type in C0/C1 language. (`NATIVE_INDEX=[55, 62)`)

#### File I/O :x:

> There are no recent plans to implement native functions in this category (`NATIVE_INDEX=[62, 67)`)

#### Float Arithmetic :x:

> We will not support this since there is no `float` type in C0/C1 language. (`NATIVE_INDEX=[67, 77)`)

| Native Functions   | Support?             |
| ------------------ | -------------------- |
| NATIVE\_PRINT\_HEX | :white\_check\_mark: |
| NATIVE\_PRINT\_INT | :white\_check\_mark: |

#### Image :x:

> There are no recent plans to implement native functions in this category (`NATIVE_INDEX = [77, 85)`)

#### Parse String :white\_check\_mark:

| Native Functions      | Support?             |
| --------------------- | -------------------- |
| NATIVE\_INT\_TOKENS   | :white\_check\_mark: |
| NATIVE\_NUM\_TOKENS   | :white\_check\_mark: |
| NATIVE\_PARSE\_BOOL   | :white\_check\_mark: |
| NATIVE\_PARSE\_INT    | :white\_check\_mark: |
| NATIVE\_PARSE\_INTS   | :white\_check\_mark: |
| NATIVE\_PARSE\_TOKENS | :white\_check\_mark: |

#### String Operations :white\_check\_mark:

| Native Functions                | Support?             |
| ------------------------------- | -------------------- |
| NATIVE\_CHAR\_CHR               | :white\_check\_mark: |
| NATIVE\_CHAR\_ORD               | :white\_check\_mark: |
| NATIVE\_STRING\_CHARAT          | :white\_check\_mark: |
| NATIVE\_STRING\_COMPARE         | :white\_check\_mark: |
| NATIVE\_STRING\_EQUAL           | :white\_check\_mark: |
| NATIVE\_STRING\_FROM\_CHARARRAY | :white\_check\_mark: |
| NATIVE\_STRING\_FROMBOOL        | :white\_check\_mark: |
| NATIVE\_STRING\_FROMCHAR        | :white\_check\_mark: |
| NATIVE\_STRING\_FROMINT         | :white\_check\_mark: |
| NATIVE\_STRING\_JOIN            | :white\_check\_mark: |
| NATIVE\_STRING\_LENGTH          | :white\_check\_mark: |
| NATIVE\_STRING\_SUB             | :white\_check\_mark: |
| NATIVE\_STRING\_TERMINATED      | :white\_check\_mark: |
| NATIVE\_STRING\_TO\_CHARARRAY   | :white\_check\_mark: |
| NATIVE\_STRING\_TOLOWER         | :white\_check\_mark: |

