runtime-utils.h 6.33 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_RUNTIME_RUNTIME_UTILS_H_
#define V8_RUNTIME_RUNTIME_UTILS_H_
7

8
#include "src/base/logging.h"
9
#include "src/common/globals.h"
10
#include "src/objects/objects.h"
11
#include "src/runtime/runtime.h"
12 13 14 15 16 17

namespace v8 {
namespace internal {

// Cast the given object to a value of the specified type and store
// it in a variable with the given name.  If the object is not of the
18
// expected type we crash safely.
19
#define CONVERT_ARG_CHECKED(Type, name, index) \
20
  CHECK(args[index].Is##Type());               \
21
  Type name = Type::cast(args[index]);
22 23

#define CONVERT_ARG_HANDLE_CHECKED(Type, name, index) \
24
  CHECK(args[index].Is##Type());                      \
25 26 27
  Handle<Type> name = args.at<Type>(index);

#define CONVERT_NUMBER_ARG_HANDLE_CHECKED(name, index) \
28
  CHECK(args[index].IsNumber());                       \
29
  Handle<Object> name = args.at(index);
30 31

// Cast the given object to a boolean and store it in a variable with
32
// the given name.  If the object is not a boolean we crash safely.
33
#define CONVERT_BOOLEAN_ARG_CHECKED(name, index) \
34 35
  CHECK(args[index].IsBoolean());                \
  bool name = args[index].IsTrue(isolate);
36 37

// Cast the given argument to a Smi and store its value in an int variable
38
// with the given name.  If the argument is not a Smi we crash safely.
39 40 41 42 43 44 45 46 47 48 49 50 51
#define CONVERT_SMI_ARG_CHECKED(name, index)       \
  CHECK(args[index].IsSmi());                      \
  int name = args.smi_at(index);                   \
  /* Ensure we have a Smi and not a TaggedIndex */ \
  DCHECK_IMPLIES(args[index].IsTaggedIndex(),      \
                 name == TaggedIndex(args[index].ptr()).value());

// Cast the given argument to a TaggedIndex and store its value in an int
// variable with the given name.  If the argument is not a TaggedIndex we crash
// safely.
#define CONVERT_TAGGED_INDEX_ARG_CHECKED(name, index) \
  CHECK(args[index].IsTaggedIndex());                 \
  int name = args.tagged_index_at(index);
52 53 54

// Cast the given argument to a double and store it in a variable with
// the given name.  If the argument is not a number (as opposed to
55
// the number not-a-number) we crash safely.
56
#define CONVERT_DOUBLE_ARG_CHECKED(name, index) \
57
  CHECK(args[index].IsNumber());                \
58 59
  double name = args.number_at(index);

binji's avatar
binji committed
60
// Cast the given argument to a size_t and store its value in a variable with
61
// the given name.  If the argument is not a size_t we crash safely.
62
#define CONVERT_SIZE_ARG_CHECKED(name, index)    \
63
  CHECK(args[index].IsNumber());                 \
64 65
  Handle<Object> name##_object = args.at(index); \
  size_t name = 0;                               \
66
  CHECK(TryNumberToSize(*name##_object, &name));
binji's avatar
binji committed
67

68 69
// Call the specified converter on the object *comand store the result in
// a variable of the specified type with the given name.  If the
70
// object is not a Number we crash safely.
71
#define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \
72
  CHECK(obj.IsNumber());                              \
73 74 75
  type name = NumberTo##Type(obj);

// Cast the given argument to PropertyDetails and store its value in a
76
// variable with the given name.  If the argument is not a Smi we crash safely.
77
#define CONVERT_PROPERTY_DETAILS_CHECKED(name, index) \
78
  CHECK(args[index]->IsSmi());                        \
79 80
  PropertyDetails name = PropertyDetails(Smi::cast(args[index]));

81 82
// Assert that the given argument has a valid value for a LanguageMode
// and store it in a LanguageMode variable with the given name.
83
#define CONVERT_LANGUAGE_MODE_ARG_CHECKED(name, index) \
84 85 86 87 88
  CHECK(args[index]->IsNumber());                      \
  int32_t __tmp_##name = 0;                            \
  CHECK(args[index]->ToInt32(&__tmp_##name));          \
  CHECK(is_valid_language_mode(__tmp_##name));         \
  LanguageMode name = static_cast<LanguageMode>(__tmp_##name);
89 90

// Assert that the given argument is a number within the Int32 range
91
// and convert it to int32_t.  If the argument is not an Int32 we crash safely.
92
#define CONVERT_INT32_ARG_CHECKED(name, index) \
93
  CHECK(args[index].IsNumber());               \
94
  int32_t name = 0;                            \
95
  CHECK(args[index].ToInt32(&name));
96

97 98 99 100
// Assert that the given argument is a number within the Uint32 range
// and convert it to uint32_t.  If the argument is not an Uint32 call
// IllegalOperation and return.
#define CONVERT_UINT32_ARG_CHECKED(name, index) \
101
  CHECK(args[index].IsNumber());                \
102
  uint32_t name = 0;                            \
103
  CHECK(args[index].ToUint32(&name));
104

105
// Cast the given argument to PropertyAttributes and store its value in a
106 107
// variable with the given name.  If the argument is not a Smi or the
// enum value is out of range, we crash safely.
108
#define CONVERT_PROPERTY_ATTRIBUTES_CHECKED(name, index)                    \
109
  CHECK(args[index].IsSmi());                                               \
110
  CHECK_EQ(args.smi_at(index) & ~(READ_ONLY | DONT_ENUM | DONT_DELETE), 0); \
111 112
  PropertyAttributes name = static_cast<PropertyAttributes>(args.smi_at(index));

113 114 115 116 117 118 119 120 121 122
// A mechanism to return a pair of Object pointers in registers (if possible).
// How this is achieved is calling convention-dependent.
// All currently supported x86 compiles uses calling conventions that are cdecl
// variants where a 64-bit value is returned in two 32-bit registers
// (edx:eax on ia32, r1:r0 on ARM).
// In AMD-64 calling convention a struct of two pointers is returned in rdx:rax.
// In Win64 calling convention, a struct of two pointers is returned in memory,
// allocated by the caller, and passed as a pointer in a hidden first parameter.
#ifdef V8_HOST_ARCH_64_BIT
struct ObjectPair {
123 124
  Address x;
  Address y;
125 126
};

127
static inline ObjectPair MakePair(Object x, Object y) {
128
  ObjectPair result = {x.ptr(), y.ptr()};
129 130 131 132 133
  // Pointers x and y returned in rax and rdx, in AMD-x64-abi.
  // In Win64 they are assigned to a hidden first argument.
  return result;
}
#else
134
using ObjectPair = uint64_t;
135
static inline ObjectPair MakePair(Object x, Object y) {
136
#if defined(V8_TARGET_LITTLE_ENDIAN)
137
  return x.ptr() | (static_cast<ObjectPair>(y.ptr()) << 32);
138
#elif defined(V8_TARGET_BIG_ENDIAN)
139
  return y->ptr() | (static_cast<ObjectPair>(x->ptr()) << 32);
140 141 142 143 144
#else
#error Unknown endianness
#endif
}
#endif
145

146 147
}  // namespace internal
}  // namespace v8
148

149
#endif  // V8_RUNTIME_RUNTIME_UTILS_H_