> ## Documentation Index
> Fetch the complete documentation index at: https://docs.js-confuser.com/llms.txt
> Use this file to discover all available pages before exploring further.

# RGF

> RGF (Runtime-Generated-Functions) creates executable code from strings.
        - **This can break your code.**
        - **Due to the security concerns of arbitrary code execution, you must enable this yourself.**
        - The arbitrary code is also obfuscated.

* Option name: `"rgf"`

* Option values: `true/false/0-1/Function`

<Card title="Requires Eval" type="warning">
  The obfuscated code will contain unsafe eval expressions.
  The code will not work properly in [environments that have disabled eval](https://developer.mozilla.org/en-US/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_eval_expressions).
</Card>

***

### Input / Output

This example showcases how `RGF` transforms the code. Try it out by changing the input code and see changes apply in real-time.

```js title="Input.js" lines interactive-mode="obfuscate" theme={null}
// Input.js
function printToConsole(message) {
  console.log(message);
}

printToConsole('Hello World'); // "Hello World"

// Output.js
function ZQIWtZJ(ZQIWtZJ = !0) {
  return ZQIWtZJ;
}
var zQZeULs = ZQIWtZJ(),
  YqeQsOH = [
    mc2oyOd(
      'function gw49xP(){var[gw49xP,Syn9bRU]=arguments;function Eaw6eWv(gw49xP){console.log(gw49xP)}return Eaw6eWv.apply(this,Syn9bRU)}gw49xP;',
    ),
  ];
function F01ISEq() {
  return YqeQsOH[0].apply(this, [YqeQsOH, arguments]);
}
function mc2oyOd(ZQIWtZJ) {
  if (zQZeULs) {
    return eval(ZQIWtZJ);
  }
}
F01ISEq('Hello World');
```

***

### Independent Functions

RGF will only transform functions that are independent of their scope. A function referencing a variable outside of its scope disqualifies it from being transformed.

If you enable [Flatten](./flatten), you can isolate functions from their original scope so then RGF can then apply on them. This is the recommended way to use RGF.

***

### Custom Implementation

#### `options.rgf(fnName)`

Control which functions are changed. Returns a `boolean`.

| Parameter | Type     | Description                               |
| --------- | -------- | ----------------------------------------- |
| `fnName`  | `string` | The function name proposed to be changed. |

***

### Usage Example

The provided code example will obfuscate the file `input.js` and write the output to a file named `output.js`.

```js title="Usage Example" lines theme={null}
import JSConfuser from "js-confuser";
import {readFileSync, writeFileSync} from "fs";

// Read input code
const sourceCode = readFileSync("input.js", "utf8");
const options = {
  target: 'browser',
  rgf: true,
};

JSConfuser.obfuscate(sourceCode, options).then((result)=>{
  // Write output code
  writeFileSync("output.js", result.code);
}).catch(err=>{
  // Error occurred
  console.error(err);  
});
```

***

#### Enabled In

* [High Preset](/presets/high): No
* [Medium Preset](/presets/medium): No
* [Low Preset](/presets/low): No

***

### Other notes

RGF only applies to:

* Function Declarations or Expressions
* Cannot be async / generator function
* Cannot rely on outside-scoped variables
* Cannot use `this`, `arguments`, or `eval`

***

#### See Also

* [Flatten](./flatten)
* [String Concealing](./stringConcealing)
