Bug 739740 - Export libc constants to JS. r=khuey

This commit is contained in:
David Rajchenbach-Teller 2012-05-09 12:00:48 +02:00
parent 5615fc9ed3
commit 8860f0b8cd
7 changed files with 269 additions and 2 deletions

View File

@ -9,7 +9,7 @@
namespace mozilla {
namespace dom {
static bool
bool
DefineConstants(JSContext* cx, JSObject* obj, ConstantSpec* cs)
{
for (; cs->name; ++cs) {

View File

@ -233,6 +233,12 @@ struct ConstantSpec
JS::Value value;
};
/**
* Add constants to an object.
*/
bool
DefineConstants(JSContext* cx, JSObject* obj, ConstantSpec* cs);
/*
* Create a DOM interface object (if constructorClass is non-null) and/or a
* DOM interface prototype object (if protoClass is non-null).

View File

@ -63,11 +63,19 @@ endif
CPPSRCS = \
nsDeviceSensors.cpp \
OSFileConstants.cpp \
$(NULL)
# We fire the nsDOMDeviceAcceleration
LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src
LOCAL_INCLUDES += \
-I$(topsrcdir)/dom/base \
-I$(topsrcdir)/dom/bindings \
-I$(topsrcdir)/js/xpconnect/src \
-I$(topsrcdir)/js/xpconnect/wrappers \
$(NULL)
# On Systems that have build in geolocation providers,
# we really do not need these.
ifneq (Android,$(OS_TARGET))
@ -79,10 +87,16 @@ EXTRA_COMPONENTS = \
$(NULL)
endif
EXPORTS_NAMESPACES = mozilla
EXPORTS = \
nsDeviceSensors.h \
$(NULL)
EXPORTS_mozilla = \
OSFileConstants.h \
$(NULL)
# We fire the nsDOMDeviceAcceleration
LOCAL_INCLUDES += -I$(topsrcdir)/content/events/src

View File

@ -0,0 +1,222 @@
/* 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

View File

@ -0,0 +1,22 @@
/* 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/. */
#ifndef mozilla_osfileconstants_h__
#define mozilla_osfileconstants_h__
#include "jspubtd.h"
namespace mozilla {
/**
* Define OS-specific constants.
*
* This function creates or uses JS object |OS.Constants| to store
* all its constants.
*/
bool DefineOSFileConstants(JSContext *cx, JSObject *global);
}
#endif // mozilla_osfileconstants_h__

View File

@ -61,6 +61,7 @@ LOCAL_INCLUDES = \
-I$(topsrcdir)/content/base/src \
-I$(topsrcdir)/content/events/src \
-I$(topsrcdir)/dom/base \
-I$(topsrcdir)/dom/system \
-I$(topsrcdir)/xpcom/build \
-I$(topsrcdir)/js/xpconnect/src \
$(NULL)

View File

@ -47,6 +47,7 @@
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/XMLHttpRequestBinding.h"
#include "mozilla/dom/XMLHttpRequestUploadBinding.h"
#include "mozilla/OSFileConstants.h"
#include "nsTraceRefcnt.h"
#include "xpcpublic.h"
@ -988,7 +989,8 @@ CreateDedicatedWorkerGlobalScope(JSContext* aCx)
if (worker->IsChromeWorker() &&
(!chromeworker::InitClass(aCx, global, workerProto, false) ||
!DefineChromeWorkerFunctions(aCx, global))) {
!DefineChromeWorkerFunctions(aCx, global)) ||
!DefineOSFileConstants(aCx, global)) {
return NULL;
}