1
0

JSAPI.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. // helper functions for IDispatch
  3. #define JSAPI_PARAM_INDEX(paramInfo, paramNumber) (paramInfo->cArgs - paramNumber)
  4. #define JSAPI_PARAM_EXISTS(paramInfo, paramNumber) (paramInfo->cArgs >= paramNumber)
  5. #define JSAPI_NUM_PARAMS(paramInfo) (paramInfo->cArgs)
  6. #define JSAPI_VERIFY_PARAMCOUNT(paramInfo, count) if (paramInfo->cArgs != count) return DISP_E_BADPARAMCOUNT
  7. #define JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(paramInfo, minParams, maxParams) if (paramInfo->cArgs < minParams || paramInfo->cArgs > maxParams) return DISP_E_BADPARAMCOUNT
  8. #define JSAPI_VERIFY_PARAMTYPE(paramInfo, paramNumber, paramType, argErr) if (paramInfo->rgvarg[paramInfo->cArgs - paramNumber].vt != paramType) { *argErr = paramInfo->cArgs - paramNumber; return DISP_E_TYPEMISMATCH; }
  9. #define JSAPI_VERIFY_PARAMTYPE_OPTIONAL(paramInfo, paramNumber, paramType, argErr) if (JSAPI_PARAM_EXISTS(paramInfo, paramNumber) && paramInfo->rgvarg[paramInfo->cArgs - paramNumber].vt != paramType) { *argErr = paramInfo->cArgs - paramNumber; return DISP_E_TYPEMISMATCH; }
  10. #define JSAPI_GETSTRING(str, paramInfo, paramNumber, argErr) switch(paramInfo->rgvarg[paramInfo->cArgs - paramNumber].vt) { case VT_BSTR: str = paramInfo->rgvarg[paramInfo->cArgs - paramNumber].bstrVal; break; default: *argErr = paramInfo->cArgs - paramNumber; return DISP_E_TYPEMISMATCH; }
  11. #define JSAPI_GETNUMBER_AS_STRING(str, buffer, paramInfo, paramNumber, argErr) \
  12. if (JSAPI_PARAM(paramInfo, paramNumber).vt == VT_I4) {\
  13. int val_int = JSAPI_PARAM(paramInfo, paramNumber).lVal;\
  14. StringCbPrintfW(buffer, sizeof(buffer), L"%d", val_int);\
  15. str = buffer;\
  16. } else if (JSAPI_PARAM(paramInfo, paramNumber).vt == VT_BSTR)\
  17. str = JSAPI_PARAM(paramInfo, paramNumber).bstrVal;\
  18. else {\
  19. if (argErr) *argErr = paramInfo->cArgs - paramNumber;\
  20. return DISP_E_TYPEMISMATCH;\
  21. }
  22. #define JSAPI_GETUNSIGNED_AS_NUMBER(num, paramInfo, paramNumber, argErr) \
  23. if (JSAPI_PARAM(paramInfo, paramNumber).vt == VT_I4) num = (UINT)JSAPI_PARAM(paramInfo, paramNumber).lVal;\
  24. else if (JSAPI_PARAM(paramInfo, paramNumber).vt == VT_BSTR) num = wcstoul(JSAPI_PARAM(paramInfo, paramNumber).bstrVal,0, 10); \
  25. else return DISP_E_TYPEMISMATCH;
  26. #define JSAPI_PARAM(paramInfo, paramNumber) (paramInfo->rgvarg[paramInfo->cArgs - paramNumber])
  27. #define JSAPI_PARAM_OPTIONAL(paramInfo, paramNumber, dispID, opt) (JSAPI_PARAM_EXISTS(paramInfo, paramNumber)?paramInfo->rgvarg[paramInfo->cArgs - paramNumber].##dispID:(opt))
  28. #define JSAPI_VERIFY_METHOD(flags) if (!(wFlags & DISPATCH_METHOD)) return DISP_E_MEMBERNOTFOUND
  29. #define JSAPI_INIT_RESULT(result, type) if (result) { VariantInit(result); V_VT(result) = type; }
  30. #define JSAPI_SET_RESULT(result, field, value) if (result) { (result)->field = value; }
  31. #define JSAPI_EMPTY_RESULT(result) if (result) { V_VT(result) = VT_EMPTY; }
  32. #define JSAPI_SET_VARIANT(result, macro, value) if (result) { macro(result) = value; }
  33. #define JSAPI_DISP_ENUMIFY(x) __jsapi__enum__ ## x