1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 | #include "BooleanObject.h"
|
---|
23 |
|
---|
24 | #include "JSGlobalObject.h"
|
---|
25 | #include "error_object.h"
|
---|
26 | #include "operations.h"
|
---|
27 | #include <wtf/Assertions.h>
|
---|
28 |
|
---|
29 | namespace KJS {
|
---|
30 |
|
---|
31 | // ------------------------------ BooleanObject ---------------------------
|
---|
32 |
|
---|
33 | const ClassInfo BooleanObject::info = { "Boolean", 0, 0, 0 };
|
---|
34 |
|
---|
35 | BooleanObject::BooleanObject(JSObject* proto)
|
---|
36 | : JSWrapperObject(proto)
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 | // ------------------------------ BooleanPrototype --------------------------
|
---|
41 |
|
---|
42 | // Functions
|
---|
43 | static JSValue* booleanProtoFuncToString(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
44 | static JSValue* booleanProtoFuncValueOf(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
45 |
|
---|
46 | // ECMA 15.6.4
|
---|
47 |
|
---|
48 | BooleanPrototype::BooleanPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype)
|
---|
49 | : BooleanObject(objectPrototype)
|
---|
50 | {
|
---|
51 | setInternalValue(jsBoolean(false));
|
---|
52 |
|
---|
53 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, booleanProtoFuncToString), DontEnum);
|
---|
54 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().valueOf, booleanProtoFuncValueOf), DontEnum);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | // ------------------------------ Functions --------------------------
|
---|
59 |
|
---|
60 | // ECMA 15.6.4.2 + 15.6.4.3
|
---|
61 |
|
---|
62 | JSValue* booleanProtoFuncToString(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)
|
---|
63 | {
|
---|
64 | if (thisValue == jsBoolean(false))
|
---|
65 | return jsString(exec, "false");
|
---|
66 |
|
---|
67 | if (thisValue == jsBoolean(true))
|
---|
68 | return jsString(exec, "true");
|
---|
69 |
|
---|
70 | if (!thisValue->isObject(&BooleanObject::info))
|
---|
71 | return throwError(exec, TypeError);
|
---|
72 |
|
---|
73 | if (static_cast<BooleanObject*>(thisValue)->internalValue() == jsBoolean(false))
|
---|
74 | return jsString(exec, "false");
|
---|
75 |
|
---|
76 | ASSERT(static_cast<BooleanObject*>(thisValue)->internalValue() == jsBoolean(true));
|
---|
77 | return jsString(exec, "true");
|
---|
78 | }
|
---|
79 |
|
---|
80 | JSValue* booleanProtoFuncValueOf(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)
|
---|
81 | {
|
---|
82 | if (JSImmediate::isBoolean(thisValue))
|
---|
83 | return thisValue;
|
---|
84 |
|
---|
85 | if (!thisValue->isObject(&BooleanObject::info))
|
---|
86 | return throwError(exec, TypeError);
|
---|
87 |
|
---|
88 | return static_cast<BooleanObject*>(thisValue)->internalValue();
|
---|
89 | }
|
---|
90 |
|
---|
91 | // ------------------------------ BooleanConstructor -----------------------------
|
---|
92 |
|
---|
93 |
|
---|
94 | BooleanConstructor::BooleanConstructor(ExecState* exec, FunctionPrototype* functionPrototype, BooleanPrototype* booleanPrototype)
|
---|
95 | : InternalFunction(functionPrototype, Identifier(exec, booleanPrototype->classInfo()->className))
|
---|
96 | {
|
---|
97 | putDirect(exec->propertyNames().prototype, booleanPrototype, DontEnum | DontDelete | ReadOnly);
|
---|
98 |
|
---|
99 | // no. of arguments for constructor
|
---|
100 | putDirect(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontDelete | DontEnum);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // ECMA 15.6.2
|
---|
104 | JSObject* constructBoolean(ExecState* exec, const ArgList& args)
|
---|
105 | {
|
---|
106 | BooleanObject* obj = new (exec) BooleanObject(exec->lexicalGlobalObject()->booleanPrototype());
|
---|
107 | obj->setInternalValue(jsBoolean(args[0]->toBoolean(exec)));
|
---|
108 | return obj;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static JSObject* constructWithBooleanConstructor(ExecState* exec, JSObject*, const ArgList& args)
|
---|
112 | {
|
---|
113 | return constructBoolean(exec, args);
|
---|
114 | }
|
---|
115 |
|
---|
116 | ConstructType BooleanConstructor::getConstructData(ConstructData& constructData)
|
---|
117 | {
|
---|
118 | constructData.native.function = constructWithBooleanConstructor;
|
---|
119 | return ConstructTypeNative;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // ECMA 15.6.1
|
---|
123 | static JSValue* callBooleanConstructor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
124 | {
|
---|
125 | return jsBoolean(args[0]->toBoolean(exec));
|
---|
126 | }
|
---|
127 |
|
---|
128 | CallType BooleanConstructor::getCallData(CallData& callData)
|
---|
129 | {
|
---|
130 | callData.native.function = callBooleanConstructor;
|
---|
131 | return CallTypeNative;
|
---|
132 | }
|
---|
133 |
|
---|
134 | JSObject* constructBooleanFromImmediateBoolean(ExecState* exec, JSValue* immediateBooleanValue)
|
---|
135 | {
|
---|
136 | BooleanObject* obj = new (exec) BooleanObject(exec->lexicalGlobalObject()->booleanPrototype());
|
---|
137 | obj->setInternalValue(immediateBooleanValue);
|
---|
138 | return obj;
|
---|
139 | }
|
---|
140 |
|
---|
141 | } // namespace KJS
|
---|