]>
git.saurik.com Git - cycript.git/blob - Library.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
25 #include "cycript.hpp"
27 #include "Pooling.hpp"
32 #include <ext/stdio_filebuf.h>
40 #include "Cycript.tab.hh"
44 #include "Execute.hpp"
47 template <typename Type_
>
48 _finline
size_t iconv_(size_t (*iconv
)(iconv_t
, Type_
, size_t *, char **, size_t *), iconv_t cd
, char **inbuf
, size_t *inbytesleft
, char **outbuf
, size_t *outbytesleft
) {
49 return iconv(cd
, const_cast<Type_
>(inbuf
), inbytesleft
, outbuf
, outbytesleft
);
53 #define UCS_2_INTERNAL "UCS-2"
55 #define UCS_2_INTERNAL "UCS-2-INTERNAL"
58 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, CYUTF16String utf16
) {
59 _assert(pool
!= NULL
);
61 const char *in(reinterpret_cast<const char *>(utf16
.data
));
63 iconv_t
conversion(_syscall(iconv_open("UTF-8", UCS_2_INTERNAL
)));
66 size_t size(utf16
.size
* 5);
67 char *out(new(pool
) char[size
]);
68 CYUTF8String
utf8(out
, size
);
70 size
= utf16
.size
* 2;
71 _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
));
74 utf8
.size
= out
- utf8
.data
;
76 _syscall(iconv_close(conversion
));
81 CYUTF16String
CYPoolUTF16String(apr_pool_t
*pool
, CYUTF8String utf8
) {
82 _assert(pool
!= NULL
);
84 const char *in(utf8
.data
);
86 iconv_t
conversion(_syscall(iconv_open(UCS_2_INTERNAL
, "UTF-8")));
89 size_t size(utf8
.size
* 5);
90 uint16_t *temp(new (pool
) uint16_t[size
]);
91 CYUTF16String
utf16(temp
, size
* 2);
92 char *out(reinterpret_cast<char *>(temp
));
95 _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf16
.size
));
97 utf16
.size
= reinterpret_cast<uint16_t *>(out
) - utf16
.data
;
100 _syscall(iconv_close(conversion
));
105 /* Index Offsets {{{ */
106 size_t CYGetIndex(const CYUTF8String
&value
) {
107 if (value
.data
[0] != '0') {
109 for (size_t i(0); i
!= value
.size
; ++i
) {
110 if (!DigitRange_
[value
.data
[i
]])
113 index
+= value
.data
[i
] - '0';
116 } else if (value
.size
== 1)
122 // XXX: this isn't actually right
123 bool CYGetOffset(const char *value
, ssize_t
&index
) {
124 if (value
[0] != '0') {
126 index
= strtol(value
, &end
, 10);
127 if (value
+ strlen(value
) == end
)
129 } else if (value
[1] == '\0') {
137 /* JavaScript *ify {{{ */
138 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
139 unsigned quot(0), apos(0);
140 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
143 else if (*value
== '\'')
146 bool single(quot
> apos
);
148 str
<< (single
? '\'' : '"');
150 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
152 case '\\': str
<< "\\\\"; break;
153 case '\b': str
<< "\\b"; break;
154 case '\f': str
<< "\\f"; break;
155 case '\n': str
<< "\\n"; break;
156 case '\r': str
<< "\\r"; break;
157 case '\t': str
<< "\\t"; break;
158 case '\v': str
<< "\\v"; break;
173 // this test is designed to be "awesome", generating neither warnings nor incorrect results
174 if (*value
< 0x20 || *value
>= 0x7f)
175 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
));
180 str
<< (single
? '\'' : '"');
183 void CYNumerify(std::ostringstream
&str
, double value
) {
185 // XXX: I want this to print 1e3 rather than 1000
186 sprintf(string
, "%.17g", value
);
190 bool CYIsKey(CYUTF8String value
) {
191 const char *data(value
.data
);
192 size_t size(value
.size
);
197 if (DigitRange_
[data
[0]]) {
198 size_t index(CYGetIndex(value
));
199 if (index
== _not(size_t))
202 if (!WordStartRange_
[data
[0]])
204 for (size_t i(1); i
!= size
; ++i
)
205 if (!WordEndRange_
[data
[i
]])
213 double CYCastDouble(const char *value
, size_t size
) {
215 double number(strtod(value
, &end
));
216 if (end
!= value
+ size
)
221 double CYCastDouble(const char *value
) {
222 return CYCastDouble(value
, strlen(value
));
225 extern "C" void CydgetPoolParse(apr_pool_t
*remote
, const uint16_t **data
, size_t *size
) {
229 cy::parser
parser(driver
);
231 CYUTF8String
utf8(CYPoolUTF8String(local
, CYUTF16String(*data
, *size
)));
233 driver
.data_
= utf8
.data
;
234 driver
.size_
= utf8
.size
;
236 if (parser
.parse() != 0 || !driver
.errors_
.empty())
240 CYContext
context(options
);
241 driver
.program_
->Replace(context
);
242 std::ostringstream str
;
243 CYOutput
out(str
, options
);
244 out
<< *driver
.program_
;
245 std::string
code(str
.str());
247 CYUTF16String
utf16(CYPoolUTF16String(remote
, CYUTF8String(code
.c_str(), code
.size())));
253 static apr_pool_t
*Pool_
;
255 static bool initialized_
;
257 void CYInitializeStatic() {
262 _aprcall(apr_initialize());
263 _aprcall(apr_pool_create(&Pool_
, NULL
));
266 apr_pool_t
*CYGetGlobalPool() {
267 CYInitializeStatic();
271 void CYThrow(const char *format
, ...) {
273 va_start(args
, format
);
274 throw CYPoolError(format
, args
);
275 // XXX: does this matter? :(
279 const char *CYPoolError::PoolCString(apr_pool_t
*pool
) const {
280 return apr_pstrdup(pool
, message_
);
283 CYPoolError::CYPoolError(const char *format
, ...) {
285 va_start(args
, format
);
286 message_
= apr_pvsprintf(pool_
, format
, args
);
290 CYPoolError::CYPoolError(const char *format
, va_list args
) {
291 message_
= apr_pvsprintf(pool_
, format
, args
);