]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpObject.h
2b49ba2a44fbc7f59955e2abaf31f2801e7ca73f
[apple/javascriptcore.git] / runtime / RegExpObject.h
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008, 2012 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 #ifndef RegExpObject_h
22 #define RegExpObject_h
23
24 #include "JSObject.h"
25 #include "RegExp.h"
26
27 namespace JSC {
28
29 class RegExpObject : public JSNonFinalObject {
30 public:
31 typedef JSNonFinalObject Base;
32
33 static RegExpObject* create(VM& vm, Structure* structure, RegExp* regExp)
34 {
35 RegExpObject* object = new (NotNull, allocateCell<RegExpObject>(vm.heap)) RegExpObject(vm, structure, regExp);
36 object->finishCreation(vm);
37 return object;
38 }
39
40 void setRegExp(VM& vm, RegExp* r) { m_regExp.set(vm, this, r); }
41 RegExp* regExp() const { return m_regExp.get(); }
42
43 void setLastIndex(ExecState* exec, size_t lastIndex)
44 {
45 m_lastIndex.setWithoutWriteBarrier(jsNumber(lastIndex));
46 if (LIKELY(m_lastIndexIsWritable))
47 m_lastIndex.setWithoutWriteBarrier(jsNumber(lastIndex));
48 else
49 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
50 }
51 void setLastIndex(ExecState* exec, JSValue lastIndex, bool shouldThrow)
52 {
53 if (LIKELY(m_lastIndexIsWritable))
54 m_lastIndex.set(exec->vm(), this, lastIndex);
55 else if (shouldThrow)
56 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
57 }
58 JSValue getLastIndex() const
59 {
60 return m_lastIndex.get();
61 }
62
63 bool test(ExecState* exec, JSString* string) { return match(exec, string); }
64 JSValue exec(ExecState*, JSString*);
65
66 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
67 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
68
69 DECLARE_EXPORT_INFO;
70
71 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
72 {
73 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
74 }
75
76 protected:
77 JS_EXPORT_PRIVATE RegExpObject(VM&, Structure*, RegExp*);
78 JS_EXPORT_PRIVATE void finishCreation(VM&);
79
80 static const unsigned StructureFlags = OverridesVisitChildren | OverridesGetOwnPropertySlot | Base::StructureFlags;
81
82 static void visitChildren(JSCell*, SlotVisitor&);
83
84 JS_EXPORT_PRIVATE static bool deleteProperty(JSCell*, ExecState*, PropertyName);
85 JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
86 JS_EXPORT_PRIVATE static void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
87 JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
88
89 private:
90 MatchResult match(ExecState*, JSString*);
91
92 WriteBarrier<RegExp> m_regExp;
93 WriteBarrier<Unknown> m_lastIndex;
94 bool m_lastIndexIsWritable;
95 };
96
97 RegExpObject* asRegExpObject(JSValue);
98
99 inline RegExpObject* asRegExpObject(JSValue value)
100 {
101 ASSERT(asObject(value)->inherits(RegExpObject::info()));
102 return static_cast<RegExpObject*>(asObject(value));
103 }
104
105 } // namespace JSC
106
107 #endif // RegExpObject_h