Menu

#1350 generate wrong PHP code for the function with overloading, default values, and %newobject

None
closed-fixed
nobody
php (58)
5
2017-10-08
2013-12-04
A.Kosaka
No

Hi.

I found that wrong PHP code will be generated in some conditions.

  • with overloading
  • with argument(s) which has default value
  • with %newobject

the minimal sample interface file (test.i):

%module test
%newobject factory::create;
class product {};
class factory {
public:
    product * create(short id, short type = 0);
    product * create(const char * name, short type = 0);
};

generate wrappers:

$swig -c++ -php5 test.i

generated code in test.php:

/* PHP Proxy Classes */
class product {
/* snip */
    function create($id_or_name,$type=0) {
        if (!is_resource($r)) return $r;    // $r is not defined
        switch (get_resource_type($r)) {
        case '_p_product': return new product($r);
        default: return new product($r);
        }
    }
/* snip */
}

our workaround:

  1. comment out %newobject factory::create; and generate wrappers.
    (then correct test.php will be generated.)
  2. mv test.php test_correct.php
  3. uncomment %newobject factory::create; and generate wrappers.
  4. mv test_correct.php test.php

swig version

$swig -version
SWIG Version 2.0.11
Compiled with g++ [x86_64-unknown-linux-gnu]
Configured options: +pcre
2 Attachments

Discussion

  • Olly Betts

    Olly Betts - 2013-12-12

    Thanks for a nice clear example demonstrating the issue.

    The problem is we're omitting the call to the actual function ($r should be set to the result of this). The reason this call is missing is pretty obvious - the attached patch shows the line in SWIG to comment out to make it appear.

    However, this check is clearly there for a reason, as this change breaks the testsuite. But I'm not sure quite what the check should be instead. This is going to need a bit more work to actually solve...

     
  • Olly Betts

    Olly Betts - 2016-09-12

    Current git master generates:

    function create($id_or_name,$type=0) { 
            if (!is_resource($r)) return $r; 
            return new product($r); 
    }
    

    So the redundant switch is gone, but this bug still remains.

     
  • Olly Betts

    Olly Betts - 2016-09-12

    Aha, the issue seems to be that the newobject check should be constructor. I now have a patch which solves this case, and doesn't affect the testsuite (it doesn't even affect the code generated for any testcase in the testsuite!)

    I'll turn your reproducer into a testcase and commit it soon.

     

    Last edit: Olly Betts 2016-09-12
  • Olly Betts

    Olly Betts - 2016-09-12

    Code produced is now:

        function create($id_or_name,$type=0) {
                $r=factory_create($this->_cPtr,$id_or_name,$type);
                if (!is_resource($r)) return $r;
                return new product($r);
        }
    
     
  • William Fulton

    William Fulton - 2017-10-07

    If this was committed, can this issue be closed?

     
  • Olly Betts

    Olly Betts - 2017-10-08

    It looks like I didn't actually commit this - I think I was waiting to turn the reproducer into a testcase to accompany the change.

    Will sort it out - thanks for the prod.

     
  • Olly Betts

    Olly Betts - 2017-10-08
    • status: open --> closed-fixed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2017-10-08

    Fixed in 0c56d0cb72dd9c9ecd4c393985cc27b03f2510f0 and 0ca47dd7cc2a4b9a54b288dd08c9b1b5ff8fde65.

     

Log in to post a comment.

MongoDB Logo MongoDB