File: module.c

package info (click to toggle)
seed 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,900 kB
  • sloc: ansic: 24,336; sh: 11,196; makefile: 773; xml: 187; python: 173
file content (1468 lines) | stat: -rw-r--r-- 40,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 * Seed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include <seed.h>
#include <seed-debug.h>

#include "util/dbus.h"
#include "dbus-values.h"
#include "dbus-exports.h"

#define DBUS_CONNECTION_FROM_TYPE(type) ((type) == DBUS_BUS_SESSION ? session_bus : system_bus)

SeedContext ctx;
SeedContextGroup group;

SeedObject namespace_ref;
SeedClass dbus_namespace_class;
SeedClass dbus_bus_class;

static gboolean session_bus_weakref_added = FALSE;
static DBusConnection *session_bus = NULL;
static gboolean system_bus_weakref_added = FALSE;
static DBusConnection *system_bus = NULL;

SeedObject bus_proto;

static DBusBusType
get_bus_type_from_object (SeedContext ctx,
			  SeedObject object, SeedException * exception)
{
  SeedValue jsbus_type =
    seed_object_get_property (ctx, object, "_dbusBusType");

  return seed_value_to_int (ctx, jsbus_type, exception);
}

static gboolean
bus_check (SeedContext ctx, DBusBusType bus_type, SeedException * exception)
{
  gboolean bus_weakref_added;
  DBusConnection **bus_connection;

  bus_weakref_added =
    bus_type ==
    DBUS_BUS_SESSION ? session_bus_weakref_added : system_bus_weakref_added;

  bus_connection = bus_type == DBUS_BUS_SESSION ? &session_bus : &system_bus;

  /* This is all done lazily only if a dbus-related method is actually invoked */

  if (!bus_weakref_added)
    {
      big_dbus_add_bus_weakref (bus_type, bus_connection);
    }

  if (*bus_connection == NULL)
    big_dbus_try_connecting_now (bus_type);	/* force a synchronous connection attempt */

  /* Throw exception if connection attempt failed */
  if (*bus_connection == NULL)
    {
      const char *bus_type_name =
	bus_type == DBUS_BUS_SESSION ? "session" : "system";
      seed_make_exception (ctx, exception, "BusError",
			   "Not connected to %s message bus", bus_type_name);
      return FALSE;
    }

  return TRUE;
}

static DBusMessage *
prepare_call (SeedContext ctx,
	      SeedObject obj,
	      SeedObject arg_array,
	      guint argc,
	      const SeedValue * argv,
	      DBusBusType bus_type,
	      SeedException * exception)
{
  DBusMessage *message;
  const char *bus_name;
  const char *path;
  const char *interface;
  const char *method;
  gboolean auto_start;
  const char *out_signature;
  const char *in_signature;
  DBusMessageIter arg_iter;
  DBusSignatureIter sig_iter;

  if (!bus_check (ctx, bus_type, exception))
    return NULL;

  bus_name = seed_value_to_string (ctx, argv[0], exception);
  if (bus_name == NULL)
    return NULL;

  path = seed_value_to_string (ctx, argv[1], exception);
  if (path == NULL)
    return NULL;

  if (seed_value_is_null (ctx, argv[2]))
    {
      interface = NULL;
    }
  else
    {
      interface = seed_value_to_string (ctx, argv[2], exception);
      if (interface == NULL)
	return NULL;		/* exception was set */
    }

  method = seed_value_to_string (ctx, argv[3], exception);
  if (method == NULL)
    return NULL;

  out_signature = seed_value_to_string (ctx, argv[4], exception);
  if (out_signature == NULL)
    return NULL;

  in_signature = seed_value_to_string (ctx, argv[5], exception);
  if (in_signature == NULL)
    return NULL;

  g_assert (bus_name && path && method && in_signature && out_signature);

  auto_start = seed_value_to_boolean (ctx, argv[6], exception);

  /* FIXME should validate the bus_name, path, interface, method really, but
   * we should just not write buggy JS ;-)
   */
  message = dbus_message_new_method_call (bus_name, path, interface, method);
  if (message == NULL)
    {
      seed_make_exception (ctx, exception, "DBusError",
			   "Could not create new method call. (OOM?)");
      return NULL;
    }

  dbus_message_set_auto_start (message, auto_start);

  dbus_message_iter_init_append (message, &arg_iter);

  if (in_signature)
    dbus_signature_iter_init (&sig_iter, in_signature);
  else
    dbus_signature_iter_init (&sig_iter, "a{sv}");

  if (!seed_js_values_to_dbus
      (ctx, 0, arg_array, &arg_iter, &sig_iter, exception))
    {
      SEED_NOTE(MODULE, "Failed to marshal call from JS to dbus");
      dbus_message_unref (message);
      return NULL;
    }

  return message;
}

