Package: gjs / 1.32.0-5

02_gerror_class.patch Patch series | 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
From 2ab1b3f090edc4ac64075fb1a7f387e724ba63ff Mon Sep 17 00:00:00 2001
From: Giovanni Campagna <gcampagna@src.gnome.org>
Date: Fri, 02 Mar 2012 20:17:13 +0000
Subject: Introduce special marshalling for GErrors

Previously GErrors were transformed into plain Error with a custom
message, which removed the code and domain metadata. This commit
introduces a new class hierarchy (derived from GLib.Error) for each
enumeration representing an error domain, and modifies existing
code to throw instances of that when a function fails.

https://bugzilla.gnome.org/show_bug.cgi?id=591480
---
Index: gjs-1.32.0/Makefile.am
===================================================================
--- gjs-1.32.0.orig/Makefile.am	2012-11-26 10:59:32.382177869 +0100
+++ gjs-1.32.0/Makefile.am	2012-11-26 10:59:33.526183462 +0100
@@ -53,7 +53,8 @@ nobase_gjs_module_include_HEADERS =	\
 	gi/function.h	\
 	gi/keep-alive.h	\
 	gi/interface.h	\
-	gi/gtype.h
+	gi/gtype.h	\
+	gi/gerror.h
 
 noinst_HEADERS +=		\
 	gjs/jsapi-private.h	\
@@ -137,7 +138,8 @@ libgjs_la_SOURCES += \
 	gi/union.c	\
         gi/value.c	\
 	gi/interface.c	\
-	gi/gtype.c
+	gi/gtype.c	\
+	gi/gerror.c
 
 # Also, these files used to be a separate library
 gdbus_wrapper_source_files = \
Index: gjs-1.32.0/gi/arg.c
===================================================================
--- gjs-1.32.0.orig/gi/arg.c	2012-03-21 14:51:34.000000000 +0100
+++ gjs-1.32.0/gi/arg.c	2012-11-26 10:59:33.534183501 +0100
@@ -31,6 +31,7 @@
 #include "union.h"
 #include "param.h"
 #include "value.h"
+#include "gerror.h"
 #include "gjs/byteArray.h"
 #include <gjs/gjs-module.h>
 #include <gjs/compat.h>
@@ -1326,7 +1327,6 @@ gjs_value_to_g_argument(JSContext      *
                     arg->v_pointer = NULL;
                     wrong = TRUE;
                 }
