]> git.saurik.com Git - cycript.git/blob - Decode.cpp
Implement CommonJS-compliant require() function.
[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 case sig::block_P: _assert(false); break;
42
43 case sig::object_P: {
44 if (type->name == NULL)
45 return $ CYTypedIdentifier($V("id"));
46 else
47 return $ CYTypedIdentifier($V(type->name), $ CYTypePointerTo());
48 } break;
49
50 case sig::boolean_P: return $ CYTypedIdentifier($V("bool"));
51 case sig::uchar_P: return $ CYTypedIdentifier($V("uchar"));
52 case sig::uint_P: return $ CYTypedIdentifier($V("uint"));
53 case sig::ulong_P: return $ CYTypedIdentifier($V("ulong"));
54 case sig::ulonglong_P: return $ CYTypedIdentifier($V("ulonglong"));
55 case sig::ushort_P: return $ CYTypedIdentifier($V("ushort"));
56 case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
57
58 // XXX: once again, the issue of void here is incorrect
59 case sig::pointer_P: {
60 CYTypedIdentifier *typed;
61 if (type->data.data.type == NULL)
62 typed = $ CYTypedIdentifier($V("void"));
63 else
64 typed = Decode(pool, type->data.data.type);
65 return typed->Modify($ CYTypePointerTo());
66 } break;
67
68 case sig::bit_P: _assert(false); break;
69 case sig::char_P: return $ CYTypedIdentifier($V("char"));
70 case sig::double_P: return $ CYTypedIdentifier($V("double"));
71 case sig::float_P: return $ CYTypedIdentifier($V("float"));
72 case sig::int_P: return $ CYTypedIdentifier($V("int"));
73 case sig::long_P: return $ CYTypedIdentifier($V("long"));
74 case sig::longlong_P: return $ CYTypedIdentifier($V("longlong"));
75 case sig::short_P: return $ CYTypedIdentifier($V("short"));
76
77 // XXX: this happens to work, but is totally wrong
78 case sig::void_P: return $ CYTypedIdentifier($V("void"));
79
80 case sig::struct_P: {
81 _assert(type->name != NULL);
82 return $ CYTypedIdentifier($V(type->name));
83 } break;
84 }
85
86 _assert(false);
87 return NULL;
88 }
89
90 CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
91 CYTypedIdentifier *typed(Decode_(pool, type));
92 if ((type->flags & JOC_TYPE_CONST) != 0) {
93 if (type->primitive == sig::string_P)
94 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
95 else
96 typed = typed->Modify($ CYTypeConstant());
97 }
98 return typed;
99 }