summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm
blob: c5c5051026135456376e1fa01bc21a49053176cc (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * OS.File utilities used by all threads.
 *
 * This module defines:
 * - logging;
 * - the base constants;
 * - base types and primitives for declaring new types;
 * - primitives for importing C functions;
 * - primitives for dealing with integers, pointers, typed arrays;
 * - the base class OSError;
 * - a few additional utilities.
 */

// Boilerplate used to be able to import this module both from the main
// thread and from worker threads.

// Since const is lexically scoped, hoist the
// conditionally-useful definition ourselves.
const Cu = typeof Components != "undefined" ? Components.utils : undefined;
const Ci = typeof Components != "undefined" ? Components.interfaces : undefined;
const Cc = typeof Components != "undefined" ? Components.classes : undefined;

/**
 * A constructor for messages that require transfers instead of copies.
 *
 * See BasePromiseWorker.Meta.
 *
 * @constructor
 */
var Meta;
if (typeof Components != "undefined") {
  // Global definition of |exports|, to keep everybody happy.
  // In non-main thread, |exports| is provided by the module
  // loader.
  this.exports = {};

  Cu.import("resource://gre/modules/Services.jsm", this);
  Meta = Cu.import("resource://gre/modules/PromiseWorker.jsm", {}).BasePromiseWorker.Meta;
} else {
  importScripts("resource://gre/modules/workers/require.js");
  Meta = require("resource://gre/modules/workers/PromiseWorker.js").Meta;
}

var EXPORTED_SYMBOLS = [
  "LOG",
  "clone",
  "Config",
  "Constants",
  "Type",
  "HollowStructure",
  "OSError",
  "Library",
  "declareFFI",
  "declareLazy",
  "declareLazyFFI",
  "normalizeBufferArgs",
  "projectValue",
  "isArrayBuffer",
  "isTypedArray",
  "defineLazyGetter",
  "OS" // Warning: this exported symbol will disappear
];

////////////////////// Configuration of OS.File

var Config = {
  /**
   * If |true|, calls to |LOG| are shown. Otherwise, they are hidden.
   *
   * This configuration option is controlled by preference "toolkit.osfile.log".
   */
  DEBUG: false,

  /**
   * TEST
   */
  TEST: false
};
exports.Config = Config;

////////////////////// OS Constants

if (typeof Components != "undefined") {
  // On the main thread, OS.Constants is defined by a xpcom
  // component. On other threads, it is available automatically
  Cu.import("resource://gre/modules/ctypes.jsm");
  Cc["@mozilla.org/net/osfileconstantsservice;1"].
    getService(Ci.nsIOSFileConstantsService).init();
}

exports.Constants = OS.Constants;

///////////////////// Utilities

// Define a lazy getter for a property
var defineLazyGetter = function defineLazyGetter(object, name, getter) {
  Object.defineProperty(object, name, {
    configurable: true,
    get: function lazy() {
      delete this[name];
      let value = getter.call(this);
      Object.defineProperty(object, name, {
        value: value
      });
      return value;
    }
  });
};
exports.defineLazyGetter = defineLazyGetter;


///////////////////// Logging

/**
 * The default implementation of the logger.
 *
 * The choice of logger can be overridden with Config.TEST.
 */
var gLogger;
if (typeof window != "undefined" && window.console && console.log) {
  gLogger = console.log.bind(console, "OS");
} else {
  gLogger = function(...args) {
    dump("OS " + args.join(" ") + "\n");
  };
}

/**
 * Attempt to stringify an argument into something useful for
 * debugging purposes, by using |.toString()| or |JSON.stringify|
 * if available.
 *
 * @param {*} arg An argument to be stringified if possible.
 * @return {string} A stringified version of |arg|.
 */
var stringifyArg = function stringifyArg(arg) {
  if (typeof arg === "string") {
    return arg;
  }
  if (arg && typeof arg === "object") {
    let argToString = "" + arg;

    /**
     * The only way to detect whether this object has a non-default
     * implementation of |toString| is to check whether it returns
     * '[object Object]'. Unfortunately, we cannot simply compare |arg.toString|
     * and |Object.prototype.toString| as |arg| typically comes from another
     * compartment.
     */
    if (argToString === "[object Object]") {
      return JSON.stringify(arg, function(key, value) {
        if (isTypedArray(value)) {
          return "["+ value.constructor.name + " " + value.byteOffset + " " + value.byteLength + "]";
        }
        if (isArrayBuffer(arg)) {
          return "[" + value.constructor.name + " " + value.byteLength + "]";
        }
        return value;
      });
    } else {
      return argToString;
    }
  }
  return arg;
};

var LOG = function (...args) {
  if (!Config.DEBUG) {
    // If logging is deactivated, don't log
    return;
  }

  let logFunc = gLogger;
  if (Config.TEST && typeof Components != "undefined") {
    // If _TESTING_LOGGING is set, and if we are on the main thread,
    // redirect logs to Services.console, for testing purposes
    logFunc = function logFunc(...args) {
      let message = ["TEST", "OS"].concat(args).join(" ");
      Services.console.logStringMessage(message + "\n");
    };
  }
  logFunc.apply(null, args.map(stringifyArg));
};

exports.LOG = LOG;

/**
 * Return a shallow clone of the enumerable properties of an object.
 *
 * Utility used whenever normalizing options requires making (shallow)
 * changes to an option object. The copy ensures that we do not modify
 * a client-provided object by accident.
 *
 * Note: to reference and not copy specific fields, provide an optional
 * |refs| argument containing their names.
 *
 * @param {JSON} object Options to be cloned.
 * @param {Array} refs An optional array of field names to be passed by
 * reference instead of copying.
 */
var clone = function (object, refs = []) {
  let result = {};
  // Make a reference between result[key] and object[key].
  let refer = function refer(result, key, object) {
    Object.defineProperty(result, key, {
      enumerable: true,
      get: function() {
        return object[key];
      },
      set: function(value) {
        object[key] = value;
      }
    });
  };
  for (let k in object) {
    if (refs.indexOf(k) < 0) {
      result[k] = object[k];
    } else {
      refer(result, k, object);
    }
  }
  return result;
};

exports.clone = clone;

///////////////////// Abstractions above js-ctypes

/**
 * Abstraction above js-ctypes types.
 *
 * Use values of this type to register FFI functions. In addition to the
 * usual features of js-ctypes, values of this type perform the necessary
 * transformations to ensure that C errors are handled nicely, to connect
 * resources with their finalizer, etc.
 *
 * @param {string} name The name of the type. Must be unique.
 * @param {CType} implementation The js-ctypes implementation of the type.
 *
 * @constructor
 */
function Type(name, implementation) {
  if (!(typeof name == "string")) {
    throw new TypeError("Type expects as first argument a name, got: "
                        + name);
  }
  if (!(implementation instanceof ctypes.CType)) {
    throw new TypeError("Type expects as second argument a ctypes.CType"+
                        ", got: " + implementation);
  }
  Object.defineProperty(this, "name", { value: name });
  Object.defineProperty(this, "implementation", { value: implementation });
}
Type.prototype = {
  /**
   * Serialize a value of |this| |Type| into a format that can
   * be transmitted as a message (not necessarily a string).
   *
   * In the default implementation, the method returns the
   * value unchanged.
   */
  toMsg: function default_toMsg(value) {
    return value;
  },
  /**
   * Deserialize a message to a value of |this| |Type|.
   *
   * In the default implementation, the method returns the
   * message unchanged.
   */
  fromMsg: function default_fromMsg(msg) {
    return msg;
  },
  /**
   * Import a value from C.
   *
   * In this default implementation, return the value
   * unchanged.
   */
  importFromC: function default_importFromC(value) {
    return value;
  },

  /**
   * A pointer/array used to pass data to the foreign function.
   */
  get in_ptr() {
    delete this.in_ptr;
    let ptr_t = new PtrType(
      "[in] " + this.name + "*",
      this.implementation.ptr,
      this);
    Object.defineProperty(this, "in_ptr",
    {
      get: function() {
        return ptr_t;
      }
    });
    return ptr_t;
  },

  /**
   * A pointer/array used to receive data from the foreign function.
   */
  get out_ptr() {
    delete this.out_ptr;
    let ptr_t = new PtrType(
      "[out] " + this.name + "*",
      this.implementation.ptr,
      this);
    Object.defineProperty(this, "out_ptr",
    {
      get: function() {
        return ptr_t;
      }
    });
    return ptr_t;
  },

  /**
   * A pointer/array used to both pass data to the foreign function
   * and receive data from the foreign function.
   *
   * Whenever possible, prefer using |in_ptr| or |out_ptr|, which
   * are generally faster.
   */
  get inout_ptr() {
    delete this.inout_ptr;
    let ptr_t = new PtrType(
      "[inout] " + this.name + "*",
      this.implementation.ptr,
      this);
    Object.defineProperty(this, "inout_ptr",
    {
      get: function() {
        return ptr_t;
      }
    });
    return ptr_t;
  },

  /**
   * Attach a finalizer to a type.
   */
  releaseWith: function releaseWith(finalizer) {
    let parent = this;
    let type = this.withName("[auto " + this.name + ", " + finalizer + "] ");
    type.importFromC = function importFromC(value, operation) {
      return ctypes.CDataFinalizer(
        parent.importFromC(value, operation),
        finalizer);
    };
    return type;
  },

  /**
   * Lazy variant of releaseWith.
   * Attach a finalizer lazily to a type.
   *
   * @param {function} getFinalizer The function that
   * returns finalizer lazily.
   */
  releaseWithLazy: function releaseWithLazy(getFinalizer) {
    let parent = this;
    let type = this.withName("[auto " + this.name + ", (lazy)] ");
    type.importFromC = function importFromC(value, operation) {
      return ctypes.CDataFinalizer(
        parent.importFromC(value, operation),
        getFinalizer());
    };
    return type;
  },

  /**
   * Return an alias to a type with a different name.
   */
  withName: function withName(name) {
    return Object.create(this, {name: {value: name}});
  },

  /**
   * Cast a C value to |this| type.
   *
   * Throw an error if the value cannot be casted.
   */
  cast: function cast(value) {
    return ctypes.cast(value, this.implementation);
  },

  /**
   * Return the number of bytes in a value of |this| type.
   *
   * This may not be defined, e.g. for |void_t|, array types
   * without length, etc.
   */
  get size() {
    return this.implementation.size;
  }
};

/**
 * Utility function used to determine whether an object is a typed array
 */
var isTypedArray = function isTypedArray(obj) {
  return obj != null && typeof obj == "object"
    && "byteOffset" in obj;
};
exports.isTypedArray = isTypedArray;

/**
 * Utility function used to determine whether an object is an ArrayBuffer.
 */
var isArrayBuffer = function(obj) {
  return obj != null && typeof obj == "object" &&
    obj.constructor.name == "ArrayBuffer";
};
exports.isArrayBuffer = isArrayBuffer;

/**
 * A |Type| of pointers.
 *
 * @param {string} name The name of this type.
 * @param {CType} implementation The type of this pointer.
 * @param {Type} targetType The target type.
 */
function PtrType(name, implementation, targetType) {
  Type.call(this, name, implementation);
  if (targetType == null || !targetType instanceof Type) {
    throw new TypeError("targetType must be an instance of Type");
  }
  /**
   * The type of values targeted by this pointer type.
   */
  Object.defineProperty(this, "targetType", {
    value: targetType
  });
}
PtrType.prototype = Object.create(Type.prototype);

/**
 * Convert a value to a pointer.
 *
 * Protocol:
 * - |null| returns |null|
 * - a string returns |{string: value}|
 * - a typed array returns |{ptr: address_of_buffer}|
 * - a C array returns |{ptr: address_of_buffer}|
 * everything else raises an error
 */
PtrType.prototype.toMsg = function ptr_toMsg(value) {
  if (value == null) {
    return null;
  }
  if (typeof value == "string") {
    return { string: value };
  }
  if (isTypedArray(value)) {
    // Automatically transfer typed arrays
    return new Meta({data: value}, {transfers: [value.buffer]});
  }
  if (isArrayBuffer(value)) {
    // Automatically transfer array buffers
    return new Meta({data: value}, {transfers: [value]});
  }
  let normalized;
  if ("addressOfElement" in value) { // C array
    normalized = value.addressOfElement(0);
  } else if ("isNull" in value) { // C pointer
    normalized = value;
  } else {
    throw new TypeError("Value " + value +
      " cannot be converted to a pointer");
  }
  let cast = Type.uintptr_t.cast(normalized);
  return {ptr: cast.value.toString()};
};

/**
 * Convert a message back to a pointer.
 */
PtrType.prototype.fromMsg = function ptr_fromMsg(msg) {
  if (msg == null) {
    return null;
  }
  if ("string" in msg) {
    return msg.string;
  }
  if ("data" in msg) {
    return msg.data;
  }
  if ("ptr" in msg) {
    let address = ctypes.uintptr_t(msg.ptr);
    return this.cast(address);
  }
  throw new TypeError("Message " + msg.toSource() +
    " does not represent a pointer");
};

exports.Type = Type;


/*
 * Some values are large integers on 64 bit platforms. Unfortunately,
 * in practice, 64 bit integers cannot be manipulated in JS. We
 * therefore project them to regular numbers whenever possible.
 */

var projectLargeInt = function projectLargeInt(x) {
  let str = x.toString();
  let rv = parseInt(str, 10);
  if (rv.toString() !== str) {
    throw new TypeError("Number " + str + " cannot be projected to a double");
  }
  return rv;
};
var projectLargeUInt = function projectLargeUInt(x) {
  return projectLargeInt(x);
};
var projectValue = function projectValue(x) {
  if (!(x instanceof ctypes.CData)) {
    return x;
  }
  if (!("value" in x)) { // Sanity check
    throw new TypeError("Number " + x.toSource() + " has no field |value|");
  }
  return x.value;
};

function projector(type, signed) {
  LOG("Determining best projection for", type,
    "(size: ", type.size, ")", signed?"signed":"unsigned");
  if (type instanceof Type) {
    type = type.implementation;
  }
  if (!type.size) {
    throw new TypeError("Argument is not a proper C type");
  }
  // Determine if type is projected to Int64/Uint64
  if (type.size == 8           // Usual case
      // The following cases have special treatment in js-ctypes
      // Regardless of their size, the value getter returns
      // a Int64/Uint64
      || type == ctypes.size_t // Special cases
      || type == ctypes.ssize_t
      || type == ctypes.intptr_t
      || type == ctypes.uintptr_t
      || type == ctypes.off_t) {
    if (signed) {
      LOG("Projected as a large signed integer");
      return projectLargeInt;
    } else {
      LOG("Projected as a large unsigned integer");
      return projectLargeUInt;
    }
  }
  LOG("Projected as a regular number");
  return projectValue;
};
exports.projectValue = projectValue;

/**
 * Get the appropriate type for an unsigned int of the given size.
 *
 * This function is useful to define types such as |mode_t| whose
 * actual width depends on the OS/platform.
 *
 * @param {number} size The number of bytes requested.
 */
Type.uintn_t = function uintn_t(size) {
  switch (size) {
  case 1: return Type.uint8_t;
  case 2: return Type.uint16_t;
  case 4: return Type.uint32_t;
  case 8: return Type.uint64_t;
  default:
    throw new Error("Cannot represent unsigned integers of " + size + " bytes");
  }
};

/**
 * Get the appropriate type for an signed int of the given size.
 *
 * This function is useful to define types such as |mode_t| whose
 * actual width depends on the OS/platform.
 *
 * @param {number} size The number of bytes requested.
 */
Type.intn_t = function intn_t(size) {
  switch (size) {
  case 1: return Type.int8_t;
  case 2: return Type.int16_t;
  case 4: return Type.int32_t;
  case 8: return Type.int64_t;
  default:
    throw new Error("Cannot represent integers of " + size + " bytes");
  }
};

/**
 * Actual implementation of common C types.
 */

/**
 * The void value.
 */
Type.void_t =
  new Type("void",
           ctypes.void_t);

/**
 * Shortcut for |void*|.
 */
Type.voidptr_t =
  new PtrType("void*",
              ctypes.voidptr_t,
              Type.void_t);

// void* is a special case as we can cast any pointer to/from it
// so we have to shortcut |in_ptr|/|out_ptr|/|inout_ptr| and
// ensure that js-ctypes' casting mechanism is invoked directly
["in_ptr", "out_ptr", "inout_ptr"].forEach(function(key) {
  Object.defineProperty(Type.void_t, key,
  {
    value: Type.voidptr_t
  });
});

/**
 * A Type of integers.
 *
 * @param {string} name The name of this type.
 * @param {CType} implementation The underlying js-ctypes implementation.
 * @param {bool} signed |true| if this is a type of signed integers,
 * |false| otherwise.
 *
 * @constructor
 */
function IntType(name, implementation, signed) {
  Type.call(this, name, implementation);
  this.importFromC = projector(implementation, signed);
  this.project = this.importFromC;
};
IntType.prototype = Object.create(Type.prototype);
IntType.prototype.toMsg = function toMsg(value) {
  if (typeof value == "number") {
    return value;
  }
  return this.project(value);
};

/**
 * A C char (one byte)
 */
Type.char =
  new Type("char",
           ctypes.char);

/**
 * A C wide char (two bytes)
 */
Type.char16_t =
  new Type("char16_t",
           ctypes.char16_t);

 /**
  * Base string types.
  */
Type.cstring = Type.char.in_ptr.withName("[in] C string");
Type.wstring = Type.char16_t.in_ptr.withName("[in] wide string");
Type.out_cstring = Type.char.out_ptr.withName("[out] C string");
Type.out_wstring = Type.char16_t.out_ptr.withName("[out] wide string");

/**
 * A C integer (8-bits).
 */
Type.int8_t =
  new IntType("int8_t", ctypes.int8_t, true);

Type.uint8_t =
  new IntType("uint8_t", ctypes.uint8_t, false);

/**
 * A C integer (16-bits).
 *
 * Also known as WORD under Windows.
 */
Type.int16_t =
  new IntType("int16_t", ctypes.int16_t, true);

Type.uint16_t =
  new IntType("uint16_t", ctypes.uint16_t, false);

/**
 * A C integer (32-bits).
 *
 * Also known as DWORD under Windows.
 */
Type.int32_t =
  new IntType("int32_t", ctypes.int32_t, true);

Type.uint32_t =
  new IntType("uint32_t", ctypes.uint32_t, false);

/**
 * A C integer (64-bits).
 */
Type.int64_t =
  new IntType("int64_t", ctypes.int64_t, true);

Type.uint64_t =
  new IntType("uint64_t", ctypes.uint64_t, false);

 /**
 * A C integer
 *
 * Size depends on the platform.
 */
Type.int = Type.intn_t(ctypes.int.size).
  withName("int");

Type.unsigned_int = Type.intn_t(ctypes.unsigned_int.size).
  withName("unsigned int");

/**
 * A C long integer.
 *
 * Size depends on the platform.
 */
Type.long =
  Type.intn_t(ctypes.long.size).withName("long");

Type.unsigned_long =
  Type.intn_t(ctypes.unsigned_long.size).withName("unsigned long");

/**
 * An unsigned integer with the same size as a pointer.
 *
 * Used to cast a pointer to an integer, whenever necessary.
 */
Type.uintptr_t =
  Type.uintn_t(ctypes.uintptr_t.size).withName("uintptr_t");

/**
 * A boolean.
 * Implemented as a C integer.
 */
Type.bool = Type.int.withName("bool");
Type.bool.importFromC = function projectBool(x) {
  return !!(x.value);
};

/**
 * A user identifier.
 *
 * Implemented as a C integer.
 */
Type.uid_t =
  Type.int.withName("uid_t");

/**
 * A group identifier.
 *
 * Implemented as a C integer.
 */
Type.gid_t =
  Type.int.withName("gid_t");

/**
 * An offset (positive or negative).
 *
 * Implemented as a C integer.
 */
Type.off_t =
  new IntType("off_t", ctypes.off_t, true);

/**
 * A size (positive).
 *
 * Implemented as a C size_t.
 */
Type.size_t =
  new IntType("size_t", ctypes.size_t, false);

/**
 * An offset (positive or negative).
 * Implemented as a C integer.
 */
Type.ssize_t =
  new IntType("ssize_t", ctypes.ssize_t, true);

/**
 * Encoding/decoding strings
 */
Type.uencoder =
  new Type("uencoder", ctypes.StructType("uencoder"));
Type.udecoder =
  new Type("udecoder", ctypes.StructType("udecoder"));

/**
 * Utility class, used to build a |struct| type
 * from a set of field names, types and offsets.
 *
 * @param {string} name The name of the |struct| type.
 * @param {number} size The total size of the |struct| type in bytes.
 */
function HollowStructure(name, size) {
  if (!name) {
    throw new TypeError("HollowStructure expects a name");
  }
  if (!size || size < 0) {
    throw new TypeError("HollowStructure expects a (positive) size");
  }

  // A mapping from offsets in the struct to name/type pairs
  // (or nothing if no field starts at that offset).
  this.offset_to_field_info = [];

  // The name of the struct
  this.name = name;

  // The size of the struct, in bytes
  this.size = size;

  // The number of paddings inserted so far.
  // Used to give distinct names to padding fields.
  this._paddings = 0;
}
HollowStructure.prototype = {
  /**
   * Add a field at a given offset.
   *
   * @param {number} offset The offset at which to insert the field.
   * @param {string} name The name of the field.
   * @param {CType|Type} type The type of the field.
   */
  add_field_at: function add_field_at(offset, name, type) {
    if (offset == null) {
      throw new TypeError("add_field_at requires a non-null offset");
    }
    if (!name) {
      throw new TypeError("add_field_at requires a non-null name");
    }
    if (!type) {
      throw new TypeError("add_field_at requires a non-null type");
    }
    if (type instanceof Type) {
      type = type.implementation;
    }
    if (this.offset_to_field_info[offset]) {
      throw new Error("HollowStructure " + this.name +
                      " already has a field at offset " + offset);
    }
    if (offset + type.size > this.size) {
      throw new Error("HollowStructure " + this.name +
                      " cannot place a value of type " + type +
                      " at offset " + offset +
                      " without exceeding its size of " + this.size);
    }
    let field = {name: name, type:type};
    this.offset_to_field_info[offset] = field;
  },

  /**
   * Create a pseudo-field that will only serve as padding.
   *
   * @param {number} size The number of bytes in the field.
   * @return {Object} An association field-name => field-type,
   * as expected by |ctypes.StructType|.
   */
  _makePaddingField: function makePaddingField(size) {
    let field = ({});
    field["padding_" + this._paddings] =
      ctypes.ArrayType(ctypes.uint8_t, size);
    this._paddings++;
    return field;
  },

  /**
   * Convert this |HollowStructure| into a |Type|.
   */
  getType: function getType() {
    // Contents of the structure, in the format expected
    // by ctypes.StructType.
    let struct = [];

    let i = 0;
    while (i < this.size) {
      let currentField = this.offset_to_field_info[i];
      if (!currentField) {
        // No field was specified at this offset, we need to
        // introduce some padding.

        // Firstly, determine how many bytes of padding
        let padding_length = 1;
        while (i + padding_length < this.size
            && !this.offset_to_field_info[i + padding_length]) {
          ++padding_length;
        }

        // Then add the padding
        struct.push(this._makePaddingField(padding_length));

        // And proceed
        i += padding_length;
      } else {
        // We have a field at this offset.

        // Firstly, ensure that we do not have two overlapping fields
        for (let j = 1; j < currentField.type.size; ++j) {
          let candidateField = this.offset_to_field_info[i + j];
          if (candidateField) {
            throw new Error("Fields " + currentField.name +
              " and " + candidateField.name +
              " overlap at position " + (i + j));
          }
        }

        // Then add the field
        let field = ({});
        field[currentField.name] = currentField.type;
        struct.push(field);

        // And proceed
        i += currentField.type.size;
      }
    }
    let result = new Type(this.name, ctypes.StructType(this.name, struct));
    if (result.implementation.size != this.size) {
      throw new Error("Wrong size for type " + this.name +
          ": expected " + this.size +
          ", found " + result.implementation.size +
          " (" + result.implementation.toSource() + ")");
    }
    return result;
  }
};
exports.HollowStructure = HollowStructure;

/**
 * Representation of a native library.
 *
 * The native library is opened lazily, during the first call to its
 * field |library| or whenever accessing one of the methods imported
 * with declareLazyFFI.
 *
 * @param {string} name A human-readable name for the library. Used
 * for debugging and error reporting.
 * @param {string...} candidates A list of system libraries that may
 * represent this library. Used e.g. to try different library names
 * on distinct operating systems ("libxul", "XUL", etc.).
 *
 * @constructor
 */
function Library(name, ...candidates) {
  this.name = name;
  this._candidates = candidates;
};
Library.prototype = Object.freeze({
  /**
   * The native library as a js-ctypes object.
   *
   * @throws {Error} If none of the candidate libraries could be opened.
   */
  get library() {
    let library;
    delete this.library;
    for (let candidate of this._candidates) {
      try {
        library = ctypes.open(candidate);
        break;
      } catch (ex) {
        LOG("Could not open library", candidate, ex);
      }
    }
    this._candidates = null;
    if (library) {
      Object.defineProperty(this, "library", {
        value: library
      });
      Object.freeze(this);
      return library;
    }
    let error = new Error("Could not open library " + this.name);
    Object.defineProperty(this, "library", {
      get: function() {
        throw error;
      }
    });
    Object.freeze(this);
    throw error;
  },

  /**
   * Declare a function, lazily.
   *
   * @param {object} The object containing the function as a field.
   * @param {string} The name of the field containing the function.
   * @param {string} symbol The name of the function, as defined in the
   * library.
   * @param {ctypes.abi} abi The abi to use, or |null| for default.
   * @param {Type} returnType The type of values returned by the function.
   * @param {...Type} argTypes The type of arguments to the function.
   */
  declareLazyFFI: function(object, field, ...args) {
    let lib = this;
    Object.defineProperty(object, field, {
      get: function() {
        delete this[field];
        let ffi = declareFFI(lib.library, ...args);
        if (ffi) {
          return this[field] = ffi;
        }
        return undefined;
      },
      configurable: true,
      enumerable: true
    });
  },

  /**
   * Define a js-ctypes function lazily using ctypes method declare.
   *
   * @param {object} The object containing the function as a field.
   * @param {string} The name of the field containing the function.
   * @param {string} symbol The name of the function, as defined in the
   * library.
   * @param {ctypes.abi} abi The abi to use, or |null| for default.
   * @param {ctypes.CType} returnType The type of values returned by the function.
   * @param {...ctypes.CType} argTypes The type of arguments to the function.
   */
  declareLazy: function(object, field, ...args) {
    let lib = this;
    Object.defineProperty(object, field, {
      get: function() {
        delete this[field];
        let ffi = lib.library.declare(...args);
        if (ffi) {
          return this[field] = ffi;
        }
        return undefined;
      },
      configurable: true,
      enumerable: true
    });
  },

  /**
   * Define a js-ctypes function lazily using ctypes method declare,
   * with a fallback library to use if this library can't be opened
   * or the function cannot be declared.
   *
   * @param {fallbacklibrary} The fallback Library object.
   * @param {object} The object containing the function as a field.
   * @param {string} The name of the field containing the function.
   * @param {string} symbol The name of the function, as defined in the
   * library.
   * @param {ctypes.abi} abi The abi to use, or |null| for default.
   * @param {ctypes.CType} returnType The type of values returned by the function.
   * @param {...ctypes.CType} argTypes The type of arguments to the function.
   */
  declareLazyWithFallback: function(fallbacklibrary, object, field, ...args) {
    let lib = this;
    Object.defineProperty(object, field, {
      get: function() {
        delete this[field];
        try {
          let ffi = lib.library.declare(...args);
          if (ffi) {
            return this[field] = ffi;
          }
        } catch (ex) {
          // Use the fallback library and get the symbol from there.
          fallbacklibrary.declareLazy(object, field, ...args);
          return object[field];
        }
        return undefined;
      },
      configurable: true,
      enumerable: true
    });
  },

  toString: function() {
    return "[Library " + this.name + "]";
  }
});
exports.Library = Library;

/**
 * Declare a function through js-ctypes
 *
 * @param {ctypes.library} lib The ctypes library holding the function.
 * @param {string} symbol The name of the function, as defined in the
 * library.
 * @param {ctypes.abi} abi The abi to use, or |null| for default.
 * @param {Type} returnType The type of values returned by the function.
 * @param {...Type} argTypes The type of arguments to the function.
 *
 * @return null if the function could not be defined (generally because
 * it does not exist), or a JavaScript wrapper performing the call to C
 * and any type conversion required.
 */
var declareFFI = function declareFFI(lib, symbol, abi,
                                     returnType /*, argTypes ...*/) {
  LOG("Attempting to declare FFI ", symbol);
  // We guard agressively, to avoid any late surprise
  if (typeof symbol != "string") {
    throw new TypeError("declareFFI expects as first argument a string");
  }
  abi = abi || ctypes.default_abi;
  if (Object.prototype.toString.call(abi) != "[object CABI]") {
    // Note: This is the only known manner of checking whether an object
    // is an abi.
    throw new TypeError("declareFFI expects as second argument an abi or null");
  }
  if (!returnType.importFromC) {
    throw new TypeError("declareFFI expects as third argument an instance of Type");
  }
  let signature = [symbol, abi];
  let argtypes  = [];
  for (let i = 3; i < arguments.length; ++i) {
    let current = arguments[i];
    if (!current) {
      throw new TypeError("Missing type for argument " + ( i - 3 ) +
                          " of symbol " + symbol);
    }
    if (!current.implementation) {
      throw new TypeError("Missing implementation for argument " + (i - 3)
                          + " of symbol " + symbol
                          + " ( " + current.name + " )" );
    }
    signature.push(current.implementation);
  }
  try {
    let fun = lib.declare.apply(lib, signature);
    let result = function ffi(...args) {
      for (let i = 0; i < args.length; i++) {
        if (typeof args[i] == "undefined") {
          throw new TypeError("Argument " + i + " of " + symbol + " is undefined");
        }
      }
      let result = fun.apply(fun, args);
      return returnType.importFromC(result, symbol);
    };
    LOG("Function", symbol, "declared");
    return result;
  } catch (x) {
    // Note: Not being able to declare a function is normal.
    // Some functions are OS (or OS version)-specific.
    LOG("Could not declare function ", symbol, x);
    return null;
  }
};
exports.declareFFI = declareFFI;

/**
 * Define a lazy getter to a js-ctypes function using declareFFI.
 *
 * @param {object} The object containing the function as a field.
 * @param {string} The name of the field containing the function.
 * @param {ctypes.library} lib The ctypes library holding the function.
 * @param {string} symbol The name of the function, as defined in the
 * library.
 * @param {ctypes.abi} abi The abi to use, or |null| for default.
 * @param {Type} returnType The type of values returned by the function.
 * @param {...Type} argTypes The type of arguments to the function.
 */
function declareLazyFFI(object, field, ...declareFFIArgs) {
  Object.defineProperty(object, field, {
    get: function() {
      delete this[field];
      let ffi = declareFFI(...declareFFIArgs);
      if (ffi) {
        return this[field] = ffi;
      }
      return undefined;
    },
    configurable: true,
    enumerable: true
  });
}
exports.declareLazyFFI = declareLazyFFI;

/**
 * Define a lazy getter to a js-ctypes function using ctypes method declare.
 *
 * @param {object} The object containing the function as a field.
 * @param {string} The name of the field containing the function.
 * @param {ctypes.library} lib The ctypes library holding the function.
 * @param {string} symbol The name of the function, as defined in the
 * library.
 * @param {ctypes.abi} abi The abi to use, or |null| for default.
 * @param {ctypes.CType} returnType The type of values returned by the function.
 * @param {...ctypes.CType} argTypes The type of arguments to the function.
 */
function declareLazy(object, field, lib, ...declareArgs) {
  Object.defineProperty(object, field, {
    get: function() {
      delete this[field];
      try {
        let ffi = lib.declare(...declareArgs);
        return this[field] = ffi;
      } catch (ex) {
        // The symbol doesn't exist
        return undefined;
      }
    },
    configurable: true
  });
}
exports.declareLazy = declareLazy;

/**
 * Utility function used to sanity check buffer and length arguments.  The
 * buffer must be a Typed Array.
 *
 * @param {Typed array} candidate The buffer.
 * @param {number} bytes The number of bytes that |candidate| should contain.
 *
 * @return number The bytes argument clamped to the length of the buffer.
 */
function normalizeBufferArgs(candidate, bytes) {
  if (!candidate) {
    throw new TypeError("Expecting a Typed Array");
  }
  if (!isTypedArray(candidate)) {
    throw new TypeError("Expecting a Typed Array");
  }
  if (bytes == null) {
    bytes = candidate.byteLength;
  } else if (candidate.byteLength < bytes) {
    throw new TypeError("Buffer is too short. I need at least " +
                       bytes +
                       " bytes but I have only " +
                       candidate.byteLength +
                        "bytes");
  }
  return bytes;
};
exports.normalizeBufferArgs = normalizeBufferArgs;

///////////////////// OS interactions

/**
 * An OS error.
 *
 * This class is provided mostly for type-matching. If you need more
 * details about an error, you should use the platform-specific error
 * codes provided by subclasses of |OS.Shared.Error|.
 *
 * @param {string} operation The operation that failed.
 * @param {string=} path The path of the file on which the operation failed,
 * or nothing if there was no file involved in the failure.
 *
 * @constructor
 */
function OSError(operation, path = "") {
  Error.call(this);
  this.operation = operation;
  this.path = path;
}
OSError.prototype = Object.create(Error.prototype);
exports.OSError = OSError;


///////////////////// Temporary boilerplate
// Boilerplate, to simplify the transition to require()
// Do not rely upon this symbol, it will disappear with
// bug 883050.
exports.OS = {
  Constants: exports.Constants,
  Shared: {
    LOG: LOG,
    clone: clone,
    Type: Type,
    HollowStructure: HollowStructure,
    Error: OSError,
    declareFFI: declareFFI,
    projectValue: projectValue,
    isTypedArray: isTypedArray,
    defineLazyGetter: defineLazyGetter
  }
};

Object.defineProperty(exports.OS.Shared, "DEBUG", {
  get: function() {
    return Config.DEBUG;
  },
  set: function(x) {
    return Config.DEBUG = x;
  }
});
Object.defineProperty(exports.OS.Shared, "TEST", {
  get: function() {
    return Config.TEST;
  },
  set: function(x) {
    return Config.TEST = x;
  }
});


///////////////////// Permanent boilerplate
if (typeof Components != "undefined") {
  this.EXPORTED_SYMBOLS = EXPORTED_SYMBOLS;
  for (let symbol of EXPORTED_SYMBOLS) {
    this[symbol] = exports[symbol];
  }
}