1 // Copyright 2006, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // Common definitions for shared code between the Firefox and IE implementations
29 #ifndef GEARS_BASE_COMMON_COMMON_H__
30 #define GEARS_BASE_COMMON_COMMON_H__
34 #include "common/genfiles/product_constants.h" // from OUTDIR
35 #include "gears/base/common/int_types.h"
36 #include "gears/base/common/string16.h"
38 // TODO(michaeln): would be nice to be able to include base/basictypes.h
39 // here rather than cherry pick and duplicate code, resolve issues with
42 // ARRAYSIZE performs essentially the same calculation as arraysize,
43 // but can be used on anonymous types or types defined inside
44 // functions. It's less safe than arraysize as it accepts some
45 // (although not all) pointers. Therefore, you should use arraysize
48 // The expression ARRAYSIZE(a) is a compile-time constant of type
51 // ARRAYSIZE catches a few type errors. If you see a compiler error
53 // "warning: division by zero in ..."
55 // when using ARRAYSIZE, you are (wrongfully) giving it a pointer.
56 // You should only use ARRAYSIZE on statically allocated arrays.
58 // The following comments are on the implementation details, and can
59 // be ignored by the users.
61 // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
62 // the array) and sizeof(*(arr)) (the # of bytes in one array
63 // element). If the former is divisible by the latter, perhaps arr is
64 // indeed an array, in which case the division result is the # of
65 // elements in the array. Otherwise, arr cannot possibly be an array,
66 // and we generate a compiler error to prevent the code from
69 // Since the size of bool is implementation-defined, we need to cast
70 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
71 // result has type size_t.
73 // This macro is not perfect as it wrongfully accepts certain
74 // pointers, namely where the pointer size is divisible by the pointee
75 // size. Since all our code has to go through a 32-bit compiler,
76 // where a pointer is 4 bytes, this means all pointers to a type whose
77 // size is 3 or greater than 4 will be (righteously) rejected.
79 // Kudos to Jorg Brown for this simple and elegant implementation.
83 // Starting with Visual C++ 2005, ARRAYSIZE is defined in WinNT.h
84 #if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(WINCE)
87 #define ARRAYSIZE(a) \
88 ((sizeof(a) / sizeof(*(a))) / \
89 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
92 #define WIDEN2(x) L ## x
93 #define WIDEN(x) WIDEN2(x)
94 #define __WFILE__ WIDEN(__FILE__)
96 #define GET_INTERNAL_ERROR_MESSAGE() \
97 (std::string16(STRING16(L"Internal Error: " __WFILE__ L" Line ")) + \
98 IntegerToString16(__LINE__))
100 // JS represents all numbers as doubles (IEEE 754). This
101 // effectively gives the [-2^53, 2^53] range of integers that
102 // can be represented by IEEE 754 without loss of precision.
103 // For this reason, we cap the range of supported integers
104 // to [-2^53, 2^53] by throwing an exception if the values
105 // are outside this interval. This restriction is enforced,
106 // for example, in GearsDatabase::get_lastInsertRowId() and
107 // GearsResultSet::field().
108 #define JS_INT_MAX (GG_LONGLONG(1) << DBL_MANT_DIG) // 2^53
109 #define JS_INT_MIN (-JS_INT_MAX) // -2^53
111 // Defines private prototypes for copy constructor and assigment operator. Do
112 // not implement these methods.
113 #define DISALLOW_EVIL_CONSTRUCTORS(CLASS) \
115 CLASS(const CLASS&); \
116 CLASS& operator=(const CLASS&)
119 // Macros for returning success/failure from scripted objects.
121 // TODO(cprince): Move these to base_class.h when IE has such a file.
122 // This generic "common.h" should eventually go away. Each file should
123 // explicitly #include what it really needs, instead of using this catch-all.
126 #include "gears/base/common/common_ie.h"
128 #define RETURN_NORMAL() return S_OK
129 #define RETURN_EXCEPTION(msg) \
131 /* AtlReportError has char* and WCHAR* versions */ \
132 return AtlReportError(CLSID_NULL, msg); \
137 #include "gears/base/common/common_ff.h"
139 #define RETURN_NORMAL() return NS_OK
140 #define RETURN_EXCEPTION(msg) \
142 LOG(("Exception: %s", msg)); \
143 return JsSetException(this->EnvPageJsContext(), this->GetJsRunner(), \
144 msg, !this->EnvIsWorker()); \
149 #include "gears/base/common/common_np.h"
153 #include "gears/base/common/common_sf.h"
155 #define RETURN_NORMAL() return 0
156 #define RETURN_EXCEPTION(msg) \
158 /* TODO(waylonis): Return e.message to try/catch blocks in Safari. */ \
159 NSLog(@"Exception: %s", msg); \
164 #error "common.h: BROWSER_?? not defined."
168 #endif // GEARS_BASE_COMMON_COMMON_H__