File indexing completed on 2024-05-12 15:43:35

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Lesser General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Lesser General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Lesser General Public
0016  *  License along with this library; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  *
0019  */
0020 
0021 #ifndef STRING_OBJECT_H_
0022 #define STRING_OBJECT_H_
0023 
0024 #include "function_object.h"
0025 #include "JSWrapperObject.h"
0026 #include "internal.h"
0027 
0028 namespace KJS
0029 {
0030 
0031 class StringInstance : public JSWrapperObject
0032 {
0033 public:
0034     StringInstance(JSObject *proto);
0035     StringInstance(JSObject *proto, StringImp *);
0036     StringInstance(JSObject *proto, const UString &);
0037 
0038     bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
0039     bool getOwnPropertySlot(ExecState *, unsigned propertyName, PropertySlot &) override;
0040 
0041     using KJS::JSObject::put;
0042     void put(ExecState *exec, const Identifier &propertyName, JSValue *, int attr = None) override;
0043     using KJS::JSObject::deleteProperty;
0044     bool deleteProperty(ExecState *exec, const Identifier &propertyName) override;
0045     void getOwnPropertyNames(ExecState *, PropertyNameArray &, PropertyMap::PropertyMode mode) override;
0046     bool getOwnPropertyDescriptor(ExecState *, const Identifier &, PropertyDescriptor &) override;
0047 
0048     UString toString(ExecState *exec) const override;
0049     JSObject *valueClone(Interpreter *targetCtx) const override;
0050 
0051     const ClassInfo *classInfo() const override
0052     {
0053         return &info;
0054     }
0055     static const ClassInfo info;
0056 
0057     StringImp *internalValue() const
0058     {
0059         return static_cast<StringImp *>(JSWrapperObject::internalValue());
0060     }
0061 
0062     bool conversionsCustomized() const
0063     {
0064         return m_conversionsCustomized;
0065     }
0066 private:
0067     bool inlineGetOwnPropertySlot(ExecState *, unsigned, PropertySlot &);
0068 
0069     static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0070     static JSValue *indexGetter(ExecState *, JSObject *, unsigned, const PropertySlot &);
0071 
0072     bool m_conversionsCustomized;
0073 };
0074 
0075 /**
0076  * @internal
0077  *
0078  * The initial value of String.prototype (and thus all objects created
0079  * with the String constructor
0080  */
0081 class StringPrototype : public StringInstance
0082 {
0083 public:
0084     StringPrototype(ExecState *exec,
0085                     ObjectPrototype *objProto);
0086     using KJS::StringInstance::getOwnPropertySlot;
0087     bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
0088     const ClassInfo *classInfo() const override
0089     {
0090         return &info;
0091     }
0092     static const ClassInfo info;
0093 };
0094 
0095 /**
0096  * @internal
0097  *
0098  * Class to implement all methods that are properties of the
0099  * String.prototype object
0100  */
0101 class StringProtoFunc : public InternalFunctionImp
0102 {
0103 public:
0104     StringProtoFunc(ExecState *exec, int i, int len, const Identifier &);
0105 
0106     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0107 
0108     enum { ToString, ValueOf, CharAt, CharCodeAt, Concat, IndexOf, LastIndexOf,
0109            Match, Replace, Search, Slice, Split,
0110            Substr, Substring, FromCharCode, ToLowerCase, ToUpperCase,
0111            ToLocaleLowerCase, ToLocaleUpperCase, Trim, LocaleCompare,
0112            StartsWith, EndsWith, Includes,
0113            // ES6 (Draft 08.11.2013
0114            Repeat
0115 #ifndef KJS_PURE_ECMA
0116            , Big, Small, Blink, Bold, Fixed, Italics, Strike, Sub, Sup,
0117            Fontcolor, Fontsize, Anchor, Link, TrimLeft, TrimRight
0118 #endif
0119          };
0120 
0121     static void setToLowerFunction(UnicodeSupport::StringConversionFunction f);
0122     static void setToUpperFunction(UnicodeSupport::StringConversionFunction f);
0123 private:
0124     int id;
0125 };
0126 
0127 /**
0128  * @internal
0129  *
0130  * The initial value of the global variable's "String" property
0131  */
0132 class StringObjectImp : public InternalFunctionImp
0133 {
0134 public:
0135     StringObjectImp(ExecState *exec,
0136                     FunctionPrototype *funcProto,
0137                     StringPrototype *stringProto);
0138 
0139     bool implementsConstruct() const override;
0140     using KJS::JSObject::construct;
0141     JSObject *construct(ExecState *exec, const List &args) override;
0142     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0143 };
0144 
0145 /**
0146  * @internal
0147  *
0148  * Class to implement all methods that are properties of the
0149  * String object
0150  */
0151 class StringObjectFuncImp : public InternalFunctionImp
0152 {
0153 public:
0154     StringObjectFuncImp(ExecState *, FunctionPrototype *, const Identifier &);
0155     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0156 };
0157 
0158 } // namespace
0159 
0160 #endif
0161