Bug 705019 - Make toSource() and toString() work on sCTypeProtoClass and sCDataProtoClass objects. r=jorendorff

This commit is contained in:
Bobby Holley 2011-11-29 18:26:12 -08:00
parent 457374946b
commit 373b613af3
2 changed files with 68 additions and 34 deletions

View File

@ -3157,19 +3157,28 @@ JSBool
CType::ToString(JSContext* cx, uintN argc, jsval* vp)
{
JSObject* obj = JS_THIS_OBJECT(cx, vp);
if (!obj || !CType::IsCType(cx, obj)) {
if (!obj ||
!(CType::IsCType(cx, obj) || CType::IsCTypeProto(cx, obj)))
{
JS_ReportError(cx, "not a CType");
return JS_FALSE;
}
AutoString type;
AppendString(type, "type ");
AppendString(type, GetName(cx, obj));
JSString* result = NewUCString(cx, type);
// Create the appropriate string depending on whether we're sCTypeClass or
// sCTypeProtoClass.
JSString* result;
if (CType::IsCType(cx, obj)) {
AutoString type;
AppendString(type, "type ");
AppendString(type, GetName(cx, obj));
result = NewUCString(cx, type);
}
else {
result = JS_NewStringCopyZ(cx, "[CType proto object]");
}
if (!result)
return JS_FALSE;
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(result));
return JS_TRUE;
}
@ -3178,17 +3187,26 @@ JSBool
CType::ToSource(JSContext* cx, uintN argc, jsval* vp)
{
JSObject* obj = JS_THIS_OBJECT(cx, vp);
if (!obj || !CType::IsCType(cx, obj)) {
if (!obj ||
!(CType::IsCType(cx, obj) || CType::IsCTypeProto(cx, obj)))
{
JS_ReportError(cx, "not a CType");
return JS_FALSE;
}
AutoString source;
BuildTypeSource(cx, obj, false, source);
JSString* result = NewUCString(cx, source);
// Create the appropriate string depending on whether we're sCTypeClass or
// sCTypeProtoClass.
JSString* result;
if (CType::IsCType(cx, obj)) {
AutoString source;
BuildTypeSource(cx, obj, false, source);
result = NewUCString(cx, source);
} else {
result = JS_NewStringCopyZ(cx, "[CType proto object]");
}
if (!result)
return JS_FALSE;
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(result));
return JS_TRUE;
}
@ -5945,29 +5963,35 @@ CData::ToSource(JSContext* cx, uintN argc, jsval* vp)
}
JSObject* obj = JS_THIS_OBJECT(cx, vp);
if (!obj || !CData::IsCData(cx, obj)) {
if (!obj ||
!(CData::IsCData(cx, obj) || CData::IsCDataProto(cx, obj))) {
JS_ReportError(cx, "not a CData");
return JS_FALSE;
}
JSObject* typeObj = CData::GetCType(cx, obj);
void* data = CData::GetData(cx, obj);
JSString* result;
if (CData::IsCData(cx, obj)) {
JSObject* typeObj = CData::GetCType(cx, obj);
void* data = CData::GetData(cx, obj);
// Walk the types, building up the toSource() string.
// First, we build up the type expression:
// 't.ptr' for pointers;
// 't.array([n])' for arrays;
// 'n' for structs, where n = t.name, the struct's name. (We assume this is
// bound to a variable in the current scope.)
AutoString source;
BuildTypeSource(cx, typeObj, true, source);
AppendString(source, "(");
if (!BuildDataSource(cx, typeObj, data, false, source))
return JS_FALSE;
// Walk the types, building up the toSource() string.
// First, we build up the type expression:
// 't.ptr' for pointers;
// 't.array([n])' for arrays;
// 'n' for structs, where n = t.name, the struct's name. (We assume this is
// bound to a variable in the current scope.)
AutoString source;
BuildTypeSource(cx, typeObj, true, source);
AppendString(source, "(");
if (!BuildDataSource(cx, typeObj, data, false, source))
return JS_FALSE;
AppendString(source, ")");
AppendString(source, ")");
JSString* result = NewUCString(cx, source);
result = NewUCString(cx, source);
}
else
result = JS_NewStringCopyZ(cx, "[CData proto object]");
if (!result)
return JS_FALSE;

View File

@ -296,8 +296,11 @@ function run_abstract_class_tests()
do_check_throws(function() { ctypes.CType.prototype.size; }, Error);
do_check_throws(function() { ctypes.CType.prototype.ptr; }, Error);
do_check_throws(function() { ctypes.CType.prototype.array(); }, Error);
do_check_throws(function() { ctypes.CType.prototype.toString(); }, Error);
do_check_throws(function() { ctypes.CType.prototype.toSource(); }, Error);
// toString and toSource are called by the web console during inspection,
// so we don't want them to throw.
do_check_eq(typeof ctypes.CType.prototype.toString(), 'string');
do_check_eq(typeof ctypes.CType.prototype.toSource(), 'string');
// Test that ctypes.CData is an abstract constructor that throws.
do_check_throws(function() { ctypes.CData(); }, Error);
@ -331,8 +334,12 @@ function run_abstract_class_tests()
do_check_throws(function() { ctypes.CData.prototype.value = null; }, Error);
do_check_throws(function() { ctypes.CData.prototype.address(); }, Error);
do_check_throws(function() { ctypes.CData.prototype.readString(); }, Error);
do_check_throws(function() { ctypes.CData.prototype.toString(); }, Error);
do_check_throws(function() { ctypes.CData.prototype.toSource(); }, Error);
// toString and toSource are called by the web console during inspection,
// so we don't want them to throw.
do_check_eq(ctypes.CData.prototype.toString(), '[CData proto object]');
do_check_eq(ctypes.CData.prototype.toSource(), '[CData proto object]');
}
function run_Int64_tests() {
@ -799,8 +806,11 @@ function run_basic_class_tests(t)
do_check_throws(function() { t.prototype.value = null; }, Error);
do_check_throws(function() { t.prototype.address(); }, Error);
do_check_throws(function() { t.prototype.readString(); }, Error);
do_check_throws(function() { t.prototype.toString(); }, Error);
do_check_throws(function() { t.prototype.toSource(); }, Error);
// toString and toSource are called by the web console during inspection,
// so we don't want them to throw.
do_check_eq(t.prototype.toString(), '[CData proto object]');
do_check_eq(t.prototype.toSource(), '[CData proto object]');
// Test that an instance 'd' of 't' is a CData.
let d = t();