Fold ApiObjectConverter use into ApiMessageReader

TODO from change on Friday.

R=turnidge@google.com

Review URL: https://codereview.chromium.org/1523013002 .
This commit is contained in:
Zachary Anderson
2015-12-14 11:12:25 -08:00
parent d8494d2f50
commit fa8a0613a3
3 changed files with 39 additions and 27 deletions
+29 -7
View File
@@ -18,15 +18,26 @@ ApiMessageReader::ApiMessageReader(const uint8_t* buffer, intptr_t length)
backward_references_(kNumInitialReferences),
vm_isolate_references_(kNumInitialReferences),
vm_symbol_references_(NULL) {
// We need to have an enclosing ApiNativeScope.
ASSERT(ApiNativeScope::Current() != NULL);
zone_ = ApiNativeScope::Current()->zone();
ASSERT(zone_ != NULL);
Init();
}
ApiMessageReader::ApiMessageReader(Message* msg)
: BaseReader(msg->IsRaw() ? reinterpret_cast<uint8_t*>(msg->raw_obj())
: msg->data(),
msg->len()),
zone_(NULL),
backward_references_(kNumInitialReferences),
vm_isolate_references_(kNumInitialReferences),
vm_symbol_references_(NULL) {
}
void ApiMessageReader::Init() {
// We need to have an enclosing ApiNativeScope.
ASSERT(ApiNativeScope::Current() != NULL);
zone_ = ApiNativeScope::Current()->zone();
ASSERT(zone_ != NULL);
// Initialize marker objects used to handle Lists.
// TODO(sjesse): Remove this when message serialization format is
// updated.
@@ -40,8 +51,19 @@ void ApiMessageReader::Init() {
Dart_CObject* ApiMessageReader::ReadMessage() {
// Read the object out of the message.
return ReadObject();
Init();
if (PendingBytes() > 0) {
// Read the object out of the message.
return ReadObject();
} else {
const RawObject* raw_obj =
reinterpret_cast<const RawObject*>(CurrentBufferAddress());
ASSERT(ApiObjectConverter::CanConvert(raw_obj));
Dart_CObject* cobj =
reinterpret_cast<Dart_CObject*>(allocator(sizeof(Dart_CObject)));
ApiObjectConverter::Convert(raw_obj, cobj);
return cobj;
}
}
+6 -4
View File
@@ -9,6 +9,7 @@
#include "platform/utils.h"
#include "vm/allocation.h"
#include "vm/dart_api_state.h"
#include "vm/message.h"
#include "vm/raw_object.h"
#include "vm/snapshot.h"
@@ -48,6 +49,7 @@ class ApiMessageReader : public BaseReader {
// Allocation of all C Heap objects is done in the zone associated with
// the enclosing ApiNativeScope.
ApiMessageReader(const uint8_t* buffer, intptr_t length);
explicit ApiMessageReader(Message* message);
~ApiMessageReader() { }
Dart_CObject* ReadMessage();
@@ -212,13 +214,13 @@ class ApiMessageWriter : public BaseWriter {
// as well.
class ApiObjectConverter : public AllStatic {
public:
static bool CanConvert(RawObject* raw_obj) {
static bool CanConvert(const RawObject* raw_obj) {
return !raw_obj->IsHeapObject() || (raw_obj == Object::null());
}
static bool Convert(RawObject* raw_obj, Dart_CObject* c_obj) {
static bool Convert(const RawObject* raw_obj, Dart_CObject* c_obj) {
if (!raw_obj->IsHeapObject()) {
ConvertSmi(reinterpret_cast<RawSmi*>(raw_obj), c_obj);
ConvertSmi(reinterpret_cast<const RawSmi*>(raw_obj), c_obj);
} else if (raw_obj == Object::null()) {
ConvertNull(c_obj);
} else {
@@ -228,7 +230,7 @@ class ApiObjectConverter : public AllStatic {
}
private:
static void ConvertSmi(RawSmi* raw_smi, Dart_CObject* c_obj) {
static void ConvertSmi(const RawSmi* raw_smi, Dart_CObject* c_obj) {
ASSERT(!raw_smi->IsHeapObject());
intptr_t value = Smi::Value(raw_smi);
if (Utils::IsInt(31, value)) {
+4 -16
View File
@@ -42,22 +42,10 @@ MessageHandler::MessageStatus NativeMessageHandler::HandleMessage(
// All allocation of objects for decoding the message is done in the
// zone associated with this scope.
ApiNativeScope scope;
if (message->IsRaw()) {
// TODO(zra): This should be folded into ApiMessageReader. This will likely
// require ApiMessageReader to have a constructor that takes a Message*.
ASSERT(ApiObjectConverter::CanConvert(message->raw_obj()));
Dart_CObject object;
bool success = ApiObjectConverter::Convert(message->raw_obj(), &object);
ASSERT(success);
(*func())(message->dest_port(), &object);
} else {
Dart_CObject* object;
ApiMessageReader reader(message->data(), message->len());
object = reader.ReadMessage();
(*func())(message->dest_port(), object);
}
Dart_CObject* object;
ApiMessageReader reader(message);
object = reader.ReadMessage();
(*func())(message->dest_port(), object);
delete message;
return kOK;
}