Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testArrayBufferOrViewAPI.cpp
blob6b9189af0482b37f2b7fd1661095d636487cac3b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
5 /* This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include <type_traits>
11 #include "jsfriendapi.h"
13 #include "js/ArrayBuffer.h" // JS::NewArrayBuffer
14 #include "js/experimental/TypedData.h"
15 #include "js/ScalarType.h" // JS::Scalar::Type
16 #include "jsapi-tests/tests.h"
18 #include "vm/Realm-inl.h"
20 using namespace js;
22 template <class ViewType>
23 static JSObject* CreateObj(JSContext* cx, size_t len) {
24 return ViewType::create(cx, len).asObject();
27 template <>
28 JSObject* CreateObj<JS::DataView>(JSContext* cx, size_t len) {
29 JS::Rooted<JSObject*> buffer(cx, JS::NewArrayBuffer(cx, len));
30 if (!buffer) {
31 return nullptr;
33 return JS_NewDataView(cx, buffer, 0, len);
36 BEGIN_TEST(testArrayBufferOrView_type) {
37 JS::RealmOptions options;
38 JS::RootedObject otherGlobal(
39 cx, JS_NewGlobalObject(cx, basicGlobalClass(), nullptr,
40 JS::DontFireOnNewGlobalHook, options));
41 CHECK(otherGlobal);
43 CHECK((TestType<JS::TypedArray<Scalar::Uint8>>(cx, otherGlobal)));
44 CHECK((TestType<JS::TypedArray<Scalar::Int8>>(cx, otherGlobal)));
45 CHECK((TestType<JS::TypedArray<Scalar::Uint8Clamped>>(cx, otherGlobal)));
46 CHECK((TestType<JS::TypedArray<Scalar::Uint16>>(cx, otherGlobal)));
47 CHECK((TestType<JS::TypedArray<Scalar::Int16>>(cx, otherGlobal)));
48 CHECK((TestType<JS::TypedArray<Scalar::Uint32>>(cx, otherGlobal)));
49 CHECK((TestType<JS::TypedArray<Scalar::Int32>>(cx, otherGlobal)));
50 CHECK((TestType<JS::TypedArray<Scalar::Float32>>(cx, otherGlobal)));
51 CHECK((TestType<JS::TypedArray<Scalar::Float64>>(cx, otherGlobal)));
52 CHECK((TestType<JS::DataView>(cx, otherGlobal)));
53 CHECK((TestType<JS::ArrayBuffer>(cx, otherGlobal)));
55 return true;
58 template <class APIType>
59 bool TestType(JSContext* cx, Handle<JSObject*> otherGlobal) {
60 JS::Rooted<JSObject*> obj(cx, CreateObj<APIType>(cx, 8));
61 CHECK(obj);
63 // Any of these should be creatable as an ArrayBufferOrView.
64 JS::Rooted<JS::ArrayBufferOrView> abov(
65 cx, JS::ArrayBufferOrView::fromObject(obj));
66 CHECK(abov);
68 // And that should allow unwrapping as well.
69 abov = JS::ArrayBufferOrView::unwrap(obj);
70 CHECK(abov);
72 if constexpr (!std::is_same_v<APIType, JS::Uint16Array>) {
73 // Check that we can't make an API object of a different type.
74 JS::Rooted<JS::Uint16Array> nope(cx, JS::Uint16Array::unwrap(obj));
75 CHECK(!nope);
77 // And that we can't make an API object from an object of a different type.
78 JS::Rooted<JSObject*> u16array(cx, CreateObj<JS::Uint16Array>(cx, 10));
79 CHECK(u16array);
80 auto deny = APIType::fromObject(u16array);
81 CHECK(!deny);
82 deny = APIType::unwrap(u16array);
83 CHECK(!deny);
86 CHECK_EQUAL(abov.asObject(), obj);
88 JS::Rooted<JSObject*> wrapped(cx);
90 AutoRealm ar(cx, otherGlobal);
91 wrapped = CreateObj<APIType>(cx, 8); // Not wrapped yet!
92 CHECK(wrapped);
94 CHECK(wrapped->compartment() == otherGlobal->compartment());
95 CHECK(JS_WrapObject(cx, &wrapped)); // Now it's wrapped.
96 CHECK(wrapped->compartment() == global->compartment());
98 abov = JS::ArrayBufferOrView::fromObject(wrapped);
99 CHECK(!abov);
100 abov = JS::ArrayBufferOrView::unwrap(wrapped);
101 CHECK(abov);
103 JS::Rooted<APIType> dummy(cx, APIType::fromObject(obj));
104 CHECK(obj);
105 CHECK(dummy);
106 CHECK(dummy.asObject());
108 return true;
111 END_TEST(testArrayBufferOrView_type)