Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSUtil.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 // included from SpiderMonkey.h
19 
20 extern JSBool jsu_report_param_error(JSContext* cx, jsval* vp);
21 
22 
23 // consistent argc checking for normal function wrappers: reports an
24 // error via JS and returns if number of parameters is incorrect.
25 // .. require exact number (most common case)
26 #define JSU_REQUIRE_PARAMS(exact_number)\
27  if(argc != exact_number)\
28  return jsu_report_param_error(cx, vp);
29 // .. require 0 params (avoids L4 warning "unused argv param")
30 #define JSU_REQUIRE_NO_PARAMS()\
31  if(argc != 0)\
32  return jsu_report_param_error(cx, vp);
33 // .. require a certain range (e.g. due to optional params)
34 #define JSU_REQUIRE_PARAM_RANGE(min_number, max_number)\
35  if(!(min_number <= argc && argc <= max_number))\
36  return jsu_report_param_error(cx, vp);
37 // .. require at most a certain count
38 #define JSU_REQUIRE_MAX_PARAMS(max_number)\
39  if(argc > max_number)\
40  return jsu_report_param_error(cx, vp);
41 // .. require at least a certain count (rarely needed)
42 #define JSU_REQUIRE_MIN_PARAMS(min_number)\
43  if(argc < min_number)\
44  return jsu_report_param_error(cx, vp);
45 
46 // same as JSU_REQUIRE_PARAMS, but used from C++ functions that are
47 // a bit further removed from SpiderMonkey, i.e. return a
48 // C++ bool indicating success, and not taking an rval param.
49 #define JSU_REQUIRE_PARAMS_CPP(exact_number)\
50  if(argc != exact_number)\
51  {\
52  jsu_report_param_error(cx, 0);\
53  return false;\
54  }
55 
56 
57 #define JSU_ASSERT(expr, msg)\
58 STMT(\
59  if(!(expr))\
60  {\
61  JS_ReportError(cx, msg);\
62  return JS_FALSE;\
63  }\
64 )
JSBool jsu_report_param_error(JSContext *cx, jsval *vp)
Definition: JSUtil.cpp:22