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

Side by Side Diff: runtime/vm/native_arguments.h

Issue 25062003: - Add special access for receiver as it is a frequently used operation (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 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/vm/dart_api_impl.cc ('k') | no next file » | 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 #ifndef VM_NATIVE_ARGUMENTS_H_ 5 #ifndef VM_NATIVE_ARGUMENTS_H_
6 #define VM_NATIVE_ARGUMENTS_H_ 6 #define VM_NATIVE_ARGUMENTS_H_
7 7
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/globals.h" 9 #include "vm/globals.h"
10 #include "vm/handles_impl.h" 10 #include "vm/handles_impl.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 // The return value is set as follows: 80 // The return value is set as follows:
81 // arguments.SetReturn(result); 81 // arguments.SetReturn(result);
82 // NOTE: Since we pass 'this' as a pass-by-value argument in the stubs we don't 82 // NOTE: Since we pass 'this' as a pass-by-value argument in the stubs we don't
83 // have DISALLOW_COPY_AND_ASSIGN in the class definition and do not make it a 83 // have DISALLOW_COPY_AND_ASSIGN in the class definition and do not make it a
84 // subclass of ValueObject. 84 // subclass of ValueObject.
85 class NativeArguments { 85 class NativeArguments {
86 public: 86 public:
87 Isolate* isolate() const { return isolate_; } 87 Isolate* isolate() const { return isolate_; }
88 int ArgCount() const { return ArgcBits::decode(argc_tag_); } 88 int ArgCount() const { return ArgcBits::decode(argc_tag_); }
89 89
90 // Returns true if the arguments are those of an instance function call.
91 bool ToInstanceFunction() const {
92 return InstanceFunctionBit::decode(argc_tag_);
93 }
94
95 // Returns true if the arguments are those of a closure function call.
96 bool ToClosureFunction() const {
97 return ClosureFunctionBit::decode(argc_tag_);
98 }
99
100 RawObject* ArgAt(int index) const { 90 RawObject* ArgAt(int index) const {
101 ASSERT((index >= 0) && (index < ArgCount())); 91 ASSERT((index >= 0) && (index < ArgCount()));
102 return (*argv_)[-index]; 92 return (*argv_)[-index];
103 } 93 }
104 94
105 int NumHiddenArgs() const { 95 int NativeArgCount() const {
106 // For static closure functions, the closure at index 0 is hidden. 96 int function_bits = FunctionBits::decode(argc_tag_);
107 // In the instance closure function case, the receiver is accessed from 97 return ArgCount() - NumHiddenArgs(function_bits);
108 // the context and the closure at index 0 is hidden, so the apparent
109 // argument count remains unchanged.
110 if (ToClosureFunction() && !ToInstanceFunction()) {
111 return 1;
112 }
113 return 0;
114 } 98 }
115 99
116 int NativeArgCount() const { 100 RawObject* NativeReceiver() const {
117 return ArgCount() - NumHiddenArgs(); 101 int function_bits = FunctionBits::decode(argc_tag_);
102 if (function_bits == (kClosureFunctionBit | kInstanceFunctionBit)) {
103 // Retrieve the receiver from the context.
104 const Context& context = Context::Handle(isolate_->top_context());
105 return context.At(0);
106 }
107 return ArgAt(NumHiddenArgs(function_bits));
118 } 108 }
119 109
120 RawObject* NativeArgAt(int index) const { 110 RawObject* NativeArgAt(int index) const {
121 ASSERT((index >= 0) && (index < NativeArgCount())); 111 ASSERT((index >= 0) && (index < NativeArgCount()));
122 if ((index == 0) && ToClosureFunction() && ToInstanceFunction()) { 112 if (index == 0) {
123 // Retrieve the receiver from the context. 113 return NativeReceiver();
regis 2013/09/27 19:34:39 I would add a comment that calling NativeReceiver(
siva 2013/09/27 21:12:37 Good point. As discussed offline I have added a p
124 const Context& context = Context::Handle(isolate_->top_context());
125 return context.At(0);
126 } else {
127 const int actual_index = index + NumHiddenArgs();
128 return ArgAt(actual_index);
129 } 114 }
115 int function_bits = FunctionBits::decode(argc_tag_);
116 const int actual_index = index + NumHiddenArgs(function_bits);
117 return ArgAt(actual_index);
130 } 118 }
131 119
132 void SetReturn(const Object& value) const { 120 void SetReturn(const Object& value) const {
133 *retval_ = value.raw(); 121 *retval_ = value.raw();
134 } 122 }
135 123
136 static intptr_t isolate_offset() { 124 static intptr_t isolate_offset() {
137 return OFFSET_OF(NativeArguments, isolate_); 125 return OFFSET_OF(NativeArguments, isolate_);
138 } 126 }
139 static intptr_t argc_tag_offset() { 127 static intptr_t argc_tag_offset() {
(...skipping 15 matching lines...) Expand all
155 // index 0, thereby hidding the closure object at index 0. 143 // index 0, thereby hidding the closure object at index 0.
156 count--; 144 count--;
157 } 145 }
158 return count; 146 return count;
159 } 147 }
160 148
161 static int ComputeArgcTag(const Function& function) { 149 static int ComputeArgcTag(const Function& function) {
162 ASSERT(function.is_native()); 150 ASSERT(function.is_native());
163 ASSERT(!function.IsConstructor()); // Not supported. 151 ASSERT(!function.IsConstructor()); // Not supported.
164 int tag = ArgcBits::encode(function.NumParameters()); 152 int tag = ArgcBits::encode(function.NumParameters());
165 tag = InstanceFunctionBit::update(!function.is_static(), tag); 153 int function_bits = 0;
166 tag = ClosureFunctionBit::update(function.IsClosureFunction(), tag); 154 if (!function.is_static()) {
167 return tag; 155 function_bits |= kInstanceFunctionBit;
156 }
157 if (function.IsClosureFunction()) {
158 function_bits |= kClosureFunctionBit;
159 }
160 return FunctionBits::update(function_bits, tag);
168 } 161 }
169 162
170 private: 163 private:
164 enum {
165 kInstanceFunctionBit = 1,
166 kClosureFunctionBit = 2,
167 };
171 enum ArgcTagBits { 168 enum ArgcTagBits {
172 kArgcBit = 0, 169 kArgcBit = 0,
173 kArgcSize = 24, 170 kArgcSize = 24,
174 kInstanceFunctionBit = 24, 171 kFunctionBit = 24,
175 kClosureFunctionBit = 25, 172 kFunctionSize = 2,
176 }; 173 };
177 class ArgcBits : public BitField<int, kArgcBit, kArgcSize> {}; 174 class ArgcBits : public BitField<int, kArgcBit, kArgcSize> {};
178 class InstanceFunctionBit : public BitField<bool, kInstanceFunctionBit, 1> {}; 175 class FunctionBits : public BitField<int, kFunctionBit, kFunctionSize> {};
179 class ClosureFunctionBit : public BitField<bool, kClosureFunctionBit, 1> {};
180 friend class Api; 176 friend class Api;
181 friend class BootstrapNatives; 177 friend class BootstrapNatives;
182 friend class Simulator; 178 friend class Simulator;
183 179
184 // Since this function is passed a RawObject directly, we need to be 180 // Since this function is passed a RawObject directly, we need to be
185 // exceedingly careful when we use it. If there are any other side 181 // exceedingly careful when we use it. If there are any other side
186 // effects in the statement that may cause GC, it could lead to 182 // effects in the statement that may cause GC, it could lead to
187 // bugs. 183 // bugs.
188 void SetReturnUnsafe(RawObject* value) const { 184 void SetReturnUnsafe(RawObject* value) const {
189 *retval_ = value; 185 *retval_ = value;
190 } 186 }
191 187
188 // Returns true if the arguments are those of an instance function call.
189 bool ToInstanceFunction() const {
190 return (FunctionBits::decode(argc_tag_) & kInstanceFunctionBit);
191 }
192
193 // Returns true if the arguments are those of a closure function call.
194 bool ToClosureFunction() const {
195 return (FunctionBits::decode(argc_tag_) & kClosureFunctionBit);
196 }
197
198 int NumHiddenArgs(int function_bits) const {
199 // For static closure functions, the closure at index 0 is hidden.
200 // In the instance closure function case, the receiver is accessed from
201 // the context and the closure at index 0 is hidden, so the apparent
202 // argument count remains unchanged.
203 if (function_bits == kClosureFunctionBit) {
204 return 1;
205 }
206 return 0;
207 }
208
192 Isolate* isolate_; // Current isolate pointer. 209 Isolate* isolate_; // Current isolate pointer.
193 int argc_tag_; // Encodes argument count and invoked native call type. 210 int argc_tag_; // Encodes argument count and invoked native call type.
194 RawObject*(*argv_)[]; // Pointer to an array of arguments to runtime call. 211 RawObject*(*argv_)[]; // Pointer to an array of arguments to runtime call.
195 RawObject** retval_; // Pointer to the return value area. 212 RawObject** retval_; // Pointer to the return value area.
196 }; 213 };
197 214
198 } // namespace dart 215 } // namespace dart
199 216
200 #endif // VM_NATIVE_ARGUMENTS_H_ 217 #endif // VM_NATIVE_ARGUMENTS_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698