static gboolean
complete_call (SeedContext ctx,
	       SeedValue * retval,
	       DBusMessage * reply,
	       DBusError * derror, SeedException * exception)
{
  DBusMessageIter arg_iter;
  GArray *ret_values;
  int array_length;

  if (dbus_error_is_set (derror))
    {
      SEED_NOTE(MODULE,
                "Error sending call: %s: %s",
                derror->name, derror->message);
      seed_make_exception (ctx, exception, "DBusError",
			   "DBus error: %s: %s",
			   derror->name, derror->message);
      dbus_error_free (derror);
      return FALSE;
    }

  if (reply == NULL)
    {
      SEED_NOTE(MODULE,
		"No reply received to call");
      return FALSE;
    }

  if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
    {
      dbus_set_error_from_message (derror, reply);
      seed_make_exception (ctx, exception, "DBusError",
			   "DBus error: %s: %s",
			   derror->name, derror->message);
      SEED_NOTE(MODULE, "DBus error: %s: %s",
			   derror->name, derror->message);
      dbus_error_free (derror);

      return FALSE;
    }

  dbus_message_iter_init (reply, &arg_iter);

  if (!seed_js_values_from_dbus (ctx, &arg_iter, &ret_values, exception))
    {
      SEED_NOTE(MODULE, "Failed to marshal dbus call reply back to JS");
      return FALSE;
    }

  g_assert (ret_values != NULL);
  // TODO: I AM HERE

  array_length = ret_values->len;
  if (array_length == 1)
    {
      /* If the array only has one element return that element alone */
      *retval = g_array_index (ret_values, SeedValue, 0);
    }
  else
    {
      /* Otherwise return an array with all the return values. The
       * funny assignment is to avoid creating a temporary JSObject
       * we'd need to root
       */
      *retval =
	seed_make_array (ctx, ret_values->data, array_length, exception);
    }

  g_array_free (ret_values, TRUE);

  seed_js_add_dbus_props (ctx, reply, *retval, exception);

  return TRUE;
}

static void
pending_notify (DBusPendingCall * pending, void *user_data)
{
  SeedException exception = NULL;
  GClosure *closure;
  SeedValue argv[2];
  DBusMessage *reply;
  DBusError derror;

  closure = user_data;

  SEED_NOTE(MODULE,
              "Notified of reply to async call closure %p",
	    closure);

   if (closure == NULL) {
        SEED_NOTE(MODULE,
            "Closure destroyed before we could complete pending call");
         return;
    }

  /* reply may be NULL if none received? I think it may never be if
   * we've already been notified, but be safe here.
   */

  
  reply = dbus_pending_call_steal_reply (pending);

  dbus_error_init (&derror);
  /* argv[0] will be the return value if any, argv[1] we fill with exception if any */
  argv[0] = seed_make_null (ctx);
  argv[1] = seed_make_null (ctx);
  if (!complete_call (ctx, &argv[0], reply, &derror, &exception))
	goto noreply;
  g_assert (!dbus_error_is_set (&derror));	/* not supposed to be left set by complete_call() */

  if (reply)
    dbus_message_unref (reply);

  if (exception)
	  argv[1] = exception;
  seed_closure_invoke(closure, &argv[0], 2, &exception);
  if (exception && seed_value_is_object (ctx, exception))
    seed_closure_warn_exception(closure, ctx, exception);
  seed_context_unref (ctx);
  // TODO: Do something with exception

  return;

noreply:
    SEED_NOTE(MODULE,
            "No reply recieved from complete_call");
  if (reply)
    dbus_message_unref (reply);
  seed_context_unref (ctx);
}

static void
pending_free_closure (void *data)
{
  GClosure *closure;

  closure = data;

  g_closure_invalidate (data);
  g_closure_unref (data);
}


static SeedValue
seed_js_dbus_call_async (SeedContext ctx,
			 SeedObject function,
			 SeedObject this_object,
			 size_t argument_count,
			 const SeedValue arguments[],
			 SeedException * exception)
{
  GClosure *closure;
  DBusMessage *message;
  DBusPendingCall *pending;
  DBusConnection *bus_connection;
  DBusBusType bus_type;
  int timeout;

  if (argument_count < 10)
    {
      seed_make_exception (ctx, exception, "ArgmuentError",
			   "Not enough args, need bus name, object path, interface, method, out signature, in signature, autostart flag, timeout limit, args, and callback");
      return seed_make_null (ctx);
    }

  if (!seed_value_is_object (ctx, arguments[9])
      || !seed_value_is_function (ctx, arguments[9]))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "arg 10 must be a callback to invoke when call completes");
      return FALSE;
    }

  timeout = seed_value_to_int (ctx, arguments[7], exception);

  bus_type = get_bus_type_from_object (ctx, this_object, exception);

  message =
	  prepare_call (ctx, this_object, arguments[8], argument_count, arguments, bus_type,
		  exception);

  if (message == NULL)
    return seed_make_null (ctx);

  bus_connection = DBUS_CONNECTION_FROM_TYPE (bus_type);

  pending = NULL;
  if (!dbus_connection_send_with_reply
      (bus_connection, message, &pending, timeout) || pending == NULL)
    {
      SEED_NOTE(MODULE, "Failed to send async dbus message");
      seed_make_exception (ctx, exception, "DBusError",
			   "Failed to send dbus message");
      dbus_message_unref (message);
      return seed_make_null (ctx);
    }

  g_assert (pending != NULL);

  dbus_message_unref (message);

  /* We cheat a bit here and use a closure to store a JavaScript function
   * and deal with the GC root and other issues, even though we
   * won't ever marshal via GValue
   */
  closure = seed_closure_new (ctx, arguments[9], NULL, "async DBus reply");
  if (closure == NULL)
    {
      dbus_pending_call_unref (pending);
      return seed_make_null (ctx);
    }

  g_closure_ref (closure);
  g_closure_sink (closure);
  dbus_pending_call_set_notify (pending, pending_notify, closure,
				pending_free_closure);

  dbus_pending_call_unref (pending);	/* DBusConnection should still hold a ref until it's completed */

  return seed_value_from_boolean (ctx, TRUE, exception);
}



