PHP和FFI返回struct / union未实现

问题描述

我想将rust库绑定到php。 我有带有某些函数和结构声明的.h文件:

beforeEnter: (to,from,next) => {
    if (!to.hash) {
        window.scrollTo(0,0);
    }

    next();
}

当我从PHP调用时:

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 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);

我得到了错误:

$ffi->tc_read_json_response($handle);

如何执行退货?

解决方法

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

您可以从此处的 PHP src 中看到: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 包装器,提供返回更简单数据类型的函数。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...