OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_DARTUTILS_H_ | 5 #ifndef BIN_DARTUTILS_H_ |
6 #define BIN_DARTUTILS_H_ | 6 #define BIN_DARTUTILS_H_ |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 static const char* kIdFieldName; | 97 static const char* kIdFieldName; |
98 | 98 |
99 private: | 99 private: |
100 static const char* GetCanonicalPath(const char* reference_dir, | 100 static const char* GetCanonicalPath(const char* reference_dir, |
101 const char* filename); | 101 const char* filename); |
102 | 102 |
103 DISALLOW_ALLOCATION(); | 103 DISALLOW_ALLOCATION(); |
104 DISALLOW_IMPLICIT_CONSTRUCTORS(DartUtils); | 104 DISALLOW_IMPLICIT_CONSTRUCTORS(DartUtils); |
105 }; | 105 }; |
106 | 106 |
107 | |
108 class CObject { | |
Mads Ager (google)
2012/02/21 08:51:20
As discussed offline I think we should get rid of
Søren Gjesse
2012/02/21 09:07:23
Will do.
| |
109 public: | |
110 explicit CObject(Dart_CObject *cobject) : cobject_(cobject) {} | |
111 Dart_CObject::Type type() { return cobject_->type; } | |
112 | |
113 bool IsNull() { return type() == Dart_CObject::kNull; } | |
114 bool IsBool() { return type() == Dart_CObject::kBool; } | |
115 bool IsInt32() { return type() == Dart_CObject::kInt32; } | |
116 bool IsInt64() { return type() == Dart_CObject::kInt64; } | |
117 bool IsBigint() { return type() == Dart_CObject::kBigint; } | |
118 bool IsDouble() { return type() == Dart_CObject::kDouble; } | |
119 bool IsString() { return type() == Dart_CObject::kString; } | |
120 bool IsArray() { return type() == Dart_CObject::kArray; } | |
121 bool IsByteArray() { return type() == Dart_CObject::kByteArray; } | |
122 | |
123 bool IsTrue() { | |
124 return type() == Dart_CObject::kBool && cobject_->value.as_bool; | |
125 } | |
126 | |
127 bool IsFalse() { | |
128 return type() == Dart_CObject::kBool && !cobject_->value.as_bool; | |
129 } | |
130 | |
131 void* operator new(size_t size) { | |
132 return Dart_ScopeAllocate(size); | |
133 } | |
134 | |
135 static CObject* Null(); | |
136 static CObject* True(); | |
137 static CObject* False(); | |
138 static CObject* Bool(bool value); | |
139 static Dart_CObject* NewInt32(int32_t value); | |
140 static Dart_CObject* NewInt64(int64_t value); | |
141 // TODO(sgjesse): Add support for kBigint. | |
142 static Dart_CObject* NewDouble(double value); | |
143 static Dart_CObject* NewString(int length); | |
144 static Dart_CObject* NewString(const char* str); | |
145 static Dart_CObject* NewArray(int length); | |
146 // TODO(sgjesse): Add support for kByteArray. | |
147 | |
148 Dart_CObject* AsApiCObject() { return cobject_; } | |
149 | |
150 protected: | |
151 CObject() : cobject_(NULL) {} | |
152 Dart_CObject* cobject_; | |
153 | |
154 private: | |
155 static Dart_CObject* New(Dart_CObject::Type type, int additional_bytes = 0); | |
156 | |
157 static Dart_CObject api_null_; | |
158 static Dart_CObject api_true_; | |
159 static Dart_CObject api_false_; | |
160 static CObject null_; | |
161 static CObject true_; | |
162 static CObject false_; | |
163 }; | |
164 | |
165 | |
166 #define DECLARE_COBJECT_CONSTRUCTORS(t) \ | |
167 explicit CObject##t(Dart_CObject *cobject) : CObject(cobject) { \ | |
168 ASSERT(type() == Dart_CObject::k##t); \ | |
169 cobject_ = cobject; \ | |
170 } \ | |
171 explicit CObject##t(CObject* cobject) : CObject() { \ | |
172 ASSERT(cobject != NULL); \ | |
173 ASSERT(cobject->type() == Dart_CObject::k##t); \ | |
174 cobject_ = cobject->AsApiCObject(); \ | |
175 } | |
176 | |
177 | |
178 class CObjectBool : public CObject { | |
179 public: | |
180 DECLARE_COBJECT_CONSTRUCTORS(Bool) | |
181 | |
182 bool Value() const { return cobject_->value.as_bool; } | |
183 }; | |
184 | |
185 | |
186 class CObjectInt32 : public CObject { | |
187 public: | |
188 DECLARE_COBJECT_CONSTRUCTORS(Int32) | |
189 | |
190 int32_t Value() const { return cobject_->value.as_int32; } | |
191 }; | |
192 | |
193 | |
194 class CObjectInt64 : public CObject { | |
195 public: | |
196 DECLARE_COBJECT_CONSTRUCTORS(Int64) | |
197 | |
198 int64_t Value() const { return cobject_->value.as_int64; } | |
199 }; | |
200 | |
201 | |
202 class CObjectBigint : public CObject { | |
203 public: | |
204 DECLARE_COBJECT_CONSTRUCTORS(Bigint) | |
205 | |
206 char* Value() const { return cobject_->value.as_bigint; } | |
207 }; | |
208 | |
209 | |
210 class CObjectDouble : public CObject { | |
211 public: | |
212 DECLARE_COBJECT_CONSTRUCTORS(Double) | |
213 | |
214 double Value() const { return cobject_->value.as_double; } | |
215 }; | |
216 | |
217 | |
218 class CObjectString : public CObject { | |
219 public: | |
220 DECLARE_COBJECT_CONSTRUCTORS(String) | |
221 | |
222 int Length() const { return strlen(cobject_->value.as_string); } | |
223 char* CString() const { return cobject_->value.as_string; } | |
224 }; | |
225 | |
226 | |
227 class CObjectArray : public CObject { | |
228 public: | |
229 DECLARE_COBJECT_CONSTRUCTORS(Array) | |
230 | |
231 int Length() const { return cobject_->value.as_array.length; } | |
232 CObject* operator[](int index) const { | |
233 return new CObject(cobject_->value.as_array.values[index]); | |
234 } | |
235 void SetAt(int index, CObject* value) { | |
236 cobject_->value.as_array.values[index] = value->AsApiCObject(); | |
237 } | |
238 }; | |
239 | |
107 #endif // BIN_DARTUTILS_H_ | 240 #endif // BIN_DARTUTILS_H_ |
OLD | NEW |