static SeedValue
seed_js_dbus_get_machine_id (SeedContext ctx,
			     SeedObject object,
			     SeedString property_name,
			     SeedException * exception)
{
  SeedValue ret;
  gchar *id;

  id = dbus_get_local_machine_id ();
  ret = seed_value_from_string (ctx, id, exception);
  dbus_free (id);

  return ret;
}

static void
fill_with_null_or_string (SeedContext ctx, const char **string_p,
			  SeedValue value, SeedException * exception)
{
  if (seed_value_is_null (ctx, value))
    *string_p = NULL;
  else
    *string_p = seed_value_to_string (ctx, value, exception);
}

typedef struct
{
  int refcount;
  DBusBusType bus_type;
  int connection_id;
  GClosure *closure;
} SignalHandler;
/* allow removal by passing in the callable
 * FIXME don't think we ever end up using this,
 * could get rid of it, it predates having an ID
 * to remove by
 */
static GHashTable *signal_handlers_by_callable = NULL;

static void signal_on_closure_invalidated (void *data, GClosure * closure);
static void signal_handler_ref (SignalHandler * handler);
static void signal_handler_unref (SignalHandler * handler);

static SignalHandler *
signal_handler_new (SeedContext ctx,
		    SeedValue callable, SeedException * exception)
{
  SignalHandler *handler;

  if (signal_handlers_by_callable &&
      g_hash_table_lookup (signal_handlers_by_callable, callable) != NULL)
    {
      /* To fix this, get rid of signal_handlers_by_callable
       * and just require removal by id. Not sure we ever use
       * removal by callable anyway.
       */
      seed_make_exception (ctx, exception, "ArgumentError",
			   "For now, same callback cannot be the handler for two dbus signal connections");
      return NULL;
    }

  handler = g_slice_new0 (SignalHandler);
  handler->refcount = 1;

  /* We cheat a bit here and use a closure to store a JavaScript function
   * and deal with the GC root and other issues, even though we
   * won't ever marshal via GValue
   */
  handler->closure = seed_closure_new (ctx, callable, NULL, "DBus signal handler");
  if (handler->closure == NULL)
    {
      g_free (handler);
      return NULL;
    }

  g_closure_ref (handler->closure);
  g_closure_sink (handler->closure);

  g_closure_add_invalidate_notifier (handler->closure, handler,
				     signal_on_closure_invalidated);

  if (!signal_handlers_by_callable)
    {
      signal_handlers_by_callable =
	g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
    }

  /* We keep a weak reference on the closure in a table indexed
   * by the object, so we can retrieve it when removing the signal
   * watch. The signal_handlers_by_callable owns one ref to the SignalHandler.
   */
  signal_handler_ref (handler);
  g_hash_table_replace (signal_handlers_by_callable, callable, handler);

  return handler;
}

static void
signal_handler_ref (SignalHandler * handler)
{
  g_assert (handler->refcount > 0);
  handler->refcount += 1;
}

static void
signal_handler_dispose (SignalHandler * handler)
{
  g_assert (handler->refcount > 0);

  signal_handler_ref (handler);

  if (handler->closure)
    {
      /* invalidating closure could dispose
       * re-entrantly, so set handler->closure
       * NULL before we invalidate
       */
      GClosure *closure = handler->closure;
      handler->closure = NULL;

      g_hash_table_remove (signal_handlers_by_callable,
			   seed_closure_get_callable (closure));
      if (g_hash_table_size (signal_handlers_by_callable) == 0)
	{
	  g_hash_table_destroy (signal_handlers_by_callable);
	  signal_handlers_by_callable = NULL;
	}
      /* the hash table owned 1 ref */
      signal_handler_unref (handler);

      g_closure_invalidate (closure);
      g_closure_unref (closure);
    }

  /* remove signal if it hasn't been */
  if (handler->connection_id != 0)
    {
      int id = handler->connection_id;
      handler->connection_id = 0;

      /* this should clear another ref off the
       * handler by calling signal_on_watch_removed
       */
      big_dbus_unwatch_signal_by_id (handler->bus_type, id);
    }

  signal_handler_unref (handler);
}

static void
signal_handler_unref (SignalHandler * handler)
{
  g_assert (handler->refcount > 0);

  if (handler->refcount == 1)
    {
      signal_handler_dispose (handler);
    }

  handler->refcount -= 1;
  if (handler->refcount == 0)
    {
      g_assert (handler->closure == NULL);
      g_assert (handler->connection_id == 0);
      g_slice_free (SignalHandler, handler);
    }
}

static void
signal_on_watch_removed (void *data)
{
  SignalHandler *handler = data;

  handler->connection_id = 0;	/* don't re-remove it */

  /* The watch owns a ref; removing it
   * also forces dispose, which invalidates
   * the closure if that hasn't been done.
   */
  signal_handler_dispose (handler);
  signal_handler_unref (handler);
}

