JSAPI_ObjectArray.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "JSAPI_ObjectArray.h"
  2. #include "JSAPI.h"
  3. #include <strsafe.h>
  4. JSAPI::ObjectArray::ObjectArray()
  5. {
  6. refCount = 1;
  7. }
  8. JSAPI::ObjectArray::~ObjectArray()
  9. {
  10. for ( IDispatch *l_object : objects )
  11. l_object->Release();
  12. }
  13. enum
  14. {
  15. OBJ_ARRAY_DISP_LENGTH,
  16. OBJ_ARRAY_NUM_DISP,
  17. };
  18. HRESULT JSAPI::ObjectArray::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  19. {
  20. bool unknowns = false;
  21. for (unsigned int i = 0;i != cNames;i++)
  22. {
  23. rgdispid[i] = DISPID_UNKNOWN;
  24. if (!wcscmp(rgszNames[i], L"length"))
  25. rgdispid[i]=OBJ_ARRAY_DISP_LENGTH;
  26. else
  27. {
  28. if (rgszNames[i][0] >= L'0' && rgszNames[i][0] <= L'9')
  29. rgdispid[i]=_wtoi(rgszNames[i]) + OBJ_ARRAY_NUM_DISP;
  30. else
  31. unknowns=true;
  32. }
  33. }
  34. if (unknowns)
  35. return DISP_E_UNKNOWNNAME;
  36. else
  37. return S_OK;
  38. }
  39. HRESULT JSAPI::ObjectArray::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  40. {
  41. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  42. switch(dispid)
  43. {
  44. case OBJ_ARRAY_DISP_LENGTH:
  45. {
  46. JSAPI_INIT_RESULT(pvarResult, VT_I4);
  47. JSAPI_SET_RESULT(pvarResult, lVal, (LONG)objects.size());
  48. return S_OK;
  49. }
  50. default:
  51. {
  52. if (dispid < OBJ_ARRAY_NUM_DISP)
  53. return DISP_E_MEMBERNOTFOUND;
  54. size_t index = (size_t)dispid - OBJ_ARRAY_NUM_DISP;
  55. if (index>=objects.size())
  56. return DISP_E_MEMBERNOTFOUND;
  57. objects[index]->AddRef();
  58. JSAPI_INIT_RESULT(pvarResult, VT_DISPATCH);
  59. JSAPI_SET_RESULT(pvarResult, pdispVal, objects[index]);
  60. return S_OK;
  61. }
  62. }
  63. }
  64. HRESULT JSAPI::ObjectArray::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  65. {
  66. return E_NOTIMPL;
  67. }
  68. HRESULT JSAPI::ObjectArray::GetTypeInfoCount(unsigned int FAR * pctinfo)
  69. {
  70. return E_NOTIMPL;
  71. }
  72. STDMETHODIMP JSAPI::ObjectArray::QueryInterface(REFIID riid, PVOID *ppvObject)
  73. {
  74. if (!ppvObject)
  75. return E_POINTER;
  76. else if (IsEqualIID(riid, IID_IDispatch))
  77. *ppvObject = (IDispatch *)this;
  78. else if (IsEqualIID(riid, IID_IUnknown))
  79. *ppvObject = this;
  80. else if (IsEqualIID(riid, IID_IDispatchEx))
  81. *ppvObject = (IDispatchEx *)this;
  82. else
  83. {
  84. *ppvObject = NULL;
  85. return E_NOINTERFACE;
  86. }
  87. AddRef();
  88. return S_OK;
  89. }
  90. ULONG JSAPI::ObjectArray::AddRef(void)
  91. {
  92. return InterlockedIncrement(&refCount);
  93. }
  94. ULONG JSAPI::ObjectArray::Release(void)
  95. {
  96. LONG lRef = InterlockedDecrement(&refCount);
  97. if (lRef == 0) delete this;
  98. return lRef;
  99. }
  100. void JSAPI::ObjectArray::AddObject(IDispatch *obj)
  101. {
  102. obj->AddRef();
  103. objects.push_back(obj);
  104. }
  105. HRESULT JSAPI::ObjectArray::GetDispID(BSTR bstrName, DWORD grfdex, DISPID *pid)
  106. {
  107. if (!wcscmp(bstrName, L"length"))
  108. *pid=OBJ_ARRAY_DISP_LENGTH;
  109. else
  110. {
  111. if (bstrName[0] >= L'0' && bstrName[0] <= L'9')
  112. *pid=_wtoi(bstrName) + OBJ_ARRAY_NUM_DISP;
  113. else
  114. return DISP_E_MEMBERNOTFOUND;
  115. }
  116. return S_OK;
  117. }
  118. HRESULT JSAPI::ObjectArray::InvokeEx(
  119. /* [in] */ DISPID id,
  120. /* [in] */ LCID lcid,
  121. /* [in] */ WORD wFlags,
  122. /* [in] */ DISPPARAMS *pdp,
  123. /* [out] */ VARIANT *pvarRes,
  124. /* [out] */ EXCEPINFO *pei,
  125. /* [unique][in] */ IServiceProvider *pspCaller)
  126. {
  127. JSAPI_VERIFY_PARAMCOUNT(pdp, 0);
  128. switch(id)
  129. {
  130. case OBJ_ARRAY_DISP_LENGTH:
  131. {
  132. JSAPI_INIT_RESULT(pvarRes, VT_I4);
  133. JSAPI_SET_RESULT(pvarRes, lVal, (LONG)objects.size());
  134. return S_OK;
  135. }
  136. default:
  137. {
  138. if (id < OBJ_ARRAY_NUM_DISP)
  139. return DISP_E_MEMBERNOTFOUND;
  140. size_t index = (size_t)id - OBJ_ARRAY_NUM_DISP;
  141. if (index>=objects.size())
  142. return DISP_E_MEMBERNOTFOUND;
  143. objects[index]->AddRef();
  144. JSAPI_INIT_RESULT(pvarRes, VT_DISPATCH);
  145. JSAPI_SET_RESULT(pvarRes, pdispVal, objects[index]);
  146. return S_OK;
  147. }
  148. }
  149. }
  150. HRESULT JSAPI::ObjectArray::DeleteMemberByName(
  151. /* [in] */ BSTR bstrName,
  152. /* [in] */ DWORD grfdex)
  153. {
  154. return E_NOTIMPL;
  155. }
  156. HRESULT JSAPI::ObjectArray::DeleteMemberByDispID(
  157. /* [in] */ DISPID id)
  158. {
  159. return E_NOTIMPL;
  160. }
  161. HRESULT JSAPI::ObjectArray::GetMemberProperties(
  162. /* [in] */ DISPID id,
  163. /* [in] */ DWORD grfdexFetch,
  164. /* [out] */ DWORD *pgrfdex)
  165. {
  166. return E_NOTIMPL;
  167. }
  168. HRESULT JSAPI::ObjectArray::GetMemberName(
  169. /* [in] */ DISPID id,
  170. /* [out] */ BSTR *pbstrName)
  171. {
  172. if (id >= OBJ_ARRAY_NUM_DISP)
  173. {
  174. wchar_t temp[64] = {0};
  175. StringCbPrintfW(temp, sizeof(temp), L"%d", id-OBJ_ARRAY_NUM_DISP);
  176. *pbstrName = SysAllocString(temp);
  177. return S_OK;
  178. }
  179. return E_NOTIMPL;
  180. }
  181. HRESULT JSAPI::ObjectArray::GetNextDispID(
  182. /* [in] */ DWORD grfdex,
  183. /* [in] */ DISPID id,
  184. /* [out] */ DISPID *pid)
  185. {
  186. if (grfdex == fdexEnumDefault)
  187. {
  188. if (id == DISPID_UNKNOWN)
  189. {
  190. if (objects.empty())
  191. return S_FALSE;
  192. else
  193. {
  194. *pid = OBJ_ARRAY_NUM_DISP;
  195. return S_OK;
  196. }
  197. }
  198. else if (id < OBJ_ARRAY_NUM_DISP)
  199. {
  200. return S_FALSE;
  201. }
  202. else
  203. {
  204. size_t index = (id - OBJ_ARRAY_NUM_DISP) + 1;
  205. if (index >= objects.size())
  206. {
  207. return S_FALSE;
  208. }
  209. else
  210. {
  211. *pid = OBJ_ARRAY_NUM_DISP + (LONG)index;
  212. return S_OK;
  213. }
  214. }
  215. }
  216. return E_NOTIMPL;
  217. }
  218. HRESULT JSAPI::ObjectArray::GetNameSpaceParent(
  219. /* [out] */ IUnknown **ppunk)
  220. {
  221. return E_NOTIMPL;
  222. }