runtime-bigint.cc 5.84 KB
Newer Older
1 2 3 4
// Copyright 2017 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
#include "src/arguments-inl.h"
6 7
#include "src/counters.h"
#include "src/objects-inl.h"
8
#include "src/objects/bigint.h"
9
#include "src/runtime/runtime-utils.h"
10 11 12 13

namespace v8 {
namespace internal {

14 15 16 17 18 19
RUNTIME_FUNCTION(Runtime_BigIntCompareToBigInt) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Smi, mode, 0);
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 1);
  CONVERT_ARG_HANDLE_CHECKED(BigInt, rhs, 2);
20 21
  bool result = ComparisonResultToBool(static_cast<Operation>(mode->value()),
                                       BigInt::CompareToBigInt(lhs, rhs));
22 23 24 25 26 27 28 29 30
  return *isolate->factory()->ToBoolean(result);
}

RUNTIME_FUNCTION(Runtime_BigIntCompareToNumber) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Smi, mode, 0);
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, rhs, 2);
31 32
  bool result = ComparisonResultToBool(static_cast<Operation>(mode->value()),
                                       BigInt::CompareToNumber(lhs, rhs));
33
  return *isolate->factory()->ToBoolean(result);
34 35 36 37 38 39 40 41
}

RUNTIME_FUNCTION(Runtime_BigIntCompareToString) {
  HandleScope scope(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Smi, mode, 0);
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 1);
  CONVERT_ARG_HANDLE_CHECKED(String, rhs, 2);
42 43 44
  bool result =
      ComparisonResultToBool(static_cast<Operation>(mode->value()),
                             BigInt::CompareToString(isolate, lhs, rhs));
45
  return *isolate->factory()->ToBoolean(result);
46 47
}

48
RUNTIME_FUNCTION(Runtime_BigIntEqualToBigInt) {
49 50
  SealHandleScope shs(isolate);
  DCHECK_EQ(2, args.length());
51 52 53
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 0);
  CONVERT_ARG_HANDLE_CHECKED(BigInt, rhs, 1);
  bool result = BigInt::EqualToBigInt(*lhs, *rhs);
54
  return *isolate->factory()->ToBoolean(result);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

RUNTIME_FUNCTION(Runtime_BigIntEqualToNumber) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, rhs, 1);
  bool result = BigInt::EqualToNumber(lhs, rhs);
  return *isolate->factory()->ToBoolean(result);
}

RUNTIME_FUNCTION(Runtime_BigIntEqualToString) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BigInt, lhs, 0);
  CONVERT_ARG_HANDLE_CHECKED(String, rhs, 1);
71
  bool result = BigInt::EqualToString(isolate, lhs, rhs);
72
  return *isolate->factory()->ToBoolean(result);
73 74 75 76 77 78
}

RUNTIME_FUNCTION(Runtime_BigIntToBoolean) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BigInt, bigint, 0);
79
  return *isolate->factory()->ToBoolean(bigint->ToBoolean());
80 81
}

82 83 84 85
RUNTIME_FUNCTION(Runtime_BigIntToNumber) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BigInt, x, 0);
86
  return *BigInt::ToNumber(isolate, x);
87 88
}

89 90 91 92 93 94 95
RUNTIME_FUNCTION(Runtime_ToBigInt) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, x, 0);
  RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
}

96 97 98 99 100 101
RUNTIME_FUNCTION(Runtime_BigIntBinaryOp) {
  HandleScope scope(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, left_obj, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, right_obj, 1);
  CONVERT_SMI_ARG_CHECKED(opcode, 2);
102
  Operation op = static_cast<Operation>(opcode);
103 104 105 106 107 108 109 110

  if (!left_obj->IsBigInt() || !right_obj->IsBigInt()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kBigIntMixedTypes));
  }
  Handle<BigInt> left(Handle<BigInt>::cast(left_obj));
  Handle<BigInt> right(Handle<BigInt>::cast(right_obj));
  MaybeHandle<BigInt> result;
111 112
  switch (op) {
    case Operation::kAdd:
113
      result = BigInt::Add(isolate, left, right);
114
      break;
115
    case Operation::kSubtract:
116
      result = BigInt::Subtract(isolate, left, right);
117
      break;
118
    case Operation::kMultiply:
119
      result = BigInt::Multiply(isolate, left, right);
120
      break;
121
    case Operation::kDivide:
122
      result = BigInt::Divide(isolate, left, right);
123
      break;
124
    case Operation::kModulus:
125
      result = BigInt::Remainder(isolate, left, right);
126
      break;
127
    case Operation::kExponentiate:
128
      result = BigInt::Exponentiate(isolate, left, right);
129
      break;
130
    case Operation::kBitwiseAnd:
131
      result = BigInt::BitwiseAnd(isolate, left, right);
132
      break;
133
    case Operation::kBitwiseOr:
134
      result = BigInt::BitwiseOr(isolate, left, right);
135
      break;
136
    case Operation::kBitwiseXor:
137
      result = BigInt::BitwiseXor(isolate, left, right);
138
      break;
139
    case Operation::kShiftLeft:
140
      result = BigInt::LeftShift(isolate, left, right);
141
      break;
142
    case Operation::kShiftRight:
143
      result = BigInt::SignedRightShift(isolate, left, right);
144
      break;
145
    case Operation::kShiftRightLogical:
146
      result = BigInt::UnsignedRightShift(isolate, left, right);
147
      break;
148 149 150 151 152 153
    default:
      UNREACHABLE();
  }
  RETURN_RESULT_OR_FAILURE(isolate, result);
}

154 155 156 157 158
RUNTIME_FUNCTION(Runtime_BigIntUnaryOp) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BigInt, x, 0);
  CONVERT_SMI_ARG_CHECKED(opcode, 1);
159
  Operation op = static_cast<Operation>(opcode);
160 161

  MaybeHandle<BigInt> result;
162 163
  switch (op) {
    case Operation::kBitwiseNot:
164
      result = BigInt::BitwiseNot(isolate, x);
165
      break;
166
    case Operation::kNegate:
167
      result = BigInt::UnaryMinus(isolate, x);
168
      break;
169
    case Operation::kIncrement:
170
      result = BigInt::Increment(isolate, x);
171
      break;
172
    case Operation::kDecrement:
173
      result = BigInt::Decrement(isolate, x);
174 175 176 177 178 179 180
      break;
    default:
      UNREACHABLE();
  }
  RETURN_RESULT_OR_FAILURE(isolate, result);
}

181 182
}  // namespace internal
}  // namespace v8