1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is SpiderMonkey method-guard code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2012
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef MethodGuard_inl_h___
41 : #define MethodGuard_inl_h___
42 :
43 : #include "jsobj.h"
44 :
45 : #include "MethodGuard.h"
46 :
47 : #include "jsobjinlines.h"
48 :
49 : #include "BooleanObject-inl.h"
50 : #include "NumberObject-inl.h"
51 : #include "StringObject-inl.h"
52 :
53 : namespace js {
54 :
55 : namespace detail {
56 :
57 : template<typename T> class PrimitiveBehavior { };
58 :
59 : template<>
60 : class PrimitiveBehavior<bool> {
61 : public:
62 437 : static inline bool isType(const Value &v) { return v.isBoolean(); }
63 14 : static inline bool extract(const Value &v) { return v.toBoolean(); }
64 225 : static inline bool extract(JSObject &obj) { return obj.asBoolean().unbox(); }
65 423 : static inline Class *getClass() { return &BooleanClass; }
66 : };
67 :
68 : template<>
69 : class PrimitiveBehavior<double> {
70 : public:
71 246567 : static inline bool isType(const Value &v) { return v.isNumber(); }
72 245869 : static inline double extract(const Value &v) { return v.toNumber(); }
73 311 : static inline double extract(JSObject &obj) { return obj.asNumber().unbox(); }
74 698 : static inline Class *getClass() { return &NumberClass; }
75 : };
76 :
77 : template<>
78 : class PrimitiveBehavior<JSString *> {
79 : public:
80 5424 : static inline bool isType(const Value &v) { return v.isString(); }
81 5129 : static inline JSString *extract(const Value &v) { return v.toString(); }
82 169 : static inline JSString *extract(JSObject &obj) { return obj.asString().unbox(); }
83 295 : static inline Class *getClass() { return &StringClass; }
84 : };
85 :
86 : } /* namespace detail */
87 :
88 : inline JSObject *
89 1805934 : NonGenericMethodGuard(JSContext *cx, CallArgs args, Native native, Class *clasp, bool *ok)
90 : {
91 1805934 : const Value &thisv = args.thisv();
92 1805934 : if (thisv.isObject()) {
93 1805916 : JSObject &obj = thisv.toObject();
94 1805916 : if (obj.getClass() == clasp) {
95 1800120 : *ok = true; /* quell gcc overwarning */
96 1800120 : return &obj;
97 : }
98 : }
99 :
100 5814 : *ok = HandleNonGenericMethodClassMismatch(cx, args, native, clasp);
101 5814 : return NULL;
102 : }
103 :
104 : template <typename T>
105 : inline bool
106 252428 : BoxedPrimitiveMethodGuard(JSContext *cx, CallArgs args, Native native, T *v, bool *ok)
107 : {
108 : typedef detail::PrimitiveBehavior<T> Behavior;
109 :
110 252428 : const Value &thisv = args.thisv();
111 252428 : if (Behavior::isType(thisv)) {
112 251012 : *v = Behavior::extract(thisv);
113 251012 : return true;
114 : }
115 :
116 1416 : if (!NonGenericMethodGuard(cx, args, native, Behavior::getClass(), ok))
117 711 : return false;
118 :
119 705 : *v = Behavior::extract(thisv.toObject());
120 705 : return true;
121 : }
122 :
123 : } /* namespace js */
124 :
125 : #endif /* MethodGuard_inl_h___ */
|