]> git.saurik.com Git - cycript.git/blob - Decode.cpp
Provide a new ?destroy to release the JSContext.
[cycript.git] / Decode.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 <sstream>
23
24 #include "Decode.hpp"
25 #include "Replace.hpp"
26
27 CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
28 switch (type->primitive) {
29 case sig::function_P: {
30 _assert(type->data.signature.count != 0);
31 CYTypedParameter *parameter(NULL);
32 for (size_t i(type->data.signature.count - 1); i != 0; --i)
33 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
34 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeFunctionWith(parameter));
35 } break;
36
37 case sig::typename_P: return $ CYTypedIdentifier($V("Class"));
38 case sig::union_P: _assert(false); break;
39 case sig::string_P: return $ CYTypedIdentifier($V("char"), $ CYTypePointerTo());
40 case sig::selector_P: return $ CYTypedIdentifier($V("SEL"));
41
42 case sig::block_P: {
43 _assert(type->data.signature.count != 0);
44 CYTypedParameter *parameter(NULL);
45 for (size_t i(type->data.signature.count - 1); i != 0; --i)
46 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
47 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter));
48 } break;
49
50 case sig::object_P: {
51 if (type->name == NULL)
52 return $ CYTypedIdentifier($V("id"));
53 else
54 return $ CYTypedIdentifier($V(type->name), $ CYTypePointerTo());
55 } break;
56
57 case sig::boolean_P: return $ CYTypedIdentifier($V("bool"));
58 case sig::uchar_P: return $ CYTypedIdentifier($V("uchar"));
59 case sig::uint_P: return $ CYTypedIdentifier($V("uint"));
60 case sig::ulong_P: return $ CYTypedIdentifier($V("ulong"));
61 case sig::ulonglong_P: return $ CYTypedIdentifier($V("ulonglong"));
62 case sig::ushort_P: return $ CYTypedIdentifier($V("ushort"));
63 case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
64
65 // XXX: once again, the issue of void here is incorrect
66 case sig::pointer_P: {
67 CYTypedIdentifier *typed;
68 if (type->data.data.type == NULL)
69 typed = $ CYTypedIdentifier($V("void"));
70 else
71 typed = Decode(pool, type->data.data.type);
72 return typed->Modify($ CYTypePointerTo());
73 } break;
74
75 case sig::bit_P: _assert(false); break;
76 case sig::char_P: return $ CYTypedIdentifier($V("char"));
77 case sig::double_P: return $ CYTypedIdentifier($V("double"));
78 case sig::float_P: return $ CYTypedIdentifier($V("float"));
79 case sig::int_P: return $ CYTypedIdentifier($V("int"));
80 case sig::long_P: return $ CYTypedIdentifier($V("long"));
81 case sig::longlong_P: return $ CYTypedIdentifier($V("longlong"));
82 case sig::short_P: return $ CYTypedIdentifier($V("short"));
83
84 // XXX: this happens to work, but is totally wrong
85 case sig::void_P: return $ CYTypedIdentifier($V("void"));
86
87 case sig::struct_P: {
88 _assert(type->name != NULL);
89 return $ CYTypedIdentifier($V(type->name));
90 } break;
91 }
92
93 _assert(false);
94 return NULL;
95 }
96
97 CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
98 CYTypedIdentifier *typed(Decode_(pool, type));
99 if ((type->flags & JOC_TYPE_CONST) != 0) {
100 if (type->primitive == sig::string_P)
101 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
102 else
103 typed = typed->Modify($ CYTypeConstant());
104 }
105 return typed;
106 }