11c34730a8d494819b4ef59b4bb01264afbe8917
[gnome.seed] / libseed / seed.h
1 /* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */
2
3 /*
4  * This file is part of Seed, the GObject Introspection<->Javascript bindings.
5  *
6  * Seed is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  * Seed is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
18  */
19
20 #include <glib.h>
21 #include <glib-object.h>
22 #include <girepository.h>
23
24 #ifndef _SEED_H
25 #define _SEED_H
26
27 typedef gpointer SeedString;
28 typedef gpointer SeedValue;
29 typedef gpointer SeedObject;
30 typedef gpointer SeedClass;
31 typedef gpointer SeedException;
32
33 typedef gpointer SeedContext;
34 typedef gpointer SeedGlobalContext;
35 typedef gpointer SeedContextGroup;
36
37 typedef enum
38 {
39   SEED_TYPE_UNDEFINED,
40   SEED_TYPE_NULL,
41   SEED_TYPE_BOOLEAN,
42   SEED_TYPE_NUMBER,
43   SEED_TYPE_STRING,
44   SEED_TYPE_OBJECT
45 } SeedType;
46
47 typedef enum
48 {
49   SEED_PROPERTY_ATTRIBUTE_NONE = 0,
50   SEED_PROPERTY_ATTRIBUTE_READ_ONLY = 1 << 1,
51   SEED_PROPERTY_ATTRIBUTE_DONT_ENUM = 1 << 2,
52   SEED_PROPERTY_ATTRIBUTE_DONT_DELETE = 1 << 3
53 } SeedPropertyAttributes;
54
55 typedef enum
56 {
57   SEED_CLASS_ATTRIBUTE_NONE = 0,
58   SEED_CLASS_ATTRIBUTE_NO_SHARED_PROTOTYPE = 1 << 1
59 } SeedClassAttributes;
60
61 typedef struct _SeedScript SeedScript;
62
63 typedef struct _SeedEngine
64 {
65   SeedGlobalContext context;
66   SeedValue global;
67   gchar **search_path;
68
69   SeedContextGroup group;
70 } SeedEngine;
71
72 /*
73  * seed-engine.c
74  */
75 SeedEngine *seed_init (gint *argc, gchar ***argv);
76 SeedEngine *seed_init_with_context_group (gint *argc, gchar ***argv,
77                                           SeedContextGroup group);
78
79 void seed_engine_destroy (SeedEngine *eng);
80
81 SeedValue seed_simple_evaluate (SeedContext ctx,
82                                 gchar * source,
83                                 SeedException *exception);
84
85 SeedScript *seed_make_script (SeedContext ctx,
86                               const gchar * js,
87                               const gchar * source_url,
88                               gint line_number);
89 SeedScript *seed_script_new_from_file (SeedContext ctx, gchar * file);
90 SeedException seed_script_exception (SeedScript * s);
91 void seed_script_destroy (SeedScript *s);
92 void seed_make_exception (SeedContext ctx, SeedException exception,
93                           const gchar * name, const gchar * message, ...) G_GNUC_PRINTF (4,5);
94 gchar *seed_exception_get_name (SeedContext ctx, SeedException exception);
95 gchar *seed_exception_get_message (SeedContext ctx, SeedException exception);
96 guint seed_exception_get_line (SeedContext ctx, SeedException exception);
97 gchar *seed_exception_get_file (SeedContext ctx, SeedException exception);
98 gchar *seed_exception_to_string (SeedContext ctx, SeedException exception);
99
100 SeedValue seed_evaluate (SeedContext ctx, SeedScript * s, SeedObject this);
101
102 GOptionGroup * seed_get_option_group (void);
103
104 /*
105  * seed-api.c
106  */
107
108 SeedGlobalContext seed_context_create (SeedContextGroup group,
109                                        SeedClass global_class);
110 SeedGlobalContext seed_context_ref (SeedGlobalContext ctx);
111 void seed_context_unref (SeedGlobalContext ctx);
112 void seed_context_collect (SeedGlobalContext ctx);
113
114 SeedObject seed_context_get_global_object (SeedContext ctx);
115
116 void seed_importer_add_global(SeedContext ctx, gchar *name);
117 void seed_importer_set_search_path (SeedContext ctx,
118                                     gchar **search_path);
119 void seed_prepare_global_context (SeedContext ctx);
120
121
122
123 SeedValue seed_make_null (SeedContext ctx);
124 SeedValue seed_make_undefined (SeedContext ctx);
125
126 SeedString seed_string_ref (SeedString string);
127 void seed_string_unref (SeedString string);
128
129 gsize seed_string_get_maximum_size (SeedString string);
130 gsize seed_string_to_utf8_buffer (SeedString string, gchar * buffer,
131                                   size_t buffer_size);
132
133 gboolean seed_string_is_equal (SeedString a, SeedString b);
134 gboolean seed_string_is_equal_utf8 (SeedString a, const gchar * b);
135
136 gboolean seed_value_is_null (SeedContext ctx, SeedValue value);
137 gboolean seed_value_is_undefined (SeedContext ctx, SeedValue value);
138 gboolean seed_value_is_object (SeedContext ctx, SeedValue value);
139 gboolean seed_value_is_object_of_class (SeedContext ctx, SeedValue value, SeedClass klass);
140 gboolean seed_value_is_function (SeedContext ctx, SeedObject value);
141 gboolean seed_value_is_string (SeedContext ctx, SeedValue value);
142 gboolean seed_value_is_number (SeedContext ctx, SeedValue value);
143
144 void seed_value_unprotect (SeedContext ctx, SeedValue value);
145 void seed_value_protect (SeedContext ctx, SeedValue value);
146
147 gboolean seed_value_to_boolean (SeedContext ctx,
148                                 SeedValue val, SeedException * exception);
149
150 SeedValue seed_value_from_boolean (SeedContext ctx,
151                                    gboolean val, SeedException * exception);
152
153 guint seed_value_to_uint (SeedContext ctx,
154                           SeedValue val, SeedException * exception);
155
156 SeedValue seed_value_from_uint (SeedContext ctx,
157                                 guint val, SeedException * exception);
158
159 gint seed_value_to_int (SeedContext ctx,
160                         SeedValue val, SeedException * exception);
161
162 SeedValue seed_value_from_int (SeedContext ctx,
163                                gint val, SeedException * exception);
164
165 gchar seed_value_to_char (SeedContext ctx,
166                           SeedValue val, SeedException * exception);
167 SeedValue seed_value_from_char (SeedContext ctx,
168                                 gchar val, SeedException * exception);
169
170 guchar seed_value_to_uchar (SeedContext ctx,
171                             SeedValue val, SeedException * exception);
172 SeedValue seed_value_from_uchar (SeedContext ctx,
173                                  guchar val, SeedException * exception);
174
175 glong seed_value_to_long (SeedContext ctx,
176                           SeedValue val, SeedException * exception);
177 SeedValue seed_value_from_long (SeedContext ctx,
178                                 glong val, SeedException * exception);
179
180 gulong seed_value_to_ulong (SeedContext ctx,
181                             SeedValue val, SeedException * exception);
182 SeedValue seed_value_from_ulong (SeedContext ctx,
183                                  gulong val, SeedException * exception);
184
185 gint64 seed_value_to_int64 (SeedContext ctx,
186                             SeedValue val, SeedException * exception);
187 SeedValue seed_value_from_int64 (SeedContext ctx,
188                                  gint64 val, SeedException * exception);
189
190 guint64 seed_value_to_uint64 (SeedContext ctx,
191                               SeedValue val, SeedException * exception);
192 SeedValue seed_value_from_uint64 (SeedContext ctx,
193                                   guint64 val, SeedException * exception);
194
195 gfloat seed_value_to_float (SeedContext ctx,
196                             SeedValue val, SeedException * exception);
197 SeedValue seed_value_from_float (SeedContext ctx,
198                                  gfloat val, SeedException * exception);
199
200 gdouble seed_value_to_double (SeedContext ctx,
201                               SeedValue val, SeedException * exception);
202 SeedValue seed_value_from_double (SeedContext ctx,
203                                   gdouble val, SeedException * exception);
204
205 gchar *seed_value_to_string (SeedContext ctx,
206                              SeedValue val, SeedException * exception);
207 SeedValue seed_value_from_string (SeedContext ctx,
208                                   const gchar * val, SeedException * exception);
209
210 gchar *seed_value_to_filename (SeedContext ctx,
211                                SeedValue val, SeedValue *exception);
212 SeedValue seed_value_from_filename (SeedContext ctx,
213                                     SeedValue val, SeedValue *exception);
214
215 SeedValue seed_value_from_binary_string (SeedContext ctx,
216                                          const gchar *bytes,
217                                          gint n_bytes, 
218                                          SeedException *exception);
219
220 SeedType seed_value_get_type (SeedContext ctx, SeedValue value);
221
222 gboolean
223 seed_value_to_format (SeedContext ctx,
224                       const gchar *format,
225                       SeedValue *values,
226                       SeedValue *exception,
227                       ...);
228
229 typedef SeedObject (*SeedModuleInitCallback) (SeedEngine * eng);
230
231 gboolean seed_object_set_property (SeedContext ctx,
232                                    SeedObject object,
233                                    const gchar * name, SeedValue value);
234 SeedValue seed_object_get_property (SeedContext ctx,
235                                     SeedObject object, const gchar * name);
236
237 void seed_object_set_property_at_index (SeedContext ctx,
238                                         SeedObject object,
239                                         gint index,
240                                         SeedValue value,
241                                         SeedException * exception);
242
243 SeedValue seed_object_get_property_at_index (SeedContext ctx,
244                                              SeedObject object,
245                                              gint index,
246                                              SeedException *exception);
247
248 SeedValue seed_object_call (SeedContext ctx,
249                             SeedObject object,
250                             SeedObject this,
251                             gsize argument_count,
252                             const SeedValue arguments[],
253                             SeedException * exception);
254
255 GObject *seed_value_to_object (SeedContext ctx,
256                                SeedValue val, SeedException * exception);
257 SeedValue seed_value_from_object (SeedContext ctx,
258                                   GObject * val, SeedException * exception);
259
260 SeedObject seed_make_object (SeedContext ctx, SeedClass class,
261                              gpointer private);
262
263 gpointer seed_object_get_private (SeedObject object);
264 void seed_object_set_private (SeedObject object, gpointer value);
265
266 gchar **seed_object_copy_property_names(SeedContext ctx, SeedObject object);
267
268 gpointer seed_pointer_get_pointer (SeedContext ctx, SeedValue pointer);
269
270 SeedObject
271 seed_object_get_prototype (SeedContext ctx, SeedObject obj);
272
273 gboolean
274 seed_object_is_of_class (SeedContext ctx, SeedObject obj, SeedClass class);
275
276 SeedValue seed_make_pointer (SeedContext ctx, gpointer pointer);
277
278 typedef SeedValue (*SeedFunctionCallback) (SeedContext ctx,
279                                            SeedObject function,
280                                            SeedObject this_object,
281                                            gsize argument_count,
282                                            const SeedValue arguments[],
283                                            SeedException * exception);
284
285 void seed_create_function (SeedContext ctx,
286                            gchar * name, SeedFunctionCallback func,
287                            SeedObject obj);
288 SeedObject seed_make_function (SeedContext ctx, SeedFunctionCallback func, const gchar *name);
289
290
291 SeedObject seed_make_array (SeedContext ctx, const SeedValue elements,
292                             gsize num_elements, SeedException *exception);
293
294
295
296 typedef void (*SeedObjectInitializeCallback) (SeedContext ctx,
297                                               SeedObject object);
298
299 /* Using any functions that require a context from
300  *this callback has undefined results */
301 typedef void (*SeedObjectFinalizeCallback) (SeedObject object);
302
303 typedef gboolean (*SeedObjectHasPropertyCallback) (SeedContext ctx,
304                                                    SeedObject object,
305                                                    SeedString string);
306 typedef SeedValue (*SeedObjectGetPropertyCallback) (SeedContext ctx,
307                                                     SeedObject object,
308                                                     SeedString property_name,
309                                                     SeedException * e);
310 typedef gboolean (*SeedObjectSetPropertyCallback) (SeedContext ctx,
311                                                    SeedObject object,
312                                                    SeedString property_name,
313                                                    SeedValue value,
314                                                    SeedException * e);
315 typedef gboolean (*SeedObjectDeletePropertyCallback) (SeedContext ctx,
316                                                       SeedObject object,
317                                                       SeedString
318                                                       property_name,
319                                                       SeedValue value,
320                                                       SeedException * e);
321 /* TODO: Have to decide on accumulator API
322 //typedef void (*SeedObjectGetPropertyNamesCallback) (SeedContext ctx, */
323
324 typedef void (*SeedObjectGetPropertyNamesCallback) (void);
325
326 typedef SeedValue (*SeedObjectCallAsFunctionCallback) (SeedContext ctx,
327                                                        SeedObject function,
328                                                        SeedObject this_object,
329                                                        gsize argument_count,
330                                                        const SeedValue
331                                                        arguments[],
332                                                        SeedException *
333                                                        exception);
334 typedef SeedValue (*SeedObjectCallAsConstructorCallback) (SeedContext ctx,
335                                                           SeedObject
336                                                           constructor,
337                                                           gsize
338                                                           argument_count,
339                                                           const SeedValue
340                                                           arguments[],
341                                                           SeedException *
342                                                           exception);
343
344 typedef gboolean (*SeedObjectHasInstanceCallback) (SeedContext ctx,
345                                                    SeedObject constructor,
346                                                    SeedObject instance_p,
347                                                    SeedException * exception);
348
349 typedef SeedValue (*SeedObjectConvertToTypeCallback) (SeedContext ctx,
350                                                       SeedObject object,
351                                                       SeedType type,
352                                                       SeedException *
353                                                       exception);
354
355 typedef struct _seed_static_value
356 {
357   const gchar *const name;
358   SeedObjectGetPropertyCallback get_property;
359   SeedObjectSetPropertyCallback set_property;
360   SeedPropertyAttributes attributes;
361 } seed_static_value;
362
363 typedef struct _seed_static_function
364 {
365   const gchar *const name;
366   SeedObjectCallAsFunctionCallback callback;
367   SeedPropertyAttributes attributes;
368 } seed_static_function;
369
370 typedef struct _seed_class_definition
371 {
372   int version;                  /* Always 0 */
373   SeedClassAttributes attributes;
374
375   const gchar *class_name;
376   SeedClass parent_class;
377
378   const seed_static_value *static_values;
379   const seed_static_function *static_functions;
380
381   SeedObjectInitializeCallback initialize;
382   SeedObjectFinalizeCallback finalize;
383   SeedObjectHasPropertyCallback has_property;
384   SeedObjectGetPropertyCallback get_property;
385   SeedObjectSetPropertyCallback set_property;
386   SeedObjectDeletePropertyCallback delete_property;
387   SeedObjectGetPropertyNamesCallback get_property_names;
388   SeedObjectCallAsFunctionCallback call_as_function;
389   SeedObjectCallAsConstructorCallback call_as_constructor;
390   SeedObjectHasInstanceCallback has_instance;
391   SeedObjectConvertToTypeCallback convert_to_type;
392 } seed_class_definition;
393
394 #define seed_empty_class { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
395
396 SeedClass seed_create_class (seed_class_definition * def);
397
398 SeedObject seed_make_constructor (SeedContext ctx,
399                                   SeedClass class,
400                                   SeedObjectCallAsConstructorCallback constructor);
401
402 void seed_engine_set_search_path (SeedEngine * eng, const gchar * path);
403 gchar **seed_engine_get_search_path (SeedEngine * eng);
404
405 void
406 seed_signal_connect (SeedContext ctx,
407                      GObject *object,
408                      const gchar *signal,
409                      const gchar *script);
410
411 void
412 seed_signal_connect_value (SeedContext ctx,
413                            GObject *object,
414                            const gchar *signal,
415                            SeedValue function,
416                            SeedValue user_data);
417
418 GClosure *seed_closure_new (SeedContext ctx,
419                             SeedObject function,
420                             SeedObject user_data,
421                             const gchar *description);
422
423
424 SeedObject
425 seed_closure_get_callable (GClosure *c);
426
427 SeedValue
428 seed_closure_invoke (GClosure *closure, SeedValue *args, guint argc, SeedException *exception);
429
430 SeedValue
431 seed_closure_invoke_with_context (SeedContext ctx, GClosure *closure, SeedValue *args, guint argc, SeedException *exception);
432
433 void
434 seed_closure_warn_exception (GClosure *c, SeedContext ctx, SeedException exception);
435
436 #endif