3 * Copyright (C) 2010 Sam Thursfield <ssssam@gmail.com>
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.
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.
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
20 /* NOTE: this binding is quite inaccurate and you will have to a bunch of fixing for some functions.
22 * This is a basic interface to JSCore, a more friendly binding is Seed. See
23 * http://live.gnome.org/Seed for more information.
26 [CCode (lower_case_cprefix = "js_", cheader_filename = "JavaScriptCore/JavaScript.h")]
29 [CCode (cname = "void", free_function = "JSContextGroupRelease")]
30 public class ContextGroup {
31 [CCode (cname = "JSContextGroupCreate")]
32 public ContextGroup();
34 [CCode (cname = "JSContextCreateInGroup")]
35 public ContextGroup Retain ();
40 [CCode (cname = "void")]
41 public class Context {
42 /* Script Evaluation */
43 [CCode (cname = "JSEvaluateScript")]
44 public Value evaluate_script (String script,
47 int startingLineNumber,
50 [CCode (cname = "JSCheckScriptSyntax")]
51 public bool check_script_syntax (JSCore.String script,
52 JSCore.String? sourceURL, int startingLineNumber,
53 out JSCore.Value exception);
55 [CCode (cname = "JSGarbageCollect")]
56 public void garbage_collect ();
58 [CCode (cname = "JSContextGetGlobalObject")]
59 public JSCore.Object get_global_object ();
61 [CCode (cname = "JSContextGetGroup")]
62 public ContextGroup get_group ();
68 [CCode (cname = "void", free_function = "JSGlobalContextRelease")]
69 public class GlobalContext: Context {
70 [CCode (cname = "JSGlobalContextCreate")]
71 public GlobalContext (Class globalObjectClass);
73 [CCode (cname = "JSGlobalContextCreateInGroup")]
74 public GlobalContext.in_group (ContextGroup group, Class globalObjectClass);
76 [CCode (cname = "JSGlobalContextRetain")]
77 public GlobalContext retain ();
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;
87 typedef wchar_t JSChar;
92 [CCode (cname = "void", free_function = "JSStringRelease")]
94 [CCode (cname = "JSStringCreateWithCharacters")]
95 public String.with_characters (ushort *chars, size_t num_chars);
97 [CCode (cname = "JSStringCreateWithUTF8CString")]
98 public String.with_utf8_c_string (string _string);
100 [CCode (cname = "JSStringRetain")]
101 public String retain ();
103 [CCode (cname = "JSStringGetLength")]
104 public size_t get_length ();
106 [CCode (cname = "JSStringGetCharactersPtr")]
107 public ushort *get_characters_ptr ();
109 [CCode (cname = "JSStringGetMaximumUTF8CStringSize")]
110 public size_t get_maximum_utf8_c_string_size ();
112 [CCode (cname = "JSStringGetUTF8CString")]
113 public size_t get_utf8_c_string (char *buffer, size_t buffer_size);
115 [CCode (cname = "JSStringIsEqual")]
116 public bool is_equal (String b);
118 [CCode (cname = "JSStringIsEqualToUTF8CString")]
119 public bool is_equal_to_utf8_c_string (string b);
123 [CCode (has_target = false)]
124 public delegate void ObjectInitializeCallback (Context ctx,
125 JSCore.Object object);
127 [CCode (has_target = false)]
128 public delegate void ObjectFinalizeCallback (JSCore.Object object);
130 [CCode (has_target = false)]
131 public delegate bool ObjectHasPropertyCallback (Context ctx,
132 JSCore.Object object,
133 JSCore.String propertyName);
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);
141 [CCode (has_target = false)]
142 public delegate bool ObjectSetPropertyCallback (Context ctx,
143 JSCore.Object object,
144 JSCore.String propertyName,
146 out JSCore.Value exception);
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);
154 [CCode (has_target = false)]
155 public delegate void ObjectGetPropertyNamesCallback (Context ctx,
156 JSCore.Object object,
157 PropertyNameAccumulator propertyNames);
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);
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);
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);
180 [CCode (has_target = false)]
181 public delegate JSCore.Value ObjectConvertToTypeCallback (Context ctx,
182 JSCore.Object object,
184 out JSCore.Value exception);
186 public struct StaticValue {
188 public ObjectGetPropertyCallback getProperty;
189 public ObjectSetPropertyCallback setProperty;
190 public PropertyAttribute attributes;
193 [CCode (cname = "JSStaticFunction")]
194 public struct StaticFunction {
196 public ObjectCallAsFunctionCallback callAsFunction;
197 public PropertyAttribute attributes;
200 [CCode (cname = "JSClassDefinition")]
201 public struct ClassDefinition {
203 public ClassAttribute attributes;
205 public string className;
206 public JSCore.Class parentClass;
208 public StaticValue *staticValues;
209 public StaticFunction *staticFunction;
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;
224 [CCode (cname="kJSClassDefinitionEmpty")]
225 extern ClassDefinition ClassDefinitionEmpty;
227 //typedef unsigned JSClassAttributes;
228 [CCode (cprefix="kJSClassAttribute")]
230 public enum ClassAttribute {
231 None, NoAutomaticPrototype
235 [CCode (cname = "void", free_function = "JSClassRelease")]
237 [CCode (cname="JSClassCreate")]
238 public Class (ClassDefinition definition);
240 [CCode (cname="JSClassRetain")]
241 public Class retain (Class js_class);
245 //typedef unsigned JSPropertyAttributes;
246 [CCode (cprefix="kJSPropertyAttribute")]
248 public enum PropertyAttribute {
249 None, ReadOnly, DontEnum, DontDelete
253 [CCode (cname = "void *", free_function = "JSPropertyNameArrayRelease")]
254 public class PropertyNameArray {
255 [CCode (cname = "JSPropertyNameArrayRetain")]
256 public PropertyNameArray retain ();
258 [CCode (cname = "JSPropertyNameArrayGetCount")]
259 public size_t get_count ();
261 [CCode (cname = "JSPropertyNameArrayGetNameAtIndex")]
262 public JSCore.String get_name_at_index (size_t index);
266 [CCode (cname = "void")]
267 public class PropertyNameAccumulator {
268 [CCode (cname = "JSPropertyNameAccumulatorAddName")]
269 public void add_name (String property_name);
273 [CCode (cprefix="kJSType")]
275 Undefined, Null, Boolean, Number, String, Object
279 /* FIXME: free_function causes a warning */
280 [CCode (cname = "void", free_function = "")]
282 [CCode (cname = "JSValueMakeUndefined")]
283 public Value.undefined (Context ctx);
285 [CCode (cname = "JSValueMakeNull")]
286 public Value.null (Context ctx);
288 [CCode (cname = "JSValueMakeBoolean")]
289 public Value.boolean (Context ctx, bool boolean);
291 [CCode (cname = "JSValueMakeNumber")]
292 public Value.number (Context ctx, double number);
294 [CCode (cname = "JSValueMakeString")]
295 public Value.string (Context ctx, String js_string);
297 [CCode (cname = "JSValueGetType", instance_pos=1.1)]
298 public JSCore.Type get_type (Context ctx);
300 [CCode (cname = "JSValueIsUndefined", instance_pos=1.1)]
301 public bool is_undefined (Context ctx);
303 [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
304 public bool is_null (Context ctx);
306 [CCode (cname = "JSValueIsBoolean", instance_pos=1.1)]
307 public bool is_boolean (Context ctx);
309 [CCode (cname = "JSValueIsNumber", instance_pos=1.1)]
310 public bool is_number (Context ctx);
312 [CCode (cname = "JSValueIsString", instance_pos=1.1)]
313 public bool is_string (Context ctx);
315 [CCode (cname = "JSValueIsObject", instance_pos=1.1)]
316 public bool is_object (Context ctx);
318 [CCode (cname = "JSValueIsNull", instance_pos=1.1)]
319 public bool is_object_of_class (Context ctx, Value js_value, Class js_class);
321 [CCode (cname = "JSValueIsEqual", instance_pos=1.1)]
322 public bool is_equal (Context ctx, Value b, Value *exception);
324 [CCode (cname = "JSValueIsStrictEqual", instance_pos=1.1)]
325 public bool is_strict_equal (Context ctx, Value b);
327 [CCode (cname = "JSValueIsInstanceOfConstructor", instance_pos=1.1)]
328 public bool is_instance_of_constructor (Context ctx, Value js_value, Object constructor,
331 [CCode (cname = "JSValueToBoolean", instance_pos=1.1)]
332 public bool to_boolean (Context ctx);
334 [CCode (cname = "JSValueToNumber", instance_pos=1.1)]
335 public double to_number (Context ctx, Value *exception);
337 [CCode (cname = "JSValueToStringCopy", instance_pos=1.1)]
338 public JSCore.String to_string_copy (Context ctx, out Value *exception);
340 [CCode (cname = "JSValueToObject", instance_pos=1.1)]
341 public Object to_object (Context ctx, out Value *exception);
343 [CCode (cname = "JSValueProtect", instance_pos=1.1)]
344 public void protect (Context ctx);
346 [CCode (cname = "JSValueUnprotect", instance_pos=1.1)]
347 public void unprotect (Context ctx);
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);
360 [CCode (cname = "JSObjectMakeFunctionWithCallback")]
361 public Object.function_with_callback (Context ctx, JSCore.String name,
362 ObjectCallAsFunctionCallback callAsFunction);
364 [CCode (cname = "JSObjectMakeConstructor")]
365 public Object.constructor (Context ctx, JSCore.Class js_class,
366 ObjectCallAsConstructorCallback callAsConstructor);
368 [CCode (cname = "JSObjectMakeArray")]
369 public Object.array (Context ctx, size_t argument_count,
370 JSCore.Value[] arguments,
371 out JSCore.Value exception);
373 [CCode (cname = "JSObjectMakeDate")]
374 public Object.date (Context ctx, size_t argument_count,
375 JSCore.Value[] arguments,
376 out JSCore.Value exception);
378 [CCode (cname = "JSObjectMakeError")]
379 public Object.error (Context ctx, size_t argument_count,
380 JSCore.Value[] arguments,
381 out JSCore.Value exception);
383 [CCode (cname = "JSObjectMakeRegExp")]
384 public Object.regexp (Context ctx, size_t argument_count,
385 JSCore.Value[] arguments,
386 out JSCore.Value exception);
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);
394 [CCode (cname = "JSObjectGetPrototype", instance_pos=1.1)]
395 public JSCore.Value get_prototype (Context ctx);
397 [CCode (cname = "JSObjectSetPrototype", instance_pos=1.1)]
398 public JSCore.Value set_prototype (Context ctx, JSCore.Value _value);
400 [CCode (cname = "JSObjectHasProperty", instance_pos=1.1)]
401 public bool has_property (Context ctx, JSCore.String propertyName);
403 [CCode (cname = "JSObjectGetProperty", instance_pos=1.1)]
404 public JSCore.Value get_property (Context ctx, JSCore.String propertyName,
405 out JSCore.Value exception);
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);
412 [CCode (cname = "JSObjectDeleteProperty", instance_pos=1.1)]
413 public bool delete_property (Context ctx, JSCore.String property_name,
414 out JSCore.Value exception);
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);
420 [CCode (cname = "JSObjectSetPropertyAtIndex", instance_pos=1.1)]
421 public void set_property_at_index (Context ctx, uint property_index,
423 out JSCore.Value exception);
425 [CCode (cname = "JSObjectGetPrivate")]
426 public void *get_private ();
428 [CCode (cname = "JSObjectSetPrivate")]
429 public bool set_private (void *data);
431 [CCode (cname = "JSObjectIsFunction", instance_pos=1.1)]
432 public bool is_function (Context ctx);
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);
442 [CCode (cname = "JSObjectIsConstructor", instance_pos=1.1)]
443 public bool is_constructor (Context ctx);
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);
451 [CCode (cname = "JSObjectCopyPropertyNames", instance_pos=1.1)]
452 public unowned PropertyNameArray copy_property_names (Context ctx);
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);
460 unowned PropertyNameArray property_names = this.copy_property_names (ctx);
462 stream.printf ("Object: %s, %i properties\n", (string)c_string, (int)property_names.get_count());
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);