Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

JSClass.H

00001 /* $Id: JSClass.H,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * 
00004  * Copyright (C) 1999  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 #ifndef JSCLASS_H
00023 #define JSCLASS_H
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <SFW/Object.H>
00028 
00029 extern "C" {
00030 #include <js.h>
00031 }
00032 
00033 namespace SFW {
00034     class JavaScriptClass : public Object {
00035     string name;
00036 
00037     friend bool JavaScriptInterpreter::define(JavaScriptClass& jsc);
00038     
00039     protected:
00040     JSClassPtr jsc;
00041 
00042         JavaScriptType instantiate(JavaScriptInterpreter &jsi,
00043                                    void *ctx,
00044                                    JSFreeProc destructor);
00045     
00046     public:
00047         TYPE_INFO(SFW::JavaScriptClass, SFW::Object);
00048         
00049     JavaScriptClass(const string& s);
00050         ~JavaScriptClass();
00051 
00052     const string& getName(void) const;
00053     };
00054 
00055 }
00056 
00057 /* Horrible #define magic to get around some libjs and C++
00058    limitations */
00059 
00060 /*
00061  * Usage (in constructor):
00062  *
00063  * JSCLASS_DEF;
00064  *
00065  */
00066 #define JSCLASS_DEF jsc = js_class_create((void*)this, (JSFreeProc)0, 1, \
00067                       (JSConstructor)constructCB)
00068 
00069 /*
00070  * Usage:
00071  *
00072  * JSCONSTRUCTOR(void **  context, JSTArray &argv,
00073  *               string &error) { ... }
00074  *
00075  */
00076 
00077 #define JSCONSTRUCTOR static JSMethodResult                    \
00078 constructCB (JSClassPtr cls, JSInterpPtr interp, int argc,     \
00079          JSType *argv, void **context,                     \
00080          JSFreeProc *context_destructor,                   \
00081          char *error_return) {                             \
00082     SFW::JSTArray jstav(argc);                                 \
00083     SFW::JavaScriptType ret;                                   \
00084     string error;                                              \
00085     bool res;                                                  \
00086                                                            \
00087     for(int i = 0; i < argc; i++)                              \
00088     jstav[i] = SFW::JavaScriptType(*(JSNode*)&argv[i]);    \
00089                                                            \
00090     res = construct(context, jstav, error);                    \
00091                                                                \
00092     *context_destructor = (JSFreeProc)destructCB;              \
00093     error.copy(error_return, error.length());                  \
00094     error_return[error.length()] = (char)0;                    \
00095                                                            \
00096     return (res ? JS_OK : JS_ERROR);                           \
00097 }                                                              \
00098                                                                \
00099 static bool construct
00100 
00101 /*** out-of-line version ***/
00102 
00103 #define JSCONSTRUCTOR_DECL static JSMethodResult               \
00104 constructCB (JSClassPtr cls, JSInterpPtr interp, int argc,     \
00105          JSType *argv, void **context,                     \
00106          JSFreeProc *context_destructor,                   \
00107          char *error_return);                              \
00108                                                                \
00109 static bool construct(void **  context, SFW::JSTArray &argv,   \
00110                       string &error);
00111 
00112 #define JSCONSTRUCTOR_IMPL(class) JSMethodResult               \
00113 class::constructCB (JSClassPtr cls, JSInterpPtr interp,        \
00114             int argc,                                  \
00115             JSType *argv, void **context,              \
00116             JSFreeProc *context_destructor,            \
00117             char *error_return) {                      \
00118     SFW::JSTArray jstav(argc);                                 \
00119     SFW::JavaScriptType ret;                                   \
00120     string error;                                              \
00121     bool res;                                                  \
00122                                                            \
00123     for(int i = 0; i < argc; i++)                              \
00124     jstav[i] = SFW::JavaScriptType(*(JSNode*)&argv[i]);    \
00125                                                            \
00126     res = construct(context, jstav, error);                    \
00127                                                                \
00128     *context_destructor = (JSFreeProc)destructCB;              \
00129     error.copy(error_return, error.length());                  \
00130     error_return[error.length()] = (char)0;                    \
00131                                                            \
00132     return (res ? JS_OK : JS_ERROR);                           \
00133 }                                                              \
00134                                                                \
00135 bool class::construct
00136 
00137 /*
00138  * Usage:
00139  *
00140  * JSDESTRUCTOR(void *context) { ... }
00141  *
00142  */
00143 
00144 #define JSDESTRUCTOR static void destructCB
00145 
00146 /*** out-of-line version ***/
00147 #define JSDESTRUCTOR_DECL static void destructCB(void *context);
00148 #define JSDESTRUCTOR_IMPL(class) void class::destructCB
00149 
00150 
00151 /*
00152  * Usage:
00153  *
00154  * JSMETHOD(name) (void *context, SFW::JSTArray &argv,
00155  *                 SFW::JavaScriptType& ret, string &error) { ... }
00156  *
00157  */
00158 
00159 #define JSMETHOD(x) static JSMethodResult                      \
00160 x ## MCB(JSClassPtr cls, void * context, JSInterpPtr interp,   \
00161          int argc, JSType * argv, JSType * return_result,      \
00162          char * error_return) {                                \
00163     SFW::JSTArray jstav(argc);                                 \
00164     SFW::JavaScriptType ret;                                   \
00165     string error;                                              \
00166     bool res;                                                  \
00167                                                            \
00168     for(int i = 0; i < argc; i++)                              \
00169     jstav[i] = SFW::JavaScriptType(*(JSNode*)&argv[i]);    \
00170                                                            \
00171     res = x ## Method(context, jstav, ret, error);             \
00172                                                            \
00173     error.copy(error_return, error.length());                  \
00174     error_return[error.length()] = (char)0;                    \
00175     ret.copyToI(return_result, interp);                        \
00176                                                            \
00177     return (res ? JS_OK : JS_ERROR);                           \
00178 }                                                              \
00179                                                                \
00180 static bool                                                    \
00181 x ## Method
00182 
00183 /*** out-of-line version ***/
00184 
00185 #define JSMETHOD_DECL(x) static JSMethodResult                 \
00186 x ## MCB(JSClassPtr cls, void * context, JSInterpPtr interp,   \
00187          int argc, JSType * argv, JSType * return_result,      \
00188          char * error_return);                                 \
00189                                                                \
00190 static bool                                                    \
00191 x ## Method(void *context, SFW::JSTArray &argv,                \
00192         SFW::JavaScriptType& ret, string &error);
00193 
00194 #define JSMETHOD_IMPL(class, name)                             \
00195 JSMethodResult                                                 \
00196 class::name ## MCB(JSClassPtr cls, void * context,             \
00197            JSInterpPtr interp, int argc,               \
00198            JSType * argv, JSType * return_result,      \
00199            char * error_return) {                      \
00200     SFW::JSTArray jstav(argc);                                 \
00201     SFW::JavaScriptType ret;                                   \
00202     string error;                                              \
00203     bool res;                                                  \
00204                                                            \
00205     for(int i = 0; i < argc; i++)                              \
00206     jstav[i] = SFW::JavaScriptType(*(JSNode*)&argv[i]);    \
00207                                                            \
00208     res = name ## Method(context, jstav, ret, error);          \
00209                                                            \
00210     error.copy(error_return, error.length());                  \
00211     error_return[error.length()] = (char)0;                    \
00212     ret.copyToI(return_result, interp);                        \
00213                                                            \
00214     return (res ? JS_OK : JS_ERROR);                           \
00215 }                                                              \
00216                                                                \
00217 bool class::name ## Method
00218 
00219 
00220 /*
00221  * Usage:
00222  *
00223  * JSPROP(name);
00224  *
00225  * Note: This needs followed by JSPROP_SET and JSPROP_GET
00226  */
00227 
00228 #define JSPROP(x) static JSMethodResult                        \
00229 x ## PCB(JSClassPtr cls, void * context, JSInterpPtr interp,   \
00230          int setp, JSType * value, char * error_return) {      \
00231     SFW::JavaScriptType jst;                                   \
00232     string         error;                                      \
00233     bool           res;                                        \
00234                                                                \
00235     if(setp) {                                                 \
00236     jst = *(JSNode*)value;                                 \
00237     res = x ## SetProp(context, jst, error);               \
00238     } else {                                                   \
00239     res = x ## GetProp(context, jst, error);               \
00240     }                                                          \
00241                                                                \
00242     error.copy(error_return, error.length());                  \
00243     error_return[error.length()] = (char)0;                    \
00244     jst.copyToI(value, interp);                                \
00245                                                                \
00246     return (res ? JS_OK : JS_ERROR);                           \
00247 }
00248 
00249 /*** out-of-line version ***/
00250 
00251 #define JSPROP_DECL(x) static JSMethodResult                   \
00252 x ## PCB(JSClassPtr cls, void * context, JSInterpPtr interp,   \
00253          int setp, JSType * value, char * error_return);       \
00254                                                                \
00255 static bool x ## SetProp(void * context, SFW::JavaScriptType& t,    \
00256              string& error);                       \
00257                                                                \
00258 static bool x ## GetProp(void * context, SFW::JavaScriptType& t,    \
00259              string& error);
00260 
00261 /*
00262  * Usage:
00263  *
00264  * JSPROP_SET(name) (void * context, SFW::JavaScriptType& t,
00265  *                   string& error) { ... }
00266  *
00267  */
00268 
00269 #define JSPROP_SET(x) static bool x ## SetProp
00270 
00271 /*** out-of-line ***/
00272 #define JSPROP_SET_IMPL(class, x) JSMethodResult               \
00273 class::x ## PCB(JSClassPtr cls, void * context,                \
00274         JSInterpPtr interp,                            \
00275         int setp, JSType * value, char * error_return) \
00276 {                                                              \
00277     SFW::JavaScriptType jst;                                   \
00278     string         error;                                      \
00279     bool           res;                                        \
00280                                                                \
00281     if(setp) {                                                 \
00282     jst = *(JSNode*)value;                                 \
00283     res = x ## SetProp(context, jst, error);               \
00284     } else {                                                   \
00285     res = x ## GetProp(context, jst, error);               \
00286     }                                                          \
00287                                                                \
00288     error.copy(error_return, error.length());                  \
00289     error_return[error.length()] = (char)0;                    \
00290     jst.copyToI(value, interp);                                \
00291                                                                \
00292     return (res ? JS_OK : JS_ERROR);                           \
00293 }                                                              \
00294                                                                \
00295 bool class::x ## SetProp
00296 
00297 /*
00298  * Usage:
00299  *
00300  * JSPROP_GET(name) (void * context, SFW::JavaScriptType& t,
00301  *                   string& error) { ... }
00302  *
00303  */
00304                 
00305 #define JSPROP_GET(x) static bool x ## GetProp
00306 
00307 /*** out-of-line ***/
00308 #define JSPROP_GET_IMPL(class, x) bool class::x ## GetProp
00309 
00310 /*
00311  * Usage (in constructor):
00312  *
00313  * JSMETHOD_ADD(name, flags);
00314  * JSPROP_ADD(name, flags);
00315  *
00316  * Note: flags is JS_CF_STATIC or NULL.
00317  *
00318  */
00319 
00320 #define JSMETHOD_ADD(x, y) js_class_define_method(jsc, #x, y, x ## MCB);
00321 #define JSPROP_ADD(x, y)   js_class_define_property(jsc, #x, y, x ## PCB);
00322 
00323 #endif /* JSCLASS_H */

Generated at Tue Jan 2 15:38:33 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000