]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
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 "bool_object.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 | // ------------------------------ BooleanInstance --------------------------- | |
32 | ||
33 | const ClassInfo BooleanInstance::info = { "Boolean", 0, 0 }; | |
34 | ||
35 | BooleanInstance::BooleanInstance(JSObject* proto) | |
36 | : JSWrapperObject(proto) | |
37 | { | |
38 | } | |
39 | ||
40 | // ------------------------------ BooleanPrototype -------------------------- | |
41 | ||
42 | // Functions | |
43 | static JSValue* booleanProtoFuncToString(ExecState*, JSObject*, const List&); | |
44 | static JSValue* booleanProtoFuncValueOf(ExecState*, JSObject*, const List&); | |
45 | ||
46 | // ECMA 15.6.4 | |
47 | ||
48 | BooleanPrototype::BooleanPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype) | |
49 | : BooleanInstance(objectPrototype) | |
50 | { | |
51 | setInternalValue(jsBoolean(false)); | |
52 | ||
53 | putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, booleanProtoFuncToString), DontEnum); | |
54 | putDirectFunction(new 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* thisObj, const List&) | |
63 | { | |
64 | if (!thisObj->inherits(&BooleanInstance::info)) | |
65 | return throwError(exec, TypeError); | |
66 | ||
67 | JSValue* v = static_cast<BooleanInstance*>(thisObj)->internalValue(); | |
68 | ASSERT(v); | |
69 | ||
70 | return jsString(v->toString(exec)); | |
71 | } | |
72 | JSValue* booleanProtoFuncValueOf(ExecState* exec, JSObject* thisObj, const List&) | |
73 | { | |
74 | if (!thisObj->inherits(&BooleanInstance::info)) | |
75 | return throwError(exec, TypeError); | |
76 | ||
77 | JSValue* v = static_cast<BooleanInstance*>(thisObj)->internalValue(); | |
78 | ASSERT(v); | |
79 | ||
80 | // TODO: optimize for bool case | |
81 | return jsBoolean(v->toBoolean(exec)); | |
82 | } | |
83 | ||
84 | // ------------------------------ BooleanObjectImp ----------------------------- | |
85 | ||
86 | ||
87 | BooleanObjectImp::BooleanObjectImp(ExecState* exec, FunctionPrototype* functionPrototype, BooleanPrototype* booleanPrototype) | |
88 | : InternalFunctionImp(functionPrototype, booleanPrototype->classInfo()->className) | |
89 | { | |
90 | putDirect(exec->propertyNames().prototype, booleanPrototype, DontEnum | DontDelete | ReadOnly); | |
91 | ||
92 | // no. of arguments for constructor | |
93 | putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum); | |
94 | } | |
95 | ||
96 | ||
97 | bool BooleanObjectImp::implementsConstruct() const | |
98 | { | |
99 | return true; | |
100 | } | |
101 | ||
102 | // ECMA 15.6.2 | |
103 | JSObject* BooleanObjectImp::construct(ExecState* exec, const List& args) | |
104 | { | |
105 | BooleanInstance* obj(new BooleanInstance(exec->lexicalGlobalObject()->booleanPrototype())); | |
106 | obj->setInternalValue(jsBoolean(args[0]->toBoolean(exec))); | |
107 | return obj; | |
108 | } | |
109 | ||
110 | // ECMA 15.6.1 | |
111 | JSValue* BooleanObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args) | |
112 | { | |
113 | // TODO: optimize for bool case | |
114 | return jsBoolean(args[0]->toBoolean(exec)); | |
115 | } | |
116 | ||
117 | } // namespace KJS |