Files

85 lines
2.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 9c40251a81d8f369c184e83fd142c4cc656a7261 Maintainer: leonardolara Status: ready -->
<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="luasandbox.examples">
&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>Uso básico para LuaSandbox</title>
<simpara>
Depois de compilado o PHP com suporte ao LuaSandbox, pode-se começar a usar o LuaSandbox para executar com segurança o código Lua fornecido pelo usuário.
</simpara>
<example>
<title>Executando um código Lua</title>
<programlisting role="php" xml:id="luasandbox.examples.uniqueidhere">
<![CDATA[
<?php
$sandbox = new LuaSandbox;
$sandbox->setMemoryLimit( 50 * 1024 * 1024 );
$sandbox->setCPULimit( 10 );
// Registra algumas funções no ambiente Lua
function frobnosticate( $v ) {
return [ $v + 42 ];
}
$sandbox->registerLibrary( 'php', [
'frobnosticate' => 'frobnosticate',
'output' => function ( $string ) {
echo "$string\n";
},
'error' => function () {
throw new LuaSandboxRuntimeError( "Algo está errado" );
}
] );
// Executa algum código Lua, incluindo funções de retorno em PHP e em Lua
$luaCode = <<<EOF
php.output( "Olá, mundo" );
return "Oi", function ( v )
return php.frobnosticate( v + 200 )
end
EOF;
list( $hi, $frob ) = $sandbox->loadString( $luaCode )->call();
assert( $frob->call( 4000 ) === [ 4242 ] );
// Exceções LuaSandboxRuntimeError geradas pelo PHP podem ser capturadas dentro do Lua
list( $ok, $message ) = $sandbox->loadString( 'return pcall( php.error )' )->call();
assert( !$ok );
assert( $message === 'Algo está errado' );
?>
]]>
</programlisting>
</example>
</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
-->