Bug 896105 - Implement ES6 ArrayBuffer.isView. r=nmatsakis

--HG--
extra : rebase_source : 5ec4753296af4e52155ab576d16b71c03ce9e80a
This commit is contained in:
Till Schneidereit 2013-12-22 02:49:51 +01:00
parent 32076f8b23
commit 631dc5bfb8
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,25 @@
var BUGNUMBER = 896105;
var summary = 'ArrayBuffer.isView';
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
function runTests() {
assertEq(ArrayBuffer.isView(), false);
assertEq(ArrayBuffer.isView(undefined), false);
assertEq(ArrayBuffer.isView(null), false);
assertEq(ArrayBuffer.isView("primitive"), false);
assertEq(ArrayBuffer.isView({}), false);
assertEq(ArrayBuffer.isView([]), false);
assertEq(ArrayBuffer.isView(new ArrayBuffer(10)), false);
assertEq(ArrayBuffer.isView(new Int8Array(10)), true);
assertEq(ArrayBuffer.isView(new Int8Array(10).subarray(0, 3)), true);
if (typeof reportCompare !== 'undefined')
reportCompare(true, true);
print("Tests complete");
}
runTests();

View File

@ -184,6 +184,18 @@ ArrayBufferObject::fun_slice(JSContext *cx, unsigned argc, Value *vp)
return CallNonGenericMethod<IsArrayBuffer, fun_slice_impl>(cx, args);
}
/*
* ArrayBuffer.isView(obj); ES6 (Dec 2013 draft) 24.1.3.1
*/
bool
ArrayBufferObject::fun_isView(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
args.rval().setBoolean(args.get(0).isObject() &&
JS_IsArrayBufferViewObject(&args.get(0).toObject()));
return true;
}
/*
* new ArrayBuffer(byteLength)
*/
@ -3472,6 +3484,11 @@ const JSFunctionSpec ArrayBufferObject::jsfuncs[] = {
JS_FS_END
};
const JSFunctionSpec ArrayBufferObject::jsstaticfuncs[] = {
JS_FN("isView", ArrayBufferObject::fun_isView, 1, 0),
JS_FS_END
};
/*
* TypedArrayObject boilerplate
*/
@ -3763,6 +3780,9 @@ InitArrayBufferClass(JSContext *cx)
JS_DATA_TO_FUNC_PTR(PropertyOp, getter), nullptr, flags, 0, 0))
return nullptr;
if (!JS_DefineFunctions(cx, ctor, ArrayBufferObject::jsstaticfuncs))
return nullptr;
if (!JS_DefineFunctions(cx, arrayBufferProto, ArrayBufferObject::jsfuncs))
return nullptr;

View File

@ -56,11 +56,14 @@ class ArrayBufferObject : public JSObject
static const Class protoClass;
static const JSFunctionSpec jsfuncs[];
static const JSFunctionSpec jsstaticfuncs[];
static bool byteLengthGetter(JSContext *cx, unsigned argc, Value *vp);
static bool fun_slice(JSContext *cx, unsigned argc, Value *vp);
static bool fun_isView(JSContext *cx, unsigned argc, Value *vp);
static bool class_constructor(JSContext *cx, unsigned argc, Value *vp);
static JSObject *create(JSContext *cx, uint32_t nbytes, bool clear = true);