mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-17 01:18:55 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347858 c90b9560-bf6c-de11-be94-00142212c4b1
89 lines
2.2 KiB
XML
89 lines
2.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
|
|
<chapter xml:id="luasandbox.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
&reftitle.examples;
|
|
|
|
<!-- Add one or more of these <section>'s, with one ore more <example>'s -->
|
|
<!-- Make sure each xml:id is unique -->
|
|
<section xml:id="luasandbox.examples-basic">
|
|
<title>Basic usage for LuaSandbox</title>
|
|
<para>
|
|
Once you've compiled PHP with LuaSandbox support, you can begin using LuaSandbox to safely run user-provided Lua code.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Execute some Lua code</title>
|
|
<programlisting role="php" xml:id="luasandbox.examples.uniqueidhere">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$sandbox = new LuaSandbox;
|
|
$sandbox->setMemoryLimit( 50 * 1024 * 1024 );
|
|
$sandbox->setCPULimit( 10 );
|
|
|
|
// 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" );
|
|
}
|
|
] );
|
|
|
|
// Execute some Lua code, including callbacks into PHP and into Lua
|
|
|
|
$luaCode = <<<EOF
|
|
php.output( "Hello, world" );
|
|
|
|
return "Hi", function ( v )
|
|
return php.frobnosticate( v + 200 )
|
|
end
|
|
EOF;
|
|
|
|
list( $hi, $frob ) = $sandbox->loadString( $luaCode )->call();
|
|
assert( $frob->call( 4000 ) === [ 4242 ] );
|
|
|
|
// PHP-thrown LuaSandboxRuntimeError exceptions can be caught inside Lua
|
|
|
|
list( $ok, $message ) = $sandbox->loadString( 'return pcall( php.error )' )->call();
|
|
assert( !$ok );
|
|
assert( $message === 'Something is wrong' );
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|
|
|