4 #include <caml/mlvalues.h>
5 #include <caml/alloc.h>
6 #include <caml/memory.h>
7 #include <caml/callback.h>
13 value Val_GSList (GSList *list, value (*func)(gpointer))
15 value new_cell, result, last_cell, cell;
17 if (list == NULL) return Val_unit;
19 last_cell = cell = Val_unit;
20 result = func(list->data);
21 Begin_roots3 (last_cell, cell, result);
22 cell = last_cell = alloc_tuple (2);
23 Field(cell,0) = result;
24 Field(cell,1) = Val_unit;
26 while (list != NULL) {
27 result = func(list->data);
28 new_cell = alloc_tuple(2);
29 Field(new_cell,0) = result;
30 Field(new_cell,1) = Val_unit;
31 modify(&Field(last_cell,1), new_cell);
39 GSList *GSList_val (value list, gpointer (*func)(value))
42 GSList **current = &res;
44 if (list == Val_unit) return res;
46 while (cell != Val_unit) {
47 *current = g_slist_alloc ();
48 (*current)->data = func(Field(cell,0));
50 current = &(*current)->next;
57 value Val_GList (GList *list, value (*func)(gpointer))
59 value new_cell, result, last_cell, cell;
61 if (list == NULL) return Val_unit;
63 last_cell = cell = Val_unit;
64 result = func(list->data);
65 Begin_roots3 (last_cell, cell, result);
66 cell = last_cell = alloc_small(2,0);
67 Field(cell,0) = result;
68 Field(cell,1) = Val_unit;
70 while (list != NULL) {
71 result = func(list->data);
72 new_cell = alloc_small(2,0);
73 Field(new_cell,0) = result;
74 Field(new_cell,1) = Val_unit;
75 modify(&Field(last_cell,1), new_cell);
83 GList *GList_val (value list, gpointer (*func)(value))
87 if (list == Val_unit) CAMLreturn (res);
88 for (; Is_block(list); list = Field(list,1))
89 res = g_list_append (res, func(Field(list,0)));
93 static value ml_warning_handler = 0L;
95 static void ml_warning_wrapper (const gchar *msg)
97 value arg = copy_string ((char*)msg);
98 callback (ml_warning_handler, arg);
101 value ml_g_set_warning_handler (value clos)
103 value old_handler = ml_warning_handler ? ml_warning_handler : clos;
104 if (!ml_warning_handler) register_global_root (&ml_warning_handler);
105 g_set_warning_handler (ml_warning_wrapper);
106 ml_warning_handler = clos;
110 static value ml_print_handler = 0L;
112 static void ml_print_wrapper (const gchar *msg)
114 value arg = copy_string ((char*)msg);
115 callback (ml_print_handler, arg);
118 value ml_g_set_print_handler (value clos)
120 value old_handler = ml_print_handler ? ml_print_handler : clos;
121 if (!ml_print_handler) register_global_root (&ml_print_handler);
122 g_set_print_handler (ml_print_wrapper);
123 ml_print_handler = clos;
127 value ml_get_null (value unit) { return 0L; }
129 #define GMainLoop_val(val) ((GMainLoop*)Addr_val(val))
130 ML_1 (g_main_new, Bool_val, Val_addr)
131 ML_1 (g_main_iteration, Bool_val, Val_bool)
132 ML_0 (g_main_pending, Val_bool)
133 ML_1 (g_main_is_running, GMainLoop_val, Val_bool)
134 ML_1 (g_main_quit, GMainLoop_val, Unit)
135 ML_1 (g_main_destroy, GMainLoop_val, Unit)