11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/base/bits.h"
61cb0ef41Sopenharmony_ci#include "src/execution/arguments-inl.h"
71cb0ef41Sopenharmony_ci#include "src/execution/isolate-inl.h"
81cb0ef41Sopenharmony_ci#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
91cb0ef41Sopenharmony_ci#include "src/init/bootstrapper.h"
101cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
111cb0ef41Sopenharmony_ci#include "src/runtime/runtime-utils.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_StringToNumber) {
171cb0ef41Sopenharmony_ci  HandleScope handle_scope(isolate);
181cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
191cb0ef41Sopenharmony_ci  Handle<String> subject = args.at<String>(0);
201cb0ef41Sopenharmony_ci  return *String::ToNumber(isolate, subject);
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// ES6 18.2.5 parseInt(string, radix) slow path
251cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_StringParseInt) {
261cb0ef41Sopenharmony_ci  HandleScope handle_scope(isolate);
271cb0ef41Sopenharmony_ci  DCHECK_EQ(2, args.length());
281cb0ef41Sopenharmony_ci  Handle<Object> string = args.at(0);
291cb0ef41Sopenharmony_ci  Handle<Object> radix = args.at(1);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Convert {string} to a String first, and flatten it.
321cb0ef41Sopenharmony_ci  Handle<String> subject;
331cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, subject,
341cb0ef41Sopenharmony_ci                                     Object::ToString(isolate, string));
351cb0ef41Sopenharmony_ci  subject = String::Flatten(isolate, subject);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Convert {radix} to Int32.
381cb0ef41Sopenharmony_ci  if (!radix->IsNumber()) {
391cb0ef41Sopenharmony_ci    ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
401cb0ef41Sopenharmony_ci                                       Object::ToNumber(isolate, radix));
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  int radix32 = DoubleToInt32(radix->Number());
431cb0ef41Sopenharmony_ci  if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
441cb0ef41Sopenharmony_ci    return ReadOnlyRoots(isolate).nan_value();
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  double result = StringToInt(isolate, subject, radix32);
481cb0ef41Sopenharmony_ci  return *isolate->factory()->NewNumber(result);
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// ES6 18.2.4 parseFloat(string)
531cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_StringParseFloat) {
541cb0ef41Sopenharmony_ci  HandleScope shs(isolate);
551cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
561cb0ef41Sopenharmony_ci  Handle<String> subject = args.at<String>(0);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
591cb0ef41Sopenharmony_ci                                std::numeric_limits<double>::quiet_NaN());
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  return *isolate->factory()->NewNumber(value);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_NumberToStringSlow) {
651cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
661cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
671cb0ef41Sopenharmony_ci  return *isolate->factory()->NumberToString(args.at(0),
681cb0ef41Sopenharmony_ci                                             NumberCacheMode::kSetOnly);
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_MaxSmi) {
721cb0ef41Sopenharmony_ci  SealHandleScope shs(isolate);
731cb0ef41Sopenharmony_ci  DCHECK_EQ(0, args.length());
741cb0ef41Sopenharmony_ci  return Smi::FromInt(Smi::kMaxValue);
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_IsSmi) {
791cb0ef41Sopenharmony_ci  SealHandleScope shs(isolate);
801cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
811cb0ef41Sopenharmony_ci  Object obj = args[0];
821cb0ef41Sopenharmony_ci  return isolate->heap()->ToBoolean(obj.IsSmi());
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
871cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
881cb0ef41Sopenharmony_ci  DCHECK_EQ(0, args.length());
891cb0ef41Sopenharmony_ci  return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
941cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
951cb0ef41Sopenharmony_ci  DCHECK_EQ(0, args.length());
961cb0ef41Sopenharmony_ci  return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
971cb0ef41Sopenharmony_ci}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci}  // namespace internal
1001cb0ef41Sopenharmony_ci}  // namespace v8
101