| OLD | NEW |
| 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 Loading... |
| 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 ASSERT(ToInstanceFunction()); |
| 102 return NativeArg0(); |
| 118 } | 103 } |
| 119 | 104 |
| 120 RawObject* NativeArgAt(int index) const { | 105 RawObject* NativeArgAt(int index) const { |
| 121 ASSERT((index >= 0) && (index < NativeArgCount())); | 106 ASSERT((index >= 0) && (index < NativeArgCount())); |
| 122 if ((index == 0) && ToClosureFunction() && ToInstanceFunction()) { | 107 if (index == 0) { |
| 123 // Retrieve the receiver from the context. | 108 return NativeArg0(); |
| 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 } | 109 } |
| 110 int function_bits = FunctionBits::decode(argc_tag_); |
| 111 const int actual_index = index + NumHiddenArgs(function_bits); |
| 112 return ArgAt(actual_index); |
| 130 } | 113 } |
| 131 | 114 |
| 132 void SetReturn(const Object& value) const { | 115 void SetReturn(const Object& value) const { |
| 133 *retval_ = value.raw(); | 116 *retval_ = value.raw(); |
| 134 } | 117 } |
| 135 | 118 |
| 136 static intptr_t isolate_offset() { | 119 static intptr_t isolate_offset() { |
| 137 return OFFSET_OF(NativeArguments, isolate_); | 120 return OFFSET_OF(NativeArguments, isolate_); |
| 138 } | 121 } |
| 139 static intptr_t argc_tag_offset() { | 122 static intptr_t argc_tag_offset() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 155 // index 0, thereby hidding the closure object at index 0. | 138 // index 0, thereby hidding the closure object at index 0. |
| 156 count--; | 139 count--; |
| 157 } | 140 } |
| 158 return count; | 141 return count; |
| 159 } | 142 } |
| 160 | 143 |
| 161 static int ComputeArgcTag(const Function& function) { | 144 static int ComputeArgcTag(const Function& function) { |
| 162 ASSERT(function.is_native()); | 145 ASSERT(function.is_native()); |
| 163 ASSERT(!function.IsConstructor()); // Not supported. | 146 ASSERT(!function.IsConstructor()); // Not supported. |
| 164 int tag = ArgcBits::encode(function.NumParameters()); | 147 int tag = ArgcBits::encode(function.NumParameters()); |
| 165 tag = InstanceFunctionBit::update(!function.is_static(), tag); | 148 int function_bits = 0; |
| 166 tag = ClosureFunctionBit::update(function.IsClosureFunction(), tag); | 149 if (!function.is_static()) { |
| 167 return tag; | 150 function_bits |= kInstanceFunctionBit; |
| 151 } |
| 152 if (function.IsClosureFunction()) { |
| 153 function_bits |= kClosureFunctionBit; |
| 154 } |
| 155 return FunctionBits::update(function_bits, tag); |
| 168 } | 156 } |
| 169 | 157 |
| 170 private: | 158 private: |
| 159 enum { |
| 160 kInstanceFunctionBit = 1, |
| 161 kClosureFunctionBit = 2, |
| 162 }; |
| 171 enum ArgcTagBits { | 163 enum ArgcTagBits { |
| 172 kArgcBit = 0, | 164 kArgcBit = 0, |
| 173 kArgcSize = 24, | 165 kArgcSize = 24, |
| 174 kInstanceFunctionBit = 24, | 166 kFunctionBit = 24, |
| 175 kClosureFunctionBit = 25, | 167 kFunctionSize = 2, |
| 176 }; | 168 }; |
| 177 class ArgcBits : public BitField<int, kArgcBit, kArgcSize> {}; | 169 class ArgcBits : public BitField<int, kArgcBit, kArgcSize> {}; |
| 178 class InstanceFunctionBit : public BitField<bool, kInstanceFunctionBit, 1> {}; | 170 class FunctionBits : public BitField<int, kFunctionBit, kFunctionSize> {}; |
| 179 class ClosureFunctionBit : public BitField<bool, kClosureFunctionBit, 1> {}; | |
| 180 friend class Api; | 171 friend class Api; |
| 181 friend class BootstrapNatives; | 172 friend class BootstrapNatives; |
| 182 friend class Simulator; | 173 friend class Simulator; |
| 183 | 174 |
| 184 // Since this function is passed a RawObject directly, we need to be | 175 // 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 | 176 // 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 | 177 // effects in the statement that may cause GC, it could lead to |
| 187 // bugs. | 178 // bugs. |
| 188 void SetReturnUnsafe(RawObject* value) const { | 179 void SetReturnUnsafe(RawObject* value) const { |
| 189 *retval_ = value; | 180 *retval_ = value; |
| 190 } | 181 } |
| 191 | 182 |
| 183 // Returns true if the arguments are those of an instance function call. |
| 184 bool ToInstanceFunction() const { |
| 185 return (FunctionBits::decode(argc_tag_) & kInstanceFunctionBit); |
| 186 } |
| 187 |
| 188 // Returns true if the arguments are those of a closure function call. |
| 189 bool ToClosureFunction() const { |
| 190 return (FunctionBits::decode(argc_tag_) & kClosureFunctionBit); |
| 191 } |
| 192 |
| 193 int NumHiddenArgs(int function_bits) const { |
| 194 // For static closure functions, the closure at index 0 is hidden. |
| 195 // In the instance closure function case, the receiver is accessed from |
| 196 // the context and the closure at index 0 is hidden, so the apparent |
| 197 // argument count remains unchanged. |
| 198 if (function_bits == kClosureFunctionBit) { |
| 199 return 1; |
| 200 } |
| 201 return 0; |
| 202 } |
| 203 |
| 204 RawObject* NativeArg0() const { |
| 205 int function_bits = FunctionBits::decode(argc_tag_); |
| 206 if (function_bits == (kClosureFunctionBit | kInstanceFunctionBit)) { |
| 207 // Retrieve the receiver from the context. |
| 208 const Context& context = Context::Handle(isolate_->top_context()); |
| 209 return context.At(0); |
| 210 } |
| 211 return ArgAt(NumHiddenArgs(function_bits)); |
| 212 } |
| 213 |
| 192 Isolate* isolate_; // Current isolate pointer. | 214 Isolate* isolate_; // Current isolate pointer. |
| 193 int argc_tag_; // Encodes argument count and invoked native call type. | 215 int argc_tag_; // Encodes argument count and invoked native call type. |
| 194 RawObject*(*argv_)[]; // Pointer to an array of arguments to runtime call. | 216 RawObject*(*argv_)[]; // Pointer to an array of arguments to runtime call. |
| 195 RawObject** retval_; // Pointer to the return value area. | 217 RawObject** retval_; // Pointer to the return value area. |
| 196 }; | 218 }; |
| 197 | 219 |
| 198 } // namespace dart | 220 } // namespace dart |
| 199 | 221 |
| 200 #endif // VM_NATIVE_ARGUMENTS_H_ | 222 #endif // VM_NATIVE_ARGUMENTS_H_ |
| OLD | NEW |