blob: 72bbe1dd1f176845e7490b63e0f80c693b321e4b [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ARGUMENTS_H_
29#define V8_ARGUMENTS_H_
30
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36// Arguments provides access to runtime call parameters.
37//
38// It uses the fact that the instance fields of Arguments
39// (length_, arguments_) are "overlayed" with the parameters
40// (no. of parameters, and the parameter pointer) passed so
41// that inside the C++ function, the parameters passed can
42// be accessed conveniently:
43//
44// Object* Runtime_function(Arguments args) {
45// ... use args[i] here ...
46// }
47
48class Arguments BASE_EMBEDDED {
49 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000050 Arguments(int length, Object** arguments)
51 : length_(length), arguments_(arguments) { }
52
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053 Object*& operator[] (int index) {
mads.s.ager31e71382008-08-13 09:32:07 +000054 ASSERT(0 <= index && index < length_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 return arguments_[-index];
56 }
57
58 template <class S> Handle<S> at(int index) {
59 Object** value = &((*this)[index]);
60 // This cast checks that the object we're accessing does indeed have the
61 // expected type.
62 S::cast(*value);
63 return Handle<S>(reinterpret_cast<S**>(value));
64 }
65
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000066 int smi_at(int index) {
67 return Smi::cast((*this)[index])->value();
68 }
69
70 double number_at(int index) {
71 return (*this)[index]->Number();
72 }
73
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074 // Get the total number of arguments including the receiver.
mads.s.ager31e71382008-08-13 09:32:07 +000075 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000077 Object** arguments() { return arguments_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 private:
79 int length_;
80 Object** arguments_;
81};
82
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000083
ager@chromium.orgb26c50a2010-03-26 09:27:16 +000084// Custom arguments replicate a small segment of stack that can be
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000085// accessed through an Arguments object the same way the actual stack
86// can.
87class CustomArguments : public Relocatable {
88 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 inline CustomArguments(Isolate* isolate,
90 Object* data,
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +000091 Object* self,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 JSObject* holder) : Relocatable(isolate) {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +000093 values_[2] = self;
94 values_[1] = holder;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000095 values_[0] = data;
96 }
fschneider@chromium.orge03fb642010-11-01 12:34:09 +000097
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000098 inline explicit CustomArguments(Isolate* isolate) : Relocatable(isolate) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +000099#ifdef DEBUG
100 for (size_t i = 0; i < ARRAY_SIZE(values_); i++) {
101 values_[i] = reinterpret_cast<Object*>(kZapValue);
102 }
103#endif
104 }
105
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000106 void IterateInstance(ObjectVisitor* v);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000107 Object** end() { return values_ + ARRAY_SIZE(values_) - 1; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000108 private:
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000109 Object* values_[3];
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000110};
111
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000112
113#define DECLARE_RUNTIME_FUNCTION(Type, Name) \
114Type Name(Arguments args, Isolate* isolate)
115
116
117#define RUNTIME_FUNCTION(Type, Name) \
118Type Name(Arguments args, Isolate* isolate)
119
120
121#define RUNTIME_ARGUMENTS(isolate, args) args, isolate
122
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124} } // namespace v8::internal
125
126#endif // V8_ARGUMENTS_H_