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:
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/. */
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"
22 template <class ViewType
>
23 static JSObject
* CreateObj(JSContext
* cx
, size_t len
) {
24 return ViewType::create(cx
, len
).asObject();
28 JSObject
* CreateObj
<JS::DataView
>(JSContext
* cx
, size_t len
) {
29 JS::Rooted
<JSObject
*> buffer(cx
, JS::NewArrayBuffer(cx
, len
));
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
));
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
)));
58 template <class APIType
>
59 bool TestType(JSContext
* cx
, Handle
<JSObject
*> otherGlobal
) {
60 JS::Rooted
<JSObject
*> obj(cx
, CreateObj
<APIType
>(cx
, 8));
63 // Any of these should be creatable as an ArrayBufferOrView.
64 JS::Rooted
<JS::ArrayBufferOrView
> abov(
65 cx
, JS::ArrayBufferOrView::fromObject(obj
));
68 // And that should allow unwrapping as well.
69 abov
= JS::ArrayBufferOrView::unwrap(obj
);
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
));
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));
80 auto deny
= APIType::fromObject(u16array
);
82 deny
= APIType::unwrap(u16array
);
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!
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
);
100 abov
= JS::ArrayBufferOrView::unwrap(wrapped
);
103 JS::Rooted
<APIType
> dummy(cx
, APIType::fromObject(obj
));
106 CHECK(dummy
.asObject());
111 END_TEST(testArrayBufferOrView_type
)