]> git.saurik.com Git - cycript.git/blob - Library.cpp
Automatically generate FFI bridges using libclang.
[cycript.git] / Library.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
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.
11
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.
16
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/>.
19 **/
20 /* }}} */
21
22 #include "cycript.hpp"
23
24 #include <iostream>
25 #include <set>
26 #include <map>
27 #include <iomanip>
28 #include <sstream>
29 #include <cmath>
30
31 #include <dlfcn.h>
32
33 #include <sys/mman.h>
34
35 #include "Code.hpp"
36 #include "ConvertUTF.h"
37 #include "Driver.hpp"
38 #include "Error.hpp"
39 #include "Execute.hpp"
40 #include "Pooling.hpp"
41 #include "String.hpp"
42 #include "Syntax.hpp"
43
44 template <>
45 ::pthread_key_t CYLocal<CYPool>::key_ = Key_();
46
47 /* C Strings {{{ */
48 CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) {
49 // XXX: this is wrong
50 size_t size(utf16.size * 5);
51 char *temp(new(pool) char[size]);
52
53 const uint16_t *lhs(utf16.data);
54 uint8_t *rhs(reinterpret_cast<uint8_t *>(temp));
55 _assert(ConvertUTF16toUTF8(&lhs, lhs + utf16.size, &rhs, rhs + size, lenientConversion) == conversionOK);
56
57 *rhs = 0;
58 return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp);
59 }
60
61 CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) {
62 // XXX: this is wrong
63 size_t size(utf8.size * 5);
64 uint16_t *temp(new (pool) uint16_t[size]);
65
66 const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8.data));
67 uint16_t *rhs(temp);
68 _assert(ConvertUTF8toUTF16(&lhs, lhs + utf8.size, &rhs, rhs + size, lenientConversion) == conversionOK);
69
70 *rhs = 0;
71 return CYUTF16String(temp, rhs - temp);
72 }
73 /* }}} */
74 /* Index Offsets {{{ */
75 size_t CYGetIndex(const CYUTF8String &value) {
76 if (value.data[0] != '0') {
77 size_t index(0);
78 for (size_t i(0); i != value.size; ++i) {
79 if (!DigitRange_[value.data[i]])
80 return _not(size_t);
81 index *= 10;
82 index += value.data[i] - '0';
83 }
84 return index;
85 } else if (value.size == 1)
86 return 0;
87 else
88 return _not(size_t);
89 }
90
91 // XXX: this isn't actually right
92 bool CYGetOffset(const char *value, ssize_t &index) {
93 if (value[0] != '0') {
94 char *end;
95 index = strtol(value, &end, 10);
96 if (value + strlen(value) == end)
97 return true;
98 } else if (value[1] == '\0') {
99 index = 0;
100 return true;
101 }
102
103 return false;
104 }
105 /* }}} */
106 /* JavaScript *ify {{{ */
107 void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c) {
108 bool single;
109 if (c)
110 single = false;
111 else {
112 unsigned quot(0), apos(0);
113 for (const char *value(data), *end(data + size); value != end; ++value)
114 if (*value == '"')
115 ++quot;
116 else if (*value == '\'')
117 ++apos;
118
119 single = quot > apos;
120 }
121
122 str << (single ? '\'' : '"');
123
124 for (const char *value(data), *end(data + size); value != end; ++value)
125 switch (uint8_t next = *value) {
126 case '\\': str << "\\\\"; break;
127 case '\b': str << "\\b"; break;
128 case '\f': str << "\\f"; break;
129 case '\n': str << "\\n"; break;
130 case '\r': str << "\\r"; break;
131 case '\t': str << "\\t"; break;
132 case '\v': str << "\\v"; break;
133
134 case '"':
135 if (!single)
136 str << "\\\"";
137 else goto simple;
138 break;
139
140 case '\'':
141 if (single)
142 str << "\\'";
143 else goto simple;
144 break;
145
146 case '\0':
147 if (value[1] >= '0' && value[1] <= '9')
148 str << "\\x00";
149 else
150 str << "\\0";
151 break;
152
153 default:
154 if (next >= 0x20 && next < 0x7f) simple:
155 str << *value;
156 else {
157 unsigned levels(1);
158 if ((next & 0x80) != 0)
159 while ((next & 0x80 >> ++levels) != 0);
160
161 unsigned point(next & 0xff >> levels);
162 while (--levels != 0)
163 point = point << 6 | uint8_t(*++value) & 0x3f;
164
165 if (point < 0x100)
166 str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << point;
167 else if (point < 0x10000)
168 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << point;
169 else {
170 point -= 0x10000;
171 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xd800 | point >> 0x0a);
172 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xdc00 | point & 0x3ff);
173 }
174 }
175 }
176
177 str << (single ? '\'' : '"');
178 }
179
180 void CYNumerify(std::ostringstream &str, double value) {
181 if (std::isinf(value)) {
182 if (value < 0)
183 str << '-';
184 str << "Infinity";
185 return;
186 }
187
188 char string[32];
189 // XXX: I want this to print 1e3 rather than 1000
190 sprintf(string, "%.17g", value);
191 str << string;
192 }
193
194 bool CYIsKey(CYUTF8String value) {
195 const char *data(value.data);
196 size_t size(value.size);
197
198 if (size == 0)
199 return false;
200
201 if (DigitRange_[data[0]]) {
202 size_t index(CYGetIndex(value));
203 if (index == _not(size_t))
204 return false;
205 } else {
206 if (!WordStartRange_[data[0]])
207 return false;
208 for (size_t i(1); i != size; ++i)
209 if (!WordEndRange_[data[i]])
210 return false;
211 }
212
213 return true;
214 }
215 /* }}} */
216
217 double CYCastDouble(const char *value, size_t size) {
218 char *end;
219 double number(strtod(value, &end));
220 if (end != value + size)
221 return NAN;
222 return number;
223 }
224
225 double CYCastDouble(const char *value) {
226 return CYCastDouble(value, strlen(value));
227 }
228
229 _visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) {
230 return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0;
231 }
232
233 CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) {
234 CYLocalPool local;
235 CYDriver driver(local, stream);
236 _assert(!driver.Parse());
237 _assert(driver.errors_.empty());
238
239 CYOptions options;
240 CYContext context(options);
241 driver.script_->Replace(context);
242
243 std::stringbuf str;
244 CYOutput out(str, options);
245 out << *driver.script_;
246 return $pool.strdup(str.str().c_str());
247 }
248
249 CYUTF8String CYPoolCode(CYPool &pool, CYUTF8String code) {
250 CYStream stream(code.data, code.data + code.size);
251 return CYPoolCode(pool, stream);
252 }
253
254 CYPool &CYGetGlobalPool() {
255 static CYPool pool;
256 return pool;
257 }
258
259 _visible void CYThrow(const char *format, ...) {
260 va_list args;
261 va_start(args, format);
262 throw CYPoolError(format, args);
263 // XXX: does this matter? :(
264 va_end(args);
265 }
266
267 const char *CYPoolError::PoolCString(CYPool &pool) const {
268 return pool.strdup(message_);
269 }
270
271 CYPoolError::CYPoolError(const CYPoolError &rhs) :
272 message_(pool_.strdup(rhs.message_))
273 {
274 }
275
276 CYPoolError::CYPoolError(const char *format, ...) {
277 va_list args;
278 va_start(args, format);
279 // XXX: there might be a beter way to think about this
280 message_ = pool_.vsprintf(64, format, args);
281 va_end(args);
282 }
283
284 CYPoolError::CYPoolError(const char *format, va_list args) {
285 // XXX: there might be a beter way to think about this
286 message_ = pool_.vsprintf(64, format, args);
287 }