SWIG和PHP7:奇怪的字符串矢量行为

SWIG and PHP7: Strange string vector behaviour

本文关键字:字符串 PHP7 SWIG      更新时间:2023-10-16

我正在使用swig编写一些包装代码,以将我的C 功能公开为php。

my_module.i

%module phpMyModule
%include "exception.i"
%include "std_string.i"
%include "typemaps.i"
// INPUT: convert PHP native array to std::vector<std::string>
%typemap(in) const std::vector<std::string> & %{
    if (Z_TYPE($input) == IS_ARRAY) {
        std::vector<std::string> temp2;
        $1 = &temp2;
        HashTable *ht = Z_ARRVAL($input);
        zval *data;
        HashPosition pos;
        for (zend_hash_internal_pointer_reset_ex(ht, &pos);
             (data = zend_hash_get_current_data_ex(ht, &pos)) != nullptr;
             zend_hash_move_forward_ex(ht, &pos)
        ) {
            convert_to_string(data);
            $1->push_back( std::string( Z_STRVAL_P(data), Z_STRLEN_P(data) ) );
        }
    }
    else SWIG_exception( SWIG_TypeError, "Type Error: Only PHP array is supported!" );
%}
%{
#include "MyModule.h"
%}
extern int initialize_engine( const std::string& script_file, const std::vector<std::string>& input_vars );

swig生成的包装代码

ZEND_NAMED_FUNCTION(_wrap_initialize_engine) {
    std::string *arg1 = 0 ;
    std::vector< std::string > *arg2 = 0 ;
    std::string temp1 ;
    zval args[2];
    int result;
    SWIG_ResetError();
    if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_array_ex(2, args) != SUCCESS) {
        WRONG_PARAM_COUNT;
    }
    convert_to_string(&args[0]);
    temp1.assign(Z_STRVAL(args[0]), Z_STRLEN(args[0]));
    arg1 = &temp1;
    if (Z_TYPE(args[1]) == IS_ARRAY) {
        std::vector<std::string> temp2;
        arg2 = &temp2;
        HashTable *ht = Z_ARRVAL(args[1]);
        zval *data;
        HashPosition pos;
        for (zend_hash_internal_pointer_reset_ex(ht, &pos);
             (data = zend_hash_get_current_data_ex(ht, &pos)) != nullptr;
             zend_hash_move_forward_ex(ht, &pos)
        ) {
            convert_to_string(data);
            arg2->push_back( std::string( Z_STRVAL_P(data), Z_STRLEN_P(data) ) );
        }
    }
    else SWIG_exception( SWIG_TypeError, "Type Error: Only PHP array is supported!" );
    result = (int)initialize_engine((std::string const &)*arg1,(std::vector< std::string > const &)*arg2);
    RETVAL_LONG(result);
thrown:
    return;
fail:
    SWIG_FAIL();
}

test.php

<?php
    require_once '<path>/<to>/phpMyModule.php';
    $handle = phpMyModule::initialize_engine(
        '<path>/<to>/test.script',
        ["var1", "var2", "var3"]
    );
    echo "Handle #1 Value: $handlen";
    phpMyModule::terminate_engine($handle);
?>

基本上,上述代码是用PHP字符串(script_file)和可变名称(input_vars)的PHP数组调用_wrap_initialize_engine()。SWIG使用typemap将PHP字符串和数组转换为STD :: String :: string and std :: vector,然后调用真实的initialize_engine()

initialize_engine( const std::string& script_file, const std::vector<std::string>& input_vars )中,我有:

std::for_each( input_vars.begin(), input_vars.end(), [&]( const std::string& name ) {
    std::cout << "Adding " << name << " ..." << std::endl;
    // signature is Data::addVariable( const std::string& name, const VariableVector& values );
    // Data::VariableVector is actually std::vector<double>
    data.addVariable( name, Data::VariableVector() );
} );

这有效。打印是

Adding var1 ...
Adding var2 ...
...

但是,如果我评论std::cout ...,则所有对data.addVariable()的呼叫都将为name参数收到一个空字符串。(我知道这是因为在通话中,我在使用重复项时对现有名称测试名称。没有std::cout ...,我就会出现错误,说"名称'''已经存在" ...)

我的问题

这怎么会发生?const std::vector<std::string>&不应受到我是否在其上调用std::cout的影响。

我唯一的猜测是std ::字符串重复使用char*点而不是复制它们?如果是这种情况,那么真正的缓冲区仍在PHP内,并且可能已更改?我相信情况并非如此,而只是希望一个在C 中具有更好知识的人来确认我。

但是,如果不是这样,那么为什么上述奇怪的行为会发生?

这是我犯了一个愚蠢的错误...对不起...

my_module.i

%module phpMyModule
%include "exception.i"
%include "std_string.i"
%include "typemaps.i"
// INPUT: convert PHP native array to std::vector<std::string>
%typemap(in) const std::vector<std::string> & %{
    std::vector<std::string> temp2;  // should be declared here!
    if (Z_TYPE($input) == IS_ARRAY) {
        // if declared here, temp2 will be released before init_engine() was called!
        // Wrong: std::vector<std::string> temp2;
        $1 = &temp2;
        ......
}
else SWIG_exception( SWIG_TypeError, "Type Error: Only PHP array is supported!" );

%}