summaryrefslogtreecommitdiffstats
path: root/js/src/vm/ArrayBufferObjectMaybeShared.cpp
blob: f5e544255a7441d1f70667e333ce4e6e528e597e (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
50
51
52
53
54
55
56
57
58
59
60
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Assertions.h"  // MOZ_ASSERT

#include <stdint.h>  // uint8_t, uint32_t

#include "jstypes.h"  // JS_PUBLIC_API

#include "js/ArrayBufferMaybeShared.h"
#include "vm/ArrayBufferObject.h"  // js::ArrayBufferObject
#include "vm/JSObject.h"           // JSObject
#include "vm/SharedArrayObject.h"  // js::SharedArrayBufferObject
#include "vm/SharedMem.h"          // SharedMem

using namespace js;

JS_PUBLIC_API bool JS::IsArrayBufferObjectMaybeShared(JSObject* obj) {
  return obj->canUnwrapAs<ArrayBufferObjectMaybeShared>();
}

JS_PUBLIC_API JSObject* JS::UnwrapArrayBufferMaybeShared(JSObject* obj) {
  return obj->maybeUnwrapIf<ArrayBufferObjectMaybeShared>();
}

JS_PUBLIC_API void JS::GetArrayBufferMaybeSharedLengthAndData(
    JSObject* obj, uint32_t* length, bool* isSharedMemory, uint8_t** data) {
  MOZ_ASSERT(obj->is<ArrayBufferObjectMaybeShared>());

  if (obj->is<SharedArrayBufferObject>()) {
    auto* buffer = &obj->as<SharedArrayBufferObject>();
    *length = buffer->byteLength().deprecatedGetUint32();
    *data = buffer->dataPointerShared().unwrap();
    *isSharedMemory = true;
  } else {
    auto* buffer = &obj->as<ArrayBufferObject>();
    *length = buffer->byteLength().deprecatedGetUint32();
    *data = buffer->dataPointer();
    *isSharedMemory = false;
  }
}

JS_PUBLIC_API uint8_t* JS::GetArrayBufferMaybeSharedData(
    JSObject* obj, bool* isSharedMemory, const JS::AutoRequireNoGC&) {
  MOZ_ASSERT(obj->maybeUnwrapIf<ArrayBufferObjectMaybeShared>());

  if (ArrayBufferObject* aobj = obj->maybeUnwrapIf<ArrayBufferObject>()) {
    *isSharedMemory = false;
    return aobj->dataPointer();
  } else if (SharedArrayBufferObject* saobj =
                 obj->maybeUnwrapIf<SharedArrayBufferObject>()) {
    *isSharedMemory = true;
    return saobj->dataPointerShared().unwrap();
  }

  return nullptr;
}