Index: runtime/bin/dartutils.cc |
diff --git a/runtime/bin/dartutils.cc b/runtime/bin/dartutils.cc |
index cb0ae2bac099356a22c9d3b2b502fd65866c60cd..b472e7975946cb8c2b8f01a183560ba6053f06e6 100644 |
--- a/runtime/bin/dartutils.cc |
+++ b/runtime/bin/dartutils.cc |
@@ -252,9 +252,7 @@ const char* DartUtils::GetCanonicalPath(const char* reference_dir, |
bool DartUtils::PostNull(Dart_Port port_id) { |
// Post a message with just the null object. |
- Dart_CObject object; |
- object.type = Dart_CObject::kNull; |
- return Dart_PostCObject(port_id, &object); |
+ return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
} |
@@ -268,3 +266,89 @@ bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
object.value.as_int32 = value; |
return Dart_PostCObject(port_id, &object); |
} |
+ |
+ |
+// Statically allocated Dart_CObject instances for immutable |
+// objects. As these will be used by different threads the use of |
+// these depends on the fact that the marking internally in the |
+// Dart_CObject structure is not marking simple value objects. |
+Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; |
+Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; |
+Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; |
+CObject CObject::null_ = CObject(&api_null_); |
+CObject CObject::true_ = CObject(&api_true_); |
+CObject CObject::false_ = CObject(&api_false_); |
+ |
+ |
+CObject* CObject::Null() { |
+ return &null_; |
+} |
+ |
+ |
+CObject* CObject::True() { |
+ return &true_; |
+} |
+ |
+ |
+CObject* CObject::False() { |
+ return &false_; |
+} |
+ |
+ |
+CObject* CObject::Bool(bool value) { |
+ return value ? &true_ : &false_; |
+} |
+ |
+ |
+Dart_CObject* CObject::New(Dart_CObject::Type type, int additional_bytes) { |
+ Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( |
+ Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); |
+ cobject->type = type; |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewInt32(int32_t value) { |
+ Dart_CObject* cobject = New(Dart_CObject::kInt32); |
+ cobject->value.as_int32 = value; |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewInt64(int64_t value) { |
+ Dart_CObject* cobject = New(Dart_CObject::kInt64); |
+ cobject->value.as_int64 = value; |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewDouble(double value) { |
+ Dart_CObject* cobject = New(Dart_CObject::kDouble); |
+ cobject->value.as_double = value; |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewString(int length) { |
+ Dart_CObject* cobject = New(Dart_CObject::kString, length + 1); |
+ cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewString(const char* str) { |
+ int length = strlen(str); |
+ Dart_CObject* cobject = NewString(length); |
+ memmove(cobject->value.as_string, str, length + 1); |
+ return cobject; |
+} |
+ |
+ |
+Dart_CObject* CObject::NewArray(int length) { |
+ Dart_CObject* cobject = |
+ New(Dart_CObject::kArray, length * sizeof(Dart_CObject*)); // NOLINT |
+ cobject->value.as_array.length = length; |
+ cobject->value.as_array.values = |
+ reinterpret_cast<Dart_CObject**>(cobject + 1); |
+ return cobject; |
+} |