How to pass struct as argument of function
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Bắt đầu với các khai báo Fiddle::Importer, Fiddle::CParser và struct được nêu trong issue, sau đó tái hiện các lệnh gọi tc_create_context, tc_read_string và tc_destroy_string đối với header C được cung cấp. Xác nhận cách biểu diễn đối số struct và giá trị trả về, đồng thời ghi lại một ví dụ hoạt động được mà không gặp các lỗi và segmentation fault đã báo cáo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hi, sorry for my english. I will show the problem with examples:
header.h
typedef struct {
const char* content;
uint32_t len;
} tc_string_data_t;
typedef struct tc_string_handle_t tc_string_handle_t;
tc_string_handle_t* tc_create_context(tc_string_data_t config);
tc_string_data_t tc_read_string(const tc_string_handle_t* handle);
void tc_destroy_string(const tc_string_handle_t* handle);
How its work in ruby with gem ffi
require "json"
require "ffi"
module Binding
class TcStringDataT < FFI::Struct
layout :content, :pointer,
:len, :uint32
end
end
module Binding
extend FFI::Library
ffi_lib "/path_to_lib"
def self.setup_bindings
attach_function :tc_create_context, [TcStringDataT.by_value], :pointer
attach_function :tc_read_string, [:pointer], TcStringDataT.by_value
attach_function :tc_destroy_string, [:pointer], :void
end
end
config_json = {network: {endpoints: ["http://some_url"]}}.to_json
# make struct
data_struct = Binding::TcStringDataT.new
data_struct[:content] = FFI::MemoryPointer.from_string(config_json)
data_struct[:len] = config_json.bytesize
# create context
string_ref = Binding.tc_create_context(data_struct)
# read context response
result_data_struct = Binding.tc_read_string(string_ref)
result_string = result_data_struct[:content].read_string(result_data_struct[:len])
# destroy ref
Binding.tc_destroy_string(string_ref)
ffi gem works, but sometimes i get segmentation fault :(
So I decided to try to do it with fiddle !
But how ? How to do it with fiddle ? I climbed the entire Internet. I haven't found a single example of how to pass a struct to a function. There are only very few examples of how to pass ordinary primitives and not a single example of how to pass a structure. I have tried many options and still have not been able to achieve a result. Please help me !!! Write an example of calling these functions and passing arguments to them. Really looking forward to the answer.
Here is what I do with fidl:
require 'fiddle'
require 'fiddle/import'
require 'json'
module TestFiddle
extend Fiddle::Importer
extend Fiddle::CParser
dlload('/lib_client.dylib')
TcStringDataT = struct [
'const char* content',
'uint len'
]
# tc_string_handle_t* tc_create_context(tc_string_data_t config);
# extern 'TcStringDataT* tc_create_context(TcStringDataT config)' ## unknown type TcStringDataT
extern 'TcStringDataT* tc_create_context(TcStringDataT* config)' ## OK
# tc_string_data_t tc_read_string(const tc_string_handle_t* handle);
# extern 'TcStringDataT tc_read_string(const TcStringDataT* handle)' ## unknown type TcStringDataT
extern 'TcStringDataT* tc_read_string(const TcStringDataT* handle)' ## OK
# void tc_destroy_string(const tc_string_handle_t* handle)
# extern 'void tc_destroy_string(const TcStringDataT handle)' ## unknown type TcStringDataT
extern 'void tc_destroy_string(const TcStringDataT* handle)' ## OK
end
config_json = {network: {endpoints: ["http://some_url"]}}.to_json
struct = TestFiddle::TcStringDataT.malloc
struct.content = Fiddle::Pointer.to_ptr(config_json)
struct.len = config_json.bytesize
# How to pass this struct ???
ctx_ptr = TestFiddle.tc_create_context(struct) ## ERROR ERROR ERROR, many many zeros and ones
string_ptr = TestFiddle.tc_read_string(ctx_ptr)
struct = Test::TcStringDataT.new(string_ptr)
p struct.content.to_s
please help me with this 🙏
- Ngôn ngữ chính
- Ruby
- Star
- 195
- Fork
- 53
- Merge trung bình
- 23 giờ 57 phút
- Pull request đã merge (30 ngày)
- 1
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ruby/fiddle
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 52/100
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
バグ
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
Version bump for OpenVox 9 Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
voxpupuli/puppet-epel#186 · 1 bình luận ·
-
external_created_at is no longer used for the message timestamp since the new message UI (v4.4.0) Đang mởBug Frontend
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100