ocaml c 互操作传递结构

ocaml c interop passing struct

我在尝试从 ocaml 调用 c 时遇到了奇怪的情况。 这是事物的 c 面:

typedef struct {
  TSNode node;
} AstNode;

CAMLprim value caml_ts_document_root_node(value document) {
  CAMLparam1(document);
  TSNode root_node = ts_document_root_node(document);
  AstNode elNode;
  elNode.node = root_node;
  CAMLreturn(&elNode);
}

CAMLprim value caml_ts_node_string(value node) {
  CAMLparam1(node)
  CAMLlocal1(mls);

  AstNode* n = (AstNode*) node;

  char *s = ts_node_string(n->node);
  mls = caml_copy_string(s);
  CAMLreturn(mls);
}

在 ocaml 方面

type ts_point
type ts_document

external ts_node_string : ts_node -> string = "caml_ts_node_string"
external ts_document_root_node : ts_document -> ts_node = "caml_ts_document_root_node"

如果您看到代码,我将 caml_ts_document_root_nodeTSNode root_node = ts_document_root_node(document); 包装在一个额外定义的结构 AstNode.

然而,当我编写以下实现时:

CAMLprim value caml_ts_document_root_node(value document) {
  CAMLparam1(document);
  TSNode root_node = ts_document_root_node(document);

  CAMLreturn(&root_node);

}

我的代码在 caml_ts_document_root_node 返回的节点上调用 caml_ts_node_string 时出现段错误。

当我从 ocaml 进行互操作时,没有将 TSNode 包装在额外的结构中时,有没有人提示为什么会出现段错误?

这似乎与ocaml互操作部分无关;您正在此函数中返回局部变量的地址:

CAMLprim value caml_ts_document_root_node(value document) {
  // ...
  AstNode elNode;
  // ...
  CAMLreturn(&elNode);
}

当它returns时,它引用的(堆栈)内存无效(在下一个函数调用时将被重用)。

这绝对不是国外接口的正确用法!您不能只获取一个值并将其转换为 OCaml 值。 OCaml 值经过特殊编码,甚至是整数,并且具有与 C 值不同的表示形式。

如果你想将 C 值编码为 OCaml 值,你应该使用 custom values

首先,你需要实现自定义值的接口,幸运的是,你可以依赖默认值:

static struct custom_operations ast_ops = {
        "ast_node",
        custom_finalize_default
        custom_compare_default,
        custom_hash_default,
        custom_serialize_default,
        custom_deserialize_default,
        custom_compare_ext_default
};

接下来,您需要了解如何分配自定义块。例如,以下调用将在 OCaml 堆中分配新的 AstNode:

    res = caml_alloc_custom(&ast_ops, sizeof(AstNode), 0, 1);

要访问值本身,您需要使用 Data_custom_val 宏,例如

   if (res) {
      AstNode *node = Data_custom_val(res);
      TsNode *tsnode = res->node;
   }

第一个函数的正确(我希望)实现的完整示例如下:

CAMLprim value caml_ts_document_root_node(value document) {
  CAMLparam1(document);
  CAMLlocal1(res);
  res = caml_alloc_custom(&ast_ops, sizeof(AstNodes), 0, 1);
  if (res) {
    AstNode *ast = (AstNode *)Data_custom_val(res);
    ast->node = ts_document_root_node(document);
  } 
  CAMLreturn(res);
}

如您所见,这并非微不足道,而是 low-level。虽然没有什么真正神奇的,尤其是在你阅读了 OCaml documentation. However, it is much easier to use the CTypes library 的相应部分之后,它隐藏了大部分复杂性并允许你直接从 OCaml

调用 C 函数