https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114920

Revision: 114920
Author:   tstarling
Date:     2012-04-16 05:42:06 +0000 (Mon, 16 Apr 2012)
Log Message:
-----------
* 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
    trunk/php/luasandbox/tests/invalid_state.phpt
    trunk/php/luasandbox/tests/reentrant.phpt

Modified: trunk/php/luasandbox/luasandbox.c
===================================================================
--- trunk/php/luasandbox/luasandbox.c   2012-04-16 04:32:08 UTC (rev 114919)
+++ trunk/php/luasandbox/luasandbox.c   2012-04-16 05:42:06 UTC (rev 114920)
@@ -1038,10 +1038,12 @@
  * each value is a corresponding PHP callback.
  *
  * Both Lua and PHP allow functions to be called with any number of arguments. 
- * The parameters to the Lua function will be passed through to the PHP. A 
- * single value will always be returned to Lua, which is the return value from
- * the PHP function. If the PHP function does not return any value, Lua will 
- * see a return value of nil.
+ * The parameters to the Lua function will be passed through to the PHP. 
+ *
+ * Lua supports multiple return values. The PHP function should return either 
+ * null (for zero return values) or an array of return values. The keys of the
+ * return array will not be used, rather the values will be taken in their 
+ * internal order.
  */
 PHP_METHOD(LuaSandbox, registerLibrary)
 {
@@ -1136,6 +1138,7 @@
        zval **pointers;
        zval ***double_pointers;
        int num_results = 0;
+       Bucket *bucket; 
        TSRMLS_FETCH();
 
        // Based on zend_parse_arg_impl()
@@ -1173,9 +1176,20 @@
        if (zend_call_function(&fci, &fcc TSRMLS_CC) == SUCCESS 
                && fci.retval_ptr_ptr && *fci.retval_ptr_ptr) 
        {
-               // Push the return value back to Lua
-               luasandbox_push_zval(L, *fci.retval_ptr_ptr);
-               num_results = 1;
+               // Push the return values back to Lua
+               if (Z_TYPE_PP(fci.retval_ptr_ptr) == IS_NULL) {
+                       // No action
+               } else if (Z_TYPE_PP(fci.retval_ptr_ptr) == IS_ARRAY) {
+                       bucket = Z_ARRVAL_PP(fci.retval_ptr_ptr)->pListHead;
+                       while (bucket) {
+                               luasandbox_push_zval(L, *((zval 
**)bucket->pData));
+                               bucket = bucket->pListNext;
+                               num_results++;
+                       }
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, 
+                               "function tried to return a single value to Lua 
without wrapping it in an array");
+               }
                zval_ptr_dtor(&retval_ptr);
        }
 

Modified: trunk/php/luasandbox/tests/invalid_state.phpt
===================================================================
--- trunk/php/luasandbox/tests/invalid_state.phpt       2012-04-16 04:32:08 UTC 
(rev 114919)
+++ trunk/php/luasandbox/tests/invalid_state.phpt       2012-04-16 05:42:06 UTC 
(rev 114920)
@@ -8,7 +8,7 @@
 $dump = $f->dump();
 try {
        $f->call();
-} catch (LuaSandboxEmergencyTimeout $e) {
+} catch (LuaSandboxEmergencyTimeoutError $e) {
        print $e->getMessage() . "\n";
 }
 $f->call();

Modified: trunk/php/luasandbox/tests/reentrant.phpt
===================================================================
--- trunk/php/luasandbox/tests/reentrant.phpt   2012-04-16 04:32:08 UTC (rev 
114919)
+++ trunk/php/luasandbox/tests/reentrant.phpt   2012-04-16 05:42:06 UTC (rev 
114920)
@@ -23,14 +23,14 @@
 function factorial($n) {
        global $luaFactorial;
        if ($n <= 1) {
-               return 1;
+               return array(1);
        } else {
                $ret = $luaFactorial->call($n - 1);
-               return $n * $ret[0];
+               return array($n * $ret[0]);
        }
 }
 
-print factorial(10) . "\n";
+print implode('', factorial(10)) . "\n";
 var_dump( $luaFactorial->call(10) );
 
 try {


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to