Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

tinyBigGAMES/Chandra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

We have integrated the functionality into the jetLua project, available at https://github.com/tinyBigGAMES/jetLua

Chandra
Chat on Discord Follow on Bluesky

Chandra - Lua Scripting for Delphi πŸ› οΈ

Chandra is a Lua πŸ“œ integration library tailored specifically for Delphi πŸ› οΈ developers. It enables seamless interaction πŸ”„ between Delphi applications and Lua scripts, combining Delphi's robustness πŸ’ͺ with Lua's flexibility 🀸. Chandra is designed for professional developers who need to add dynamic ⚑ capabilities to their software without sacrificing performance πŸš€ and stability πŸ”’. This comprehensive guide πŸ“– aims to help you make the most out of Chandra in your projects.

Overview πŸ“

Chandra integrates Lua 5.4.7+, statically compiled into your Delphi application, with no external DLL dependencies πŸ“¦ to manage. This enhances portability 🚚 and simplifies distribution, making it easier to integrate scripting capabilities into your Delphi applications.

With Chandra, you can easily expose Delphi classes, methods, and data structures to Lua scripts, giving your applications dynamic behavior πŸŒ€ and extensibility. The library leverages Delphi's RTTI (Run-Time Type Information) 🧠 to facilitate method registration, providing a streamlined process for integrating Lua.

Key Features ✨

  • Integrated Lua Version: Lua 5.4.7+ is embedded directly, eliminating external dependencies πŸ“¦.
  • Automatic Routine Registration πŸ”„: Published Delphi methods are automatically available to Lua, simplifying the process of scripting your application.
  • Basic Types Support 🧩: Supports parameters and return types such as strings πŸ“, floating-point numbers πŸ”’, Booleans βœ…βŒ, and pointers ➑️.
  • Pointer Handling πŸ”§: Safe pointer management ensures stability πŸ”’ when working with complex data structures.
  • Error Handling ⚠️: Robust error handling during script execution is included, ensuring your application runs smoothly πŸš€ even when Lua scripts fail.
  • Interactive Debugging 🐞: Add dbg() in Lua code to begin debugging, useful for identifying issues during runtime.
  • Script Importing and Bundling πŸ“¦: Use a custom import command to combine scripts, compile them into a single file, and optionally store them as an EXE resource for a fully self-contained application.

Usage Instructions πŸ› οΈ

To get started with Chandra, follow these steps:

  1. Compile πŸ“¦ your program in the Delphi IDE with the Chandra library included.
  2. Run ▢️ the application to register the Delphi classes and methods with the Lua runtime.
  3. Write and Load Lua Scripts πŸ“πŸ“œ that interact with your Delphi classes using the integrated scripting capabilities.

Example Integration: Testbed Program πŸ§ͺ

The Testbed Program is a sample project demonstrating how to use Chandra to integrate Lua scripting with a Delphi application. It provides practical examples, including arithmetic operations βž•, string manipulation βœ‚οΈ, record handling πŸ—‚οΈ, memory management πŸ’Ύ, and error handling ⚠️.

Features Demonstrated 🧩

  • Arithmetic Operations βž•: Methods for addition and multiplication are exposed to Lua scripts.
  • String Manipulation βœ‚οΈ: Concatenates strings and creates lists accessible in Lua.
  • Record Handling πŸ—‚οΈ: Create and update Delphi records from Lua, and retrieve their values via pointers.
  • Memory Management πŸ’Ύ: Allocate, write, read, and free memory blocks from Lua scripts, demonstrating pointer management.
  • Lua Integration πŸ“œ: Load, execute, and interact with Lua scripts calling Delphi functions.

Example Lua Script πŸ“œ

print("Testing TTestClass methods...")
print("Add:", TTestClass.Add(5, 3))
print("Multiply:", TTestClass.Multiply(4.5, 2.0))
print("Concat:", TTestClass.Concat("Hello ", "World"))

-- Record handling example
local rec = TTestClass.CreateRecord(1, "Test Record", 42.0)
print("Initial Record Value:", TTestClass.GetRecordValue(rec))
TTestClass.UpdateRecord(rec, 100.0)
print("Updated Record Value:", TTestClass.GetRecordValue(rec))

-- Memory management example
local mem = TTestClass.AllocateMemory(4)
TTestClass.WriteToMemory(mem, 12345)
print("Memory Value:", TTestClass.ReadFromMemory(mem))
TTestClass.FreeMemory(mem)  

function add(a, b)
  return a + b
end

function concat(str1, str2)
  return str1 .. str2
end

function process_record(rec, str)
  print("Value:", TTestClass.GetRecordValue(rec))
  print("str: " .. str)
end

Delphi Side Example πŸ› οΈ

Below is an example of how to use Chandra in a Delphi application:

  
type
  TTestRecord = record
    ID: Integer;
    Name: string;
    Value: Double;
  end;
  PTestRecord = ^TTestRecord;

  {$M+}
  TTestClass = class
  published
    class function Add(A, B: Integer): Integer;
    class function Multiply(A, B: Double): Double;
    class function Concat(const A, B: string): string;
    class function CreateList: TStringList;
    class function GetListCount(List: TStringList): Integer;
    class function CreateRecord(ID: Integer; const Name: string; Value: Double): TTestRecord;
    class procedure UpdateRecord(P: PTestRecord; NewValue: Double);
    class function GetRecordValue(P: PTestRecord): Double;
    class function AllocateMemory(Size: Integer): Pointer;
    class procedure FreeMemory(P: Pointer);
    class procedure WriteToMemory(P: Pointer; Value: Integer);
    class function ReadFromMemory(P: Pointer): Integer;
  end;
  {$M-}  

