Bug 1575425 - Part 2: Merge ArrayBuffer and SharedArrayBuffer in DOM binding; r=bzbarsky,jwalden

We need this for upcoming change which supports having [AllowShared] on
ArrayBuffer type in WebIDL.

Differential Revision: https://phabricator.services.mozilla.com/D59989

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Edgar Chen 2020-01-23 13:48:02 +00:00
Родитель 1ff24dddf7
Коммит 7572e4a211
4 изменённых файлов: 158 добавлений и 7 удалений

Просмотреть файл

@ -10,6 +10,7 @@
#include <utility> #include <utility>
#include "js/ArrayBuffer.h" #include "js/ArrayBuffer.h"
#include "js/ArrayBufferMaybeShared.h"
#include "js/GCAPI.h" // JS::AutoCheckCannotGC #include "js/GCAPI.h" // JS::AutoCheckCannotGC
#include "js/RootingAPI.h" // JS::Rooted #include "js/RootingAPI.h" // JS::Rooted
#include "js/SharedArrayBuffer.h" #include "js/SharedArrayBuffer.h"
@ -282,15 +283,12 @@ typedef ArrayBufferView_base<js::UnwrapArrayBufferView,
js::GetArrayBufferViewLengthAndData, js::GetArrayBufferViewLengthAndData,
JS_GetArrayBufferViewType> JS_GetArrayBufferViewType>
ArrayBufferView; ArrayBufferView;
typedef TypedArray<uint8_t, JS::UnwrapArrayBuffer, JS::GetArrayBufferData, typedef TypedArray<uint8_t, JS::UnwrapArrayBufferMaybeShared,
JS::GetArrayBufferLengthAndData, JS::NewArrayBuffer> JS::GetArrayBufferMaybeSharedData,
JS::GetArrayBufferMaybeSharedLengthAndData,
JS::NewArrayBuffer>
ArrayBuffer; ArrayBuffer;
typedef TypedArray<
uint8_t, JS::UnwrapSharedArrayBuffer, JS::GetSharedArrayBufferData,
JS::GetSharedArrayBufferLengthAndData, JS::NewSharedArrayBuffer>
SharedArrayBuffer;
// A class for converting an nsTArray to a TypedArray // A class for converting an nsTArray to a TypedArray
// Note: A TypedArrayCreator must not outlive the nsTArray it was created from. // Note: A TypedArrayCreator must not outlive the nsTArray it was created from.
// So this is best used to pass from things that understand nsTArray to // So this is best used to pass from things that understand nsTArray to

Просмотреть файл

