Chromium Code Reviews| 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 { | |
| 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 bool IsFalse() { | |
|
Mads Ager (google)
2012/02/20 12:38:38
Blank line to separate these two?
Søren Gjesse
2012/02/21 08:24:25
Done.
| |
| 127 return type() == Dart_CObject::kBool && !cobject_->value.as_bool; | |
| 128 } | |
| 129 | |
| 130 void* operator new(size_t size) { | |
|
Mads Ager (google)
2012/02/20 12:38:38
Could we make the new operator private? It seems t
Søren Gjesse
2012/02/21 08:24:25
So I know it looks a bit confusing, but an instanc
| |
| 131 return Dart_ScopeAllocate(size); | |
| 132 } | |
| 133 | |
| 134 static CObject* Null(); | |
| 135 static CObject* True(); | |
| 136 static CObject* False(); | |
| 137 static CObject* Bool(bool value); | |
| 138 static Dart_CObject* New(Dart_CObject::Type type, int additional_bytes = 0); | |
|
Mads Ager (google)
2012/02/20 12:38:38
Can we make this one private as well? This doesn't
Søren Gjesse
2012/02/21 08:24:25
Done.
| |
| 139 static Dart_CObject* NewInt32(int32_t value); | |
| 140 static Dart_CObject* NewInt64(int64_t value); | |
| 141 // kBigint, | |
|
Mads Ager (google)
2012/02/20 12:38:38
Add a TODO style comment instead?
Søren Gjesse
2012/02/21 08:24:25
Done.
| |
| 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 // kByteArray, | |
|
Mads Ager (google)
2012/02/20 12:38:38
Ditto.
Søren Gjesse
2012/02/21 08:24:25
Done.
| |
| 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 api_null_; | |
| 156 static Dart_CObject api_true_; | |
| 157 static Dart_CObject api_false_; | |
| 158 static CObject null_; | |
| 159 static CObject true_; | |
| 160 static CObject false_; | |
| 161 }; | |
| 162 | |
| 163 | |
| 164 #define DECLARE_COBJECT_CONSTRUCTORS(t) \ | |
| 165 explicit CObject##t(Dart_CObject *cobject) : CObject(cobject) { \ | |
| 166 ASSERT(type() == Dart_CObject::k##t); \ | |
| 167 cobject_ = cobject; \ | |
| 168 } \ | |
| 169 explicit CObject##t(CObject* cobject) : CObject() { \ | |
| 170 ASSERT(cobject != NULL); \ | |
| 171 ASSERT(cobject->type() == Dart_CObject::k##t); \ | |
| 172 cobject_ = cobject->AsApiCObject(); \ | |
| 173 } | |
| 174 | |
| 175 | |
| 176 class CObjectBool : public CObject { | |
| 177 public: | |
| 178 DECLARE_COBJECT_CONSTRUCTORS(Bool) | |
| 179 | |
| 180 bool Value() const { return cobject_->value.as_bool; } | |
| 181 }; | |
| 182 | |
| 183 | |
| 184 class CObjectInt32 : public CObject { | |
| 185 public: | |
| 186 DECLARE_COBJECT_CONSTRUCTORS(Int32) | |
| 187 | |
| 188 int32_t Value() const { return cobject_->value.as_int32; } | |
| 189 }; | |
| 190 | |
| 191 | |
| 192 class CObjectInt64 : public CObject { | |
| 193 public: | |
| 194 DECLARE_COBJECT_CONSTRUCTORS(Int64) | |
| 195 | |
| 196 int64_t Value() const { return cobject_->value.as_int64; } | |
| 197 }; | |
| 198 | |
| 199 | |
| 200 class CObjectBigint : public CObject { | |
| 201 public: | |
| 202 DECLARE_COBJECT_CONSTRUCTORS(Bigint) | |
| 203 | |
| 204 char* Value() const { return cobject_->value.as_bigint; } | |
| 205 }; | |
| 206 | |
| 207 | |
| 208 class CObjectDouble : public CObject { | |
| 209 public: | |
| 210 DECLARE_COBJECT_CONSTRUCTORS(Double) | |
| 211 | |
| 212 double Value() const { return cobject_->value.as_double; } | |
| 213 }; | |
| 214 | |
| 215 | |
| 216 class CObjectString : public CObject { | |
| 217 public: | |
| 218 DECLARE_COBJECT_CONSTRUCTORS(String) | |
| 219 | |
| 220 int Length() const { return strlen(cobject_->value.as_string); } | |
| 221 char* CString() const { return cobject_->value.as_string; } | |
| 222 }; | |
| 223 | |
| 224 | |
| 225 class CObjectArray : public CObject { | |
| 226 public: | |
| 227 DECLARE_COBJECT_CONSTRUCTORS(Array) | |
| 228 | |
| 229 int Length() const { return cobject_->value.as_array.length; } | |
| 230 CObject* operator[](int index) const { | |
| 231 return new CObject(cobject_->value.as_array.values[index]); | |
| 232 } | |
| 233 void SetAt(int index, CObject* value) { | |
| 234 cobject_->value.as_array.values[index] = value->AsApiCObject(); | |
| 235 } | |
| 236 }; | |
| 237 | |
| 107 #endif // BIN_DARTUTILS_H_ | 238 #endif // BIN_DARTUTILS_H_ |
| OLD | NEW |