KLua is a FFI-based Lua5 library that can be used in both PHP and KPHP

Overview

KLua

KLua is a FFI-based Lua5 library that can be used in both PHP and KPHP.

Installation

Since this is a FFI library, it needs a dynamic library available during the run time.

Installation steps:

  1. Install liblua5 in your system (if you don't have it already)
  2. Locate the library file and place in under ./ffilibs/liblua5
  3. Install this composer package to use KLua classes inside your code

Depending on your system, you need to find liblua.so, liblua.dylib or liblua.dll file. Then you can copy it to the application root ffilibs folder under the liblua5 name (note: no extension suffixes).

If you're having difficulties locating the library file, use a helper script:

$ php -f locate_lib.php
note: can't locate liblua5.4, maybe it's not installed
library candidate: /lib/x86_64-linux-gnu/liblua5.3.so.0
library candidate: /lib/x86_64-linux-gnu/liblua5.3.so

run something like this to make it discoverable (unix):
	mkdir -p ffilibs && sudo ln -s /lib/x86_64-linux-gnu/liblua5.3.so ./ffilibs/liblua5.3

Then install the composer library itself:

$ composer require quasilyte/klua

Notes:

  • If you want to place library files/links globally, make ./ffilibs a symlink
  • You'll probably want to add ffilibs/ to your gitignore

Examples

Running examples with PHP:

$ php -d opcache.enable_cli=1\
      -d opcache.preload=preload.php\
      -f ./examples/1_simple.php

Running examples with KPHP:

# Step 1: compile the example:
$ kphp --mode cli --composer-root $(pwd) ./examples/simple.php
# Step 2: run the binary:
$ ./kphp_out/cli

Quick start

<?php

require_once __DIR__ . '/vendor/autoload.php';

use KLua\KLua;
use KLua\KLuaConfig;

// For PHP, loadFFI() is called from preload script.
// For KPHP, loadFFI() can be called in the beginning of the script.
if (KPHP_COMPILER_VERSION) { KLua::loadFFI(); }

KLua::init(new KLuaConfig());

KLua::eval('
    function example(x)
        return x + 1
    end
');

var_dump(KLua::call('example', 10)); // => 11

Running with PHP:

$ php -d opcache.enable_cli=1 -d opcache.preload=preload.php -f example.php
float(11)

Running with KPHP:

# Compile
$ kphp --mode cli --composer-root $(pwd) example.php
# Execute
$ ./kphp_out/cli
float(11)

Value conversion

PHP Type Lua Type Operation Cost
bool boolean free
int number free
float number free
string string string data is copied
map-like array table expensive conversion
list-like array sequence table expensive conversion

All conversions are symmetrical, except for the Lua->PHP case of sequence tables.

If KLuaConfig::pack_lua_tables is set to false, Lua tables will be returned "as is". If that option is set to true (the default), then KLua will try to return Lua sequence tables as list-like PHP arrays.

Not every PHP value can be converted to a Lua value and vice versa. Both languages should communicate to each other using the simpler protocols.

If some value can't be converted properly, a special error-like value is produced instead.

['_error' => 'error message']

The light userdata is a special case. It can't be auto-converted from a PHP value, but there are KLua::setVarUserData and call builder API pushUserData functions to pass user data from PHP to Lua. When Lua->PHP conversion is performed, the user data address is stored as PHP int value. You can convert that int addr to the CData void* by using KLua::userDataPtr. See userdata.php for the complete example.

API reference

See api_reference.md for full documentation.

All KLua class methods are static.

KLua initialization methods:

KLua main methods:

KLua call builder methods:

KLua utility methods:

You might also like...
My intention with this app is that new developers can have a concrete application with Laravel + VueJS where they can use it as example to learn the right way
My intention with this app is that new developers can have a concrete application with Laravel + VueJS where they can use it as example to learn the right way

My intention with this app is that new developers can have a concrete application with Laravel + VueJS where they can use it as example to learn the right way, implementing the best practices possible and at the same time learn how TDD is done. So this will be an example application but completely usable for any similar case.

Skosmos is a web-based tool providing services for accessing controlled vocabularies, which are used by indexers describing documents and searchers looking for suitable keywords.

Skosmos is a web-based tool providing services for accessing controlled vocabularies, which are used by indexers describing documents and searchers looking for suitable keywords.

TYPO3 CMS extension which extends TYPO3 page cache, by tags based on entities used in fluid templates.

Fluid Page Cache for TYPO3 CMS This TYPO3 CMS extension allows you to clear frontend page caches, automatically when a displayed record has been updat

Behat Transliterator library inherited from Doctrine1 and used in Behat for snippet generation

Behat Transliterator Behat Transliterator provides transliteration utilities for PHP. Transliteration data are ported from the Perl Text-Unidecode mod

Open-source library used in Gigadrive projects with common PHP utilities

PHP Commons This library provides PHP utilities used in Gigadrive projects, provided for the open-source community. Functions are registered globally

A small marshalling library used to encode/decode data from classes

A small marshalling library used to encode/decode data from classes

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific.

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific. It is part of the QuidPHP package and can also be used standalone.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

Releases(v1.0.0)
Owner
Iskander (Alex) Sharipov
🇺🇦💙💛
Iskander (Alex) Sharipov
Simple KPHP game, a proof of concept thing

KPHP Game About This is a game written in PHP using kphp-sdlite library. Gameplay video: https://www.youtube.com/watch?v=L44l4Tqm4Fc This game feature

Iskander (Alex) Sharipov 26 Dec 19, 2022
A PHP library that can be used manually as well as a CLI script that you can just run on your file

Run phpcs on files and only report new warnings/errors compared to the previous version. This is both a PHP library that can be used manually as well

Payton Swick 20 Aug 4, 2022
HLedger is cross-platform accounting software for both power users and folks new to accounting

HLedger Plain Text Accounting on Nextcloud HLedger is cross-platform accounting software for both power users and folks new to accounting. It's good f

Ryan Boder 11 Jan 20, 2022
This plugin allows you to create many-to-many relationships between pages in Kirby and synchronizes them on both sides.

Kirby 3 Many To Many Field This plugin allows you to create many-to-many relationships between pages in Kirby.

Jonas Holfeld 41 Nov 19, 2022
A Sublime Text 3 package for highlighting both Sass and SCSS syntax.

Syntax Highlighting for Sass This is a Sublime Text 3 package which purely forced on highlighting both Sass and SCSS syntax as accuracy as possible. P

Peiwen Lu 291 Jun 26, 2022
This library can be used, among other things, to retrieve the classes, interfaces, traits, enums, functions and constants declared in a file

marijnvanwezel/reflection-file Library that allows reflection of files. This library can be used, among other things, to retrieve the classes, interfa

Marijn van Wezel 5 Apr 17, 2022
This library implements a fuzzer for PHP, which can be used to find bugs in libraries

PHP Fuzzer This library implements a fuzzer for PHP, which can be used to find bugs in libraries (particularly parsing libraries) by feeding them "ran

Nikita Popov 341 Dec 25, 2022
A tool that can be used to verify BC breaks between two versions of a PHP library.

Roave Backward Compatibility Check A tool that can be used to verify BC breaks between two versions of a PHP library. Pre-requisites/assumptions Your

Roave, LLC 530 Dec 27, 2022
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
It is a simple blog application coded with PHP, HTML, CSS. You can develop, edit. You can see it as a skeleton. âš¡

PHP-BLOG-SYSTEM Simple blog system Features Adding Text Update Text Text Deletion User Login and register Bootstrap Design Profile Page How to use blo

Selçuk 2 Aug 21, 2022