-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcfunc_struct.c
More file actions
90 lines (69 loc) · 2.34 KB
/
cfunc_struct.c
File metadata and controls
90 lines (69 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// CFunc::Struct class
//
// See Copyright Notice in cfunc.h
//
#include "cfunc_struct.h"
#include "cfunc_type.h"
#include "cfunc_pointer.h"
#include "mruby/class.h"
#include "mruby/data.h"
#include "mruby/array.h"
#include "mruby/string.h"
#include "mruby/variable.h"
#include "ffi.h"
#include <stdio.h>
#include <stdbool.h>
static void
cfunc_struct_data_destructor(mrb_state *mrb, void *p_)
{
// todo
};
const struct mrb_data_type cfunc_struct_data_type = {
"cfunc_struct", cfunc_struct_data_destructor,
};
static mrb_value
cfunc_type_ffi_struct_c_to_mrb(mrb_state *mrb, void *p)
{
// todo
return mrb_nil_value();
}
static void
cfunc_type_ffi_struct_mrb_to_c(mrb_state *mrb, mrb_value val, void *p)
{
// todo
}
mrb_value
cfunc_struct_define_struct(mrb_state *mrb, mrb_value klass)
{
mrb_value elements_mrb;
mrb_get_args(mrb, "A", &elements_mrb);
struct RArray *elements = mrb_ary_ptr(elements_mrb);
ffi_type *tm_type = mrb_malloc(mrb, sizeof(ffi_type));
tm_type->type = FFI_TYPE_STRUCT;
tm_type->size = tm_type->alignment = 0;
ffi_type **tm_type_elements = mrb_malloc(mrb, sizeof(ffi_type*) * (elements->len + 1));
int i;
for(i = 0; i < elements->len; ++i) {
tm_type_elements[i] = rclass_to_mrb_ffi_type(mrb, mrb_class_ptr(elements->ptr[i]))->ffi_type_value;
}
tm_type_elements[i] = NULL;
tm_type->elements = tm_type_elements;
struct mrb_ffi_type *mft = mrb_malloc(mrb, sizeof(struct mrb_ffi_type));
mft->name = mrb_class_name(mrb, mrb_class_ptr(klass));
mft->ffi_type_value = tm_type;
mft->mrb_to_c = &cfunc_type_ffi_struct_mrb_to_c;
mft->c_to_mrb = &cfunc_type_ffi_struct_c_to_mrb;
mrb_value __ffi_type = mrb_obj_value(Data_Wrap_Struct(mrb, mrb->object_class, &cfunc_struct_data_type, mft));
mrb_obj_iv_set(mrb, (struct RObject*)(mrb_class_ptr(klass)), mrb_intern_cstr(mrb, "@ffi_type"), __ffi_type);
return mrb_nil_value();
}
void
init_cfunc_struct(mrb_state *mrb, struct RClass* module)
{
struct cfunc_state *state = cfunc_state(mrb, module);
struct RClass *struct_class = mrb_define_class_under(mrb, module, "Struct", mrb->object_class);
set_cfunc_state(mrb, struct_class, state);
state->struct_class = struct_class;
mrb_define_class_method(mrb, struct_class, "define_struct", cfunc_struct_define_struct, MRB_ARGS_REQ(1));
}