Bug 785828 - Load libxul.so lazily in OS.Shared. r=froydnj

This commit is contained in:
David Rajchenbach-Teller 2012-08-28 13:23:47 -04:00
parent a7c531b1ca
commit 65719a4f7f

View File

@ -31,6 +31,20 @@
getService(Components.interfaces.nsIOSFileConstantsService).init();
}
// Define a lazy getter for a property
let defineLazyGetter = function(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;
}
});
};
let LOG;
if (typeof console != "undefined" && console.log) {
@ -798,8 +812,11 @@
* Libxul-based utilities, shared by all back-ends.
*/
let libxul = ctypes.open(OS.Constants.Path.libxul);
exports.OS.Shared.libxul = libxul;
// Lazy getter for libxul
defineLazyGetter(exports.OS.Shared, "libxul",
function init_libxul() {
return ctypes.open(OS.Constants.Path.libxul);
});
exports.OS.Shared.Utils = {};
@ -861,49 +878,61 @@
};
let NS_Free = libxul.declare("osfile_ns_free", ctypes.default_abi,
/*return*/ Types.void_t.implementation,
/*ptr*/ Types.voidptr_t.implementation);
let Pointers = {};
defineLazyGetter(Pointers, "NS_Free",
function init_NS_Free() {
return exports.OS.Shared.libxul.declare("osfile_ns_free",
ctypes.default_abi,
/*return*/ Types.void_t.implementation,
/*ptr*/ Types.voidptr_t.implementation);
});
let wstrdup = declareFFI(libxul, "osfile_wstrdup", ctypes.default_abi,
/*return*/ Types.out_wstring.releaseWith(NS_Free),
/*ptr*/ Types.wstring);
/**
* Export a string as a wide string (e.g. a |jschar.ptr|).
*
* @param {string} string A JavaScript String.
* @return {CData} The C representation of that string, as a |jschar*|.
* This value will be automatically garbage-collected once it is
* not referenced anymore.
*/
Strings.exportWString = function exportWString(string) {
return wstrdup(string);
};
/**
* Export a string as a wide string (e.g. a |jschar.ptr|).
*
* @param {string} string A JavaScript String.
* @return {CData} The C representation of that string, as a |jschar*|.
* This value will be automatically garbage-collected once it is
* not referenced anymore.
*/
defineLazyGetter(Strings, "exportWString",
function init_exportWString() {
return declareFFI(exports.OS.Shared.libxul,
"osfile_wstrdup",
ctypes.default_abi,
/*return*/ Types.out_wstring.releaseWith(Pointers.NS_Free),
/*ptr*/ Types.wstring);
});
// Encodings
Strings.encodeAll = declareFFI(libxul, "osfile_EncodeAll",
ctypes.default_abi,
/*return*/ Types.void_t.out_ptr.releaseWith(NS_Free),
/*encoding*/ Types.cstring,
/*source*/ Types.wstring,
/*bytes*/ Types.uint32_t.out_ptr);
defineLazyGetter(Strings, "encodeAll",
function init_encodeAll() {
return declareFFI(exports.OS.Shared.libxul,
"osfile_EncodeAll",
ctypes.default_abi,
/*return*/ Types.void_t.out_ptr.releaseWith(Pointers.NS_Free),
/*encoding*/ Types.cstring,
/*source*/ Types.wstring,
/*bytes*/ Types.uint32_t.out_ptr);
});
let _decodeAll = declareFFI(libxul, "osfile_DecodeAll",
ctypes.default_abi,
/*return*/ Types.out_wstring.releaseWith(NS_Free),
/*encoding*/ Types.cstring,
/*source*/ Types.void_t.in_ptr,
/*bytes*/ Types.uint32_t);
Strings.decodeAll = function decodeAll(encoding, source, bytes) {
let decoded = _decodeAll(encoding, source, bytes);
if (!decoded) {
return null;
}
return Strings.importWString(decoded);
};
defineLazyGetter(Strings, "decodeAll",
function init_decodeAll() {
let _decodeAll = declareFFI(exports.OS.Shared.libxul, "osfile_DecodeAll",
ctypes.default_abi,
/*return*/ Types.out_wstring.releaseWith(Pointers.NS_Free),
/*encoding*/ Types.cstring,
/*source*/ Types.void_t.in_ptr,
/*bytes*/ Types.uint32_t);
return function decodeAll(encoding, source, bytes) {
let decoded = _decodeAll(encoding, source, bytes);
if (!decoded) {
return null;
}
return Strings.importWString(decoded);
};
}
);
})(this);
}