int constants accessible from JS using hardcoded InterfaceInfo

This commit is contained in:
jband%netscape.com 1999-01-07 05:18:09 +00:00
Родитель c876ae8a25
Коммит 743adb567d
4 изменённых файлов: 122 добавлений и 30 удалений

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

@ -170,9 +170,19 @@ int main()
v = OBJECT_TO_JSVAL(jsobj);
JS_SetProperty(cx, glob, "foo", &v);
char txt[] = "print(foo)";
char* p;
char txt1[] = "print('foo.five = '+ foo.five)";
char txt2[] = "print('foo.six = '+ foo.six)";
char txt3[] = "print('foo.bogus = '+ foo.bogus)";
JS_EvaluateScript(cx, glob, txt, sizeof(txt)-1, "builtin", 1, &rval);
p = txt1;
JS_EvaluateScript(cx, glob, p, strlen(p), "builtin", 1, &rval);
p = txt2;
JS_EvaluateScript(cx, glob, p, strlen(p), "builtin", 1, &rval);
p = txt3;
JS_EvaluateScript(cx, glob, p, strlen(p), "builtin", 1, &rval);
NS_RELEASE(obj);
NS_RELEASE(com_obj);

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

@ -44,8 +44,16 @@ nsInterfaceInfo::nsInterfaceInfo(REFNSIID aIID, const char* aName,
mMethodCount = 0;
mMethods = NULL;
mConstantCount = 0;
mConstants = NULL;
// XXX completely bogus hardcoded stuff...
mConstantCount = 2;
mConstants = new nsXPCConstant[2];
nsXPCVarient v;
v.type = nsXPCType::T_I32;
v.val.i32 = 5;
mConstants[0] = nsXPCConstant("five", v);
v.val.i32 = 6;
mConstants[1] = nsXPCConstant("six", v);
if(mParent)
NS_ADDREF(mParent);
@ -57,6 +65,10 @@ nsInterfaceInfo::~nsInterfaceInfo()
free(mName);
if(mParent)
NS_RELEASE(mParent);
if(mMethods)
delete [] mMethods;
if(mConstants)
delete [] mConstants;
}
nsresult
@ -114,7 +126,7 @@ nsInterfaceInfo::GetMethodInfo(unsigned index, const nsXPCMethodInfo** info)
}
// else...
*info = mMethods[mMethodBaseIndex-index];
*info = &mMethods[mMethodBaseIndex+index];
return NS_OK;
}
@ -133,7 +145,7 @@ nsInterfaceInfo::GetConstant(unsigned index, const nsXPCConstant** constant)
}
// else...
*constant = mConstants[mConstantBaseIndex-index];
*constant = &mConstants[mConstantBaseIndex+index];
return NS_OK;
}

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

@ -77,23 +77,23 @@ public:
T_INTERFACE_IS = 17 /* SPECIAL_BIT | 1 */
};
uint8 WordCount() const
uint8 WordCount() const
{
static uint8 word_table[] =
static uint8 word_table[] =
{
1, // T_I8
1, // T_I16
1, // T_I32
2, // T_I64
1, // T_U8
1, // T_U16
1, // T_U32
2, // T_U64
1, // T_FLOAT
1, // T_I8
1, // T_I16
1, // T_I32
2, // T_I64
1, // T_U8
1, // T_U16
1, // T_U32
2, // T_U64
1, // T_FLOAT
2, // T_DOUBLE
1, // T_BOOL
1, // T_CHAR
1 // T_WCHAR
1, // T_BOOL
1, // T_CHAR
1 // T_WCHAR
};
if(t & IS_POINTER)
return 1;
@ -202,7 +202,17 @@ private:
class nsXPCConstant
{
// XXX flesh out
public:
// simple ctor
nsXPCConstant(){}
nsXPCConstant(char* aName, const nsXPCVarient& aVal)
: mName(aName), mVal(aVal) {};
const char* GetName() const {return mName;}
const nsXPCVarient& GetValue() const {return mVal;}
private:
char* mName;
nsXPCVarient mVal;
};
class nsInterfaceInfo : public nsIInterfaceInfo
@ -233,11 +243,11 @@ private:
unsigned mMethodBaseIndex;
unsigned mMethodCount;
nsXPCMethodInfo** mMethods;
nsXPCMethodInfo* mMethods;
unsigned mConstantBaseIndex;
unsigned mConstantCount;
nsXPCConstant** mConstants;
nsXPCConstant* mConstants;
};
#endif /* xpcbogusii_h___ */

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

