Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cJSObject.h
Go to the documentation of this file.
1 //
4 //
5 #ifndef _INC_cJSObject_H
6 #define _INC_cJSObject_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 
12 
13 // include this only locally to isolate it!
14 #ifdef _WIN32
15 #define XP_WIN
16 #elif defined(__linux__)
17 #define XP_UNIX
18 #endif
19 
20 // #define JS_THREADSAFE // must match DLL
21 // USE_JS_CUSTOM_THUNK USE_JS_CUSTOM_CALL may be defined.
22 #define STATIC_JS_API
23 
24 #include "../../Open/js_1_80/src/jsapi.h" // hide this stuff from the rest of the world.
25 
26 struct JSRuntime {}; // This is fully opaque to me.
27 struct JSContext {}; // This is fully opaque to me.
28 struct JSObject {}; // This is fully opaque to me.
29 
30 namespace GrayLib
31 {
32  class cXObject;
33 }
34 
35 namespace GrayJS
36 {
37  class cJSXContext;
38 
39  class cJSClass : public ::JSClass
40  {
43 
44  };
45 
46  class cJSObject : public ::JSObject
47  {
50 
51  public:
52 
53  };
54 
55  class cJSContext : public ::JSContext
56  {
59  public:
61  {
62  ::JS_DestroyContext(this); // NOTE: This calls js_GC()
63  }
64 
65  bool IsRunning()
66  {
67  return ::JS_IsRunning(this) != JS_FALSE;
68  }
70  {
71  return reinterpret_cast<cJSXContext*>(::JS_GetContextPrivate(this));
72  }
74  {
75  ::JS_SetContextPrivate(this, p);
76  }
77 
78  // An object JSObject in a context JSContext.
79 
80  cJSObject* NewObject(JSClass* pJSClass, cJSObject* pjsObjProto, cJSObject* pjsObjParent)
81  {
82  return static_cast<cJSObject*>(::JS_NewObject(this, pJSClass, pjsObjProto, pjsObjParent));
83  }
84 
86  {
87  // Get the attached cXObject
88  return reinterpret_cast<GrayLib::cXObject*>(::JS_GetPrivate(this, pjsObj));
89  }
90  void SetPrivate(cJSObject* pjsObj, GrayLib::cXObject* pXObj)
91  {
92  ::JS_SetPrivate(this, pjsObj, pXObj);
93  }
94 
96  {
97  return static_cast<cJSObject*>(::JS_GetPrototype(this, pjsObjGlobal));
98  }
99  JSClass* GetClass(cJSObject* pjsObjProto)
100  {
101  ASSERT(pjsObjProto != nullptr);
102  return JS_GET_CLASS(this, pjsObjProto);
103  }
104 
105  bool LockGCThing(cJSObject* pjsObjProto)
106  {
107  // == JS_TRUE
108  return ::JS_LockGCThing(this, pjsObjProto) != JS_FALSE;
109  }
110  bool UnlockGCThing(cJSObject* pjsObjProto)
111  {
112  return ::JS_UnlockGCThing(this, pjsObjProto) != JS_FALSE;
113  }
114  };
115 
116  class cJSRuntime : public ::JSRuntime
117  {
120 
121  public:
123  {
124  ::JS_DestroyRuntime(this);
125  }
126 
127  // ::JS_SetRuntimePrivate
128  // ::JS_GetRuntimePrivate
129 
130  cJSContext* NewContext(size_t nStackSize)
131  {
132  // k_JSX_STACK_CHUNK_SIZE
133  return static_cast<cJSContext*>(::JS_NewContext(this, nStackSize));
134  }
135  };
136 
137 }
138 
139 #endif
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cJSObject.h:40
Definition: cJSObject.h:56
void SetContextPrivate(cJSXContext *p)
Definition: cJSObject.h:73
cJSObject * NewObject(JSClass *pJSClass, cJSObject *pjsObjProto, cJSObject *pjsObjParent)
Definition: cJSObject.h:80
JSClass * GetClass(cJSObject *pjsObjProto)
Definition: cJSObject.h:99
bool IsRunning()
Definition: cJSObject.h:65
void SetPrivate(cJSObject *pjsObj, GrayLib::cXObject *pXObj)
Definition: cJSObject.h:90
cJSXContext * GetContextPrivate()
Definition: cJSObject.h:69
bool UnlockGCThing(cJSObject *pjsObjProto)
Definition: cJSObject.h:110
bool LockGCThing(cJSObject *pjsObjProto)
Definition: cJSObject.h:105
GrayLib::cXObject * GetPrivate(cJSObject *pjsObj)
Definition: cJSObject.h:85
void DestroyContext()
Definition: cJSObject.h:60
cJSObject * GetPrototype(cJSObject *pjsObjGlobal)
Definition: cJSObject.h:95
Definition: cJSObject.h:47
Definition: cJSObject.h:117
cJSContext * NewContext(size_t nStackSize)
Definition: cJSObject.h:130
void DestroyRuntime()
Definition: cJSObject.h:122
Definition: cJSXContext.h:16
Definition: cXObject.h:78
Definition: cJSObject.h:36
Definition: cMesh.h:22
class __DECL_IMPORT cXObject
Definition: cXObject.h:33
Definition: cJSObject.h:27
Definition: cJSObject.h:28
Definition: cJSObject.h:26