Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tinybiggames/cscript

C99 Scripting Engine
https://github.com/tinybiggames/cscript

c-embedded-in-delphi c-script c99 delphi dll exe lib pascal win64 windows10 windows11

Last synced: 2 days ago
JSON representation

C99 Scripting Engine

Awesome Lists containing this project

README

        

![CScript](media/cscript.png)
[![Chat on Discord](https://img.shields.io/discord/754884471324672040?style=for-the-badge)](https://discord.gg/tPWjMwK)
[![Follow on Bluesky](https://img.shields.io/badge/Bluesky-tinyBigGAMES-blue?style=for-the-badge&logo=bluesky)](https://bsky.app/profile/tinyBigGAMES.bsky.social)

# ๐Ÿ…ฒ๐Ÿ†‚cript

**๐Ÿ…ฒ๐Ÿ†‚cript** represents a sophisticated ๐Ÿ…ฒ99 scripting engine meticulously crafted for experienced ๐Ÿง‘โ€๐Ÿ’ป Delphi developers. Leveraging the compact yet highly versatile **TinyCC** compiler, **๐Ÿ…ฒ๐Ÿ†‚cript** seamlessly integrates โš™๏ธ dynamic backend code generation into the Delphi development environment. With **๐Ÿ…ฒ๐Ÿ†‚cript**, Delphi developers can ๐Ÿ› ๏ธ compile and execute ๐Ÿ…ฒ scripts at runtime, directly in ๐Ÿ’พ memory, and generate output in the form of ๐ŸชŸ Win64 executables (.exe), dynamic-link libraries (.dll), or ๐Ÿ“‚ object files (.obj).

### โœจ Features

- **โš™๏ธ Dynamic Code Generation**: Facilitates runtime ๐Ÿ› ๏ธ compilation and execution of ๐Ÿ…ฒ99 code directly within the Delphi application context.
- **๐Ÿ–ฅ๏ธ Versatile Output Options**: Supports in-memory execution alongside output to executable, DLL, or object file formats.
- **๐Ÿ“ฆ Effortless Integration with ๐Ÿ…ฒ Libraries**: Capable of loading and interacting with compiled modules, including `.dll`, `.a`, and `.lib` files.
- **๐Ÿ“‚ Embedded Resources**: TinyCC runtime components (.h, .a, .o files) are encapsulated within a compressed zip resource and linked directly into your Delphi executable, thus minimizing ๐Ÿ—‚๏ธ file system dependencies while ensuring runtime availability.
- **โš ๏ธ Customizable Error Handling**: Offers the ability to assign custom error handlers, streamlining ๐Ÿž debugging and enabling robust error management.
- **๐Ÿ” Full Symbol Access**: Provides direct access to symbols generated by the compiled code and to symbols within dynamically loaded modules, simplifying the integration of complex functionalities.

### ๐Ÿ› ๏ธ How It Works

**๐Ÿ…ฒ๐Ÿ†‚cript** employs **TinyCC**, which is statically linked into the Delphi runtime, to compile and execute ๐Ÿ…ฒ code dynamically. All critical ๐Ÿ…ฒ runtime I/O operationsโ€”such as `open`, `close`, `read`, and `lseek`โ€”are re-routed to Delphi-native implementations. Typically, TinyCC would require runtime files to be available separately; however, in **๐Ÿ…ฒ๐Ÿ†‚cript**, these files are stored within the Delphi executable itself as compressed resources, resulting in a ๐Ÿ“ฆ self-contained virtualized I/O model. When **TinyCC** requests access to runtime files, a custom Delphi I/O handler checks the embedded zip archive, thereby ensuring a seamless, efficient, and isolated execution environment.

### ๐Ÿงฉ Integration with Delphi

**๐Ÿ…ฒ๐Ÿ†‚cript** functions as a conduit to harness the capabilities of ๐Ÿ…ฒ99 code within the Delphi development framework. Whether it involves optimizing particular operations for โšก enhanced performance, interfacing with pre-existing ๐Ÿ…ฒ libraries, or augmenting Delphi's feature set with the expressive capabilities of ๐Ÿ…ฒ, **๐Ÿ…ฒ๐Ÿ†‚cript** provides a cohesive and sophisticated development toolset.

### ๐Ÿ—๏ธ Key Classes and Types

**TCScript** is the primary interface for engaging with **๐Ÿ…ฒ๐Ÿ†‚cript** functionalities. Below is an overview of its core components:

#### ๐Ÿ…ƒCScriptOutputType
- `csMEMORY`: ๐Ÿ› ๏ธ Compile and execute scripts entirely in memory.
- `csLib`: Generate ๐Ÿ“‚ object files (`.obj`).
- `csEXE`: Generate a ๐ŸชŸ Win64 executable.
- `csDLL`: Generate a ๐Ÿ“„ dynamic-link library.

#### ๐Ÿ…ƒCScriptExeSubsystem
- `csCONSOLE`: Targets ๐ŸŽฎ console application subsystems.
- `csGUI`: Targets ๐ŸŽจ graphical user interface subsystems.

#### ๐Ÿ…ƒCScript Class Overview
- **๐Ÿ—๏ธ Construction and Destruction**
- `constructor Create()`: ๐Ÿ› ๏ธ Initializes an instance of **TCScript**.
- `destructor Destroy()`: ๐Ÿ’ฃ Releases resources tied to the **TCScript** instance.
- **โš ๏ธ Error Handling**
- `procedure SetErrorHandler(const ASender: Pointer; const AHandler: TCScriptErrorEvent)`: Assigns a custom error handler for error management.
- `procedure GetErrorHandler(var ASender: Pointer; var AHandler: TCScriptErrorEvent)`: Retrieves the currently assigned error handler.
- **๐Ÿ“‚ Path Management**
- `function AddLibraryPath(const APath: string): Boolean`: Registers a ๐Ÿ“ library path for the compiler.
- `function AddIncludePath(const APath: string): Boolean`: Registers an include path for locating header files.
- **๐Ÿ› ๏ธ Compilation and Execution**
- `function SetOutputType(const AOutputType: TCScriptOutputType): Boolean`: Specifies the desired output type for script compilation.
- `function CompileString(const ABuffer: string): Boolean`: Compiles a provided string of ๐Ÿ…ฒ code.
- `function AddFile(const AFilename: string): Boolean`: Adds a ๐Ÿ“„ source file to the compilation unit.
- `function Run(): Boolean`: Executes the compiled script.
- **๐Ÿ“ฆ Library Management**
- `function AddLibrary(const AName: string): Boolean`: Adds an external library to be dynamically linked during execution.
- **๐Ÿ” Symbol Management**
- `procedure AddSymbol(const AName: string; AValue: Pointer)`: Registers a symbol to the scripting context.
- `function GetSymbol(const AName: string): Pointer`: Retrieves the address of a registered symbol.
- **๐Ÿ› ๏ธ Utility Functions**
- `procedure Reset()`: Resets the **TCScript** instance, clearing previous states.
- `function SaveOutputFile(const AFilename: string): Boolean`: Saves the compiled output to a specified ๐Ÿ“„ file.

### ๐Ÿ“– Example Usage

The following example illustrates how to instantiate **TCScript**, add a file for compilation, and execute the resulting script:

```delphi
procedure CScriptErrorEvent(const ASender: Pointer; const AText: string);
begin
WriteLn(AText);
end;

procedure AddFileRun();
var
LCScript: TCScript;
begin
LCScript := TCScript.Create();
try
// Set the โš ๏ธ error handler
LCScript.SetErrorHandler(nil, CScriptErrorEvent);

// Configure the output type for in-memory execution
LCScript.SetOutputType(csMEMORY);

// Add include and library paths
LCScript.AddIncludePath('res/include');
LCScript.AddLibraryPath('res/lib');

// Add the ๐Ÿ…ฒ source file to the scripting engine
LCScript.AddFile('res/src/test01.c');

// Execute the compiled script
if not LCScript.Run() then
WriteLn('โŒ Failed to execute script.');
finally
// Release resources
LCScript.Free();
end;
end;
```

### ๐Ÿ› ๏ธ Installation

To integrate **๐Ÿ…ฒ๐Ÿ†‚cript** into your Delphi project:

1. **โฌ‡๏ธ Download the latest version** from the **๐Ÿ…ฒ๐Ÿ†‚cript** repository.
2. **๐Ÿ“ฆ Unzip** to your desired location.
3. **โž• Add the `src` folder** to the Delphi search path.
4. **โž• Add `๐Ÿ…ฒ๐Ÿ†‚cript` to your project's uses section**.
5. **๐Ÿ“– See the examples** for more information on usage.

### ๐Ÿค Contributions

Contributions to **๐Ÿ…ฒ๐Ÿ†‚cript** are highly encouraged. Please feel free to submit issues, suggest new features, or create pull requests to expand the capabilities and robustness of the scripting engine.

### ๐Ÿ“ License

**๐Ÿ…ฒ๐Ÿ†‚cript** is distributed under the ๐Ÿ†“ BSD-3-Clause license. For more details, refer to the `LICENSE` file.

### Support

- Issues
- Discussions
- Wiki
- Learn Delphi

---

Whether you seek to โšก enhance computational performance, interface seamlessly with existing ๐Ÿ…ฒ libraries, or expand the features of your Delphi applications, **๐Ÿ…ฒ๐Ÿ†‚cript** provides a powerful, integrated solution for combining the strengths of Delphi and ๐Ÿ…ฒ99 programming.

๐Ÿ”“ Unlock new possibilities with **๐Ÿ…ฒ๐Ÿ†‚cript** and elevate your Delphi development experience through hybrid programming techniques.


Delphi


Made with :heart: in Delphi