static void
signal_on_closure_invalidated (void *data, GClosure * closure)
{
  SignalHandler *handler;

  handler = data;

  /* this removes the watch if it has not been */
  signal_handler_dispose (handler);
}

static void
signal_handler_callback (DBusConnection * connection,
			 DBusMessage * message, void *data)
{
  SeedContext ctx;
  SignalHandler *handler;
  SeedValue ret_val;
  DBusMessageIter arg_iter;
  GArray *arguments;
  SeedException exception;

  SEED_NOTE(MODULE,
            "Signal handler called");

  handler = data;

  if (handler->closure == NULL)
    {
      SEED_NOTE(MODULE, "dbus signal handler invalidated, ignoring");
      return;
    }

  ctx = seed_context_create (group, NULL);
  seed_prepare_global_context (ctx);

  dbus_message_iter_init (message, &arg_iter);
  if (!seed_js_values_from_dbus (ctx, &arg_iter, &arguments, &exception))
    {
      SEED_NOTE(MODULE, "Failed to marshal dbus signal to JS");
      return;
    }

  signal_handler_ref (handler);	/* for safety, in case handler removes itself */

  g_assert (arguments != NULL);

  SEED_NOTE(MODULE,
          "Invoking closure on signal received, %d args",
	    arguments->len);
  ret_val = seed_closure_invoke_with_context (ctx, handler->closure,
					      (SeedValue *) arguments->data,
					      arguments->len, &exception);

  g_array_free (arguments, TRUE);

  signal_handler_unref (handler);	/* for safety */
}

static SeedValue
seed_js_dbus_watch_signal (SeedContext ctx,
			   SeedObject function,
			   SeedObject this_object,
			   size_t argument_count,
			   const SeedValue arguments[],
			   SeedException * exception)
{
  const char *bus_name;
  const char *object_path;
  const char *iface;
  const char *signal;
  SignalHandler *handler;
  int id;
  DBusBusType bus_type;

  if (argument_count < 5)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Not enough args, need bus name, object path, interface, signal and callback");
      return seed_make_null (ctx);
    }

  if (!seed_value_is_object (ctx, arguments[4])
      || !seed_value_is_function (ctx, arguments[4]))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "arg 5 must be a callback to invoke when call completes");
      return seed_make_null (ctx);
    }

  fill_with_null_or_string (ctx, &bus_name, arguments[0], exception);
  fill_with_null_or_string (ctx, &object_path, arguments[1], exception);
  fill_with_null_or_string (ctx, &iface, arguments[2], exception);
  fill_with_null_or_string (ctx, &signal, arguments[3], exception);

  bus_type = get_bus_type_from_object (ctx, this_object, exception);

  handler = signal_handler_new (ctx, arguments[4], exception);
  if (handler == NULL)
    return seed_make_null (ctx);

  id = big_dbus_watch_signal (bus_type,
			      bus_name,
			      object_path,
			      iface,
			      signal,
			      signal_handler_callback,
			      handler, signal_on_watch_removed);
  handler->bus_type = bus_type;
  handler->connection_id = id;

  /* signal_on_watch_removed() takes ownership of our
   * ref to the SignalHandler
   */

  return seed_value_from_int (ctx, id, exception);
}

/* Args are handler id */
static SeedValue
seed_js_dbus_unwatch_signal_by_id (SeedContext ctx,
				   SeedObject function,
				   SeedObject this_object,
				   size_t argument_count,
				   const SeedValue arguments[],
				   SeedException * exception)
{
  int id;
  DBusBusType bus_type;

  if (argument_count < 1)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Not enough args, need handler id");
      return seed_make_null (ctx);
    }

  bus_type = get_bus_type_from_object (ctx, this_object, exception);
  id = seed_value_to_int (ctx, arguments[0], exception);

  big_dbus_unwatch_signal_by_id (bus_type, id);
  return seed_make_undefined (ctx);
}

static SeedValue
seed_js_dbus_unwatch_signal (SeedContext ctx,
			     SeedObject function,
			     SeedObject this_object,
			     size_t argument_count,
			     const SeedValue arguments[],
			     SeedException * exception)
{
  const char *bus_name;
  const char *object_path;
  const char *iface;
  const char *signal;
  SignalHandler *handler;
  DBusBusType bus_type;

  if (argument_count < 5)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Not enough args, need bus name, object path, interface, signal and callback");
      return seed_make_null (ctx);
    }

  bus_type = get_bus_type_from_object (ctx, this_object, exception);

  if (!seed_value_is_object (ctx, arguments[4])
      || !seed_value_is_function (ctx, arguments[4]))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "arg 5 must be a callback to invoke when call completes");
      return seed_make_null (ctx);
    }

  fill_with_null_or_string (ctx, &bus_name, arguments[0], exception);
  fill_with_null_or_string (ctx, &object_path, arguments[1], exception);
  fill_with_null_or_string (ctx, &iface, arguments[2], exception);
  fill_with_null_or_string (ctx, &signal, arguments[3], exception);

  /* we don't complain if the signal seems to have been already removed
   * or to never have been watched, to match g_signal_handler_disconnect
   */
  if (!signal_handlers_by_callable)
    return seed_make_undefined (ctx);

  handler = g_hash_table_lookup (signal_handlers_by_callable, arguments[4]);

  if (!handler)
    return seed_make_undefined (ctx);

  /* This should dispose the handler which should in turn
   * remove it from the handler table
   */
  big_dbus_unwatch_signal (bus_type,
			   bus_name,
			   object_path,
			   iface, signal, signal_handler_callback, handler);

  g_assert (g_hash_table_lookup (signal_handlers_by_callable,
				 arguments[4]) == NULL);

  return seed_make_undefined (ctx);
}