@ -0,0 +1,94 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/**
* Functions for working with either ArrayBuffer or SharedArrayBuffer objects
* in agnostic fashion.
*/
#ifndef js_ArrayBufferMaybeShared_h
#define js_ArrayBufferMaybeShared_h
#include <stdint.h> // uint8_t, uint32_t
#include "jstypes.h" // JS_PUBLIC_API
struct JS_PUBLIC_API JSContext;
class JS_PUBLIC_API JSObject;
namespace JS {
class JS_PUBLIC_API AutoRequireNoGC;
// TYPE TESTING
/**
* Check whether obj supports the JS::GetArrayBufferMaybeShared* APIs. Note
* that this may return false if a security wrapper is encountered that denies
* the unwrapping. If this test succeeds, then it is safe to call the various
* predicate and accessor JSAPI calls defined below.
*/
extern JS_PUBLIC_API bool IsArrayBufferObjectMaybeShared(JSObject* obj);
// ACCESSORS
/*
* Test for ArrayBufferMaybeShared subtypes and return the unwrapped object if
* so, else nullptr. Never throws.
*/
extern JS_PUBLIC_API JSObject* UnwrapArrayBufferMaybeShared(JSObject* obj);
/**
* Get the length, sharedness, and data from an ArrayBufferMaybeShared subtypes.
*
* The computed length and data pointer may be invalidated by a GC or by an
* unshared array buffer becoming detached. Callers must take care not to
* perform any actions that could trigger a GC or result in an unshared array
* buffer becoming detached. If such actions nonetheless must be performed,
* callers should perform this call a second time (and sensibly handle results
* that may be different from those returned the first time). (Sharedness is an
* immutable characteristic of an array buffer or shared array buffer, so that
* boolean remains valid across GC or detaching.)
*
* |obj| must be an ArrayBufferMaybeShared subtype: an ArrayBuffer or a
* SharedArrayBuffer.
*
* |*length| will be set to bytes in the buffer.
*
* |*isSharedMemory| will be set to true if it is a SharedArrayBuffer, otherwise
* to false.
*
* |*data| will be set to a pointer to the bytes in the buffer.
*/
extern JS_PUBLIC_API void GetArrayBufferMaybeSharedLengthAndData(
JSObject* obj, uint32_t* length, bool* isSharedMemory, uint8_t** data);
/**
* Return a pointer to the start of the array buffer's data, and indicate
* whether the data is from a shared array buffer through an outparam.
*
* The returned data pointer may be invalidated by a GC or by an unshared array
* buffer becoming detached. Callers must take care not to perform any actions
* that could trigger a GC or result in an unshared array buffer becoming
* detached. If such actions nonetheless must be performed, callers should
* perform this call a second time (and sensibly handle results that may be
* different from those returned the first time). (Sharedness is an immutable
* characteristic of an array buffer or shared array buffer, so that boolean
* remains valid across GC or detaching.)
*
* |obj| must have passed a JS::IsArrayBufferObjectMaybeShared test, or somehow
* be known that it would pass such a test: it is an ArrayBuffer or
* SharedArrayBuffer or a wrapper of an ArrayBuffer/SharedArrayBuffer, and the
* unwrapping will succeed.
*
* |*isSharedMemory| will be set to true if the typed array maps a
* SharedArrayBuffer, otherwise to false.
*/
extern JS_PUBLIC_API uint8_t* GetArrayBufferMaybeSharedData(
JSObject* obj, bool* isSharedMemory, const AutoRequireNoGC&);
} // namespace JS
#endif /* js_ArrayBufferMaybeShared_h */

Просмотреть файл

@ -125,6 +125,7 @@ EXPORTS.js += [
'../public/AllocPolicy.h', '../public/AllocPolicy.h',
'../public/Array.h', '../public/Array.h',
'../public/ArrayBuffer.h', '../public/ArrayBuffer.h',
'../public/ArrayBufferMaybeShared.h',
'../public/BinASTFormat.h', '../public/BinASTFormat.h',
'../public/BuildId.h', '../public/BuildId.h',
'../public/CallArgs.h', '../public/CallArgs.h',
@ -293,6 +294,7 @@ UNIFIED_SOURCES += [
'vm/Activation.cpp', 'vm/Activation.cpp',
'vm/ArgumentsObject.cpp', 'vm/ArgumentsObject.cpp',
'vm/ArrayBufferObject.cpp', 'vm/ArrayBufferObject.cpp',
'vm/ArrayBufferObjectMaybeShared.cpp',
'vm/ArrayBufferViewObject.cpp', 'vm/ArrayBufferViewObject.cpp',
'vm/AsyncFunction.cpp', 'vm/AsyncFunction.cpp',
'vm/AsyncIteration.cpp', 'vm/AsyncIteration.cpp',

Просмотреть файл

@ -0,0 +1,57 @@
/* -*- 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>());
bool isSharedArrayBuffer = obj->is<SharedArrayBufferObject>();
*length = isSharedArrayBuffer
? obj->as<SharedArrayBufferObject>().byteLength()
: obj->as<ArrayBufferObject>().byteLength();
*data = isSharedArrayBuffer
? obj->as<SharedArrayBufferObject>().dataPointerShared().unwrap()
: obj->as<ArrayBufferObject>().dataPointer();
*isSharedMemory = isSharedArrayBuffer;
}
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;
}