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

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 _REGEXP_OBJECT_H_
0022 #define _REGEXP_OBJECT_H_
0023 
0024 #include "internal.h"
0025 #include "function_object.h"
0026 #include "regexp.h"
0027 #include <wtf/OwnArrayPtr.h>
0028 
0029 namespace KJS
0030 {
0031 class ExecState;
0032 class RegExpPrototype : public JSObject
0033 {
0034 public:
0035     RegExpPrototype(ExecState *exec,
0036                     ObjectPrototype *objProto,
0037                     FunctionPrototype *funcProto);
0038     const ClassInfo *classInfo() const override
0039     {
0040         return &info;
0041     }
0042     static const ClassInfo info;
0043 };
0044 
0045 class RegExpProtoFunc : public InternalFunctionImp
0046 {
0047 public:
0048     RegExpProtoFunc(ExecState *, FunctionPrototype *, int i, int len, const Identifier &);
0049 
0050     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0051 
0052     enum { Exec, Test, ToString, Compile };
0053 private:
0054     int id;
0055 };
0056 
0057 class RegExpImp : public JSObject
0058 {
0059 public:
0060     RegExpImp(RegExpPrototype *regexpProto);
0061     ~RegExpImp() override;
0062     void setRegExp(ExecState *exec, RegExp *r);
0063     RegExp *regExp() const
0064     {
0065         return reg;
0066     }
0067 
0068     const ClassInfo *classInfo() const override
0069     {
0070         return &info;
0071     }
0072     static const ClassInfo info;
0073 
0074     JSObject *valueClone(Interpreter *targetCtx) const override;
0075 private:
0076     RegExp *reg;
0077 };
0078 
0079 struct RegExpObjectImpPrivate;
0080 
0081 class RegExpObjectImp : public InternalFunctionImp
0082 {
0083 public:
0084     enum { Dollar1, Dollar2, Dollar3, Dollar4, Dollar5, Dollar6, Dollar7, Dollar8, Dollar9,
0085            Input, Multiline, LastMatch, LastParen, LeftContext, RightContext
0086          };
0087 
0088     RegExpObjectImp(ExecState *exec,
0089                     FunctionPrototype *funcProto,
0090                     RegExpPrototype *regProto);
0091     bool implementsConstruct() const override;
0092     using KJS::JSObject::construct;
0093     JSObject *construct(ExecState *exec, const List &args) override;
0094     JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override;
0095 
0096     using KJS::JSObject::put;
0097     void put(ExecState *, const Identifier &, JSValue *, int attr = None) override;
0098     void putValueProperty(ExecState *, int token, JSValue *, int attr);
0099     using KJS::JSObject::getOwnPropertySlot;
0100     bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot &) override;
0101     JSValue *getValueProperty(ExecState *, int token) const;
0102 
0103     // If resources are exhausted during a match, exec parameter will have an exception
0104     // set, and endOffset will be -1
0105     UString performMatch(RegExp *, ExecState *, const RegExpStringContext &, const UString &,
0106                          int startOffset = 0, int *endOffset = nullptr, int **ovector = nullptr);
0107     JSObject *arrayOfMatches(ExecState *exec, const UString &result) const;
0108 
0109     static void throwRegExpError(ExecState *);
0110 
0111     const ClassInfo *classInfo() const override
0112     {
0113         return &info;
0114     }
0115 
0116     /*
0117      Attempts to create a new regular expression engine for the string p
0118      and the flags stored in flagsInput. If this succeeds, it returns the
0119      engine. If not, it returns 0, and raises an exception in exec
0120     */
0121     static RegExp *makeEngine(ExecState *exec, const UString &p, JSValue *flagsInput);
0122 private:
0123     JSValue *getBackref(int) const;
0124     JSValue *getLastMatch() const;
0125     JSValue *getLastParen() const;
0126     JSValue *getLeftContext() const;
0127     JSValue *getRightContext() const;
0128 
0129     OwnPtr<RegExpObjectImpPrivate> d;
0130 
0131     static const ClassInfo info;
0132 };
0133 
0134 } // namespace
0135 
0136 #endif