r114920 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114919‎ | r114920 | r114921 >
Date:05:42, 16 April 2012
Author:tstarling
Status:deferred
Tags:
Comment:
* Allow PHP functions to return multiple values to PHP.
* Updated tests for this commit and the previous one.
Modified paths:
  • /trunk/php/luasandbox/luasandbox.c (modified) (history)
  • /trunk/php/luasandbox/tests/invalid_state.phpt (modified) (history)
  • /trunk/php/luasandbox/tests/reentrant.phpt (modified) (history)

Diff [purge]

Index: trunk/php/luasandbox/luasandbox.c
@@ -1038,10 +1038,12 @@
10391039 * each value is a corresponding PHP callback.
10401040 *
10411041 * Both Lua and PHP allow functions to be called with any number of arguments.
1042 - * The parameters to the Lua function will be passed through to the PHP. A
1043 - * single value will always be returned to Lua, which is the return value from
1044 - * the PHP function. If the PHP function does not return any value, Lua will
1045 - * see a return value of nil.
 1042+ * The parameters to the Lua function will be passed through to the PHP.
 1043+ *
 1044+ * Lua supports multiple return values. The PHP function should return either
 1045+ * null (for zero return values) or an array of return values. The keys of the
 1046+ * return array will not be used, rather the values will be taken in their
 1047+ * internal order.
10461048 */
10471049 PHP_METHOD(LuaSandbox, registerLibrary)
10481050 {
@@ -1136,6 +1138,7 @@
11371139 zval **pointers;
11381140 zval ***double_pointers;
11391141 int num_results = 0;
 1142+ Bucket *bucket;
11401143 TSRMLS_FETCH();
11411144
11421145 // Based on zend_parse_arg_impl()
@@ -1173,9 +1176,20 @@
11741177 if (zend_call_function(&fci, &fcc TSRMLS_CC) == SUCCESS
11751178 && fci.retval_ptr_ptr && *fci.retval_ptr_ptr)
11761179 {
1177 - // Push the return value back to Lua
1178 - luasandbox_push_zval(L, *fci.retval_ptr_ptr);
1179 - num_results = 1;
 1180+ // Push the return values back to Lua
 1181+ if (Z_TYPE_PP(fci.retval_ptr_ptr) == IS_NULL) {
 1182+ // No action
 1183+ } else if (Z_TYPE_PP(fci.retval_ptr_ptr) == IS_ARRAY) {
 1184+ bucket = Z_ARRVAL_PP(fci.retval_ptr_ptr)->pListHead;
 1185+ while (bucket) {
 1186+ luasandbox_push_zval(L, *((zval **)bucket->pData));
 1187+ bucket = bucket->pListNext;
 1188+ num_results++;
 1189+ }
 1190+ } else {
 1191+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
 1192+ "function tried to return a single value to Lua without wrapping it in an array");
 1193+ }
11801194 zval_ptr_dtor(&retval_ptr);
11811195 }
11821196
Index: trunk/php/luasandbox/tests/invalid_state.phpt
@@ -8,7 +8,7 @@
99 $dump = $f->dump();
1010 try {
1111 $f->call();
12 -} catch (LuaSandboxEmergencyTimeout $e) {
 12+} catch (LuaSandboxEmergencyTimeoutError $e) {
1313 print $e->getMessage() . "\n";
1414 }
1515 $f->call();
Index: trunk/php/luasandbox/tests/reentrant.phpt
@@ -23,14 +23,14 @@
2424 function factorial($n) {
2525 global $luaFactorial;
2626 if ($n <= 1) {
27 - return 1;
 27+ return array(1);
2828 } else {
2929 $ret = $luaFactorial->call($n - 1);
30 - return $n * $ret[0];
 30+ return array($n * $ret[0]);
3131 }
3232 }
3333
34 -print factorial(10) . "\n";
 34+print implode('', factorial(10)) . "\n";
3535 var_dump( $luaFactorial->call(10) );
3636
3737 try {

Status & tagging log