]> git.saurik.com Git - cycript.git/blob - sig/copy.cpp
new operator must return JSObject even for errors.
[cycript.git] / sig / copy.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25
26 #include "Pooling.hpp"
27 #include "sig/parse.hpp"
28
29 namespace sig {
30
31 void Copy(CYPool &pool, Element &lhs, const Element &rhs) {
32 lhs.name = pool.strdup(rhs.name);
33 _assert(rhs.type != NULL);
34 lhs.type = rhs.type->Copy(pool);
35 lhs.offset = rhs.offset;
36 }
37
38 void Copy(CYPool &pool, Signature &lhs, const Signature &rhs) {
39 size_t count(rhs.count);
40 lhs.count = count;
41 if (count == _not(size_t))
42 lhs.elements = NULL;
43 else {
44 lhs.elements = new(pool) Element[count];
45 for (size_t index(0); index != count; ++index)
46 Copy(pool, lhs.elements[index], rhs.elements[index]);
47 }
48 }
49
50 Void *Void::Copy(CYPool &pool, const char *rename) const {
51 return new(pool) Void();
52 }
53
54 Unknown *Unknown::Copy(CYPool &pool, const char *rename) const {
55 return new(pool) Unknown();
56 }
57
58 String *String::Copy(CYPool &pool, const char *rename) const {
59 return new(pool) String();
60 }
61
62 #ifdef CY_OBJECTIVEC
63 Meta *Meta::Copy(CYPool &pool, const char *rename) const {
64 return new(pool) Meta();
65 }
66
67 Selector *Selector::Copy(CYPool &pool, const char *rename) const {
68 return new(pool) Selector();
69 }
70 #endif
71
72 Bits *Bits::Copy(CYPool &pool, const char *rename) const {
73 return new(pool) Bits(size);
74 }
75
76 Pointer *Pointer::Copy(CYPool &pool, const char *rename) const {
77 return new(pool) Pointer(*type.Copy(pool));
78 }
79
80 Array *Array::Copy(CYPool &pool, const char *rename) const {
81 return new(pool) Array(*type.Copy(pool), size);
82 }
83
84 #ifdef CY_OBJECTIVEC
85 Object *Object::Copy(CYPool &pool, const char *rename) const {
86 return new(pool) Object(pool.strdup(name));
87 }
88 #endif
89
90 Enum *Enum::Copy(CYPool &pool, const char *rename) const {
91 if (rename == NULL)
92 rename = pool.strdup(name);
93 else if (rename[0] == '\0')
94 rename = NULL;
95 Enum *copy(new(pool) Enum(*type.Copy(pool), count, rename));
96 copy->constants = new(pool) Constant[count];
97 for (size_t i(0); i != count; ++i) {
98 copy->constants[i].name = pool.strdup(constants[i].name);
99 copy->constants[i].value = constants[i].value;
100 }
101 return copy;
102 }
103
104 Aggregate *Aggregate::Copy(CYPool &pool, const char *rename) const {
105 if (rename == NULL)
106 rename = pool.strdup(name);
107 else if (rename[0] == '\0')
108 rename = NULL;
109 Aggregate *copy(new(pool) Aggregate(overlap, rename));
110 sig::Copy(pool, copy->signature, signature);
111 return copy;
112 }
113
114 Function *Function::Copy(CYPool &pool, const char *rename) const {
115 Function *copy(new(pool) Function(variadic));
116 sig::Copy(pool, copy->signature, signature);
117 return copy;
118 }
119
120 #ifdef CY_OBJECTIVEC
121 Block *Block::Copy(CYPool &pool, const char *rename) const {
122 Block *copy(new(pool) Block());
123 sig::Copy(pool, copy->signature, signature);
124 return copy;
125 }
126 #endif
127
128 void Copy(CYPool &pool, ffi_type &lhs, ffi_type &rhs) {
129 lhs.size = rhs.size;
130 lhs.alignment = rhs.alignment;
131 lhs.type = rhs.type;
132 if (rhs.elements == NULL)
133 lhs.elements = NULL;
134 else {
135 size_t count(0);
136 while (rhs.elements[count] != NULL)
137 ++count;
138
139 lhs.elements = new(pool) ffi_type *[count + 1];
140 lhs.elements[count] = NULL;
141
142 for (size_t index(0); index != count; ++index) {
143 // XXX: if these are libffi native then you can just take them
144 ffi_type *ffi(new(pool) ffi_type);
145 lhs.elements[index] = ffi;
146 sig::Copy(pool, *ffi, *rhs.elements[index]);
147 }
148 }
149 }
150
151 const char *Type::GetName() const {
152 return NULL;
153 }
154
155 const char *Enum::GetName() const {
156 return name;
157 }
158
159 const char *Aggregate::GetName() const {
160 return name;
161 }
162
163 }