runtime-bigint.cc 5.95 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/execution/arguments-inl.h"
6
#include "src/logging/counters.h"
7
#include "src/objects/bigint.h"
8
#include "src/objects/objects-inl.h"
9
#include "src/runtime/runtime-utils.h"
10 11 12 13

namespace v8 {
namespace internal {

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

RUNTIME_FUNCTION(Runtime_BigIntCompareToNumber) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
28 29 30 31
  int mode = args.smi_value_at(0);
  Handle<BigInt> lhs = args.at<BigInt>(1);
  Handle<Object> rhs = args.at(2);
  bool result = ComparisonResultToBool(static_cast<Operation>(mode),
32
                                       BigInt::CompareToNumber(lhs, rhs));
33
  return *isolate->factory()->ToBoolean(result);
34 35 36 37 38
}

RUNTIME_FUNCTION(Runtime_BigIntCompareToString) {
  HandleScope scope(isolate);
  DCHECK_EQ(3, args.length());
39 40 41
  int mode = args.smi_value_at(0);
  Handle<BigInt> lhs = args.at<BigInt>(1);
  Handle<String> rhs = args.at<String>(2);
42 43 44
  Maybe<ComparisonResult> maybe_result =
      BigInt::CompareToString(isolate, lhs, rhs);
  MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
45
  bool result = ComparisonResultToBool(static_cast<Operation>(mode),
46
                                       maybe_result.FromJust());
47
  return *isolate->factory()->ToBoolean(result);
48 49
}

50
RUNTIME_FUNCTION(Runtime_BigIntEqualToBigInt) {
51 52
  SealHandleScope shs(isolate);
  DCHECK_EQ(2, args.length());
53 54
  Handle<BigInt> lhs = args.at<BigInt>(0);
  Handle<BigInt> rhs = args.at<BigInt>(1);
55
  bool result = BigInt::EqualToBigInt(*lhs, *rhs);
56
  return *isolate->factory()->ToBoolean(result);
57 58 59 60 61
}

RUNTIME_FUNCTION(Runtime_BigIntEqualToNumber) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(2, args.length());
62 63
  Handle<BigInt> lhs = args.at<BigInt>(0);
  Handle<Object> rhs = args.at(1);
64 65 66 67 68 69 70
  bool result = BigInt::EqualToNumber(lhs, rhs);
  return *isolate->factory()->ToBoolean(result);
}

RUNTIME_FUNCTION(Runtime_BigIntEqualToString) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
71 72
  Handle<BigInt> lhs = args.at<BigInt>(0);
  Handle<String> rhs = args.at<String>(1);
73 74 75
  Maybe<bool> maybe_result = BigInt::EqualToString(isolate, lhs, rhs);
  MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
  return *isolate->factory()->ToBoolean(maybe_result.FromJust());
76 77 78 79 80
}

RUNTIME_FUNCTION(Runtime_BigIntToBoolean) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(1, args.length());
81
  Handle<BigInt> bigint = args.at<BigInt>(0);
82
  return *isolate->factory()->ToBoolean(bigint->ToBoolean());
83 84
}

85 86 87
RUNTIME_FUNCTION(Runtime_BigIntToNumber) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
88
  Handle<BigInt> x = args.at<BigInt>(0);
89
  return *BigInt::ToNumber(isolate, x);
90 91
}

92 93 94
RUNTIME_FUNCTION(Runtime_ToBigInt) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
95
  Handle<Object> x = args.at(0);
96 97 98
  RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
}

99 100 101
RUNTIME_FUNCTION(Runtime_BigIntBinaryOp) {
  HandleScope scope(isolate);
  DCHECK_EQ(3, args.length());
102 103 104
  Handle<Object> left_obj = args.at(0);
  Handle<Object> right_obj = args.at(1);
  int opcode = args.smi_value_at(2);
105
  Operation op = static_cast<Operation>(opcode);
106 107 108 109 110 111 112 113

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

157 158 159
RUNTIME_FUNCTION(Runtime_BigIntUnaryOp) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
160 161
  Handle<BigInt> x = args.at<BigInt>(0);
  int opcode = args.smi_value_at(1);
162
  Operation op = static_cast<Operation>(opcode);
163 164

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

184 185
}  // namespace internal
}  // namespace v8