Oolite
Loading...
Searching...
No Matches
OORegExpMatcher.m
Go to the documentation of this file.
1/*
2
3OORegExpMatcher.m
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OORegExpMatcher.h"
29#import "OOJSFunction.h"
30#import "OOJavaScriptEngine.h"
31
32// Pseudo-singleton: a single instance exists at a given time, but can be released.
34
35@implementation OORegExpMatcher
36
37+ (instancetype)regExpMatcher
38{
39 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
40
41 if (sActiveInstance == nil) {
42 sActiveInstance = [[[self alloc] init] autorelease];
43 }
44
45 return sActiveInstance;
46}
47
48- (id)init
49{
50 if ((self = [super init])) {
51 const char* argumentNames[2] = { "string", "regexp" };
52 unsigned codeLine = __LINE__ + 1; // NB: should remain line before code.
53 NSString* code = @"return regexp.test(string);";
54
55 [OOJavaScriptEngine sharedEngine]; // Summon the beast from the Pit.
56
57 JSContext* context = OOJSAcquireContext();
58 _tester = [[OOJSFunction alloc] initWithName:@"matchesRegExp"
59#if OOJS_QUICKJS
60 scope:JS_UNDEFINED
61#else
62 scope:NULL
63#endif
64 code:code
65 argumentCount:2
66 argumentNames:argumentNames
67 fileName:[@__FILE__ lastPathComponent]
68 lineNumber:codeLine
69 context:context];
70
71 OOJSRelinquishContext(context);
72
73 if (_tester == nil)
74 DESTROY(self);
75 }
76
77 return self;
78}
79
80- (void)dealloc
81{
82 if (sActiveInstance == self)
84
88
89 [super dealloc];
90}
91
92- (BOOL)string:(NSString*)string matchesExpression:(NSString*)regExp
93{
94 return [self string:string matchesExpression:regExp flags:0];
95}
96
97- (BOOL)string:(NSString*)string matchesExpression:(NSString*)regExp flags:(NSUInteger)flags
98{
99 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
100
101 size_t expLength = [regExp length];
102 if (EXPECT_NOT(expLength == 0))
103 return NO;
104
105 JSContext* context = OOJSAcquireContext();
106
107 // Create new RegExp object if necessary.
108 if (flags != _cachedFlags || ![regExp isEqualToString:_cachedRegExpString]) {
111
112#if OOJS_QUICKJS
113 // QuickJS has no API to create a RegExp directly, so call the RegExp constructor.
114 _cachedRegExpString = [regExp retain];
115 NSString* flagString = [NSString stringWithFormat:@"%@%@",
116 (flags & kOORegExpCaseInsensitive) ? @"i" : @"",
117 (flags & kOORegExpMultiLine) ? @"m" : @""];
118 JSValue global = JS_GetGlobalObject(context);
119 JSValue constructor = JS_GetPropertyStr(context, global, "RegExp");
120 JSValue args[2] = { OOJSValueFromNativeObject(context, regExp), OOJSValueFromNativeObject(context, flagString) };
121 JSValue regExpObj = JS_CallConstructor(context, constructor, 2, args);
122 if (JS_IsException(regExpObj)) {
124 regExpObj = JS_NULL;
125 }
126 _cachedRegExpObject = [[OOJSValue alloc] initWithJSValue:regExpObj inContext:context];
127 _cachedFlags = flags;
128
129 JS_FreeValue(context, regExpObj);
130 JS_FreeValue(context, args[0]);
131 JS_FreeValue(context, args[1]);
132 JS_FreeValue(context, constructor);
133 JS_FreeValue(context, global);
134#else
135 unichar* buffer;
136 buffer = malloc(expLength * sizeof *buffer);
137 if (EXPECT_NOT(buffer == NULL))
138 return NO;
139 [regExp getCharacters:buffer];
140
141 _cachedRegExpString = [regExp retain];
142 JSObject* regExpObj = JS_NewUCRegExpObjectNoStatics(context, buffer, expLength, (uintN)flags);
143 _cachedRegExpObject = [[OOJSValue alloc] initWithJSObject:regExpObj inContext:context];
144 _cachedFlags = flags;
145
146 free(buffer);
147#endif
148 }
149
150 BOOL result = [_tester evaluatePredicateWithContext:context
151 scope:nil
152 arguments:[NSArray arrayWithObjects:string, _cachedRegExpObject, nil]];
153
154 OOJSRelinquishContext(context);
155
156 return result;
157}
158
159@end
160
161@implementation NSString (OORegExpMatcher)
162
163- (BOOL)oo_matchesRegularExpression:(NSString*)regExp
164{
165 return [[OORegExpMatcher regExpMatcher] string:self matchesExpression:regExp];
166}
167
168@end
#define DESTROY(x)
#define EXPECT_NOT(x)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSReportPendingException(JSContext *context)
OOINLINE void OOJSRelinquishContext(JSContext *context)
return nil
static OORegExpMatcher * sActiveInstance
BOOL evaluatePredicateWithContext:scope:arguments:(JSContext *context,[scope] id jsThis,[arguments] NSArray *arguments)
OOJavaScriptEngine * sharedEngine()
OOJSValue * _cachedRegExpObject
BOOL string:matchesExpression:flags:(NSString *string,[matchesExpression] NSString *regExp,[flags] NSUInteger flags)
NSString * _cachedRegExpString
NSUInteger _cachedFlags
instancetype regExpMatcher()
OOJSFunction * _tester