Bug 705019 - Allow access to {C,Pointer,Struct,Array,Function}Type.prototype.prototype. r=jorendorff

This commit is contained in:
Bobby Holley 2011-11-29 18:29:19 -08:00
Родитель d37ec23c8a
Коммит 271c986c95
3 изменённых файлов: 23 добавлений и 5 удалений

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

@ -693,6 +693,10 @@ InitTypeConstructor(JSContext* cx,
if (instanceProps && !JS_DefineProperties(cx, dataProto, instanceProps))
return false;
// Link the type prototype to the data prototype.
if (!JS_SetReservedSlot(cx, typeProto, SLOT_OURDATAPROTO, OBJECT_TO_JSVAL(dataProto)))
return false;
if (!JS_FreezeObject(cx, obj) ||
//!JS_FreezeObject(cx, dataProto) || // XXX fixme - see bug 541212!
!JS_FreezeObject(cx, typeProto))
@ -740,7 +744,7 @@ AttachProtos(JSContext* cx, JSObject* proto, JSObject** protos)
{
// For a given 'proto' of [[Class]] "CTypeProto", attach each of the 'protos'
// to the appropriate CTypeProtoSlot. (SLOT_UINT64PROTO is the last slot
// of [[Class]] "CTypeProto".)
// of [[Class]] "CTypeProto" that we fill in this automated manner.)
for (JSUint32 i = 0; i <= SLOT_UINT64PROTO; ++i) {
if (!JS_SetReservedSlot(cx, proto, i, OBJECT_TO_JSVAL(protos[i])))
return false;
@ -784,6 +788,11 @@ InitTypeClasses(JSContext* cx, JSObject* parent)
if (!CDataProto)
return false;
// Link CTypeProto to CDataProto.
if (!JS_SetReservedSlot(cx, CTypeProto, SLOT_OURDATAPROTO,
OBJECT_TO_JSVAL(CDataProto)))
return false;
// Create and attach the special class constructors: ctypes.PointerType,
// ctypes.ArrayType, ctypes.StructType, and ctypes.FunctionType.
// Each of these constructors 'c' has, respectively:
@ -3067,12 +3076,14 @@ CType::GetProtoFromType(JSContext* cx, JSObject* obj, CTypeProtoSlot slot)
JSBool
CType::PrototypeGetter(JSContext* cx, JSObject* obj, jsid idval, jsval* vp)
{
if (!CType::IsCType(cx, obj)) {
JS_ReportError(cx, "not a CType");
if (!(CType::IsCType(cx, obj) || CType::IsCTypeProto(cx, obj))) {
JS_ReportError(cx, "not a CType or CTypeProto");
return JS_FALSE;
}
ASSERT_OK(JS_GetReservedSlot(cx, obj, SLOT_PROTO, vp));
unsigned slot = CType::IsCTypeProto(cx, obj) ? (unsigned) SLOT_OURDATAPROTO
: (unsigned) SLOT_PROTO;
ASSERT_OK(JS_GetReservedSlot(cx, obj, slot, vp));
JS_ASSERT(!JSVAL_IS_PRIMITIVE(*vp) || JSVAL_IS_VOID(*vp));
return JS_TRUE;
}

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

@ -392,7 +392,8 @@ enum CTypeProtoSlot {
SLOT_FUNCTIONDATAPROTO = 8, // common ancestor of all CData objects of FunctionType
SLOT_INT64PROTO = 9, // ctypes.Int64.prototype object
SLOT_UINT64PROTO = 10, // ctypes.UInt64.prototype object
SLOT_CLOSURECX = 11, // JSContext for use with FunctionType closures
SLOT_OURDATAPROTO = 11, // the data prototype corresponding to this object
SLOT_CLOSURECX = 12, // JSContext for use with FunctionType closures
CTYPEPROTO_SLOTS
};

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

@ -291,6 +291,9 @@ function run_abstract_class_tests()
do_check_true(ctypes.CType.prototype.hasOwnProperty("toString"));
do_check_true(ctypes.CType.prototype.hasOwnProperty("toSource"));
// Make sure we can access 'prototype' on a CTypeProto.
do_check_true(ctypes.CType.prototype.prototype === ctypes.CData.prototype);
// Check that the shared properties and functions on ctypes.CType.prototype throw.
do_check_throws(function() { ctypes.CType.prototype.name; }, Error);
do_check_throws(function() { ctypes.CType.prototype.size; }, Error);
@ -1410,6 +1413,9 @@ function run_type_ctor_class_tests(c, t, t2, props, fns, instanceProps, instance
for each (let p in specialProps)
do_check_throws(function() { t.prototype[p]; }, Error);
// Make sure we can access 'prototype' on a CTypeProto.
do_check_true(Object.getPrototypeOf(c.prototype.prototype) === ctypes.CType.prototype.prototype);
// Test that an instance 'd' of 't' is a CData.
if (t.__proto__ != ctypes.FunctionType.prototype) {
let d = t();