(PECL luasandbox >= 1.0.0)
LuaSandbox::registerLibrary — 將一組 PHP 函式註冊為 Lua 函式庫
將一組 PHP 函式註冊為 Lua 函式庫,以便 Lua 可以呼叫相關的 PHP 程式碼。
關於呼叫 Lua 函式和傳回值的更多資訊,請參閱 LuaSandboxFunction::call()。
無返回值。
範例 #1 註冊 PHP 函數以供 Lua 呼叫
<?php
// 建立一個新的 LuaSandbox
$sandbox = new LuaSandbox();
// 在 Lua 環境中註冊一些函數
function frobnosticate( $v ) {
return [ $v + 42 ];
}
$sandbox->registerLibrary( 'php', [
'frobnosticate' => 'frobnosticate',
'output' => function ( $string ) {
echo "$string\n";
},
'error' => function () {
throw new LuaSandboxRuntimeError( "Something is wrong" );
}
] );
?>