Chromium Code Reviews| Index: runtime/vm/dart_api_message.h |
| diff --git a/runtime/vm/dart_api_message.h b/runtime/vm/dart_api_message.h |
| index 16a138bc44b80786a6bc730a9091adfab0a6faff..64edc8e737a49be57f929d13a1d7b2d735106d47 100644 |
| --- a/runtime/vm/dart_api_message.h |
| +++ b/runtime/vm/dart_api_message.h |
| @@ -6,7 +6,10 @@ |
| #define VM_DART_API_MESSAGE_H_ |
| #include "include/dart_native_api.h" |
| +#include "platform/utils.h" |
| +#include "vm/allocation.h" |
| #include "vm/dart_api_state.h" |
| +#include "vm/raw_object.h" |
| #include "vm/snapshot.h" |
| namespace dart { |
| @@ -201,6 +204,49 @@ class ApiMessageWriter : public BaseWriter { |
| DISALLOW_COPY_AND_ASSIGN(ApiMessageWriter); |
| }; |
| + |
| +// This class handles translation of certain RawObjects to CObjects for |
| +// NativeMessageHandlers. |
| +// |
| +// TODO(zra): Expand to support not only null, but also other VM heap objects |
| +// as well. |
| +class ApiObjectConverter : public AllStatic { |
| + public: |
| + static bool CanConvert(RawObject* raw_obj) { |
| + return !raw_obj->IsHeapObject() || (raw_obj == Object::null()); |
| + } |
| + |
| + static bool Convert(RawObject* raw_obj, Dart_CObject* c_obj) { |
| + if (!raw_obj->IsHeapObject()) { |
| + ConvertSmi(raw_obj, c_obj); |
| + } else if (raw_obj == Object::null()) { |
| + ConvertNull(raw_obj, c_obj); |
| + } else { |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + private: |
| + static void ConvertSmi(RawObject* raw_obj, Dart_CObject* c_obj) { |
|
Ivan Posva
2015/12/11 21:02:38
ConvertSmi(RawSmi* raw_smi, ...)
zra
2015/12/11 22:01:31
Done.
|
| + ASSERT(!raw_obj->IsHeapObject()); |
| + intptr_t value = reinterpret_cast<intptr_t>(raw_obj) >> kSmiTagShift; |
|
Ivan Posva
2015/12/11 21:02:38
Smi::Value(raw_smi)
zra
2015/12/11 22:01:31
Done.
|
| + if (Utils::IsInt(32, value)) { |
| + c_obj->type = Dart_CObject_kInt32; |
| + c_obj->value.as_int32 = static_cast<int32_t>(value); |
| + } else { |
| + c_obj->type = Dart_CObject_kInt64; |
| + c_obj->value.as_int64 = static_cast<int64_t>(value); |
| + } |
| + } |
| + |
| + static void ConvertNull(RawObject* raw_obj, Dart_CObject* c_obj) { |
|
Ivan Posva
2015/12/11 21:02:38
Drop the raw_obj parameter.
zra
2015/12/11 22:01:31
Done.
|
| + ASSERT(raw_obj == Object::null()); |
| + c_obj->type = Dart_CObject_kNull; |
| + c_obj->value.as_int64 = 0; |
| + } |
| +}; |
| + |
| } // namespace dart |
| #endif // VM_DART_API_MESSAGE_H_ |