2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007-2008, 2012, 2016 Apple Inc. All Rights Reserved.
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.
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.
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
26 #include "ThrowScope.h"
30 class RegExpObject : public JSNonFinalObject {
32 typedef JSNonFinalObject Base;
33 static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames;
35 static RegExpObject* create(VM& vm, Structure* structure, RegExp* regExp)
37 RegExpObject* object = new (NotNull, allocateCell<RegExpObject>(vm.heap)) RegExpObject(vm, structure, regExp);
38 object->finishCreation(vm);
42 void setRegExp(VM& vm, RegExp* r) { m_regExp.set(vm, this, r); }
43 RegExp* regExp() const { return m_regExp.get(); }
45 bool setLastIndex(ExecState* exec, size_t lastIndex)
48 auto scope = DECLARE_THROW_SCOPE(vm);
50 if (LIKELY(m_lastIndexIsWritable)) {
51 m_lastIndex.setWithoutWriteBarrier(jsNumber(lastIndex));
54 throwTypeError(exec, scope, ASCIILiteral(ReadonlyPropertyWriteError));
57 bool setLastIndex(ExecState* exec, JSValue lastIndex, bool shouldThrow)
60 auto scope = DECLARE_THROW_SCOPE(vm);
62 if (LIKELY(m_lastIndexIsWritable)) {
63 m_lastIndex.set(vm, this, lastIndex);
67 return reject(exec, scope, shouldThrow, ASCIILiteral(ReadonlyPropertyWriteError));
69 JSValue getLastIndex() const
71 return m_lastIndex.get();
74 bool test(ExecState* exec, JSGlobalObject* globalObject, JSString* string) { return !!match(exec, globalObject, string); }
75 bool testInline(ExecState* exec, JSGlobalObject* globalObject, JSString* string) { return !!matchInline(exec, globalObject, string); }
76 JSValue exec(ExecState*, JSGlobalObject*, JSString*);
77 JSValue execInline(ExecState*, JSGlobalObject*, JSString*);
78 MatchResult match(ExecState*, JSGlobalObject*, JSString*);
79 JSValue matchGlobal(ExecState*, JSGlobalObject*, JSString*);
81 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
82 static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
86 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
88 return Structure::create(vm, globalObject, prototype, TypeInfo(RegExpObjectType, StructureFlags), info());
91 static ptrdiff_t offsetOfLastIndex()
93 return OBJECT_OFFSETOF(RegExpObject, m_lastIndex);
96 static ptrdiff_t offsetOfLastIndexIsWritable()
98 return OBJECT_OFFSETOF(RegExpObject, m_lastIndexIsWritable);
101 static unsigned advanceStringUnicode(String, unsigned length, unsigned currentIndex);
104 JS_EXPORT_PRIVATE RegExpObject(VM&, Structure*, RegExp*);
105 JS_EXPORT_PRIVATE void finishCreation(VM&);
107 static void visitChildren(JSCell*, SlotVisitor&);
109 JS_EXPORT_PRIVATE static bool deleteProperty(JSCell*, ExecState*, PropertyName);
110 JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
111 JS_EXPORT_PRIVATE static void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
112 JS_EXPORT_PRIVATE static void getGenericPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
113 JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
116 MatchResult matchInline(ExecState*, JSGlobalObject*, JSString*);
118 WriteBarrier<RegExp> m_regExp;
119 WriteBarrier<Unknown> m_lastIndex;
120 bool m_lastIndexIsWritable;
123 RegExpObject* asRegExpObject(JSValue);
125 inline RegExpObject* asRegExpObject(JSValue value)
127 ASSERT(asObject(value)->inherits(RegExpObject::info()));
128 return static_cast<RegExpObject*>(asObject(value));