All API and Language binding described in this documents are subject to change. Pending API changes are filed as bug reports in rbXPCOM's bug tracking system.
Binding described in this section are applied to both using and implementing XPCOM objects.
An idl interface is mapped to a Ruby module. XPCOM.interface method provides access to these modules. The IID of the interface can be obtained from the module by iid method.
nsIRegistory = XPCOM.interface("nsIRegistry")
iid = XPCOM::ID.new("{5D41A440-8E37-11d2-8059-00600811A9C3}")
p iid == nsIRegistory.iid #=> true
# XPCOM.interface acccept IID, too.
p nsIRegistory.equal?(XPCOM.interface(iid)) #=> true
|
The module provides the constants defined in the interface.
Since Ruby does not have the concept of out parameter, in rbXPCOM, values should be returned via out params are returned as return value of the method. If a method has multiple out parameters, they are returned in an array.
# PRInt16 AddPRInt16( in PRInt16 a,
# in PRInt16 b,
# inout PRInt16 c,
# out PRInt16 neg);
ret, c, neg = test.AddPRInt16(a, b, c)
|
Parameter passing and returning follow below rules:
in parameters are passed as arguments.
out parameters are not included in the arguments list. The values are returned from the method.
inout are passed like in, and returned like out.
When multiple values are returned, the retval of the method comes first, followed by other out params in the order they appear in the idl.
One more example.
In xpidl, size_is attribute is used to specify the size of array or non-zero terminated string(wstring). In Ruby, explicitly passing or receiving the size of these objects are redundant, because objects know their size. To remove this redundancy, rbXPCOM hides the size parameters from Ruby code.
# void EchoArray1( in PRUint32 i_size,
# [array, size_is(i_size)] in PRInt16 i,
# out PRUint32 o_size,
# [array, size_is(o_size)] out PRInt16 o);
o = test.EchoArray1(i)
|
If the size parameter is passed to a method without accompanying sized object, the size parameter appears in the argument list. Following method illustrates this coner case.
Table 1. Data Type Conversion
| XPTTypeDescriptor Tags | type in Ruby |
|---|---|
| TD_INT8 | Integer |
| TD_INT16 | Integer |
| TD_INT32 | Integer |
| TD_INT64 | Integer |
| TD_UINT8 | Integer |
| TD_UINT16 | Integer |
| TD_UINT32 | Integer |
| TD_UINT64 | Integer |
| TD_FLOAT | Float |
| TD_DOUBLE | Float |
| TD_BOOL | boolean |
| TD_CHAR | Integer |
| TD_WCHAR | Integer |
| TD_VOID | - |
| TD_PNSIID | XPCOM::ID |
| TD_DOMSTRING | String |
| TD_PSTRING | String |
| TD_PWSTRING | String |
| TD_INTERFACE_TYPE | object |
| TD_INTERFACE_IS_TYPE | object |
| TD_ARRAY | Array |
| TD_PSTRING_SIZE_IS | String |
| TD_PWSTRING_SIZE_IS | String |
XPCOM object is mapped to a Ruby object that respond to the methods defined by the interface. Class of that object is not significant.
Pointer types (string, wstring, iid, interface and array) can be nil, which is mapped to NULL in the native code. nil is not allowed for DOMString, though.
Boolean value conversion follows the 'normal' boolean interpretation of C++ and Ruby.
Unicode string is not converted automatically. Helper functions XPCOM::wstr and XPCOM::sstr are provided to cast ASCII to UCS2 and vice versa, respectably. Ruby code must call them explicitly.