summaryrefslogtreecommitdiff
path: root/chromium/v8/src/builtins/builtins-shared-array.cc
blob: d1f0e4250e82962018e63bd435ec8260321590e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2022 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.

#include "src/builtins/accessors.h"
#include "src/builtins/builtins-utils-inl.h"
#include "src/objects/js-shared-array-inl.h"

namespace v8 {
namespace internal {

// We cannot allocate large objects with |AllocationType::kSharedOld|,
// see |HeapAllocator::AllocateRawLargeInternal|.
constexpr int kMaxJSSharedArraySize = (1 << 14) - 2;
static_assert(FixedArray::SizeFor(kMaxJSSharedArraySize) <=
              kMaxRegularHeapObjectSize);

BUILTIN(SharedArrayConstructor) {
  DCHECK(FLAG_shared_string_table);

  HandleScope scope(isolate);
  auto* factory = isolate->factory();

  Handle<Object> length_arg = args.atOrUndefined(isolate, 1);
  Handle<Object> length_number;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, length_number,
                                     Object::ToInteger(isolate, length_arg));
  if (!length_number->IsSmi()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewRangeError(MessageTemplate::kSharedArraySizeOutOfRange));
  }

  int length = Handle<Smi>::cast(length_number)->value();
  if (length < 0 || length > kMaxJSSharedArraySize) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewRangeError(MessageTemplate::kSharedArraySizeOutOfRange));
  }

  Handle<FixedArrayBase> storage =
      factory->NewFixedArray(length, AllocationType::kSharedOld);
  Handle<JSSharedArray> instance = Handle<JSSharedArray>::cast(
      factory->NewJSObject(args.target(), AllocationType::kSharedOld));
  instance->set_elements(*storage);

  return *instance;
}

}  // namespace internal
}  // namespace v8