« LuaSandbox::pauseUsageTimer
LuaSandbox::setCPULimit »

LuaSandbox::registerLibrary

(PECL luasandbox >= 1.0.0)

LuaSandbox::registerLibrary — Register a set of PHP functions as a Lua library

説明

public function LuaSandbox::registerLibrary(string $libname, array $functions): void

Registers a set of PHP functions as a Lua library, so that Lua can call the relevant PHP code.

For more information about calling Lua functions and the return values, see LuaSandboxFunction::call().

パラメータ

libname

The name of the library. In the Lua state, the global variable of this name will be set to the table of functions. If the table already exists, the new functions will be added to it.

functions

An array, where each key is a function name, and each value is a corresponding PHP callable.

戻り値

値を返しません。

例

例1 Registering PHP functions to call from Lua

<?php

// create a new LuaSandbox
$sandbox = new LuaSandbox();

// Register some functions in the Lua environment

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" );
    }
] );

?>

参考

関連キーワード:
functions   LuaSandbox   library   Register   registerLibrary   call   frobnosticate   libname   code   LuaSandboxFunction   table   callable   sandbox   environment   pauseUsageTimer   setCPULimit   luasandbox   Registers   relevant   the global   variable   exists   throw   LuaSandboxRuntimeError   Something
関連する関数・ページ:
luasandbox.examples-basic   luasandbox.setup   luasandbox.wrapphpfunction   luasandbox.loadstring   luasandbox.getversioninfo   book.luasandbox