【问题标题】:php and FFI return struct/union is not implementedphp 和 FFI 返回结构/联合未实现
【发布时间】:2020-09-18 16:33:35
【问题描述】:

我想将 rust 库绑定到 php。 我有 .h 文件,其中包含一些函数和结构的声明:

typedef struct {
    const char* content;
    //char content;
    uint32_t len;
} tc_string_t;

typedef struct  {
    tc_string_t result_json;
    tc_string_t error_json;
} tc_response_t;

typedef struct  {
} tc_response_handle_t;

enum tc_response_flags_t {
    tc_response_finished = 1,
};

typedef void (*tc_on_response_t)(
    int32_t request_id,
    tc_string_t result_json,
    tc_string_t error_json,
    int32_t flags);

void tc_json_request_async(
    uint32_t context,
    tc_string_t method,
    tc_string_t params_json,
    int32_t request_id,
    tc_on_response_t on_result);

tc_response_handle_t* tc_json_request(
    uint32_t context,
    tc_string_t method,
    tc_string_t params_json);

tc_response_t tc_read_json_response(const tc_response_handle_t* handle);
void tc_destroy_json_response(const tc_response_handle_t* handle);

当我从 PHP 调用时:

$ffi->tc_read_json_response($handle);

我得到了错误:

FFI return struct/union is not implemented 

如何实现退货?

【问题讨论】:

    标签: php c ffi php-ffi


    【解决方案1】:

    我相信 PHP 还不支持将嵌套结构或联合映射到 FFI\CData。基本上不可能调用将返回这些类型数据的 C API。

    您可以从此处的 PHP 源代码中看到:https://github.com/php/php-src/blob/92381864a5b5705b1d772302a673ed51888e2c30/ext/ffi/ffi.c#L343 PHP 结构中唯一支持的属性类型基本上是标量或指针。您看到的错误是在此处触发的:https://github.com/php/php-src/blob/92381864a5b5705b1d772302a673ed51888e2c30/ext/ffi/ffi.c#L2536-L2552

    我认为唯一的选择是编写自己的 C 包装器,提供返回更简单数据类型的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2013-08-23
      • 1970-01-01
      相关资源
      最近更新 更多