/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * vim: set ts=8 sts=4 et sw=4 tw=99: */ #include "jsfriendapi.h" #include "jsapi-tests/tests.h" BEGIN_TEST(testArrayBufferView_type) { CHECK((TestViewType, JS_GetObjectAsUint8Array, js::Scalar::Uint8, 7, 7>(cx))); CHECK((TestViewType, JS_GetObjectAsInt8Array, js::Scalar::Int8, 33, 33>(cx))); CHECK((TestViewType, JS_GetObjectAsUint8ClampedArray, js::Scalar::Uint8Clamped, 7, 7>(cx))); CHECK((TestViewType, JS_GetObjectAsUint16Array, js::Scalar::Uint16, 3, 6>(cx))); CHECK((TestViewType, JS_GetObjectAsInt16Array, js::Scalar::Int16, 17, 34>(cx))); CHECK((TestViewType, JS_GetObjectAsUint32Array, js::Scalar::Uint32, 15, 60>(cx))); CHECK((TestViewType, JS_GetObjectAsInt32Array, js::Scalar::Int32, 8, 32>(cx))); CHECK((TestViewType, JS_GetObjectAsFloat32Array, js::Scalar::Float32, 7, 28>(cx))); CHECK((TestViewType, JS_GetObjectAsFloat64Array, js::Scalar::Float64, 9, 72>(cx))); CHECK((TestViewType(cx))); JS::Rooted hasTypedObject(cx); EVAL("typeof TypedObject !== 'undefined'", &hasTypedObject); if (hasTypedObject.isTrue()) { JS::Rooted tval(cx); EVAL("var T = new TypedObject.StructType({ x: TypedObject.uint32 });\n" "new T(new ArrayBuffer(4));", &tval); JS::Rooted tobj(cx, &tval.toObject()); CHECK(!JS_IsArrayBufferViewObject(tobj)); } return true; } static JSObject* CreateDataView(JSContext* cx) { JS::Rooted global(cx, JS::CurrentGlobalOrNull(cx)); if (!global) return nullptr; static const char code[] = "new DataView(new ArrayBuffer(8))"; JS::Rooted val(cx); JS::CompileOptions opts(cx); if (!JS::Evaluate(cx, global, opts.setFileAndLine(__FILE__, __LINE__), code, strlen(code), &val)) return nullptr; JS::Rooted dv(cx, &val.toObject()); if (!JS_IsDataViewObject(dv)) return nullptr; return dv; } template static JSObject* Create(JSContext* cx) { return CreateTypedArray(cx, Length); } template bool TestViewType(JSContext* cx) { JS::Rooted obj(cx, CreateViewType(cx)); CHECK(obj); CHECK(JS_IsArrayBufferViewObject(obj)); CHECK(JS_GetArrayBufferViewType(obj) == ExpectedType); CHECK(JS_GetArrayBufferViewByteLength(obj) == ExpectedByteLength); { JS::AutoCheckCannotGC nogc; T* data1 = static_cast(JS_GetArrayBufferViewData(obj, nogc)); T* data2; uint32_t len; CHECK(obj == GetObjectAs(obj, &len, &data2)); CHECK(data1 == data2); CHECK(len == ExpectedLength); } return true; } END_TEST(testArrayBufferView_type)