]> git.saurik.com Git - cycript.git/blob - Library.cpp
On Mac OS X 10.9, cstddef is needed for offsetof.
[cycript.git] / Library.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <dlfcn.h>
23
24 #include "cycript.hpp"
25
26 #include "Pooling.hpp"
27
28 #include <sys/mman.h>
29
30 #include <iostream>
31 #include <ext/stdio_filebuf.h>
32 #include <set>
33 #include <map>
34 #include <iomanip>
35 #include <sstream>
36 #include <cmath>
37
38 #include "Parser.hpp"
39 #include "Cycript.tab.hh"
40
41 #include "Error.hpp"
42 #include "String.hpp"
43 #include "Execute.hpp"
44 #include "JavaScript.hpp"
45
46 #include "ConvertUTF.h"
47
48 /* C Strings {{{ */
49 CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) {
50 // XXX: this is wrong
51 size_t size(utf16.size * 5);
52 char *temp(new(pool) char[size]);
53
54 const uint16_t *lhs(utf16.data);
55 uint8_t *rhs(reinterpret_cast<uint8_t *>(temp));
56 _assert(ConvertUTF16toUTF8(&lhs, lhs + utf16.size, &rhs, rhs + size, lenientConversion) == conversionOK);
57
58 *rhs = 0;
59 return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp);
60 }
61
62 CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) {
63 // XXX: this is wrong
64 size_t size(utf8.size * 5);
65 uint16_t *temp(new (pool) uint16_t[size]);
66
67 const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8.data));
68 uint16_t *rhs(temp);
69 _assert(ConvertUTF8toUTF16(&lhs, lhs + utf8.size, &rhs, rhs + size, lenientConversion) == conversionOK);
70
71 *rhs = 0;
72 return CYUTF16String(temp, rhs - temp);
73 }
74 /* }}} */
75 /* Index Offsets {{{ */
76 size_t CYGetIndex(const CYUTF8String &value) {
77 if (value.data[0] != '0') {
78 size_t index(0);
79 for (size_t i(0); i != value.size; ++i) {
80 if (!DigitRange_[value.data[i]])
81 return _not(size_t);
82 index *= 10;
83 index += value.data[i] - '0';
84 }
85 return index;
86 } else if (value.size == 1)
87 return 0;
88 else
89 return _not(size_t);
90 }
91
92 // XXX: this isn't actually right
93 bool CYGetOffset(const char *value, ssize_t &index) {
94 if (value[0] != '0') {
95 char *end;
96 index = strtol(value, &end, 10);
97 if (value + strlen(value) == end)
98 return true;
99 } else if (value[1] == '\0') {
100 index = 0;
101 return true;
102 }
103
104 return false;
105 }
106 /* }}} */
107 /* JavaScript *ify {{{ */
108 void CYStringify(std::ostringstream &str, const char *data, size_t size) {
109 unsigned quot(0), apos(0);
110 for (const char *value(data), *end(data + size); value != end; ++value)
111 if (*value == '"')
112 ++quot;
113 else if (*value == '\'')
114 ++apos;
115
116 bool single(quot > apos);
117
118 str << (single ? '\'' : '"');
119
120 for (const char *value(data), *end(data + size); value != end; ++value)
121 switch (*value) {
122 case '\\': str << "\\\\"; break;
123 case '\b': str << "\\b"; break;
124 case '\f': str << "\\f"; break;
125 case '\n': str << "\\n"; break;
126 case '\r': str << "\\r"; break;
127 case '\t': str << "\\t"; break;
128 case '\v': str << "\\v"; break;
129
130 case '"':
131 if (!single)
132 str << "\\\"";
133 else goto simple;
134 break;
135
136 case '\'':
137 if (single)
138 str << "\\'";
139 else goto simple;
140 break;
141
142 default:
143 // this test is designed to be "awesome", generating neither warnings nor incorrect results
144 if (*value < 0x20 || *value >= 0x7f)
145 str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value));
146 else simple:
147 str << *value;
148 }
149
150 str << (single ? '\'' : '"');
151 }
152
153 void CYNumerify(std::ostringstream &str, double value) {
154 char string[32];
155 // XXX: I want this to print 1e3 rather than 1000
156 sprintf(string, "%.17g", value);
157 str << string;
158 }
159
160 bool CYIsKey(CYUTF8String value) {
161 const char *data(value.data);
162 size_t size(value.size);
163
164 if (size == 0)
165 return false;
166
167 if (DigitRange_[data[0]]) {
168 size_t index(CYGetIndex(value));
169 if (index == _not(size_t))
170 return false;
171 } else {
172 if (!WordStartRange_[data[0]])
173 return false;
174 for (size_t i(1); i != size; ++i)
175 if (!WordEndRange_[data[i]])
176 return false;
177 }
178
179 return true;
180 }
181 /* }}} */
182
183 double CYCastDouble(const char *value, size_t size) {
184 char *end;
185 double number(strtod(value, &end));
186 if (end != value + size)
187 return NAN;
188 return number;
189 }
190
191 double CYCastDouble(const char *value) {
192 return CYCastDouble(value, strlen(value));
193 }
194
195 size_t CYArrayLength(JSContextRef context, JSObjectRef array) {
196 return CYCastDouble(context, CYGetProperty(context, array, length_s));
197 }
198
199 JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) {
200 JSValueRef exception(NULL);
201 JSValueRef value(JSObjectGetPropertyAtIndex(context, array, index, &exception));
202 CYThrow(context, exception);
203 return value;
204 }
205
206 void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) {
207 JSValueRef exception(NULL);
208 JSValueRef arguments[1];
209 arguments[0] = value;
210 JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
211 JSObjectCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments, &exception);
212 CYThrow(context, exception);
213 }
214
215 extern "C" void CydgetMemoryParse(const uint16_t **data, size_t *size) {
216 CYLocalPool local;
217
218 CYUTF8String utf8(CYPoolUTF8String(local, CYUTF16String(*data, *size)));
219 CYStream stream(utf8.data, utf8.data + utf8.size);
220 CYDriver driver(stream);
221
222 cy::parser parser(driver);
223 if (parser.parse() != 0 || !driver.errors_.empty())
224 return;
225
226 CYOptions options;
227 CYContext context(options);
228 driver.program_->Replace(context);
229 std::ostringstream str;
230 CYOutput out(str, options);
231 out << *driver.program_;
232 std::string code(str.str());
233
234 CYPool pool;
235 CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(code.c_str(), code.size())));
236
237 size_t bytes(utf16.size * sizeof(uint16_t));
238 uint16_t *copy(reinterpret_cast<uint16_t *>(malloc(bytes)));
239 memcpy(copy, utf16.data, bytes);
240
241 *data = copy;
242 *size = utf16.size;
243 }
244
245 CYPool &CYGetGlobalPool() {
246 static CYPool pool;
247 return pool;
248 }
249
250 void CYThrow(const char *format, ...) {
251 va_list args;
252 va_start(args, format);
253 throw CYPoolError(format, args);
254 // XXX: does this matter? :(
255 va_end(args);
256 }
257
258 const char *CYPoolError::PoolCString(CYPool &pool) const {
259 return pool.strdup(message_);
260 }
261
262 CYPoolError::CYPoolError(const CYPoolError &rhs) :
263 message_(pool_.strdup(rhs.message_))
264 {
265 }
266
267 CYPoolError::CYPoolError(const char *format, ...) {
268 va_list args;
269 va_start(args, format);
270 // XXX: there might be a beter way to think about this
271 message_ = pool_.vsprintf(64, format, args);
272 va_end(args);
273 }
274
275 CYPoolError::CYPoolError(const char *format, va_list args) {
276 // XXX: there might be a beter way to think about this
277 message_ = pool_.vsprintf(64, format, args);
278 }