Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: runtime/lib/mirrors.cc

Issue 10175011: Rework mirrors implementation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/mirrors.h ('k') | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/dart_entry.h" 7 #include "vm/dart_entry.h"
8 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
9 #include "vm/json.h"
9 #include "vm/message.h" 10 #include "vm/message.h"
10 #include "vm/port.h" 11 #include "vm/port.h"
12 #include "vm/resolver.h"
11 13
12 namespace dart { 14 namespace dart {
13 15
14 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 16 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
15 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); 17 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
16 return reinterpret_cast<uint8_t*>(new_ptr); 18 return reinterpret_cast<uint8_t*>(new_ptr);
17 } 19 }
18 20
19 21
20 DEFINE_NATIVE_ENTRY(Mirrors_send, 3) { 22 DEFINE_NATIVE_ENTRY(Mirrors_send, 3) {
(...skipping 27 matching lines...) Expand all
48 writer.FinalizeBuffer(); 50 writer.FinalizeBuffer();
49 51
50 // Post the message. 52 // Post the message.
51 bool retval = PortMap::PostMessage(new Message( 53 bool retval = PortMap::PostMessage(new Message(
52 send_port_id, reply_port_id, data, Message::kOOBPriority)); 54 send_port_id, reply_port_id, data, Message::kOOBPriority));
53 const Bool& retval_obj = Bool::Handle(Bool::Get(retval)); 55 const Bool& retval_obj = Bool::Handle(Bool::Get(retval));
54 arguments->SetReturn(retval_obj); 56 arguments->SetReturn(retval_obj);
55 } 57 }
56 58
57 59
58 DEFINE_NATIVE_ENTRY(IsolateMirrorImpl_buildResponse, 1) { 60 static bool JSONGetString(JSONReader* reader,
59 GET_NATIVE_ARGUMENT(Instance, map, arguments->At(0)); 61 const char** value_chars,
60 String& key = String::Handle(); 62 int* value_len) {
61 Instance& value = Instance::Handle(); 63 if (reader->Type() != JSONReader::kString) {
62 Object& result = Object::Handle(); 64 return false;
65 }
66 *value_chars = reader->ValueChars();
67 *value_len = reader->ValueLen();
68 return true;
69 }
63 70
64 key = String::New("debugName"); 71
65 value = String::New(isolate->name()); 72 DEFINE_NATIVE_ENTRY(Mirrors_processResponse, 3) {
66 result = DartLibraryCalls::MapSetAt(map, key, value); 73 GET_NATIVE_ARGUMENT(Instance, port, arguments->At(0));
67 if (result.IsError()) { 74 GET_NATIVE_ARGUMENT(String, command, arguments->At(1));
68 // TODO(turnidge): Prevent mirror operations from crashing other isolates? 75 GET_NATIVE_ARGUMENT(String, response, arguments->At(2));
69 Exceptions::PropagateError(result); 76
77 const char* json_text = response.ToCString();
78 if (command.Equals("isolateMirrorOf")) {
79 JSONReader reader(json_text);
80 const char* debug_name = "";
81 int debug_name_len = 0;
82 if (!reader.Seek("ok") || !reader.IsTrue() ||
83 !reader.Seek("debugName") ||
84 !JSONGetString(&reader, &debug_name, &debug_name_len)) {
85 // TODO(turnidge): Use an exception class instead of a String.
86 Exceptions::Throw(Instance::Handle(String::NewFormatted(
87 "Error while processing mirror request.")));
88 UNREACHABLE();
89 }
90
91 // Create and return a new instance of _IsolateMirrorImpl.
92 Library& lib = Library::Handle(Library::MirrorsLibrary());
93 const String& public_class_name =
94 String::Handle(String::NewSymbol("_IsolateMirrorImpl"));
95 const String& class_name =
96 String::Handle(lib.PrivateName(public_class_name));
97 const String& function_name =
98 String::Handle(String::NewSymbol("_make"));
99 const int kNumArgs = 2;
100 const Array& kNoArgNames = Array::Handle();
101 const Function& function = Function::Handle(
102 Resolver::ResolveStatic(lib,
103 class_name,
104 function_name,
105 kNumArgs,
106 kNoArgNames,
107 Resolver::kIsQualified));
108 ASSERT(!function.IsNull());
109 GrowableArray<const Object*> args(kNumArgs);
110 args.Add(&port);
111 const String& debug_name_str = String::Handle(
112 String::NewFormatted("%.*s", debug_name_len, debug_name));
113 args.Add(&debug_name_str);
114 const Object& result = Object::Handle(
115 DartEntry::InvokeStatic(function, args, kNoArgNames));
116 arguments->SetReturn(result);
117 }
118 }
119
120
121 void HandleMirrorsMessage(Isolate* isolate,
122 Dart_Port reply_port,
123 const Instance& message) {
124 TextBuffer buffer(64);
125 if (!message.IsString()) {
126 buffer.Printf(
127 "{ \"ok\": false, \"error\": \"Malformed mirrors request\" }");
128 } else {
129 String& json_string = String::Handle();
130 json_string ^= message.raw();
131 const char* json_text = json_string.ToCString();
132 JSONReader reader(json_text);
133
134 if (reader.Seek("command")) {
135 if (reader.IsStringLiteral("isolateMirrorOf")) {
136 buffer.Printf("{ \"ok\": true, \"debugName\": \"%s\" }",
137 isolate->name());
138 } else {
139 const char* command = "";
140 int command_len = 0;
141 JSONGetString(&reader, &command, &command_len);
142 buffer.Printf(
143 "{ \"ok\": false, \"error\": \"Command '%.*s' not recognized\" }",
144 command_len, command);
145 }
146 } else {
147 buffer.Printf(
148 "{ \"ok\": false, \"error\": \"Field 'command' not found\" }");
149 }
70 } 150 }
71 151
72 key = String::New("ok"); 152 Dart_CObject reply;
73 value = Bool::True(); 153 reply.type = Dart_CObject::kString;
74 result = DartLibraryCalls::MapSetAt(map, key, value); 154 reply.value.as_string = buffer.buf();
75 if (result.IsError()) { 155 if (!Dart_PostCObject(reply_port, &reply)) {
76 Exceptions::PropagateError(result); 156 OS::PrintErr("Unable to post mirrors reply");
157 return;
77 } 158 }
78 } 159 }
79 160
80 } // namespace dart 161 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.h ('k') | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698