var
  LChandra: TChandra;
  LRec: TTestRecord;

begin
  LChandra := TChandra.Create();
  try
    try
      LChandra.RegisterRoutines(TTestClass);
      LChandra.LoadString(CScript);

      WriteLn('Integer value: ', LChandra.Call('add', [50, 50]).AsInteger);
      WriteLn('String value: ', LChandra.Call('concat', ['Hello, ', 'World!']).AsString);

      LRec.ID := 1;
      LRec.Name := 'test';
      LRec.Value := 200;

      LChandra.Call('process_record', [@LRec, 'test']);
    except
      on E: Exception do
      begin
        WriteLn(Format('Error: %s', [E.Message]));
      end;
    end;
  finally
    LChandra.Free();
  end;
end;

Key Takeaways πŸ“Œ

This example showcases the powerful interoperability πŸ”— between Delphi and Lua. It highlights how you can:

  • Extend Application Functionality ⚑: Use Lua scripts to add dynamic features to your Delphi applications.
  • Safe Memory Management πŸ’Ύ: Demonstrates best practices for handling pointers and memory between Delphi and Lua.
  • Error Handling and Debugging 🐞: Learn practical techniques to debug and handle errors when integrating scripting into Delphi applications.

Advanced Usage Notes 🧠

Integrated Lua Version 🐍

Chandra uses Lua 5.4.7+, which is statically compiled into the Delphi application. This approach eliminates the need for external DLLs πŸ“¦, ensuring your applications remain self-contained.

Automatic Registration of Delphi Routines πŸ”„

Chandra automatically registers Delphi routines declared as published class methods. You don’t need to manually bind methods to Luaβ€”simply declare them as published, and Chandra makes them available for scripting.

Supported Parameter and Return Types βœ…

Chandra supports basic types for method parameters and return values:

  • string πŸ“
  • float (single or double) πŸ”’
  • Boolean βœ…βŒ
  • Pointer ➑️

When designing methods for use with Lua, ensure parameters and return values are restricted to these types.

Pointer Management πŸ”§

Pointers created on the Delphi side must be managed by Delphi code:

  • Pointers passed to Lua can be referenced within Lua scripts, but modification or memory operations must be handled by Delphi.
  • Dynamically allocated pointers need proper cleanup to avoid memory leaks πŸ’§.

Prerequisites πŸ“‹

  • Delphi Version: Delphi 12.2 or higher is required.
  • Operating System: Windows 10 or higher; tested on Windows 11 64-bit (23H2).

Getting Started with the Chandra Testbed Program πŸš€

The Testbed Program is an example application included with Chandra. It demonstrates how to:

  • Integrate Lua scripting πŸ“œ into a Delphi application.
  • Expose Delphi methods πŸ› οΈ and data structures to Lua.
  • Combine static and dynamic programming paradigms πŸ”„ for powerful application extensibility.

How to Run the Testbed Program ▢️

  1. Compile the program in the Delphi IDE, ensuring that the Chandra library is correctly included.
  2. Run the program to see how Lua interacts with Delphi methods.
  3. Observe console outputs that demonstrate arithmetic operations βž•, record handling πŸ—‚οΈ, and memory management πŸ’Ύ.

Example Workflow πŸ”„

  • Arithmetic with Lua βž•: Lua scripts call Delphi methods to perform addition and multiplication.
  • String Operations βœ‚οΈ: Lua can concatenate strings using methods exposed from Delphi.
  • Record Management πŸ—‚οΈ: Delphi records can be created and updated via Lua scripts, showing how to pass complex data between the two environments.
  • Memory Management πŸ’Ύ: Demonstrates safe handling of dynamically allocated memory through pointers.

Notes and Recommendations πŸ“

  • Lua Interpreter Setup 🐍: Ensure the embedded Lua version is correctly linked and compiled with the Delphi project.
  • Pointer Safety πŸ”’: Pointers passed to Lua should be managed exclusively by Delphi to prevent memory issues.
  • Documentation πŸ“–: Thoroughly document any methods exposed to Lua, specifying the intended usage and parameter types.

Conclusion 🎯

Chandra offers a straightforward solution for integrating Lua scripting πŸ“œ into Delphi applications, providing developers with a way to leverage the flexibility 🀸 of Lua without sacrificing the strong typing and performance πŸš€ advantages of Delphi. By using Chandra, you can significantly enhance your applications' extensibility, maintainability πŸ› οΈ, and customization potential.

Whether you're building complex applications that require runtime customization or just want to offer script-based configuration options, Chandra makes Lua scripting integration both accessible and powerful πŸ’ͺ for Delphi developers.

Requirements βœ…

  • Delphi 12.2 or higher πŸ› οΈ
  • Windows 10 or higher πŸ–₯️ (Tested on Windows 11 64-bit, version 23H2)

Contributing

Contributions to Chandra 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

Chandra is distributed under the πŸ†“ BSD-3-Clause License, allowing for redistribution and use in both source and binary forms, with or without modification, under specific conditions. See the LICENSE file for more details.

Support


For any professional Delphi developer interested in enhancing application flexibility with scripting capabilities, Chandra offers a tested and reliable solution that keeps everything self-contained and performant. πŸš€

Delphi

Made with ❀️ in Delphi