static SeedValue
seed_js_dbus_emit_signal(SeedContext ctx,
			 SeedObject function,
			 SeedObject this_object,
			 size_t argument_count,
			 const SeedValue arguments[],
			 SeedException * exception)
{
    DBusConnection *bus_connection;
    DBusMessage *message;
    DBusMessageIter arg_iter;
    DBusSignatureIter sig_iter;
    const char *object_path;
    const char *iface;
    const char *signal;
    const char *in_signature;
    DBusBusType bus_type;

    if (argument_count < 4)
    {
      seed_make_exception(ctx, exception, "ArgumentError", "Not enough args, need object path, interface and signal and the arguments");
      return seed_make_null (ctx);
    }

    if (!seed_value_is_object (ctx, arguments[4]))
      {
	seed_make_exception(ctx, exception, "ArgumentError", "5th argument should be an array of arguments");
	return seed_make_null (ctx);
      }

    bus_type = get_bus_type_from_object (ctx, this_object, exception);

    object_path = seed_value_to_string(ctx, arguments[0], exception);
    iface = seed_value_to_string(ctx, arguments[1], exception);
    signal = seed_value_to_string(ctx, arguments[2], exception);
    in_signature = seed_value_to_string(ctx, arguments[3], exception);

    if (!bus_check(ctx, bus_type, exception))
      return seed_make_null (ctx);

    SEED_NOTE(MODULE,
            "Emitting signal %s %s %s",
            object_path,
            iface,
            signal);

    bus_connection = DBUS_CONNECTION_FROM_TYPE(bus_type);

    message = dbus_message_new_signal(object_path,
                                      iface,
                                      signal);

    dbus_message_iter_init_append(message, &arg_iter);

    dbus_signature_iter_init(&sig_iter, in_signature);

    if (!seed_js_values_to_dbus(ctx, 0, arguments[4], &arg_iter, &sig_iter, exception))
      {
        dbus_message_unref(message);
	return seed_make_null (ctx);
      }

    dbus_connection_send(bus_connection, message, NULL);

    dbus_message_unref(message);

    return seed_make_undefined (ctx);
}

static SeedValue
seed_js_dbus_call(SeedContext ctx,
		  SeedObject function,
		  SeedObject this_object,
		  size_t argument_count,
		  const SeedValue arguments[],
		  SeedException * exception)
{
    DBusMessage *message;
    DBusError derror;
    DBusMessage *reply;
    DBusConnection *bus_connection;
    DBusBusType bus_type;
    SeedValue retval;

    if (argument_count < 8)
      {
	seed_make_exception(ctx, exception, "ArgumentError",
			    "Not enough args, need bus name, object path, interface, method, out signature, in signature, autostart flag, and args");
        return seed_make_null (ctx);
      }

    bus_type = get_bus_type_from_object (ctx, this_object, exception);

    message = prepare_call(ctx, this_object, arguments[7], argument_count, arguments, bus_type, exception);

    bus_connection = DBUS_CONNECTION_FROM_TYPE(bus_type);

    /* send_with_reply_and_block() returns NULL if error was set. */
    dbus_error_init(&derror);
    reply = dbus_connection_send_with_reply_and_block(bus_connection, message, -1, &derror);

    dbus_message_unref(message);

    complete_call(ctx, &retval, reply, &derror, exception);

    if (reply)
        dbus_message_unref(reply);

    return retval;
}

typedef struct {
    BigDBusNameOwnerFuncs funcs;
    GClosure *acquired_closure;
    GClosure *lost_closure;
    DBusBusType bus_type;
} BigJSDBusNameOwner;

static void
on_name_acquired(DBusConnection *connection,
                 const char     *name,
                 void           *data)
{
    int argc;
    SeedValue argv[1];
    SeedContext ctx;
    BigJSDBusNameOwner *owner;
    SeedException exception; // TODO: Do something with this...

    owner = data;

    ctx = seed_context_create (group, NULL);
    seed_prepare_global_context (ctx);
    if (ctx == NULL)
      {
	SEED_NOTE(MODULE,
	          "Closure destroyed before we could notify name acquired");
        return;
      }

    argc = 1;

    argv[0] = seed_value_from_string (ctx, name, &exception);

    seed_closure_invoke_with_context(ctx, owner->acquired_closure,
				     argv, argc, &exception);

    seed_context_unref (ctx);
}


