Bug 768381 - Size of type mode_t depends on the platform. r=yoric

This commit is contained in:
David Rajchenbach-Teller 2012-06-27 20:15:33 -04:00
parent 83c787ba35
commit 44b7a7150b
3 changed files with 87 additions and 10 deletions

View File

@ -202,6 +202,19 @@ static dom::ConstantSpec gLibcProperties[] =
#endif // defined(EWOULDBLOCK)
INT_CONSTANT(EXDEV),
// Constants used to define data structures
//
// Many data structures have different fields/sizes/etc. on
// various OSes / versions of the same OS / platforms. For these
// data structures, we need to compute and export from C the size
// and, if necessary, the offset of fields, so as to be able to
// define the structure in JS.
#if defined(XP_UNIX)
// The size of |mode_t|.
{"OSFILE_SIZEOF_MODE_T", INT_TO_JSVAL(sizeof (mode_t)) },
#endif // defined(XP_UNIX)
PROP_END
};

View File

@ -218,8 +218,48 @@
LOG("Projected as a regular number");
return projectValue;
};
exports.OS.Shared.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.
*/
Types.uintn_t = function uintn_t(size) {
switch (size) {
case 1: return Types.uint8_t;
case 2: return Types.uint16_t;
case 4: return Types.uint32_t;
case 8: return Types.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.
*/
Types.intn_t = function intn_t(size) {
switch (size) {
case 1: return Types.int8_t;
case 2: return Types.int16_t;
case 4: return Types.int32_t;
case 8: return Types.int64_t;
default:
throw new Error("Cannot represent integers of " + size + " bytes");
}
};
/**
* Actual implementation of common C types.
*/
@ -279,6 +319,34 @@
ctypes.unsigned_int,
projector(ctypes.unsigned_int, false));
/**
* A C integer (8-bits).
*/
Types.int8_t =
new Type("int8_t",
ctypes.int8_t,
projectValue);
Types.uint8_t =
new Type("uint8_t",
ctypes.uint8_t,
projectValue);
/**
* A C integer (16-bits).
*
* Also known as WORD under Windows.
*/
Types.int16_t =
new Type("int16_t",
ctypes.int16_t,
projectValue);
Types.uint16_t =
new Type("uint16_t",
ctypes.uint16_t,
projectValue);
/**
* A C integer (32-bits).
*
@ -327,15 +395,6 @@
return !!(x.value);
});
/**
* A file access mode
* Implemented as a C integer.
*/
Types.mode_t =
new Type("mode_t",
ctypes.int,
projector(ctypes.int, true));
/**
* A user identifier.
* Implemented as a C integer.

View File

@ -139,11 +139,16 @@
new Type("string",
ctypes.char.ptr);
// Note: support for strings in js-ctypes is very limited.
// Once bug 552551 has progressed, we should extend this
// type using ctypes.readString/ctypes.writeString
/**
* Type |mode_t|
*/
Types.mode_t = Object.create(
Types.intn_t(OS.Constants.libc.OSFILE_SIZEOF_MODE_T),
{name: {value: "mode_t"}});
// Declare libc functions as functions of |OS.Unix.File|