gecko/dom/system/OSFileConstants.cpp
2012-04-30 12:14:38 +02:00

223 lines
5.3 KiB
C++

/* 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/. */
#include "fcntl.h"
#include "errno.h"
#if defined(XP_UNIX)
#include "unistd.h"
#endif // defined(XP_UNIX)
#include "jsapi.h"
#include "jsfriendapi.h"
#include "BindingUtils.h"
#include "OSFileConstants.h"
/**
* This module defines the basic libc constants (error numbers, open modes,
* etc.) used by OS.File and possibly other OS-bound JavaScript libraries.
*/
namespace mozilla {
/**
* Define a simple read-only property holding an integer.
*
* @param name The name of the constant. Used both as the JS name for the
* constant and to access its value. Must be defined.
*
* Produces a |ConstantSpec|.
*/
#define INT_CONSTANT(name) \
{ #name, INT_TO_JSVAL(name) }
/**
* End marker for ConstantSpec
*/
#define PROP_END { NULL, JSVAL_VOID }
// Define missing constants for Android
#if !defined(S_IRGRP)
#define S_IXOTH 0001
#define S_IWOTH 0002
#define S_IROTH 0004
#define S_IRWXO 0007
#define S_IXGRP 0010
#define S_IWGRP 0020
#define S_IRGRP 0040
#define S_IRWXG 0070
#define S_IXUSR 0100
#define S_IWUSR 0200
#define S_IRUSR 0400
#define S_IRWXU 0700
#endif // !defined(S_IRGRP)
/**
* The properties defined in libc.
*
* If you extend this list of properties, please
* separate categories ("errors", "open", etc.),
* keep properties organized by alphabetical order
* and #ifdef-away properties that are not portable.
*/
static dom::ConstantSpec gLibcProperties[] =
{
// Arguments for open
INT_CONSTANT(O_APPEND),
INT_CONSTANT(O_CREAT),
#if defined(O_DIRECTORY)
INT_CONSTANT(O_DIRECTORY),
#endif // defined(O_DIRECTORY)
#if defined(O_EVTONLY)
INT_CONSTANT(O_EVTONLY),
#endif // defined(O_EVTONLY)
INT_CONSTANT(O_EXCL),
#if defined(O_EXLOCK)
INT_CONSTANT(O_EXLOCK),
#endif // defined(O_EXLOCK)
#if defined(O_LARGEFILE)
INT_CONSTANT(O_LARGEFILE),
#endif // defined(O_LARGEFILE)
#if defined(O_NOFOLLOW)
INT_CONSTANT(O_NOFOLLOW),
#endif // defined(O_NOFOLLOW)
#if defined(O_NONBLOCK)
INT_CONSTANT(O_NONBLOCK),
#endif // defined(O_NONBLOCK)
INT_CONSTANT(O_RDONLY),
INT_CONSTANT(O_RDWR),
#if defined(O_RSYNC)
INT_CONSTANT(O_RSYNC),
#endif // defined(O_RSYNC)
#if defined(O_SHLOCK)
INT_CONSTANT(O_SHLOCK),
#endif // defined(O_SHLOCK)
#if defined(O_SYMLINK)
INT_CONSTANT(O_SYMLINK),
#endif // defined(O_SYMLINK)
#if defined(O_SYNC)
INT_CONSTANT(O_SYNC),
#endif // defined(O_SYNC)
INT_CONSTANT(O_TRUNC),
INT_CONSTANT(O_WRONLY),
#if defined(AT_EACCESS)
INT_CONSTANT(AT_EACCESS),
#endif //defined(AT_EACCESS)
#if defined(AT_FDCWD)
INT_CONSTANT(AT_FDCWD),
#endif //defined(AT_FDCWD)
#if defined(AT_SYMLINK_NOFOLLOW)
INT_CONSTANT(AT_SYMLINK_NOFOLLOW),
#endif //defined(AT_SYMLINK_NOFOLLOW)
// access
#if defined(F_OK)
INT_CONSTANT(F_OK),
INT_CONSTANT(R_OK),
INT_CONSTANT(W_OK),
INT_CONSTANT(X_OK),
#endif // defined(F_OK)
// modes
INT_CONSTANT(S_IRGRP),
INT_CONSTANT(S_IROTH),
INT_CONSTANT(S_IRUSR),
INT_CONSTANT(S_IRWXG),
INT_CONSTANT(S_IRWXO),
INT_CONSTANT(S_IRWXU),
INT_CONSTANT(S_IWGRP),
INT_CONSTANT(S_IWOTH),
INT_CONSTANT(S_IWUSR),
INT_CONSTANT(S_IXOTH),
INT_CONSTANT(S_IXGRP),
INT_CONSTANT(S_IXUSR),
// seek
INT_CONSTANT(SEEK_CUR),
INT_CONSTANT(SEEK_END),
INT_CONSTANT(SEEK_SET),
// error values
INT_CONSTANT(EACCES),
INT_CONSTANT(EAGAIN),
INT_CONSTANT(EBADF),
INT_CONSTANT(EEXIST),
INT_CONSTANT(EFAULT),
INT_CONSTANT(EFBIG),
INT_CONSTANT(EINVAL),
INT_CONSTANT(EIO),
INT_CONSTANT(EISDIR),
INT_CONSTANT(ELOOP),
INT_CONSTANT(EMFILE),
INT_CONSTANT(ENAMETOOLONG),
INT_CONSTANT(ENFILE),
INT_CONSTANT(ELOOP),
INT_CONSTANT(ENOENT),
INT_CONSTANT(ENOMEM),
INT_CONSTANT(ENOSPC),
INT_CONSTANT(ENOTDIR),
INT_CONSTANT(ENXIO),
INT_CONSTANT(EOPNOTSUPP),
INT_CONSTANT(EOVERFLOW),
INT_CONSTANT(EPERM),
INT_CONSTANT(ERANGE),
INT_CONSTANT(ETIMEDOUT),
INT_CONSTANT(EWOULDBLOCK),
INT_CONSTANT(EXDEV),
PROP_END
};
/**
* Get a field of an object as an object.
*
* If the field does not exist, create it. If it exists but is not an
* object, throw a JS error.
*/
JSObject *GetOrCreateObjectProperty(JSContext *cx, JSObject *aObject,
const char *aProperty)
{
jsval val;
if (JS_GetProperty(cx, aObject, aProperty, &val)
&& !JSVAL_IS_VOID(val)) {
if (JSVAL_IS_OBJECT(val)) {
return JSVAL_TO_OBJECT(val);
}
else {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_UNEXPECTED_TYPE, aProperty, "not an object");
return NULL;
}
}
return JS_DefineObject(cx, aObject, aProperty, NULL, NULL, JSPROP_ENUMERATE);
}
/**
* Define OS-specific constants.
*
* This function creates or uses JS object |OS.Constants| to store
* all its constants.
*/
bool DefineOSFileConstants(JSContext *cx, JSObject *global)
{
JSObject *objOS;
if (!(objOS = GetOrCreateObjectProperty(cx, global, "OS"))) {
return false;
}
JSObject *objConstants;
if (!(objConstants = GetOrCreateObjectProperty(cx, objOS, "Constants"))) {
return false;
}
JSObject *objLibc;
if (!(objLibc = GetOrCreateObjectProperty(cx, objConstants, "libc"))) {
return false;
}
return dom::DefineConstants(cx, objLibc, gLibcProperties);
}
} // namespace mozilla