-
             } else if (JSVAL_IS_NULL(value) &&
                        interface_type != GI_INFO_TYPE_ENUM &&
                        interface_type != GI_INFO_TYPE_FLAGS) {
@@ -1336,8 +1336,16 @@ gjs_value_to_g_argument(JSContext      *
                 if ((interface_type == GI_INFO_TYPE_STRUCT || interface_type == GI_INFO_TYPE_BOXED) &&
                     /* We special case Closures later, so skip them here */
                     !g_type_is_a(gtype, G_TYPE_CLOSURE)) {
-                    arg->v_pointer = gjs_c_struct_from_boxed(context,
-                                                             JSVAL_TO_OBJECT(value));
+
+                    /* special case GError too */
+                    if (g_type_is_a(gtype, G_TYPE_ERROR)) {
+                        arg->v_pointer = gjs_gerror_from_error(context,
+                                                               JSVAL_TO_OBJECT(value));
+                    } else {
+                        arg->v_pointer = gjs_c_struct_from_boxed(context,
+                                                                 JSVAL_TO_OBJECT(value));
+                    }
+
                     if (transfer != GI_TRANSFER_NOTHING) {
                         if (g_type_is_a(gtype, G_TYPE_BOXED))
                             arg->v_pointer = g_boxed_copy (gtype, arg->v_pointer);
@@ -2367,6 +2375,20 @@ gjs_value_from_g_argument (JSContext  *c
             return JS_TRUE;
         }
 
+    case GI_TYPE_TAG_ERROR:
+        {
+            if (arg->v_pointer) {
+                JSObject *obj = gjs_error_from_gerror(context, arg->v_pointer);
+                if (obj) {
+                    *value_p = OBJECT_TO_JSVAL(obj);
+                    return JS_TRUE;
+                }
+
+                return JS_FALSE;
+            }
+            return JS_TRUE;
+        }
+
     case GI_TYPE_TAG_INTERFACE:
         {
             jsval value;
@@ -2426,13 +2448,24 @@ gjs_value_from_g_argument (JSContext  *c
                               "gtype of INTERFACE is %s", g_type_name(gtype));
 
 
-            /* Test GValue before Struct, or it will be handled as the latter */
+            /* Test GValue and GError before Struct, or it will be handled as the latter */
             if (g_type_is_a(gtype, G_TYPE_VALUE)) {
                 if (!gjs_value_from_g_value(context, &value, arg->v_pointer))
                     value = JSVAL_VOID; /* Make sure error is flagged */
 
                 goto out;
             }
+            if (g_type_is_a(gtype, G_TYPE_ERROR)) {
+                JSObject *obj;
+
+                obj = gjs_error_from_gerror(context, arg->v_pointer);
+                if (obj)
+                    value = OBJECT_TO_JSVAL(obj);
+                else
+                    value = JSVAL_VOID;
+
+                goto out;
+            }
 
             if (interface_type == GI_INFO_TYPE_STRUCT || interface_type == GI_INFO_TYPE_BOXED) {
                 JSObject *obj;
Index: gjs-1.32.0/gi/enumeration.c
===================================================================
--- gjs-1.32.0.orig/gi/enumeration.c	2012-02-02 22:40:43.000000000 +0100
+++ gjs-1.32.0/gi/enumeration.c	2012-11-26 10:59:33.534183501 +0100
@@ -103,17 +103,50 @@ gjs_define_enum_value(JSContext    *cont
 }
 
 JSBool
+gjs_define_enum_values(JSContext    *context,
+                       JSObject     *in_object,
+                       GIEnumInfo   *info)
+{
+    GType gtype;
+    int i, n_values;
+    jsval value;
+
+    /* Fill in enum values first, so we don't define the enum itself until we're
+     * sure we can finish successfully.
+     */
+    n_values = g_enum_info_get_n_values(info);
+    for (i = 0; i < n_values; ++i) {
+        GIValueInfo *value_info = g_enum_info_get_value(info, i);
+        gboolean failed;
+
+        failed = !gjs_define_enum_value(context, in_object, value_info);
+
+        g_base_info_unref( (GIBaseInfo*) value_info);
+
+        if (failed) {
+            return JS_FALSE;
+        }
+    }
+
+    gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
+    value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, in_object, "$gtype", value,
+                      NULL, NULL, JSPROP_PERMANENT);
+
+    return JS_TRUE;
+}
+
+
+JSBool
 gjs_define_enumeration(JSContext    *context,
                        JSObject     *in_object,
                        GIEnumInfo   *info,
                        JSObject    **enumeration_p)
 {
     const char *enum_name;
-    GType gtype;
     JSObject *enum_obj;
     jsval value;
-    int i;
-    int n_values;
+
 
     /* An enumeration is simply an object containing integer attributes for
      * each enum value. It does not have a special JSClass.
@@ -150,27 +183,8 @@ gjs_define_enumeration(JSContext    *con
     JS_SetParent(context, enum_obj,
                  gjs_get_import_global (context));
 
-    /* Fill in enum values first, so we don't define the enum itself until we're
-     * sure we can finish successfully.
-     */
-    n_values = g_enum_info_get_n_values(info);
-    for (i = 0; i < n_values; ++i) {
-        GIValueInfo *value_info = g_enum_info_get_value(info, i);
-        gboolean failed;
-
-        failed = !gjs_define_enum_value(context, enum_obj, value_info);
-
-        g_base_info_unref( (GIBaseInfo*) value_info);
-
-        if (failed) {
-            return JS_FALSE;
-        }
-    }
-
-    gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
-    value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, gtype));
-    JS_DefineProperty(context, enum_obj, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    if (!gjs_define_enum_values(context, enum_obj, info))
+        return JS_FALSE;
 
     gjs_debug(GJS_DEBUG_GENUM,
               "Defining %s.%s as %p",
Index: gjs-1.32.0/gi/enumeration.h
===================================================================
--- gjs-1.32.0.orig/gi/enumeration.h	2011-06-10 00:31:56.000000000 +0200
+++ gjs-1.32.0/gi/enumeration.h	2012-11-26 10:59:33.534183501 +0100
@@ -32,6 +32,9 @@
 
 G_BEGIN_DECLS
 
+JSBool    gjs_define_enum_values       (JSContext    *context,
+                                        JSObject     *in_object,
+                                        GIEnumInfo   *info);
 JSBool    gjs_define_enumeration       (JSContext    *context,
                                         JSObject     *in_object,
                                         GIEnumInfo   *info,
Index: gjs-1.32.0/gi/function.c
===================================================================
--- gjs-1.32.0.orig/gi/function.c	2012-03-14 21:31:30.000000000 +0100
+++ gjs-1.32.0/gi/function.c	2012-11-26 10:59:33.538183526 +0100
@@ -28,6 +28,7 @@
 #include "object.h"
 #include "boxed.h"
 #include "union.h"
+#include "gerror.h"
 #include <gjs/gjs-module.h>
 #include <gjs/compat.h>
 
@@ -674,7 +675,13 @@ gjs_invoke_c_function(JSContext      *co
         g_assert_cmpuint(0, <, c_argc);
 
         if (type == GI_INFO_TYPE_STRUCT || type == GI_INFO_TYPE_BOXED) {
-            in_arg_cvalues[0].v_pointer = gjs_c_struct_from_boxed(context, obj);
+            GType gtype = g_registered_type_info_get_g_type ((GIRegisteredTypeInfo *)container);
+
+            /* GError must be special cased */
+            if (g_type_is_a(gtype, G_TYPE_ERROR))
+                in_arg_cvalues[0].v_pointer = gjs_gerror_from_error(context, obj);
+            else
+                in_arg_cvalues[0].v_pointer = gjs_c_struct_from_boxed(context, obj);
         } else if (type == GI_INFO_TYPE_UNION) {
             in_arg_cvalues[0].v_pointer = gjs_c_union_from_union(context, obj);
         } else { /* by fallback is always object */
@@ -1208,11 +1215,7 @@ release:
     }
 
     if (!failed && did_throw_gerror) {
-        gjs_throw(context, "Error invoking %s.%s: %s",
-                  g_base_info_get_namespace( (GIBaseInfo*) function->info),
-                  g_base_info_get_name( (GIBaseInfo*) function->info),
-                  local_error->message);
-        g_error_free(local_error);
+        gjs_throw_g_error(context, local_error);
         return JS_FALSE;
     } else if (failed) {
         return JS_FALSE;
Index: gjs-1.32.0/gi/gerror.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gjs-1.32.0/gi/gerror.c	2012-11-26 10:59:33.538183526 +0100
@@ -0,0 +1,589 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2008  litl, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <gjs/gjs-module.h>
+#include <gjs/compat.h>
+#include "boxed.h"
+#include "enumeration.h"
+#include "repo.h"
+#include "gerror.h"
+
+#include <util/log.h>
+
+#include <jsapi.h>
+
+#include <girepository.h>
+
+typedef struct {
+    GIEnumInfo *info;
+    GQuark domain;
+    GError *gerror; /* NULL if we are the prototype and not an instance */
+} Error;
+
+enum {
+    PROP_0,
+    PROP_DOMAIN,
+    PROP_CODE,
+    PROP_MESSAGE
+};
+
+static struct JSClass gjs_error_class;
+
+GJS_DEFINE_DYNAMIC_PRIV_FROM_JS(Error, gjs_error_class)
+
+GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
+{
+    GJS_NATIVE_CONSTRUCTOR_VARIABLES(error)
+    Error *priv;
+    Error *proto_priv;
+    JSObject *proto;
+    jsval v_message, v_code;
+    gchar *message;
+
+    /* Check early to avoid allocating memory for nothing */
+    if (argc != 1 || !JSVAL_IS_OBJECT(argv[0])) {
+        gjs_throw(context, "Invalid parameters passed to GError constructor, expected one object");
+        return JS_FALSE;
+    }
+
+    GJS_NATIVE_CONSTRUCTOR_PRELUDE(error);
+
+    priv = g_slice_new0(Error);
+
+    GJS_INC_COUNTER(gerror);
+
+    g_assert(priv_from_js(context, object) == NULL);
+    JS_SetPrivate(context, object, priv);
+
+    gjs_debug_lifecycle(GJS_DEBUG_GERROR,
+                        "GError constructor, obj %p priv %p",
+                        object, priv);
+
+    proto = JS_GetPrototype(context, object);
+    gjs_debug_lifecycle(GJS_DEBUG_GERROR, "GError instance __proto__ is %p", proto);
+
+    /* If we're the prototype, then post-construct we'll fill in priv->info.
+     * If we are not the prototype, though, then we'll get ->info from the
+     * prototype and then create a GObject if we don't have one already.
+     */
+    proto_priv = priv_from_js(context, proto);
+    if (proto_priv == NULL) {
+        gjs_debug(GJS_DEBUG_GERROR,
+                  "Bad prototype set on GError? Must match JSClass of object. JS error should have been reported.");
+        return JS_FALSE;
+    }
+
+    priv->info = proto_priv->info;
+    g_base_info_ref( (GIBaseInfo*) priv->info);
+    priv->domain = proto_priv->domain;
+
+    if (!gjs_object_require_property (context, JSVAL_TO_OBJECT(argv[0]),
+                                      "GError constructor", "message", &v_message))
+        return JS_FALSE;
+    if (!gjs_object_require_property (context, JSVAL_TO_OBJECT(argv[0]),
+                                      "GError constructor", "code", &v_code))
+        return JS_FALSE;
+    if (!gjs_string_to_utf8 (context, v_message, &message))
+        return JS_FALSE;
+
+    priv->gerror = g_error_new_literal (priv->domain, JSVAL_TO_INT(v_code),
+                                        message);
+
+    g_free (message);
+
+    GJS_NATIVE_CONSTRUCTOR_FINISH(boxed);
+
+    return JS_TRUE;
+}
+
+static void
+error_finalize(JSContext *context,
+               JSObject  *obj)
+{
+    Error *priv;
+
+    priv = priv_from_js(context, obj);
+    gjs_debug_lifecycle(GJS_DEBUG_GERROR,
+                        "finalize, obj %p priv %p", obj, priv);
+    if (priv == NULL)
+        return; /* wrong class? */
+
+    g_clear_error (&priv->gerror);
+
+    if (priv->info) {
+        g_base_info_unref( (GIBaseInfo*) priv->info);
+        priv->info = NULL;
+    }
+
+    GJS_DEC_COUNTER(gerror);
+    g_slice_free(Error, priv);
+}
+
+static JSBool
+error_get_domain(JSContext *context, JSObject *obj, jsid id, jsval *vp)
+{
+    Error *priv;
+
+    priv = priv_from_js(context, obj);
+
+    if (priv == NULL)
+        return JS_FALSE;
+
+    *vp = INT_TO_JSVAL(priv->domain);
+    return JS_TRUE;
+}
+
+static JSBool
+error_get_message(JSContext *context, JSObject *obj, jsid id, jsval *vp)
+{
+    Error *priv;
+
+    priv = priv_from_js(context, obj);
+
+    if (priv == NULL)
+        return JS_FALSE;
+
+    if (priv->gerror == NULL) {
+        /* Object is prototype, not instance */
+        gjs_throw(context, "Can't get a field from a GError prototype");
+        return JS_FALSE;
+    }
+
+    return gjs_string_from_utf8(context, priv->gerror->message, -1, vp);
+}
+
+static JSBool
+error_get_code(JSContext *context, JSObject *obj, jsid id, jsval *vp)
+{
+    Error *priv;
+
+    priv = priv_from_js(context, obj);
+
+    if (priv == NULL)
+        return JS_FALSE;
+
+    if (priv->gerror == NULL) {
+        /* Object is prototype, not instance */
+        gjs_throw(context, "Can't get a field from a GError prototype");
+        return JS_FALSE;
+    }
+
+    *vp = INT_TO_JSVAL(priv->gerror->code);
+    return JS_TRUE;
+}
+
+static JSBool
+error_to_string(JSContext *context, uintN argc, jsval *vp)
+{
+    jsval v_self;
+    JSObject *self;
+    Error *priv;
+    jsval v_out;
+    gchar *descr;
+    JSBool retval;
+
+    v_self = JS_THIS(context, vp);
+    if (!JSVAL_IS_OBJECT(v_self)) {
+        /* Lie a bit here... */
+        gjs_throw(context, "GLib.Error.prototype.toString() called on a non object");
+        return JS_FALSE;
+    }
+
+    self = JSVAL_TO_OBJECT(v_self);
+    priv = priv_from_js(context, self);
+
+    if (priv == NULL)
+        return JS_FALSE;
+
+    v_out = JSVAL_VOID;
+    retval = JS_FALSE;
+
+    /* We follow the same pattern as standard JS errors, at the expense of
+       hiding some useful information */
+
+    if (priv->gerror == NULL) {
+        descr = g_strdup_printf("%s.%s",
+                                g_base_info_get_namespace(priv->info),
+                                g_base_info_get_name(priv->info));
+
+        if (!gjs_string_from_utf8(context, descr, -1, &v_out))
+            goto out;
+    } else {
+        descr = g_strdup_printf("%s.%s: %s",
+                                g_base_info_get_namespace(priv->info),
+                                g_base_info_get_name(priv->info),
+                                priv->gerror->message);
+
+        if (!gjs_string_from_utf8(context, descr, -1, &v_out))
+            goto out;
+    }
+
+    JS_SET_RVAL(context, vp, v_out);
+    retval = JS_TRUE;
+
+ out:
+    g_free(descr);
+    return retval;
+}
+
+static JSBool
+error_constructor_value_of(JSContext *context, uintN argc, jsval *vp)
+{
+    jsval v_self, v_prototype;
+    Error *priv;
+    jsval v_out;
+
+    v_self = JS_THIS(context, vp);
+    if (!JSVAL_IS_OBJECT(v_self)) {
+        /* Lie a bit here... */
+        gjs_throw(context, "GLib.Error.valueOf() called on a non object");
+        return JS_FALSE;
+    }
+
+    if (!gjs_object_require_property(context,
+                                     JSVAL_TO_OBJECT(v_self),
+                                     "constructor",
+                                     "prototype",
+                                     &v_prototype))
+        return JS_FALSE;
+
+    if (!JSVAL_IS_OBJECT(v_prototype)) {
+        gjs_throw(context, "GLib.Error.valueOf() called on something that is not"
+                  " a constructor");
+        return JS_FALSE;
+    }
+
+    priv = priv_from_js(context, JSVAL_TO_OBJECT(v_prototype));
+
+    if (priv == NULL)
+        return JS_FALSE;
+
+    v_out = INT_TO_JSVAL(priv->domain);
+
+    JS_SET_RVAL(context, vp, v_out);
+    return TRUE;
+}
+
+
+/* The bizarre thing about this vtable is that it applies to both
+ * instances of the object, and to the prototype that instances of the
+ * class have.
+ */
+static struct JSClass gjs_error_class = {
+    NULL, /* dynamic class, no name here */
+    JSCLASS_HAS_PRIVATE |
+    JSCLASS_NEW_RESOLVE |
+    JSCLASS_NEW_RESOLVE_GETS_START,
+    JS_PropertyStub,
+    JS_PropertyStub,
+    JS_PropertyStub,
+    JS_StrictPropertyStub,
+    JS_EnumerateStub,
+    JS_ResolveStub,
+    JS_ConvertStub,
+    error_finalize,
+    NULL,
+    NULL,
+    NULL,
+    NULL, NULL, NULL, NULL, NULL
+};
+
+/* We need to shadow all fields of GError, to prevent calling the getter from GBoxed
+   (which would trash memory accessing the instance private data) */
+static JSPropertySpec gjs_error_proto_props[] = {
+    { "domain", PROP_DOMAIN, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY, error_get_domain, NULL },
+    { "code", PROP_CODE, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY, error_get_code, NULL },
+    { "message", PROP_MESSAGE, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY, error_get_message, NULL },
+    { NULL }
+};
+
+static JSFunctionSpec gjs_error_proto_funcs[] = {
+    { "toString", error_to_string, 0, GJS_MODULE_PROP_FLAGS },
+    JS_FS_END
+};
+
+static JSFunctionSpec gjs_error_constructor_funcs[] = {
+    { "valueOf", error_constructor_value_of, 0, GJS_MODULE_PROP_FLAGS },
+    JS_FS_END
+};
+
+JSObject*
+gjs_lookup_error_constructor(JSContext    *context,
+                             GIEnumInfo  *info)
+{
+    JSObject *ns;
+    JSObject *constructor;
+
+    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
+
+    if (ns == NULL)
+        return NULL;
+
+    constructor = NULL;
+    if (gjs_define_error_class(context, ns, info,
+                               &constructor, NULL))
+        return constructor;
+    else
+        return NULL;
+}
+
+JSObject*
+gjs_lookup_error_prototype(JSContext   *context,
+                           GIEnumInfo  *info)
+{
+    JSObject *ns;
+    JSObject *proto;
+
+    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
+
+    if (ns == NULL)
+        return NULL;
+
+    proto = NULL;
+    if (gjs_define_error_class(context, ns, info, NULL, &proto))
+        return proto;
+    else
+        return NULL;
+}
+
+JSClass*
+gjs_lookup_error_class(JSContext    *context,
+                       GIEnumInfo   *info)
+{
+    JSObject *prototype;
+
+    prototype = gjs_lookup_error_prototype(context, info);
+
+    return JS_GET_CLASS(context, prototype);
+}
+
+JSBool
+gjs_define_error_class(JSContext    *context,
+                       JSObject     *in_object,
+                       GIEnumInfo   *info,
+                       JSObject    **constructor_p,
+                       JSObject    **prototype_p)
+{
+    const char *constructor_name;
+    GIBoxedInfo *glib_error_info;
+    JSObject *prototype, *parent_proto;
+    JSObject *constructor;
+    jsval value;
+    Error *priv;
+
+    /* See the comment in gjs_define_boxed_class() for an
+     * explanation of how this all works; Error is pretty much the
+     * same as Boxed (except that we inherit from GLib.Error).
+     */
+
+    constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
+
+    if (gjs_object_get_property(context, in_object, constructor_name, &value)) {
+        JSObject *constructor;
+
+        if (!JSVAL_IS_OBJECT(value)) {
+            gjs_throw(context, "Existing property '%s' does not look like a constructor",
+                         constructor_name);
+            return JS_FALSE;
+        }
+
+        constructor = JSVAL_TO_OBJECT(value);
+
+        gjs_object_get_property(context, constructor, "prototype", &value);
+        if (!JSVAL_IS_OBJECT(value)) {
+            gjs_throw(context, "error %s prototype property does not appear to exist or has wrong type", constructor_name);
+            return JS_FALSE;
+        } else {
+            if (prototype_p)
+                *prototype_p = JSVAL_TO_OBJECT(value);
+            if (constructor_p)
+                *constructor_p = constructor;
+
+            return JS_TRUE;
+        }
+    }
+
+    g_irepository_require(NULL, "GLib", "2.0", 0, NULL);
+    glib_error_info = (GIBoxedInfo*) g_irepository_find_by_name(NULL, "GLib", "Error");
+    parent_proto = gjs_lookup_boxed_prototype(context, glib_error_info);
+    g_base_info_unref((GIBaseInfo*)glib_error_info);
+
+    prototype = gjs_init_class_dynamic(context, in_object,
+                                          parent_proto,
+                                          g_base_info_get_namespace( (GIBaseInfo*) info),
+                                          constructor_name,
+                                          &gjs_error_class,
+                                          gjs_error_constructor,
+                                          /* number of constructor args (less can be passed) */
+                                          1,
+                                          /* props of prototype */
+                                          &gjs_error_proto_props[0],
+                                          /* funcs of prototype */
+                                          &gjs_error_proto_funcs[0],
+                                          /* props of constructor, MyConstructor.myprop */
+                                          NULL,
+                                          /* funcs of constructor, MyConstructor.myfunc() */
+                                          &gjs_error_constructor_funcs[0]);
+    if (prototype == NULL) {
+        gjs_log_exception(context, NULL);
+        gjs_fatal("Can't init class %s", constructor_name);
+    }
+
+    g_assert(gjs_object_has_property(context, in_object, constructor_name));
+
+    priv = g_slice_new0(Error);
+    priv->info = info;
+    g_base_info_ref( (GIBaseInfo*) priv->info);
+    priv->domain = g_quark_from_string (g_enum_info_get_error_domain(priv->info));
+
+    JS_SetPrivate(context, prototype, priv);
+
+    gjs_debug(GJS_DEBUG_GBOXED, "Defined class %s prototype is %p class %p in object %p",
+              constructor_name, prototype, JS_GET_CLASS(context, prototype), in_object);
+
+    constructor = NULL;
+    gjs_object_get_property(context, in_object, constructor_name, &value);
+    if (!JSVAL_IS_VOID(value)) {
+        if (!JSVAL_IS_OBJECT(value)) {
+            gjs_throw(context, "Property '%s' does not look like a constructor",
+                      constructor_name);
+            return JS_FALSE;
+        }
+    }
+
+    constructor = JSVAL_TO_OBJECT(value);
+
+    gjs_define_enum_values(context, constructor, priv->info);
+
+    if (constructor_p)
+        *constructor_p = constructor;
+
+    if (prototype_p)
+        *prototype_p = prototype;
+
+    return JS_TRUE;
+}
+
+static GIEnumInfo *
+find_error_domain_info(GQuark domain)
+{
+    GIEnumInfo *info;
+
+    /* first an attempt without loading extra libraries */
+    info = g_irepository_find_by_error_domain(NULL, domain);
+    if (info)
+        return info;
+
+    /* load standard stuff */
+    g_irepository_require(NULL, "GLib", "2.0", 0, NULL);
+    g_irepository_require(NULL, "GObject", "2.0", 0, NULL);
+    g_irepository_require(NULL, "Gio", "2.0", 0, NULL);
+    info = g_irepository_find_by_error_domain(NULL, domain);
+    if (info)
+        return info;
+
+    /* last attempt: load GIRepository (for invoke errors, rarely
+       needed) */
+    g_irepository_require(NULL, "GIRepository", "1.0", 0, NULL);
+    info = g_irepository_find_by_error_domain(NULL, domain);
+
+    return info;
+}
+
+JSObject*
+gjs_error_from_gerror(JSContext             *context,
+                      GError                *gerror)
+{
+    JSObject *obj;
+    JSObject *proto;
+    Error *priv;
+    Error *proto_priv;
+    GIEnumInfo *info;
+
+    if (gerror == NULL)
+        return NULL;
+
+    info = find_error_domain_info(gerror->domain);
+
+    if (!info) {
+        /* We don't have error domain metadata */
+        /* Marshal the error as a plain GError */
+        GIBaseInfo *glib_boxed;
+        JSObject *retval;
+
+        glib_boxed = g_irepository_find_by_name(NULL, "GLib", "Error");
+        retval = gjs_boxed_from_c_struct(context, glib_boxed, gerror, 0);
+
+        g_base_info_unref(glib_boxed);
+        return retval;
+    }
+
+    gjs_debug_marshal(GJS_DEBUG_GBOXED,
+                      "Wrapping struct %s %p with JSObject",
+                      g_base_info_get_name((GIBaseInfo *)info), gboxed);
+
+    proto = gjs_lookup_error_prototype(context, info);
+    proto_priv = priv_from_js(context, proto);
+
+    obj = JS_NewObjectWithGivenProto(context,
+                                     JS_GET_CLASS(context, proto), proto,
+                                     gjs_get_import_global (context));
+
+    priv = g_slice_new0(Error);
+    JS_SetPrivate(context, obj, priv);
+    priv->info = info;
+    priv->domain = proto_priv->domain;
+    g_base_info_ref( (GIBaseInfo*) priv->info);
+    priv->gerror = g_error_copy(gerror);
+
+    return obj;
+}
+
+GError*
+gjs_gerror_from_error(JSContext    *context,
+                      JSObject     *obj)
+{
+    Error *priv;
+
+    if (obj == NULL)
+        return NULL;
+
+    priv = priv_from_js(context, obj);
+
+    if (priv == NULL)
+        return NULL;
+
+    if (priv->gerror == NULL) {
+        gjs_throw(context,
+                  "Object is %s.%s.prototype, not an object instance - cannot convert to a boxed instance",
+                  g_base_info_get_namespace( (GIBaseInfo*) priv->info),
+                  g_base_info_get_name( (GIBaseInfo*) priv->info));
+        return NULL;
+    }
+
+    return priv->gerror;
+}
Index: gjs-1.32.0/gi/gerror.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gjs-1.32.0/gi/gerror.h	2012-11-26 10:59:33.538183526 +0100
@@ -0,0 +1,53 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2008  litl, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __GJS_ERROR_H__
+#define __GJS_ERROR_H__
+
+#include <glib.h>
+
+#include <jsapi.h>
+
+#include <girepository.h>
+
+G_BEGIN_DECLS
+
+JSBool    gjs_define_error_class       (JSContext             *context,
+                                        JSObject              *in_object,
+                                        GIEnumInfo            *info,
+                                        JSObject             **constructor_p,
+                                        JSObject             **prototype_p);
+JSObject* gjs_lookup_error_constructor (JSContext             *context,
+                                        GIEnumInfo            *info);
+JSObject* gjs_lookup_error_prototype   (JSContext             *context,
+                                        GIEnumInfo            *info);
+JSClass*  gjs_lookup_error_class       (JSContext             *context,
+                                        GIEnumInfo            *info);
+GError*   gjs_gerror_from_error        (JSContext             *context,
+                                        JSObject              *obj);
+JSObject* gjs_error_from_gerror        (JSContext             *context,
+                                        GError                *gerror);
+
+G_END_DECLS
+
+#endif  /* __GJS_ERROR_H__ */
Index: gjs-1.32.0/gi/repo.c
===================================================================
--- gjs-1.32.0.orig/gi/repo.c	2012-02-06 15:17:48.000000000 +0100
+++ gjs-1.32.0/gi/repo.c	2012-11-26 10:59:33.542183540 +0100
@@ -34,6 +34,7 @@
 #include "arg.h"
 #include "foreign.h"
 #include "interface.h"
+#include "gerror.h"
 
 #include <gjs/compat.h>
 
@@ -499,6 +500,13 @@ gjs_define_info(JSContext  *context,
             return JS_FALSE;
         break;
     case GI_INFO_TYPE_ENUM:
+        if (g_enum_info_get_error_domain((GIEnumInfo*) info)) {
+            /* define as GError subclass */
+            if (!gjs_define_error_class(context, in_object, (GIEnumInfo*) info, NULL, NULL))
+                return JS_FALSE;
+        }
+        /* fall through */
+
     case GI_INFO_TYPE_FLAGS:
         if (!gjs_define_enumeration(context, in_object, (GIEnumInfo*) info, NULL))
             return JS_FALSE;
Index: gjs-1.32.0/gi/value.c
===================================================================
--- gjs-1.32.0.orig/gi/value.c	2012-03-05 15:48:49.000000000 +0100
+++ gjs-1.32.0/gi/value.c	2012-11-26 10:59:33.542183540 +0100
@@ -33,6 +33,7 @@
 #include "boxed.h"
 #include "union.h"
 #include "gtype.h"
+#include "gerror.h"
 #include <gjs/gjs-module.h>
 #include <gjs/compat.h>
 
@@ -387,7 +388,13 @@ gjs_value_to_g_value_internal(JSContext
         } else if (JSVAL_IS_OBJECT(value)) {
             JSObject *obj;
             obj = JSVAL_TO_OBJECT(value);
-            gboxed = gjs_c_struct_from_boxed(context, obj);
+
+            if (g_type_is_a(gtype, G_TYPE_ERROR)) {
+                /* special case GError */
+                gboxed = gjs_gerror_from_error(context, obj);
+            } else {
+                gboxed = gjs_c_struct_from_boxed(context, obj);
+            }
         } else {
             gjs_throw(context,
                       "Wrong type %s; boxed type %s expected",
@@ -665,6 +672,14 @@ gjs_value_from_g_value_internal(JSContex
             gboxed = g_value_get_variant(gvalue);
         boxed_flags = GJS_BOXED_CREATION_NONE;
 
+        /* special case GError */
+        if (g_type_is_a(gtype, G_TYPE_ERROR)) {
+            obj = gjs_error_from_gerror(context, gboxed);
+            *value_p = OBJECT_TO_JSVAL(obj);
+
+            return TRUE;
+        }
+
         /* The only way to differentiate unions and structs is from
          * their g-i info as both GBoxed */
         info = g_irepository_find_by_gtype(g_irepository_get_default(),
@@ -693,6 +708,7 @@ gjs_value_from_g_value_internal(JSContex
                       g_type_name(gtype));
             return JS_FALSE;
         }
+
         *value_p = OBJECT_TO_JSVAL(obj);
     } else if (g_type_is_a(gtype, G_TYPE_ENUM)) {
         return convert_int_to_enum(context, value_p, gtype, g_value_get_enum(gvalue));
Index: gjs-1.32.0/gjs/jsapi-util-error.c
===================================================================
--- gjs-1.32.0.orig/gjs/jsapi-util-error.c	2012-03-05 15:48:49.000000000 +0100
+++ gjs-1.32.0/gjs/jsapi-util-error.c	2012-11-26 10:59:33.546183565 +0100
@@ -25,6 +25,7 @@
 
 #include "jsapi-util.h"
 #include "compat.h"
+#include "gi/gerror.h"
 
 #include <util/log.h>
 
@@ -46,13 +47,9 @@ gjs_throw_valist(JSContext       *contex
                     va_list          args)
 {
     char *s;
-    jsval retval;
-    jsval argv[1];
-    JSFunction *func;
-    const char *body;
     JSBool result;
-    const char *names[] = { "message" };
-    guint options;
+    jsval v_constructor, v_message;
+    JSObject *err_obj;
 
     s = g_strdup_vprintf(format, args);
 
@@ -79,53 +76,20 @@ gjs_throw_valist(JSContext       *contex
 
     (void)JS_EnterLocalRootScope(context);
 
-    if (!gjs_string_from_utf8(context, s, -1, &argv[0])) {
+    if (!gjs_string_from_utf8(context, s, -1, &v_message)) {
         JS_ReportError(context, "Failed to copy exception string");
         goto out;
     }
 
-    body = "throw new Error(message);";
-    func = JS_CompileFunction(context,
-                              JS_GetGlobalObject(context), /* parent object (scope chain) */
-                              NULL, /* name of function if we wanted to define it in parent */
-                              1, /* nargs */
-                              &names[0], /* array of arg names if we had args */
-                              body,
-                              strlen(body),
-                              "gjs_throw", /* file */
-                              0); /* line */
-
-    if (func == NULL) {
-        JS_ReportError(context, "Failed to compile function");
+    if (!gjs_object_get_property(context, JS_GetGlobalObject(context),
+                                 "Error", &v_constructor)) {
+        JS_ReportError(context, "??? Missing Error constructor in global object?");
         goto out;
     }
 
-    /* we need JS_CallFunctionValue() to leave the exception set */
-    options = JS_GetOptions(context);
-    if (!(options & JSOPTION_DONT_REPORT_UNCAUGHT)) {
-        JS_SetOptions(context, options | JSOPTION_DONT_REPORT_UNCAUGHT);
-    }
-
-    retval = JSVAL_VOID;
-
-    /* note the return value is whether function succeeded, which it shouldn't, since it
-     * throws...
-     */
-    JS_CallFunctionValue(context,
-                         JS_GetGlobalObject(context),
-                         OBJECT_TO_JSVAL(JS_GetFunctionObject(func)),
-                         1, &argv[0],
-                         &retval);
-
-    if (!(options & JSOPTION_DONT_REPORT_UNCAUGHT)) {
-        JS_SetOptions(context, options);
-    }
-
-    if (!JS_IsExceptionPending(context)) {
-        JS_ReportError(context,
-                       "Failed to set exception by calling our exception-setting function");
-        goto out;
-    }
+    /* throw new Error(message) */
+    err_obj = JS_New(context, JSVAL_TO_OBJECT(v_constructor), 1, &v_message);
+    JS_SetPendingException(context, OBJECT_TO_JSVAL(err_obj));
 
     result = JS_TRUE;
 
@@ -182,17 +146,26 @@ gjs_throw_literal(JSContext       *conte
  * gjs_throw_g_error:
  *
  * Convert a GError into a JavaScript Exception, and
- * frees the GError.  Like gjs_throw(), will not overwrite
- * an already pending exception.
+ * frees the GError. Differently from gjs_throw(), it
+ * will overwrite an existing exception, as it is used
+ * to report errors from C functions.
  */
 void
 gjs_throw_g_error (JSContext       *context,
                    GError          *error)
 {
+    JSObject *err_obj;
+
     if (error == NULL)
         return;
-    gjs_throw_literal(context, error->message);
-    g_error_free (error);
+
+    JS_BeginRequest(context);
+
+    err_obj = gjs_error_from_gerror(context, error);
+    if (err_obj)
+        JS_SetPendingException(context, OBJECT_TO_JSVAL(err_obj));
+
+    JS_EndRequest(context);
 }
 
 #if GJS_BUILD_TESTS
Index: gjs-1.32.0/gjs/mem.c
===================================================================
--- gjs-1.32.0.orig/gjs/mem.c	2012-01-04 19:24:48.000000000 +0100
+++ gjs-1.32.0/gjs/mem.c	2012-11-26 10:59:33.546183565 +0100
@@ -36,6 +36,7 @@
 GJS_DEFINE_COUNTER(everything)
 
 GJS_DEFINE_COUNTER(boxed)
+GJS_DEFINE_COUNTER(gerror)
 GJS_DEFINE_COUNTER(closure)
 GJS_DEFINE_COUNTER(database)
 GJS_DEFINE_COUNTER(dbus_exports)
@@ -54,6 +55,7 @@ GJS_DEFINE_COUNTER(interface)
 
 static GjsMemCounter* counters[] = {
     GJS_LIST_COUNTER(boxed),
+    GJS_LIST_COUNTER(gerror),
     GJS_LIST_COUNTER(closure),
     GJS_LIST_COUNTER(database),
     GJS_LIST_COUNTER(dbus_exports),
Index: gjs-1.32.0/gjs/mem.h
===================================================================
--- gjs-1.32.0.orig/gjs/mem.h	2012-01-04 19:24:48.000000000 +0100
+++ gjs-1.32.0/gjs/mem.h	2012-11-26 10:59:33.546183565 +0100
@@ -44,6 +44,7 @@ typedef struct {
 GJS_DECLARE_COUNTER(everything)
 
 GJS_DECLARE_COUNTER(boxed)
+GJS_DECLARE_COUNTER(gerror)
 GJS_DECLARE_COUNTER(closure)
 GJS_DECLARE_COUNTER(database)
 GJS_DECLARE_COUNTER(dbus_exports)
Index: gjs-1.32.0/modules/overrides/GLib.js
===================================================================
--- gjs-1.32.0.orig/modules/overrides/GLib.js	2011-11-10 19:58:10.000000000 +0100
+++ gjs-1.32.0/modules/overrides/GLib.js	2012-11-26 10:59:33.550183579 +0100
@@ -240,6 +240,11 @@ function _init() {
 
     GLib = this;
 
+    // small HACK: we add a matches() method to standard Errors so that
+    // you can do "catch(e if e.matches(Ns.FooError, Ns.FooError.SOME_CODE))"
+    // without checking instanceof
+    Error.prototype.matches = function() { return false; }
+
     this.Variant.new = function (sig, value) {
 	let signature = Array.prototype.slice.call(sig);
 
Index: gjs-1.32.0/test/js/testEverythingBasic.js
===================================================================
--- gjs-1.32.0.orig/test/js/testEverythingBasic.js	2012-02-07 17:30:49.000000000 +0100
+++ gjs-1.32.0/test/js/testEverythingBasic.js	2012-11-26 10:59:33.550183579 +0100
@@ -521,4 +521,33 @@ function testVariant() {
     assertEquals(3, as.length);
 }
 
+function testGError() {
+    assertEquals(Gio.io_error_quark(), Number(Gio.IOErrorEnum));
+
+    try {
+	let file = Gio.file_new_for_path("\\/,.^!@&$_don't exist");
+	file.read(null);
+    } catch (x) {
+	assertTrue(x instanceof Gio.IOErrorEnum);
+	assertTrue(x.matches(Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND));
+	assertTrue(x.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND));
+
+	assertEquals(Gio.io_error_quark(), x.domain);
+	assertEquals(Gio.IOErrorEnum.NOT_FOUND, x.code);
+    }
+
+    Everything.test_gerror_callback(function(e) {
+	assertTrue(e instanceof Gio.IOErrorEnum);
+	assertEquals(Gio.io_error_quark(), e.domain);
+	assertEquals(Gio.IOErrorEnum.NOT_SUPPORTED, e.code);
+	assertEquals('regression test error', e.message);
+    });
+    Everything.test_owned_gerror_callback(function(e) {
+	assertTrue(e instanceof Gio.IOErrorEnum);
+	assertEquals(Gio.io_error_quark(), e.domain);
+	assertEquals(Gio.IOErrorEnum.PERMISSION_DENIED, e.code);
+	assertEquals('regression test owned error', e.message);
+    });
+}
+
 gjstestRun();
Index: gjs-1.32.0/util/log.c
===================================================================
--- gjs-1.32.0.orig/util/log.c	2011-06-10 00:31:56.000000000 +0200
+++ gjs-1.32.0/util/log.c	2012-11-26 10:59:33.550183579 +0100
@@ -277,6 +277,9 @@ gjs_debug(GjsDebugTopic topic,
     case GJS_DEBUG_BYTE_ARRAY:
         prefix = "JS BYTE ARRAY";
         break;
+    case GJS_DEBUG_GERROR:
+        prefix = "JS G ERR";
+        break;
     }
 
     if (!is_allowed_prefix(prefix))
Index: gjs-1.32.0/util/log.h
===================================================================
--- gjs-1.32.0.orig/util/log.h	2011-06-10 00:31:56.000000000 +0200
+++ gjs-1.32.0/util/log.h	2012-11-26 10:59:33.550183579 +0100
@@ -60,7 +60,8 @@ typedef enum {
     GJS_DEBUG_PROPS,
     GJS_DEBUG_SCOPE,
     GJS_DEBUG_HTTP,
-    GJS_DEBUG_BYTE_ARRAY
+    GJS_DEBUG_BYTE_ARRAY,
+    GJS_DEBUG_GERROR,
 } GjsDebugTopic;
 
 /* These defines are because we have some pretty expensive and