-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_phpfunc_table.php
More file actions
37 lines (27 loc) · 939 Bytes
/
6_phpfunc_table.php
File metadata and controls
37 lines (27 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use KLua\KLua;
use KLua\KLuaConfig;
if (KPHP_COMPILER_VERSION) { KLua::loadFFI(); }
KLua::init(new KLuaConfig());
// Suppose that you want to provide a PCRE regexp library
// to your Lua scripts. To make it more idiomatic, you would
// want to put those regexp functions in some table named "pcre".
// KLua doesn't provide a way to register a table-bound function,
// but it's easy to achieve our goal anyway.
// We use "php_" prefix to avoid accidental name clashes.
KLua::registerFunction2('php_preg_match', function ($pat, $s) {
return preg_match($pat, $s) === 1;
});
// Now let's create a neat wrapper.
KLua::eval('
pcre = {}
function pcre.match(pat, s)
return php_preg_match(pat, s)
end
');
// The rest of the Lua code can now use pcre library.
KLua::eval('
print(pcre.match("/[0-9]+/", "abc"))
print(pcre.match("/[0-9]+/", "435"))
');