]> git.saurik.com Git - cycript.git/blame - Library.cpp
On Mac OS X, detect -framework JavaVM for CY_JAVA.
[cycript.git] / Library.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
37954781
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
37954781 6/*
f95d2598
JF
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
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
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/>.
b3378a02 19**/
37954781
JF
20/* }}} */
21
37954781 22#include <dlfcn.h>
37954781
JF
23
24#include "cycript.hpp"
25
37954781
JF
26#include "Pooling.hpp"
27
28#include <sys/mman.h>
29
30#include <iostream>
37954781
JF
31#include <set>
32#include <map>
33#include <iomanip>
34#include <sstream>
35#include <cmath>
36
37954781 37#include "Error.hpp"
2f51d6ab 38#include "Execute.hpp"
b12a9965
JF
39#include "Parser.hpp"
40#include "String.hpp"
41
42#include "Cycript.tab.hh"
43#include "Driver.hpp"
37954781 44
142cb9e3 45#include "ConvertUTF.h"
59cd74c1 46
fa3c5be8
JF
47template <>
48::pthread_key_t CYLocal<CYPool>::key_ = Key_();
49
142cb9e3 50/* C Strings {{{ */
b799113b 51CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) {
59cd74c1
JF
52 // XXX: this is wrong
53 size_t size(utf16.size * 5);
142cb9e3 54 char *temp(new(pool) char[size]);
59cd74c1 55
142cb9e3
JF
56 const uint16_t *lhs(utf16.data);
57 uint8_t *rhs(reinterpret_cast<uint8_t *>(temp));
58 _assert(ConvertUTF16toUTF8(&lhs, lhs + utf16.size, &rhs, rhs + size, lenientConversion) == conversionOK);
59cd74c1 59
142cb9e3
JF
60 *rhs = 0;
61 return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp);
59cd74c1
JF
62}
63
b799113b 64CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) {
59cd74c1
JF
65 // XXX: this is wrong
66 size_t size(utf8.size * 5);
67 uint16_t *temp(new (pool) uint16_t[size]);
59cd74c1 68
142cb9e3
JF
69 const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8.data));
70 uint16_t *rhs(temp);
71 _assert(ConvertUTF8toUTF16(&lhs, lhs + utf8.size, &rhs, rhs + size, lenientConversion) == conversionOK);
59cd74c1 72
142cb9e3
JF
73 *rhs = 0;
74 return CYUTF16String(temp, rhs - temp);
59cd74c1
JF
75}
76/* }}} */
37954781
JF
77/* Index Offsets {{{ */
78size_t CYGetIndex(const CYUTF8String &value) {
79 if (value.data[0] != '0') {
ba4fa42f
JF
80 size_t index(0);
81 for (size_t i(0); i != value.size; ++i) {
82 if (!DigitRange_[value.data[i]])
83 return _not(size_t);
84 index *= 10;
85 index += value.data[i] - '0';
86 }
87 return index;
88 } else if (value.size == 1)
37954781 89 return 0;
ba4fa42f
JF
90 else
91 return _not(size_t);
37954781
JF
92}
93
ba4fa42f 94// XXX: this isn't actually right
37954781
JF
95bool CYGetOffset(const char *value, ssize_t &index) {
96 if (value[0] != '0') {
97 char *end;
98 index = strtol(value, &end, 10);
99 if (value + strlen(value) == end)
100 return true;
101 } else if (value[1] == '\0') {
102 index = 0;
103 return true;
104 }
105
106 return false;
107}
108/* }}} */
37954781
JF
109/* JavaScript *ify {{{ */
110void CYStringify(std::ostringstream &str, const char *data, size_t size) {
111 unsigned quot(0), apos(0);
112 for (const char *value(data), *end(data + size); value != end; ++value)
113 if (*value == '"')
114 ++quot;
115 else if (*value == '\'')
116 ++apos;
117
118 bool single(quot > apos);
119
120 str << (single ? '\'' : '"');
121
122 for (const char *value(data), *end(data + size); value != end; ++value)
123 switch (*value) {
124 case '\\': str << "\\\\"; break;
125 case '\b': str << "\\b"; break;
126 case '\f': str << "\\f"; break;
127 case '\n': str << "\\n"; break;
128 case '\r': str << "\\r"; break;
129 case '\t': str << "\\t"; break;
130 case '\v': str << "\\v"; break;
131
132 case '"':
133 if (!single)
134 str << "\\\"";
135 else goto simple;
136 break;
137
138 case '\'':
139 if (single)
140 str << "\\'";
141 else goto simple;
142 break;
143
144 default:
b64ab4da 145 // this test is designed to be "awesome", generating neither warnings nor incorrect results
37954781 146 if (*value < 0x20 || *value >= 0x7f)
51a72a9d 147 str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value));
37954781
JF
148 else simple:
149 str << *value;
150 }
151
152 str << (single ? '\'' : '"');
153}
154
155void CYNumerify(std::ostringstream &str, double value) {
156 char string[32];
157 // XXX: I want this to print 1e3 rather than 1000
158 sprintf(string, "%.17g", value);
159 str << string;
160}
161
162bool CYIsKey(CYUTF8String value) {
163 const char *data(value.data);
164 size_t size(value.size);
165
166 if (size == 0)
167 return false;
168
169 if (DigitRange_[data[0]]) {
170 size_t index(CYGetIndex(value));
171 if (index == _not(size_t))
172 return false;
173 } else {
174 if (!WordStartRange_[data[0]])
175 return false;
176 for (size_t i(1); i != size; ++i)
177 if (!WordEndRange_[data[i]])
178 return false;
179 }
180
181 return true;
182}
183/* }}} */
184
37954781
JF
185double CYCastDouble(const char *value, size_t size) {
186 char *end;
187 double number(strtod(value, &end));
188 if (end != value + size)
189 return NAN;
190 return number;
191}
192
193double CYCastDouble(const char *value) {
194 return CYCastDouble(value, strlen(value));
195}
196
5587a93f 197CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) {
2eb8215d 198 CYLocalPool local;
d3b63265 199 CYDriver driver(stream);
59cd74c1 200
d3b63265 201 cy::parser parser(driver);
57930129
JF
202 _assert(parser.parse() == 0);
203 _assert(driver.errors_.empty());
59cd74c1 204
029bc65b 205 CYOptions options;
2eb8215d 206 CYContext context(options);
59cd74c1 207 driver.program_->Replace(context);
57930129 208
a2b8d7ad 209 std::ostringstream str;
029bc65b 210 CYOutput out(str, options);
59cd74c1 211 out << *driver.program_;
a2b8d7ad 212 return $pool.strdup(str.str().c_str());
57930129 213}
59cd74c1 214
b799113b 215CYPool &CYGetGlobalPool() {
b799113b
JF
216 static CYPool pool;
217 return pool;
37954781
JF
218}
219
37954781
JF
220void CYThrow(const char *format, ...) {
221 va_list args;
b64ab4da 222 va_start(args, format);
37954781
JF
223 throw CYPoolError(format, args);
224 // XXX: does this matter? :(
b64ab4da 225 va_end(args);
37954781
JF
226}
227
b799113b
JF
228const char *CYPoolError::PoolCString(CYPool &pool) const {
229 return pool.strdup(message_);
37954781
JF
230}
231
0cbeddf8
JF
232CYPoolError::CYPoolError(const CYPoolError &rhs) :
233 message_(pool_.strdup(rhs.message_))
234{
235}
236
37954781
JF
237CYPoolError::CYPoolError(const char *format, ...) {
238 va_list args;
b64ab4da 239 va_start(args, format);
0cbeddf8
JF
240 // XXX: there might be a beter way to think about this
241 message_ = pool_.vsprintf(64, format, args);
b64ab4da 242 va_end(args);
37954781
JF
243}
244
245CYPoolError::CYPoolError(const char *format, va_list args) {
0cbeddf8
JF
246 // XXX: there might be a beter way to think about this
247 message_ = pool_.vsprintf(64, format, args);
37954781 248}