Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/tinybiggames/cscript
- Owner: tinyBigGAMES
- License: bsd-3-clause
- Created: 2024-11-18T09:35:12.000Z (2 days ago)
- Default Branch: main
- Last Pushed: 2024-11-18T10:09:08.000Z (2 days ago)
- Last Synced: 2024-11-18T10:55:09.236Z (2 days ago)
- Topics: c-embedded-in-delphi, c-script, c99, delphi, dll, exe, lib, pascal, win64, windows10, windows11
- Language: C
- Homepage:
- Size: 18.4 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
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.
Made with :heart: in Delphi