static void
on_name_lost(DBusConnection *connection,
                 const char     *name,
                 void           *data)
{
    int argc;
    SeedValue argv[1];
    SeedContext ctx;
    BigJSDBusNameOwner *owner;
    SeedException exception; // TODO: Do something with this...

    owner = data;

    ctx = seed_context_create (group, NULL);
    seed_prepare_global_context (ctx);
    if (ctx == NULL)
      {
	SEED_NOTE(MODULE,
	          "Closure destroyed before we could notify name acquired");
        return;
      }

    argc = 1;

    argv[0] = seed_value_from_string (ctx, name, &exception);

    seed_closure_invoke_with_context(ctx, owner->lost_closure,
				     argv, argc, &exception);

    seed_context_unref (ctx);
}

static void
owner_closure_invalidated(gpointer  data,
                          GClosure *closure)
{
    BigJSDBusNameOwner *owner;

    owner = (BigJSDBusNameOwner*)data;

    if (owner) {
        big_dbus_release_name(owner->bus_type,
                              &owner->funcs,
                              owner);

        g_closure_unref(owner->acquired_closure);
        g_closure_unref(owner->lost_closure);

        g_slice_free(BigJSDBusNameOwner, owner);
    }

}

static SeedValue
seed_js_dbus_acquire_name(SeedContext ctx,
			  SeedObject function,
			  SeedObject this_object,
			  size_t argument_count,
			  const SeedValue arguments[],
			  SeedException * exception)
{
    const char *bus_name;
    SeedObject acquire_func;
    SeedObject lost_func;
    BigJSDBusNameOwner *owner;
    DBusBusType bus_type;
    BigDBusNameType name_type;
    unsigned int id;

    if (argument_count < 4)
      {
	seed_make_exception (ctx, exception, "ArgumentError",
			     "Not enough args, need bus name, name type, acquired_func, lost_func");
	return seed_make_null (ctx);
      }

    bus_type = get_bus_type_from_object (ctx, this_object, exception);

    bus_name = seed_value_to_string (ctx, arguments[0], exception);

    name_type = (BigDBusNameType)seed_value_to_int (ctx, arguments[1], exception);

    if (!seed_value_is_object (ctx, arguments[2]) ||
	!seed_value_is_function (ctx, arguments[2]))
      {
        seed_make_exception (ctx, exception, "ArgumentError",
			     "Third arg is a callback to invoke on acquiring the name");
        return seed_make_null (ctx);
      }

    acquire_func = arguments[2];

    if (!seed_value_is_object (ctx, arguments[3]) ||
	!seed_value_is_function (ctx, arguments[3]))
      {
        seed_make_exception (ctx, exception, "ArgumentError",
			     "Fourth arg is a callback to invoke on acquiring the name");
        return seed_make_null (ctx);
      }

    lost_func = arguments[3];

    owner = g_slice_new0(BigJSDBusNameOwner);

    owner->funcs.name = g_strdup(bus_name);
    owner->funcs.type = name_type;
    owner->funcs.acquired = on_name_acquired;
    owner->funcs.lost = on_name_lost;
    owner->bus_type = bus_type;

    owner->acquired_closure = seed_closure_new (ctx, acquire_func, NULL, "DBus name acquired handler");

    g_closure_ref(owner->acquired_closure);
    g_closure_sink(owner->acquired_closure);

    owner->lost_closure = seed_closure_new (ctx, lost_func, NULL, "DBus name lost handler");

    g_closure_ref(owner->lost_closure);
    g_closure_sink(owner->lost_closure);

    /* Only add the invalidate notifier to one of the closures, should
     * be enough */
    g_closure_add_invalidate_notifier(owner->acquired_closure, owner,
                                      owner_closure_invalidated);

    id = big_dbus_acquire_name(bus_type,
                               &owner->funcs,
                               owner);

    return seed_value_from_int (ctx, id, exception);
}

static SeedValue
seed_js_dbus_release_name_by_id (SeedContext ctx,
				 SeedObject function,
				 SeedObject this_object,
				 size_t argument_count,
				 const SeedValue arguments[],
				 SeedException * exception)
{
    DBusBusType bus_type;
    unsigned int id;

    if (argument_count < 1)
      {
        seed_make_exception (ctx, exception,
			     "ArgumentError", "Not enough args, need name owner monitor id");
	return seed_make_null (ctx);
      }

    bus_type = get_bus_type_from_object(ctx, this_object, exception);

    id = seed_value_to_int (ctx, arguments[0], exception);

    big_dbus_release_name_by_id(bus_type,
                                id);
    return seed_make_undefined (ctx);
}

typedef struct {
    GClosure *appeared_closure;
    GClosure *vanished_closure;
    char *bus_name;
    DBusBusType bus_type;
} BigJSDBusNameWatcher;

static void
on_name_appeared(DBusConnection *connection,
                 const char     *name,
                 const char     *owner_unique_name,
                 void           *data)
{
    int argc;
    SeedValue argv[2];
    SeedContext ctx;
    BigJSDBusNameWatcher *watcher;
    SeedException exception;

    watcher = data;

    ctx = seed_context_create (group, NULL);
    seed_prepare_global_context (ctx);

    argc = 2;

    argv[0] = seed_value_from_string (ctx, name, &exception);
    argv[1] = seed_value_from_string (ctx, owner_unique_name, &exception);

    seed_closure_invoke_with_context (ctx, watcher->appeared_closure,
				      argv, argc, &exception);
    // TODO: Do something with exception.

    seed_context_unref (ctx);

}

