]>
git.saurik.com Git - cycript.git/blob - Library.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "cycript.hpp"
35 #include "ConvertUTF.h"
38 #include "Execute.hpp"
39 #include "Pooling.hpp"
44 ::pthread_key_t CYLocal
<CYPool
>::key_
= Key_();
47 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, CYUTF16String utf16
) {
49 size_t size(utf16
.size
* 5);
50 char *temp(new(pool
) char[size
]);
52 const uint16_t *lhs(utf16
.data
);
53 uint8_t *rhs(reinterpret_cast<uint8_t *>(temp
));
54 _assert(ConvertUTF16toUTF8(&lhs
, lhs
+ utf16
.size
, &rhs
, rhs
+ size
, lenientConversion
) == conversionOK
);
57 return CYUTF8String(temp
, reinterpret_cast<char *>(rhs
) - temp
);
60 CYUTF16String
CYPoolUTF16String(CYPool
&pool
, CYUTF8String utf8
) {
62 size_t size(utf8
.size
* 5);
63 uint16_t *temp(new (pool
) uint16_t[size
]);
65 const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8
.data
));
67 _assert(ConvertUTF8toUTF16(&lhs
, lhs
+ utf8
.size
, &rhs
, rhs
+ size
, lenientConversion
) == conversionOK
);
70 return CYUTF16String(temp
, rhs
- temp
);
73 /* Index Offsets {{{ */
74 size_t CYGetIndex(const CYUTF8String
&value
) {
75 if (value
.data
[0] != '0') {
77 for (size_t i(0); i
!= value
.size
; ++i
) {
78 if (!DigitRange_
[value
.data
[i
]])
81 index
+= value
.data
[i
] - '0';
84 } else if (value
.size
== 1)
90 // XXX: this isn't actually right
91 bool CYGetOffset(const char *value
, ssize_t
&index
) {
92 if (value
[0] != '0') {
94 index
= strtol(value
, &end
, 10);
95 if (value
+ strlen(value
) == end
)
97 } else if (value
[1] == '\0') {
105 /* JavaScript *ify {{{ */
106 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
107 unsigned quot(0), apos(0);
108 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
111 else if (*value
== '\'')
114 bool single(quot
> apos
);
116 str
<< (single
? '\'' : '"');
118 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
119 switch (uint8_t next
= *value
) {
120 case '\\': str
<< "\\\\"; break;
121 case '\b': str
<< "\\b"; break;
122 case '\f': str
<< "\\f"; break;
123 case '\n': str
<< "\\n"; break;
124 case '\r': str
<< "\\r"; break;
125 case '\t': str
<< "\\t"; break;
126 case '\v': str
<< "\\v"; break;
141 if (value
[1] >= '0' && value
[1] <= '9')
148 if (next
>= 0x20 && next
< 0x7f) simple
:
152 if ((next
& 0x80) != 0)
153 while ((next
& 0x80 >> ++levels
) != 0);
155 unsigned point(next
& 0xff >> levels
);
156 while (--levels
!= 0)
157 point
= point
<< 6 | uint8_t(*++value
) & 0x3f;
160 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << point
;
161 else if (point
< 0x10000)
162 str
<< "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << point
;
165 str
<< "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xd800 | point
>> 0x0a);
166 str
<< "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xdc00 | point
& 0x3ff);
171 str
<< (single
? '\'' : '"');
174 void CYNumerify(std::ostringstream
&str
, double value
) {
176 // XXX: I want this to print 1e3 rather than 1000
177 sprintf(string
, "%.17g", value
);
181 bool CYIsKey(CYUTF8String value
) {
182 const char *data(value
.data
);
183 size_t size(value
.size
);
188 if (DigitRange_
[data
[0]]) {
189 size_t index(CYGetIndex(value
));
190 if (index
== _not(size_t))
193 if (!WordStartRange_
[data
[0]])
195 for (size_t i(1); i
!= size
; ++i
)
196 if (!WordEndRange_
[data
[i
]])
204 double CYCastDouble(const char *value
, size_t size
) {
206 double number(strtod(value
, &end
));
207 if (end
!= value
+ size
)
212 double CYCastDouble(const char *value
) {
213 return CYCastDouble(value
, strlen(value
));
216 _visible
bool CYStartsWith(const CYUTF8String
&haystack
, const CYUTF8String
&needle
) {
217 return haystack
.size
>= needle
.size
&& strncmp(haystack
.data
, needle
.data
, needle
.size
) == 0;
220 CYUTF8String
CYPoolCode(CYPool
&pool
, std::istream
&stream
) {
222 CYDriver
driver(local
, stream
);
223 _assert(!driver
.Parse());
224 _assert(driver
.errors_
.empty());
227 CYContext
context(options
);
228 driver
.script_
->Replace(context
);
231 CYOutput
out(str
, options
);
232 out
<< *driver
.script_
;
233 return $pool
.strdup(str
.str().c_str());
236 CYPool
&CYGetGlobalPool() {
241 _visible
void CYThrow(const char *format
, ...) {
243 va_start(args
, format
);
244 throw CYPoolError(format
, args
);
245 // XXX: does this matter? :(
249 const char *CYPoolError::PoolCString(CYPool
&pool
) const {
250 return pool
.strdup(message_
);
253 CYPoolError::CYPoolError(const CYPoolError
&rhs
) :
254 message_(pool_
.strdup(rhs
.message_
))
258 CYPoolError::CYPoolError(const char *format
, ...) {
260 va_start(args
, format
);
261 // XXX: there might be a beter way to think about this
262 message_
= pool_
.vsprintf(64, format
, args
);
266 CYPoolError::CYPoolError(const char *format
, va_list args
) {
267 // XXX: there might be a beter way to think about this
268 message_
= pool_
.vsprintf(64, format
, args
);