@ -133,7 +133,7 @@ nsXPCWrappedNativeClass::BuildMemberDescriptors()
desc = &mMembers[mMemberCount-1];
NS_ASSERTION(desc->id == id,"bad setter");
NS_ASSERTION(desc->category == XPCNativeMemberDescriptor::ATTRIB_RO,"bad setter");
desc->category == XPCNativeMemberDescriptor::ATTRIB_RW;
desc->category = XPCNativeMemberDescriptor::ATTRIB_RW;
desc->index2 = i;
}
else
@ -142,14 +142,36 @@ nsXPCWrappedNativeClass::BuildMemberDescriptors()
desc = &mMembers[mMemberCount++];
desc->id = id;
if(info->IsGetter())
desc->category == XPCNativeMemberDescriptor::ATTRIB_RO;
desc->category = XPCNativeMemberDescriptor::ATTRIB_RO;
else
desc->category == XPCNativeMemberDescriptor::METHOD;
desc->category = XPCNativeMemberDescriptor::METHOD;
desc->index = i;
}
}
// XXX do constants
for(i = 0; i < constCount; i++)
{
jsval idval;
jsid id;
XPCNativeMemberDescriptor* desc;
const nsXPCConstant* constant;
if(NS_FAILED(mInfo->GetConstant(i, &constant)))
return JS_FALSE;
idval = STRING_TO_JSVAL(JS_InternString(cx, constant->GetName()));
JS_ValueToId(cx, idval, &id);
if(!id)
{
NS_ASSERTION(0,"bad constant name");
return JS_FALSE;
}
NS_ASSERTION(!LookupMemberByID(id),"duplicate method/constant name");
desc = &mMembers[mMemberCount++];
desc->id = id;
desc->category = XPCNativeMemberDescriptor::CONSTANT;
desc->index = i;
}
return JS_TRUE;
}
@ -289,14 +311,48 @@ nsXPCWrappedNativeClass::SetDescriptorCounts(XPCNativeMemberDescriptor* desc)
}
}
// Win32 can't handle u64 to double conversion
#define JAM_DOUBLE_U64(v,d) (d = (jsdouble)(int64)v, DOUBLE_TO_JSVAL(&d))
#define JAM_DOUBLE(v,d) (d = (jsdouble)v, DOUBLE_TO_JSVAL(&d))
#define FIT_32(i,d) (INT_FITS_IN_JSVAL(i) ? INT_TO_JSVAL(i) : JAM_DOUBLE(i,d))
JSBool
nsXPCWrappedNativeClass::GetConstantAsJSVal(nsXPCWrappedNative* wrapper,
const XPCNativeMemberDescriptor* desc,
jsval* vp)
{
// XXX implement
return JS_FALSE;
const nsXPCConstant* constant;
NS_ASSERTION(desc->category == XPCNativeMemberDescriptor::CONSTANT,"bad type");
if(NS_FAILED(mInfo->GetConstant(desc->index, &constant)))
{
// XX fail silently?
*vp = JSVAL_NULL;
return JS_TRUE;
}
jsdouble d;
const nsXPCVarient& var = constant->GetValue();
switch(var.type)
{
case nsXPCType::T_I8 : *vp = INT_TO_JSVAL((int32)var.val.i8); break;
case nsXPCType::T_I16 : *vp = INT_TO_JSVAL((int32)var.val.i16); break;
case nsXPCType::T_I32 : *vp = FIT_32(var.val.i32,d); break;
case nsXPCType::T_I64 : *vp = JAM_DOUBLE(var.val.i64,d); break;
case nsXPCType::T_U8 : *vp = INT_TO_JSVAL((int32)var.val.u8); break;
case nsXPCType::T_U16 : *vp = INT_TO_JSVAL((int32)var.val.u16); break;
case nsXPCType::T_U32 : *vp = FIT_32(var.val.u32,d); break;
case nsXPCType::T_U64 : *vp = JAM_DOUBLE_U64(var.val.u64,d); break;
case nsXPCType::T_FLOAT : *vp = JAM_DOUBLE(var.val.f,d); break;
case nsXPCType::T_DOUBLE : *vp = DOUBLE_TO_JSVAL(&var.val.d); break;
case nsXPCType::T_BOOL : *vp = var.val.b?JSVAL_TRUE:JSVAL_FALSE; break;
case nsXPCType::T_CHAR : *vp = INT_TO_JSVAL((int32)var.val.c); break;
case nsXPCType::T_WCHAR : *vp = INT_TO_JSVAL((int32)var.val.wc); break;
default:
NS_ASSERTION(0, "bad type");
ReportError(desc, "invalid constant type");
}
return JS_TRUE;
}
void
@ -542,7 +598,11 @@ WrappedNative_getPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
const XPCNativeMemberDescriptor* desc = clazz->LookupMemberByID(id);
if(!desc)
return JS_FALSE;
{
// XXX silently fail when property not found?
*vp = JSVAL_VOID;
return JS_TRUE;
}
switch(desc->category)
{