src/Palete/Javascript.vala
[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 = "JSValueRef", free_function = "")]
281   public class Value {
282     [CCode (cname = "JSValueMakeUndefined")]
283     public Value.undefined (Context ctx);
284
285     [CCode (cname = "JSValueMakeNull")]
286     public Value.null (Context ctx);
287
288     [CCode (cname = "JSValueMakeBoolean")]
289     public Value.boolean (Context ctx, bool boolean);
290
291     [CCode (cname = "JSValueMakeNumber")]
292     public Value.number (Context ctx, double number);
293
294     [CCode (cname = "JSValueMakeString")]
295     public Value.string (Context ctx, String js_string);
296
297     [CCode (cname = "JSValueGetType", instance_pos=1.1)]
298     public JSCore.Type get_type (Context ctx);
299
300     [CCode (cname = "JSValueIsUndefined", instance_pos=1.1)]
301     public bool is_undefined (Context ctx);
302
303     [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
304     public bool is_null (Context ctx);
305
306     [CCode (cname = "JSValueIsBoolean", instance_pos=1.1)]
307     public bool is_boolean (Context ctx);
308
309     [CCode (cname = "JSValueIsNumber", instance_pos=1.1)]
310     public bool is_number (Context ctx);
311
312     [CCode (cname = "JSValueIsString", instance_pos=1.1)]
313     public bool is_string (Context ctx);
314
315     [CCode (cname = "JSValueIsObject", instance_pos=1.1)]
316     public bool is_object (Context ctx);
317
318     [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
319     public bool is_object_of_class (Context ctx, Value js_value, Class js_class);
320
321     [CCode (cname = "JSValueIsEqual", instance_pos=1.1)]
322     public bool is_equal (Context ctx, Value b, Value *exception);
323
324     [CCode (cname = "JSValueIsStrictEqual", instance_pos=1.1)]
325     public bool is_strict_equal (Context ctx, Value b);
326
327     [CCode (cname = "JSValueIsInstanceOfConstructor", instance_pos=1.1)]
328     public bool is_instance_of_constructor (Context ctx, Value js_value, Object constructor,
329                                             Value *exception);
330
331     [CCode (cname = "JSValueToBoolean", instance_pos=1.1)]
332     public bool to_boolean (Context ctx);
333
334     [CCode (cname = "JSValueToNumber", instance_pos=1.1)]
335     public double to_number (Context ctx, Value *exception);
336
337     [CCode (cname = "JSValueToStringCopy", instance_pos=1.1)]
338     public JSCore.String to_string_copy (Context ctx, Value *exception);
339
340     [CCode (cname = "JSValueToObject", instance_pos=1.1)]
341     public Object to_object (Context ctx, out Value *exception);
342
343     [CCode (cname = "JSValueProtect", instance_pos=1.1)]
344     public void protect (Context ctx);
345
346     [CCode (cname = "JSValueUnprotect", instance_pos=1.1)]
347     public void unprotect (Context ctx);
348     
349    
350   }
351
352
353   [Compact]
354   /* FIXME: causes a warning */
355   [CCode (cname = "JSObjectRef", free_function = "")]
356   public class Object: JSCore.Value {
357     [CCode (cname = "JSObjectMake")]
358     public Object (Context ctx, Class js_class, void *data);
359
360     [CCode (cname = "JSObjectMakeFunctionWithCallback")]
361     public Object.function_with_callback (Context ctx, JSCore.String name,
362                                           ObjectCallAsFunctionCallback callAsFunction);
363
364     [CCode (cname = "JSObjectMakeConstructor")]
365     public Object.constructor (Context ctx, JSCore.Class js_class,
366                                ObjectCallAsConstructorCallback callAsConstructor);
367
368     [CCode (cname = "JSObjectMakeArray")]
369     public Object.array (Context ctx, size_t argument_count,
370                          JSCore.Value[] arguments,
371                          out JSCore.Value exception);
372
373     [CCode (cname = "JSObjectMakeDate")]
374     public Object.date (Context ctx, size_t argument_count,
375                         JSCore.Value[] arguments,
376                         out JSCore.Value exception);
377
378     [CCode (cname = "JSObjectMakeError")]
379     public Object.error (Context ctx, size_t argument_count,
380                          JSCore.Value[] arguments,
381                          out JSCore.Value exception);
382
383     [CCode (cname = "JSObjectMakeRegExp")]
384     public Object.regexp (Context ctx, size_t argument_count,
385                          JSCore.Value[] arguments,
386                          out JSCore.Value exception);
387
388     [CCode (cname = "JSObjectMakeFunction")]
389     public Object.function (Context ctx, JSCore.String name,
390                             uint parameter_count, JSCore.String[] parameter_names,
391                             JSCore.String body, JSCore.String source_url,
392                             int starting_line_number, out JSCore.Value exception);
393
394     [CCode (cname = "JSObjectGetPrototype", instance_pos=1.1)]
395     public JSCore.Value get_prototype (Context ctx);
396
397     [CCode (cname = "JSObjectSetPrototype", instance_pos=1.1)]
398     public JSCore.Value set_prototype (Context ctx, JSCore.Value _value);
399
400     [CCode (cname = "JSObjectHasProperty", instance_pos=1.1)]
401     public bool has_property (Context ctx, JSCore.String propertyName);
402
403     [CCode (cname = "JSObjectGetProperty", instance_pos=1.1)]
404     public JSCore.Value get_property (Context ctx, JSCore.String propertyName,
405                                               out JSCore.Value exception);
406
407     [CCode (cname = "JSObjectSetProperty", instance_pos=1.1)]
408     public void set_property (Context ctx, JSCore.String property_name, 
409                               JSCore.Value _value, PropertyAttribute attributes, 
410                               out JSCore.Value exception);
411
412     [CCode (cname = "JSObjectDeleteProperty", instance_pos=1.1)]
413     public bool delete_property (Context ctx, JSCore.String property_name, 
414                                  out JSCore.Value exception);
415
416     [CCode (cname = "JSObjectGetPropertyAtIndex", instance_pos=1.1)]
417     public JSCore.Value get_property_at_index (Context ctx, uint property_index,
418                                                        out JSCore.Value exception);
419
420     [CCode (cname = "JSObjectSetPropertyAtIndex", instance_pos=1.1)]
421     public void set_property_at_index (Context ctx, uint property_index,
422                                        JSCore.Value _value,
423                                        out JSCore.Value exception);
424
425     [CCode (cname = "JSObjectGetPrivate")]
426     public void *get_private ();
427
428     [CCode (cname = "JSObjectSetPrivate")]
429     public bool set_private (void *data);
430
431     [CCode (cname = "JSObjectIsFunction", instance_pos=1.1)]
432     public bool is_function (Context ctx);
433
434     [CCode (cname = "JSObjectCallAsFunction", instance_pos=1.1)]
435     public unowned JSCore.Value call_as_function (Context ctx, 
436                                                                    JSCore.Object thisObject,
437                                                                    [CCode (array_length_pos=2.9, array_length_type="size_t")]
438                                                                    JSCore.Value[] arguments,
439                                                                    out JSCore.Value exception);
440
441
442     [CCode (cname = "JSObjectIsConstructor", instance_pos=1.1)]
443     public bool is_constructor (Context ctx);
444
445     [CCode (cname = "JSObjectCallAsConstructor", instance_pos=1.1)]
446     public unowned JSCore.Object call_as_constructor (Context ctx,
447                                                           [CCode (array_length_pos=1.9, array_length_type="size_t")]
448                                                       JSCore.Value[]? arguments,
449                                                       out JSCore.Value exception);
450
451     [CCode (cname = "JSObjectCopyPropertyNames", instance_pos=1.1)]
452     public unowned PropertyNameArray copy_property_names (Context ctx);
453
454     /* Handy for debugging */
455     public void dump (JSCore.Context ctx, GLib.FileStream stream) {
456       JSCore.String js_string = this.to_string_copy (ctx, null);
457       char *c_string = new char[1024];
458       js_string.get_utf8_c_string (c_string, 1023);
459
460       unowned PropertyNameArray property_names = this.copy_property_names (ctx);
461
462       stream.printf ("Object: %s, %i properties\n", (string)c_string, (int)property_names.get_count());
463       delete c_string;
464
465       for (var i=0; i<property_names.get_count(); i++) {
466         js_string = property_names.get_name_at_index (i);
467         c_string = new char[1024];
468         js_string.get_utf8_c_string (c_string, 1023);
469         stream.printf ("\t%i: %s\n", i, (string)c_string);
470         delete c_string;
471       }
472     }
473   }
474 }