This Swift Package allows seamless integration of Lua scripting capabilities within Swift applications. It provides a comprehensive suite of functionalities, utilities, and examples to facilitate the interaction between Swift and Lua scripts.
- Purpose: Provides bridging functionality between Lua and Swift.
- Contents:
pushUserData(value: T)
: Pushes Swift values to Lua stack as user data.getUserData<T>(state: OpaquePointer, type: T.Type, index: Int32) -> T
: Retrieves user data from Lua stack.
- Purpose: Central module for Lua interaction in Swift.
- Contents:
init()
: Initializes Lua environment.execute(source: String)
: Executes Lua script from a string.execute(url: URL)
: Executes Lua code from a file URL.register(function: String, body: @escaping (Lua) -> Int32) throws
: Registers Swift functions within Lua.
- Purpose: Prints Lua stack information for debugging.
- Contents:
dumpStack(note: String?)
: Prints Lua stack information with an optional note.
- Purpose: Defines macros/utilities for Lua functions.
- Contents:
- Contains various utility functions or macros.
- Purpose: Demonstrates basic Lua usage in a Swift executable.
- Usage: Provides an example of using Lua functionalities within a Swift executable.
- Purpose: Demonstrates advanced LuaKit in Swift.
- Usage: Example of intricate interactions between Lua and Swift in a complex scenario.
- Purpose: Contains unit tests for LuaKit functionalities.
- Usage: Ensures correctness and reliability of Lua functionalities.
- Xcode: Ensure you have Xcode installed on your system.
- Swift Package Manager (SPM): The LuaKit package uses SPM for dependency management.
-
Clone or Download:
- Clone the LuaKit repository or download the source files.
-
Open Xcode Project:
- Open your Xcode project or create a new project.
-
Add Swift Package:
- Click on your Xcode project in the Navigator.
- Select your target, navigate to the
Swift Packages
tab. - Click the
+
button and chooseAdd Package Dependency
. - Enter the URL of the LuaKit GitHub repository or the local path to the package directory.
- Click
Next
, specify the version or branch, then clickFinish
.
-
Import LuaKit:
- In your Swift files where you want to use Lua functionalities, import the necessary modules:
import LuaKit // A class to manage Lua interpretation public class LuaInterpreter { let lua = Lua() // Enum for handling Lua execution errors enum LuaError: Error { case executionError(String) } // Function to interpret Lua code func interpret(luaCode: String) throws -> String { do { // Execute Lua code try lua.withUnchangedStack { lua.execute(source: luaCode) } return "Code executed successfully" } catch let error { // Throw custom error if Lua execution fails throw LuaError.executionError("Error executing Lua code: \(error.localizedDescription)") } } } // A SwiftUI View demonstrating Lua code execution struct ContentView: View { let luaInterpreter = LuaInterpreter() let fileContent = "your_lua_code_here" // Replace with your Lua code @State private var output: String = "" var body: some View { Text(output) .onAppear { do { // Attempt to execute Lua code output = try luaInterpreter.interpret(luaCode: fileContent) } catch { // Handle errors during Lua code execution output = "Error executing Lua code: \(error.localizedDescription)" } } } }
- In your Swift files where you want to use Lua functionalities, import the necessary modules:
-
Usage:
- Utilize the provided functionalities according to your requirements. For instance:
- Use
LuaKit.swift
for managing Lua interaction. - Refer to
LuaDemo.swift
andLuaIntegrationDemo.swift
for examples of Lua usage in Swift.
- Use
- Utilize the provided functionalities according to your requirements. For instance:
-
Run and Test:
- Run your project to test LuaKit functionalities.
- Refer to the
LuaIntegrationTests
directory for unit tests and to verify the functionalities.
-
Dependency Errors:
- Ensure your package dependencies are correctly specified in the
Package.swift
file. - Check for any missing or incorrect dependencies.
- Ensure your package dependencies are correctly specified in the
-
Build Issues:
- If you encounter build issues related to LuaKit, review the import statements and ensure proper module usage.
- Contributions are welcome! Fork the repository, make your changes, and submit a pull request.
This project is licensed under MIT License - see the MIT License for details.