1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2010 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef WT_JAVASCRIPT_PREAMBLE_H_
8 #define WT_JAVASCRIPT_PREAMBLE_H_
9 
10 #include <Wt/WObject.h>
11 
12 #define WT_JS(...) #__VA_ARGS__
13 
14 namespace Wt {
15 #ifdef WT_TARGET_JAVA
16 /*! \brief Enumeration for a JavaScript object type.
17  *
18  * This is an internal %Wt type.
19  */
20 #endif
21 enum JavaScriptObjectType {
22   JavaScriptFunction,     // called with this == WT
23   JavaScriptConstructor,
24   JavaScriptObject,
25   JavaScriptPrototype
26 };
27 
28 #ifdef WT_TARGET_JAVA
29 /*! \brief Enumeration for a JavaScript object scope.
30  *
31  * This is an internal %Wt type.
32  */
33 #endif
34 enum JavaScriptScope {
35   ApplicationScope,
36   WtClassScope
37 };
38 
39 #ifdef WT_TARGET_JAVA
40 /*! \brief Javascript preamble.
41  *
42  * This is an internal %Wt type.
43  */
44 #endif
45 class WT_API WJavaScriptPreamble
46 {
47 public:
48   WJavaScriptPreamble(JavaScriptScope scope, JavaScriptObjectType type,
49 		      const char *name, const char *src);
50 
51   JavaScriptScope scope;
52   JavaScriptObjectType type;
53   const char *name, *src;
54 };
55 
56 }
57 
58 #ifndef WT_DEBUG_JS
59 
60 #define WT_DECLARE_WT_MEMBER(i, type, name, ...)			\
61   namespace {								\
62   using namespace Wt;							\
63   WJavaScriptPreamble wtjs##i() {					\
64     return WJavaScriptPreamble(WtClassScope, type, name, #__VA_ARGS__); \
65   }									\
66   }
67 
68 #define WT_DECLARE_WT_MEMBER_BIG(i, type, name, ...)			\
69   namespace {								\
70   using namespace Wt;							\
71   WJavaScriptPreamble wtjs##i() {					\
72     return WJavaScriptPreamble(WtClassScope, type, name, #__VA_ARGS__); \
73   }									\
74   }
75 
76 #define WT_DECLARE_APP_MEMBER(i, type, name, ...)			\
77   namespace {								\
78   using namespace Wt;							\
79   WJavaScriptPreamble appjs##i() {					\
80     return WJavaScriptPreamble(ApplicationScope, type, name, #__VA_ARGS__); \
81   }									\
82   }
83 
84 #define LOAD_JAVASCRIPT(app, jsFile, name, jsi)	\
85   app->loadJavaScript(jsFile, jsi())
86 
87 #else // !WT_DEBUG_JS
88 
89 #define LOAD_JAVASCRIPT(app, jsFile, name, jsi)	\
90   app->loadJavaScript(jsFile)
91 
92 #endif // WT_DEBUG_JS
93 
94 #endif // WT_JAVASCRIPT_PREAMBLE_H_
95