static void
on_name_vanished(DBusConnection *connection,
                 const char     *name,
                 const char     *owner_unique_name,
                 void           *data)
{
    int argc;
    SeedValue argv[2];
    SeedContext ctx;
    BigJSDBusNameWatcher *watcher;
    SeedException exception;

    watcher = data;

    ctx = seed_context_create (group, NULL);
    seed_prepare_global_context (ctx);

    argc = 2;

    argv[0] = seed_value_from_string (ctx, name, &exception);
    argv[1] = seed_value_from_string (ctx, owner_unique_name, &exception);

    seed_closure_invoke_with_context (ctx, watcher->vanished_closure,
				      argv, argc, &exception);
    // TODO: Do something with exception.

    seed_context_unref (ctx);

}

static const BigDBusWatchNameFuncs watch_name_funcs = {
    on_name_appeared,
    on_name_vanished
};

static void
watch_closure_invalidated(gpointer  data,
                          GClosure *closure)
{
    BigJSDBusNameWatcher *watcher;

    watcher = (BigJSDBusNameWatcher*)data;

    if (watcher) {
        big_dbus_unwatch_name(watcher->bus_type,
                              watcher->bus_name,
                              &watch_name_funcs,
                              watcher);

        g_free(watcher->bus_name);
        g_closure_unref(watcher->appeared_closure);
        g_closure_unref(watcher->vanished_closure);

        g_slice_free(BigJSDBusNameWatcher, watcher);
    }

}

static SeedValue
seed_js_dbus_watch_name(SeedContext ctx,
			SeedObject function,
			SeedObject this_object,
			size_t argument_count,
			const SeedValue arguments[],
			SeedException * exception)
{
    const char *bus_name;
    gboolean start_if_not_found;
    SeedObject appeared_func;
    SeedObject vanished_func;
    BigJSDBusNameWatcher *watcher;
    DBusBusType bus_type;

    if (argument_count < 4)
      {
        seed_make_exception (ctx, exception,
			     "ArgumentError", "Not enough args, need bus name, acquired_func, lost_func");
	return seed_make_null (ctx);
      }

    bus_type = get_bus_type_from_object(ctx, this_object, exception);

    bus_name = seed_value_to_string (ctx, arguments[0], exception);

    start_if_not_found = seed_value_to_boolean (ctx, arguments[1], exception);

    if (!seed_value_is_object (ctx, arguments[2]) || !seed_value_is_function (ctx, arguments[2]))
      {
        seed_make_exception (ctx, exception, "ArgumentError", "Third arg is a callback to invoke on seeing the name");
	return seed_make_null (ctx);
      }

    appeared_func = arguments[2];

    if (!seed_value_is_object (ctx, arguments[3]) || !seed_value_is_function (ctx, arguments[3]))
      {
        seed_make_exception (ctx, exception, "ArgumentError", "Fourth arg is a callback to invoke on seeing the name");
	return seed_make_null (ctx);
      }

    vanished_func = arguments[3];

    watcher = g_slice_new0(BigJSDBusNameWatcher);

    watcher->appeared_closure = seed_closure_new (ctx, appeared_func, NULL, "DBus name appeared handler");

    g_closure_ref(watcher->appeared_closure);
    g_closure_sink(watcher->appeared_closure);

    watcher->vanished_closure = seed_closure_new (ctx, vanished_func, NULL, "DBus name vanished handler");

    g_closure_ref(watcher->vanished_closure);
    g_closure_sink(watcher->vanished_closure);

    watcher->bus_type = bus_type;
    watcher->bus_name = g_strdup(bus_name);

    /* Only add the invalidate notifier to one of the closures, should
     * be enough */
    g_closure_add_invalidate_notifier(watcher->appeared_closure, watcher,
                                      watch_closure_invalidated);

    big_dbus_watch_name(bus_type,
                        bus_name,
                        start_if_not_found ?
                        BIG_DBUS_NAME_START_IF_NOT_FOUND : 0,
                        &watch_name_funcs,
                        watcher);

    return seed_make_undefined (ctx);
}

static SeedValue
unique_name_getter (SeedContext ctx,
		    SeedObject object,
		    SeedString property_name,
		    SeedException *exception)
{
  DBusConnection *bus_connection;
  DBusBusType bus_type;

  bus_type = get_bus_type_from_object (ctx, object, exception);

  bus_check (ctx, bus_type, exception);

  bus_connection = DBUS_CONNECTION_FROM_TYPE (bus_type);

  if (bus_connection == NULL)
    {
      return seed_make_null (ctx);
    }
  else
    {
      const gchar *unique_name;

      unique_name = dbus_bus_get_unique_name (bus_connection);
      return seed_value_from_string (ctx, unique_name, exception);
    }
}

