runtime-numbers.cc 3.15 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
#include "src/base/bits.h"
6 7
#include "src/execution/arguments-inl.h"
#include "src/execution/isolate-inl.h"
8
#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
9
#include "src/init/bootstrapper.h"
10
#include "src/logging/counters.h"
11
#include "src/runtime/runtime-utils.h"
12 13 14 15 16 17

namespace v8 {
namespace internal {

RUNTIME_FUNCTION(Runtime_IsValidSmi) {
  SealHandleScope shs(isolate);
18
  DCHECK_EQ(1, args.length());
19 20 21 22 23 24 25 26

  CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]);
  return isolate->heap()->ToBoolean(Smi::IsValid(number));
}


RUNTIME_FUNCTION(Runtime_StringToNumber) {
  HandleScope handle_scope(isolate);
27
  DCHECK_EQ(1, args.length());
28
  CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
29
  return *String::ToNumber(isolate, subject);
30 31 32
}


33
// ES6 18.2.5 parseInt(string, radix) slow path
34 35
RUNTIME_FUNCTION(Runtime_StringParseInt) {
  HandleScope handle_scope(isolate);
36 37 38
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, string, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, radix, 1);
39

40 41 42 43
  // Convert {string} to a String first, and flatten it.
  Handle<String> subject;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, subject,
                                     Object::ToString(isolate, string));
44
  subject = String::Flatten(isolate, subject);
45

46 47
  // Convert {radix} to Int32.
  if (!radix->IsNumber()) {
48 49
    ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
                                       Object::ToNumber(isolate, radix));
50 51 52
  }
  int radix32 = DoubleToInt32(radix->Number());
  if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
53
    return ReadOnlyRoots(isolate).nan_value();
54 55
  }

56
  double result = StringToInt(isolate, subject, radix32);
57
  return *isolate->factory()->NewNumber(result);
58 59 60
}


61
// ES6 18.2.4 parseFloat(string)
62 63
RUNTIME_FUNCTION(Runtime_StringParseFloat) {
  HandleScope shs(isolate);
64
  DCHECK_EQ(1, args.length());
65 66
  CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);

67
  double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
68
                                std::numeric_limits<double>::quiet_NaN());
69 70 71 72

  return *isolate->factory()->NewNumber(value);
}

73
RUNTIME_FUNCTION(Runtime_NumberToString) {
74
  HandleScope scope(isolate);
75
  DCHECK_EQ(1, args.length());
76 77
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(number, 0);

78
  return *isolate->factory()->NumberToString(number);
79 80
}

81 82
RUNTIME_FUNCTION(Runtime_MaxSmi) {
  SealHandleScope shs(isolate);
83
  DCHECK_EQ(0, args.length());
84 85 86 87
  return Smi::FromInt(Smi::kMaxValue);
}


88
RUNTIME_FUNCTION(Runtime_IsSmi) {
89
  SealHandleScope shs(isolate);
90
  DCHECK_EQ(1, args.length());
91
  CONVERT_ARG_CHECKED(Object, obj, 0);
92
  return isolate->heap()->ToBoolean(obj.IsSmi());
93 94 95
}


balazs.kilvady's avatar
balazs.kilvady committed
96 97
RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
  HandleScope scope(isolate);
98
  DCHECK_EQ(0, args.length());
balazs.kilvady's avatar
balazs.kilvady committed
99 100 101 102 103 104
  return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
}


RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
  HandleScope scope(isolate);
105
  DCHECK_EQ(0, args.length());
balazs.kilvady's avatar
balazs.kilvady committed
106 107 108
  return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
}

109 110
}  // namespace internal
}  // namespace v8