sync
[app.Builder.js] / src / vapi / javascriptcore.vapi
1 /* javascriptcore.vapi
2  *
3  * Copyright (C) 2010 Sam Thursfield <ssssam@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  */
19
20 /* NOTE: this binding is quite inaccurate and you will have to a bunch of fixing for some functions.
21  *
22  * This is a basic interface to JSCore, a more friendly binding is Seed. See
23  * http://live.gnome.org/Seed for more information.
24  */
25
26 [CCode (lower_case_cprefix = "js_", cheader_filename = "JavaScriptCore/JavaScript.h")]
27 namespace JSCore {
28   [Compact]
29   [CCode (cname = "void", free_function = "JSContextGroupRelease")]
30   public class ContextGroup {
31     [CCode (cname = "JSContextGroupCreate")]
32     public ContextGroup();
33
34     [CCode (cname = "JSContextCreateInGroup")]
35     public ContextGroup Retain ();
36   }
37
38
39   [Compact]
40   [CCode (cname = "void")]
41   public class Context {
42     /* Script Evaluation */
43     [CCode (cname = "JSEvaluateScript")]
44     public Value evaluate_script (String script,
45                                           Object? thisObject,
46                                           String? sourceURL,
47                                           int startingLineNumber,
48                                           out Value exception);
49
50     [CCode (cname = "JSCheckScriptSyntax")]
51     public bool check_script_syntax (JSCore.String script,
52                                      JSCore.String? sourceURL, int startingLineNumber,
53                                      out JSCore.Value exception);
54
55     [CCode (cname = "JSGarbageCollect")]
56     public void garbage_collect ();
57
58     [CCode (cname = "JSContextGetGlobalObject")]
59     public JSCore.Object get_global_object ();
60
61     [CCode (cname = "JSContextGetGroup")]
62     public ContextGroup get_group ();
63   }
64
65
66   /* Same as Context */
67   [Compact]
68   [CCode (cname = "void", free_function = "JSGlobalContextRelease")]
69   public class GlobalContext: Context {
70     [CCode (cname = "JSGlobalContextCreate")]
71     public GlobalContext (Class globalObjectClass);
72
73     [CCode (cname = "JSGlobalContextCreateInGroup")]
74     public GlobalContext.in_group (ContextGroup group, Class globalObjectClass);
75
76     [CCode (cname = "JSGlobalContextRetain")]
77     public GlobalContext retain ();
78   }
79
80
81   /* FIXME: not sure the significance of this. Vala strings are UTF-8 while it seems
82    *   JSCore uses UCS-2 or UTF-16 ...
83        #if !defined(WIN32) && !defined(_WIN32) && !defined(__WINSCW__) \
84          && !(defined(__CC_ARM) || defined(__ARMCC__))
85          typedef unsigned short JSChar;
86        #else
87          typedef wchar_t JSChar;
88        #endif
89    */
90
91   [Compact]
92   [CCode (cname = "void", free_function = "JSStringRelease")]
93   public class String {
94     [CCode (cname = "JSStringCreateWithCharacters")]
95     public String.with_characters (ushort *chars, size_t num_chars);
96     
97     [CCode (cname = "JSStringCreateWithUTF8CString")]
98     public String.with_utf8_c_string (string _string);
99
100     [CCode (cname = "JSStringRetain")]
101     public String retain ();
102
103     [CCode (cname = "JSStringGetLength")]
104     public size_t get_length ();
105
106     [CCode (cname = "JSStringGetCharactersPtr")]
107     public ushort *get_characters_ptr ();
108
109     [CCode (cname = "JSStringGetMaximumUTF8CStringSize")]
110     public size_t get_maximum_utf8_c_string_size ();
111
112     [CCode (cname = "JSStringGetUTF8CString")]
113     public size_t get_utf8_c_string (char *buffer, size_t buffer_size);
114
115     [CCode (cname = "JSStringIsEqual")]
116     public bool is_equal (String b);
117
118     [CCode (cname = "JSStringIsEqualToUTF8CString")]
119     public bool is_equal_to_utf8_c_string (string b);
120   }
121
122
123   [CCode (has_target = false)]
124   public delegate void                  ObjectInitializeCallback        (Context ctx,
125                                                                         JSCore.Object object);
126
127   [CCode (has_target = false)]
128   public delegate void                  ObjectFinalizeCallback          (JSCore.Object object);
129
130   [CCode (has_target = false)]
131   public delegate bool                  ObjectHasPropertyCallback       (Context ctx,
132                                                                          JSCore.Object object,
133                                                                          JSCore.String propertyName);
134
135   [CCode (has_target = false)]
136   public delegate JSCore.Value  ObjectGetPropertyCallback       (Context ctx,
137                                                                          JSCore.Object object,
138                                                                          JSCore.String propertyName,
139                                                                          out JSCore.Value exception);
140
141   [CCode (has_target = false)]
142   public delegate bool                  ObjectSetPropertyCallback       (Context ctx,
143                                                                          JSCore.Object object,
144                                                                          JSCore.String propertyName,
145                                                                          JSCore.Value _value,
146                                                                          out JSCore.Value exception);
147
148   [CCode (has_target = false)]
149   public delegate bool                  ObjectDeletePropertyCallback    (Context ctx,
150                                                                          JSCore.Object object,
151                                                                          JSCore.String propertyName,
152                                                                          out JSCore.Value exception);
153
154   [CCode (has_target = false)]
155   public delegate void                  ObjectGetPropertyNamesCallback  (Context ctx,
156                                                                          JSCore.Object object,
157                                                                          PropertyNameAccumulator propertyNames);
158
159   [CCode (has_target = false)]
160   public delegate JSCore.Value  ObjectCallAsFunctionCallback    (Context ctx,
161                                                                          JSCore.Object function,
162                                                                          JSCore.Object thisObject,
163                                                                            [CCode (array_length_pos=3.9, array_length_type="size_t")]
164                                                                          JSCore.Value[] arguments,
165                                                                          out JSCore.Value exception);
166
167   [CCode (has_target = false)]
168   public delegate JSCore.Object ObjectCallAsConstructorCallback (Context ctx,
169                                                                          JSCore.Object constructor,
170                                                                            [CCode (array_length_pos=2.9, array_length_type="size_t")]
171                                                                          JSCore.Value[] arguments,
172                                                                          out JSCore.Value exception);
173
174   [CCode (has_target = false)]
175   public delegate bool                  ObjectHasInstanceCallback       (Context ctx,
176                                                                          JSCore.Object constructor,
177                                                                          JSCore.Value possibleInstance,
178                                                                          out JSCore.Value exception);
179
180   [CCode (has_target = false)]
181   public delegate JSCore.Value  ObjectConvertToTypeCallback     (Context ctx,
182                                                                          JSCore.Object object,
183                                                                          JSCore.Type type,
184                                                                          out JSCore.Value exception);
185
186   public struct StaticValue {
187     public string name;
188     public ObjectGetPropertyCallback getProperty;
189     public ObjectSetPropertyCallback setProperty;
190     public PropertyAttribute attributes;
191   }
192
193   [CCode (cname = "JSStaticFunction")]
194   public struct StaticFunction {
195     public string name;
196     public ObjectCallAsFunctionCallback callAsFunction;
197     public PropertyAttribute attributes;
198   }
199
200   [CCode (cname = "JSClassDefinition")]
201   public struct ClassDefinition {
202     public int version;
203     public ClassAttribute attributes;
204
205     public string className;
206     public JSCore.Class parentClass;
207
208     public StaticValue *staticValues;
209     public StaticFunction *staticFunction;
210
211     public ObjectInitializeCallback          initialize;
212     public ObjectFinalizeCallback            finalize;
213     public ObjectHasPropertyCallback         hasProperty;
214     public ObjectGetPropertyCallback         getProperty;
215     public ObjectSetPropertyCallback         setProperty;
216     public ObjectDeletePropertyCallback      deleteProperty;
217     public ObjectGetPropertyNamesCallback    getPropertyNames;
218     public ObjectCallAsFunctionCallback      callAsFunction;
219     public ObjectCallAsConstructorCallback   callAsConstructor;
220     public ObjectHasInstanceCallback         hasInstance;
221     public ObjectConvertToTypeCallback       convertToType;
222   }
223
224   [CCode (cname="kJSClassDefinitionEmpty")]
225   extern ClassDefinition ClassDefinitionEmpty;
226
227   //typedef unsigned JSClassAttributes;
228   [CCode (cprefix="kJSClassAttribute")]
229   [Flags]
230   public enum ClassAttribute {
231     None, NoAutomaticPrototype
232   }
233
234   [Compact]
235   [CCode (cname = "void", free_function = "JSClassRelease")]
236   public class Class {
237     [CCode (cname="JSClassCreate")]
238     public Class (ClassDefinition definition);
239
240     [CCode (cname="JSClassRetain")]
241     public Class retain (Class js_class);
242   }
243
244
245   //typedef unsigned JSPropertyAttributes;
246   [CCode (cprefix="kJSPropertyAttribute")]
247   [Flags]
248   public enum PropertyAttribute {
249     None, ReadOnly, DontEnum, DontDelete
250   }
251
252   [Compact]
253   [CCode (cname = "void *", free_function = "JSPropertyNameArrayRelease")]
254   public class PropertyNameArray {
255     [CCode (cname = "JSPropertyNameArrayRetain")]
256     public PropertyNameArray retain ();
257
258     [CCode (cname = "JSPropertyNameArrayGetCount")]
259     public size_t get_count ();
260
261     [CCode (cname = "JSPropertyNameArrayGetNameAtIndex")]
262     public JSCore.String get_name_at_index (size_t index);
263   }
264
265   [Compact]
266   [CCode (cname = "void")]
267   public class PropertyNameAccumulator {
268     [CCode (cname = "JSPropertyNameAccumulatorAddName")]
269     public void add_name (String property_name);
270   }
271
272
273   [CCode (cprefix="kJSType")]
274   public enum Type {
275     Undefined, Null, Boolean, Number, String, Object
276   }
277
278   [Compact]
279   /* FIXME: free_function causes a warning */
280   [CCode (cname = "void", free_function = "(void)0")]
281   [CCode (cname = "JSValueRef", free_function = "")]
282   public class Value {
283     [CCode (cname = "JSValueMakeUndefined")]
284     public Value.undefined (Context ctx);
285
286     [CCode (cname = "JSValueMakeNull")]
287     public Value.null (Context ctx);
288
289     [CCode (cname = "JSValueMakeBoolean")]
290     public Value.boolean (Context ctx, bool boolean);
291
292     [CCode (cname = "JSValueMakeNumber")]
293     public Value.number (Context ctx, double number);
294
295     [CCode (cname = "JSValueMakeString")]
296     public Value.string (Context ctx, String js_string);
297
298     [CCode (cname = "JSValueGetType", instance_pos=1.1)]
299     public JSCore.Type get_type (Context ctx);
300
301     [CCode (cname = "JSValueIsUndefined", instance_pos=1.1)]
302     public bool is_undefined (Context ctx);
303
304     [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
305     public bool is_null (Context ctx);
306
307     [CCode (cname = "JSValueIsBoolean", instance_pos=1.1)]
308     public bool is_boolean (Context ctx);
309
310     [CCode (cname = "JSValueIsNumber", instance_pos=1.1)]
311     public bool is_number (Context ctx);
312
313     [CCode (cname = "JSValueIsString", instance_pos=1.1)]
314     public bool is_string (Context ctx);
315
316     [CCode (cname = "JSValueIsObject", instance_pos=1.1)]
317     public bool is_object (Context ctx);
318
319     [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
320     public bool is_object_of_class (Context ctx, Value js_value, Class js_class);
321
322     [CCode (cname = "JSValueIsEqual", instance_pos=1.1)]
323     public bool is_equal (Context ctx, Value b, Value *exception);
324
325     [CCode (cname = "JSValueIsStrictEqual", instance_pos=1.1)]
326     public bool is_strict_equal (Context ctx, Value b);
327
328     [CCode (cname = "JSValueIsInstanceOfConstructor", instance_pos=1.1)]
329     public bool is_instance_of_constructor (Context ctx, Value js_value, Object constructor,
330                                             Value *exception);
331
332     [CCode (cname = "JSValueToBoolean", instance_pos=1.1)]
333     public bool to_boolean (Context ctx);
334
335     [CCode (cname = "JSValueToNumber", instance_pos=1.1)]
336     public double to_number (Context ctx, Value *exception);
337
338     [CCode (cname = "JSValueToStringCopy", instance_pos=1.1)]
339     public JSCore.String to_string_copy (Context ctx, Value *exception);
340
341     [CCode (cname = "JSValueToObject", instance_pos=1.1)]
342     public Object to_object (Context ctx, out Value *exception);
343
344     [CCode (cname = "JSValueProtect", instance_pos=1.1)]
345     public void protect (Context ctx);
346
347     [CCode (cname = "JSValueUnprotect", instance_pos=1.1)]
348     public void unprotect (Context ctx);
349     
350    
351   }
352
353
354   [Compact]
355   /* FIXME: causes a warning */
356   [CCode (cname = "JSObjectRef", free_function = "")]
357   public class Object: JSCore.Value {
358     [CCode (cname = "JSObjectMake")]
359     public Object (Context ctx, Class js_class, void *data);
360
361     [CCode (cname = "JSObjectMakeFunctionWithCallback")]
362     public Object.function_with_callback (Context ctx, JSCore.String name,
363                                           ObjectCallAsFunctionCallback callAsFunction);
364
365     [CCode (cname = "JSObjectMakeConstructor")]
366     public Object.constructor (Context ctx, JSCore.Class js_class,
367                                ObjectCallAsConstructorCallback callAsConstructor);
368
369     [CCode (cname = "JSObjectMakeArray")]
370     public Object.array (Context ctx, size_t argument_count,
371                          JSCore.Value[] arguments,
372                          out JSCore.Value exception);
373
374     [CCode (cname = "JSObjectMakeDate")]
375     public Object.date (Context ctx, size_t argument_count,
376                         JSCore.Value[] arguments,
377                         out JSCore.Value exception);
378
379     [CCode (cname = "JSObjectMakeError")]
380     public Object.error (Context ctx, size_t argument_count,
381                          JSCore.Value[] arguments,
382                          out JSCore.Value exception);
383
384     [CCode (cname = "JSObjectMakeRegExp")]
385     public Object.regexp (Context ctx, size_t argument_count,
386                          JSCore.Value[] arguments,
387                          out JSCore.Value exception);
388
389     [CCode (cname = "JSObjectMakeFunction")]
390     public Object.function (Context ctx, JSCore.String name,
391                             uint parameter_count, JSCore.String[] parameter_names,
392                             JSCore.String body, JSCore.String source_url,
393                             int starting_line_number, out JSCore.Value exception);
394
395     [CCode (cname = "JSObjectGetPrototype", instance_pos=1.1)]
396     public JSCore.Value get_prototype (Context ctx);
397
398     [CCode (cname = "JSObjectSetPrototype", instance_pos=1.1)]
399     public JSCore.Value set_prototype (Context ctx, JSCore.Value _value);
400
401     [CCode (cname = "JSObjectHasProperty", instance_pos=1.1)]
402     public bool has_property (Context ctx, JSCore.String propertyName);
403
404     [CCode (cname = "JSObjectGetProperty", instance_pos=1.1)]
405     public JSCore.Value get_property (Context ctx, JSCore.String propertyName,
406                                               out JSCore.Value exception);
407
408     [CCode (cname = "JSObjectSetProperty", instance_pos=1.1)]
409     public void set_property (Context ctx, JSCore.String property_name, 
410                               JSCore.Value _value, PropertyAttribute attributes, 
411                               out JSCore.Value exception);
412
413     [CCode (cname = "JSObjectDeleteProperty", instance_pos=1.1)]
414     public bool delete_property (Context ctx, JSCore.String property_name, 
415                                  out JSCore.Value exception);
416
417     [CCode (cname = "JSObjectGetPropertyAtIndex", instance_pos=1.1)]
418     public JSCore.Value get_property_at_index (Context ctx, uint property_index,
419                                                        out JSCore.Value exception);
420
421     [CCode (cname = "JSObjectSetPropertyAtIndex", instance_pos=1.1)]
422     public void set_property_at_index (Context ctx, uint property_index,
423                                        JSCore.Value _value,
424                                        out JSCore.Value exception);
425
426     [CCode (cname = "JSObjectGetPrivate")]
427     public void *get_private ();
428
429     [CCode (cname = "JSObjectSetPrivate")]
430     public bool set_private (void *data);
431
432     [CCode (cname = "JSObjectIsFunction", instance_pos=1.1)]
433     public bool is_function (Context ctx);
434
435     [CCode (cname = "JSObjectCallAsFunction", instance_pos=1.1)]
436     public unowned JSCore.Value call_as_function (Context ctx, 
437                                                                    JSCore.Object thisObject,
438                                                                    [CCode (array_length_pos=2.9, array_length_type="size_t")]
439                                                                    JSCore.Value[] arguments,
440                                                                    out JSCore.Value exception);
441
442
443     [CCode (cname = "JSObjectIsConstructor", instance_pos=1.1)]
444     public bool is_constructor (Context ctx);
445
446     [CCode (cname = "JSObjectCallAsConstructor", instance_pos=1.1)]
447     public unowned JSCore.Object call_as_constructor (Context ctx,
448                                                           [CCode (array_length_pos=1.9, array_length_type="size_t")]
449                                                       JSCore.Value[]? arguments,
450                                                       out JSCore.Value exception);
451
452     [CCode (cname = "JSObjectCopyPropertyNames", instance_pos=1.1)]
453     public unowned PropertyNameArray copy_property_names (Context ctx);
454
455     /* Handy for debugging */
456     public void dump (JSCore.Context ctx, GLib.FileStream stream) {
457       JSCore.String js_string = this.to_string_copy (ctx, null);
458       char *c_string = new char[1024];
459       js_string.get_utf8_c_string (c_string, 1023);
460
461       unowned PropertyNameArray property_names = this.copy_property_names (ctx);
462
463       stream.printf ("Object: %s, %i properties\n", (string)c_string, (int)property_names.get_count());
464       delete c_string;
465
466       for (var i=0; i<property_names.get_count(); i++) {
467         js_string = property_names.get_name_at_index (i);
468         c_string = new char[1024];
469         js_string.get_utf8_c_string (c_string, 1023);
470         stream.printf ("\t%i: %s\n", i, (string)c_string);
471         delete c_string;
472       }
473     }
474   }
475 }