static SeedValue
seed_js_dbus_start_service(SeedContext ctx,
			  SeedObject function,
			  SeedObject this_object,
			  size_t argument_count,
			  const SeedValue arguments[],
			  SeedException * exception)
{
    const char     *name;
    DBusBusType     bus_type;
    DBusConnection *bus_connection;

    if (argument_count != 1)
      {
        seed_make_exception (ctx, exception, "ArgumentError",
			     "Wrong number of arguments, expected service name");
	return seed_make_null (ctx);
      }

    name = seed_value_to_string (ctx, arguments[0], exception);

    bus_type = get_bus_type_from_object (ctx, this_object, exception);

    if (!bus_check(ctx, bus_type, exception))
      return seed_make_null (ctx);

    bus_connection = DBUS_CONNECTION_FROM_TYPE(bus_type);

    big_dbus_start_service(bus_connection, name);

    return seed_make_undefined (ctx);
}

static SeedValue
seed_js_dbus_signature_length (SeedContext ctx,
			       SeedObject function,
			       SeedObject this_object,
			       size_t argument_count,
			       const SeedValue arguments[],
			       SeedException * exception)
{
  const gchar *signature;
  DBusSignatureIter iter;
  gint length = 0;

  if (argument_count < 1)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "dbus.signatureLength expected 1 argument, got %zd",
			   argument_count);
      return seed_make_null (ctx);
    }

  signature = seed_value_to_string (ctx, arguments[0], exception);

  if (!dbus_signature_validate (signature, NULL))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Invalid signature");
      return seed_make_null (ctx);
    }

  if (*signature == '\0')
    return seed_value_from_int (ctx, 0, exception);

  dbus_signature_iter_init (&iter, signature);
  do
    {
      length++;
    }
  while (dbus_signature_iter_next (&iter));

  return seed_value_from_int (ctx, length, exception);
}

seed_static_value bus_values[] = {
  {"unique_name", unique_name_getter, NULL, 0},
  {NULL, NULL, NULL, 0}
};

seed_static_function bus_funcs[] = {
  {"call", seed_js_dbus_call, 0},
  {"call_async", seed_js_dbus_call_async, 0},
  {"acquire_name", seed_js_dbus_acquire_name, 0},
  {"release_name_by_id", seed_js_dbus_release_name_by_id, 0},
  {"watch_name", seed_js_dbus_watch_name, 0},
  {"watch_signal", seed_js_dbus_watch_signal, 0},
  {"unwatch_signal_by_id", seed_js_dbus_unwatch_signal_by_id, 0},
  {"unwatch_signal", seed_js_dbus_unwatch_signal, 0},
  {"emit_signal", seed_js_dbus_emit_signal, 0},
  {"start_service", seed_js_dbus_start_service, 0},
  {NULL, NULL, 0}
};

seed_static_function dbus_funcs[] = {
  {"signatureLength", seed_js_dbus_signature_length, 0},
  {NULL, NULL, 0}
};

seed_static_value dbus_values[] = {
  {"localMachineID", seed_js_dbus_get_machine_id, NULL, 0},
  {NULL, NULL, NULL, 0}
};

static void
define_bus_object (SeedContext ctx,
		   DBusBusType which_bus)
{
  SeedObject bus_obj;
  const gchar *bus_name;

  bus_name = BIG_DBUS_NAME_FROM_TYPE(which_bus);
  bus_obj = seed_make_object (ctx, dbus_bus_class, NULL);
  seed_object_set_property (ctx, bus_obj, "_dbusBusType",
			    seed_value_from_int (ctx, which_bus, NULL));
  //TODO: Define exports
  seed_js_define_dbus_exports (ctx, bus_obj, which_bus);

  seed_object_set_property (ctx, namespace_ref, bus_name, bus_obj);
}

    /*static void
seed_define_bus_proto (SeedContext ctx)
{
  bus_proto = seed_make_object (ctx, NULL, NULL);

  seed_value_protect (ctx, bus_proto);
  }*/

SeedObject
seed_module_init (SeedEngine * eng)
{
  seed_class_definition dbus_namespace_class_def = seed_empty_class;
  seed_class_definition dbus_bus_class_def = seed_empty_class;

  ctx = eng->context;
  group = eng->group;

  dbus_namespace_class_def.class_name = "dbusnative";
  dbus_namespace_class_def.static_functions = dbus_funcs;
  dbus_namespace_class_def.static_values = dbus_values;

  dbus_bus_class_def.class_name = "dbusbus";
  dbus_bus_class_def.static_functions = bus_funcs;
  dbus_bus_class_def.static_values = bus_values;

  dbus_namespace_class = seed_create_class (&dbus_namespace_class_def);
  dbus_bus_class = seed_create_class (&dbus_bus_class_def);

  namespace_ref = seed_make_object (eng->context, dbus_namespace_class, NULL);

  seed_object_set_property (ctx, namespace_ref, "BUS_SESSION",
			    seed_value_from_int (ctx, DBUS_BUS_SESSION,
						 NULL));
  seed_object_set_property (ctx, namespace_ref, "BUS_SYSTEM",
			    seed_value_from_int (ctx, DBUS_BUS_SYSTEM, NULL));
  seed_object_set_property (ctx, namespace_ref, "BUS_STARTER",
			    seed_value_from_int (ctx, DBUS_BUS_STARTER,
						 NULL));

  seed_create_function (ctx, "signatureLength",
			(SeedFunctionCallback) seed_js_dbus_signature_length,
			namespace_ref);

  define_bus_object (ctx, DBUS_BUS_SESSION);
  define_bus_object (ctx, DBUS_BUS_SYSTEM);

  return namespace_ref;
}