Skip to content

Commit 59ca668

Browse files
committed
Enhance Plugin Template Naming Conventions
1 parent d6b4ddc commit 59ca668

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

share/template/classPlugin.c

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#include <jse.h>
1717

1818
// this definition just makes it easy to change the class name
19-
#define CLASSNAME "Plugin"
19+
#define PLUGIN_CLASS_NAME "Plugin"
2020

2121
JSClass PluginClass;
2222

23-
static void ObjectInitialize(JSContext ctx, JSObject object)
23+
static void PluginObjectInitialize(JSContext ctx, JSObject object)
2424
{
2525
/*
2626
Unlike the other object call backs, the initialize callback is called on
@@ -30,8 +30,9 @@ static void ObjectInitialize(JSContext ctx, JSObject object)
3030
g_print("Called Plugin Object Initialize\n");
3131
}
3232

33-
static void ObjectFinalize(JSObject object)
34-
{ g_print("Called Plugin Object Finalize\n");
33+
static void PluginObjectFinalize(JSObject object)
34+
{
35+
g_print("Called Plugin Object Finalize\n");
3536
/*
3637
The finalize callback is called on the most derived class first, and the
3738
least derived class (the parent class) last.
@@ -43,8 +44,9 @@ static void ObjectFinalize(JSObject object)
4344
*/
4445
}
4546

46-
static bool ObjectHasProperty (JSContext ctx, JSObject object, JSString id)
47-
{ g_print("Called Plugin Object Has Property\n");
47+
static bool PluginObjectHasProperty (JSContext ctx, JSObject object, JSString id)
48+
{
49+
g_print("Called Plugin Object Has Property\n");
4850
/*
4951
If this function returns false, the hasProperty request forwards to
5052
object's statically declared properties, then its parent class chain
@@ -63,8 +65,9 @@ static bool ObjectHasProperty (JSContext ctx, JSObject object, JSString id)
6365
return false;
6466
}
6567

66-
static JSValue ObjectGetProperty(JSContext ctx, JSObject object, JSString id, JSValue * exception)
67-
{ g_print("Called Plugin Object Get Property\n");
68+
static JSValue PluginObjectGetProperty(JSContext ctx, JSObject object, JSString id, JSValue * exception)
69+
{
70+
g_print("Called Plugin Object Get Property\n");
6871
/*
6972
If this function returns NULL, the get request forwards to object's
7073
statically declared properties, then its parent class chain (which
@@ -73,8 +76,9 @@ static JSValue ObjectGetProperty(JSContext ctx, JSObject object, JSString id, JS
7376
return NULL;
7477
}
7578

76-
static bool ObjectSetProperty (JSContext ctx, JSObject object, JSString id, JSValue value, JSValue * exception)
77-
{ g_print("Called Plugin Object Set Property\n");
79+
static bool PluginObjectSetProperty (JSContext ctx, JSObject object, JSString id, JSValue value, JSValue * exception)
80+
{
81+
g_print("Called Plugin Object Set Property\n");
7882
/*
7983
If this function returns false, the set request forwards to object's
8084
statically declared properties, then its parent class chain (which
@@ -83,8 +87,9 @@ static bool ObjectSetProperty (JSContext ctx, JSObject object, JSString id, JSVa
8387
return false;
8488
}
8589

86-
static bool ObjectDeleteProperty (JSContext ctx, JSObject object, JSString id, JSValue * exception)
87-
{ g_print("Called Plugin Object Delete Property\n");
90+
static bool PluginObjectDeleteProperty (JSContext ctx, JSObject object, JSString id, JSValue * exception)
91+
{
92+
g_print("Called Plugin Object Delete Property\n");
8893
/*
8994
If this function returns false, the delete request forwards to object's
9095
statically declared properties, then its parent class chain (which
@@ -93,8 +98,9 @@ static bool ObjectDeleteProperty (JSContext ctx, JSObject object, JSString id, J
9398
return false;
9499
}
95100

96-
static void ObjectGetPropertyNames (JSContext ctx, JSObject object, JSPropertyNameAccumulator names)
97-
{ g_print("Called Plugin Object Get Property Names\n");
101+
static void PluginObjectGetPropertyNames (JSContext ctx, JSObject object, JSPropertyNameAccumulator names)
102+
{
103+
_print("Called Plugin Object Get Property Names\n");
98104
/*
99105
Use JSPropertyNameAccumulatorAddName to add property names to accumulator.
100106
A class's getPropertyNames callback only needs to provide the names of
@@ -105,8 +111,9 @@ static void ObjectGetPropertyNames (JSContext ctx, JSObject object, JSPropertyNa
105111
*/
106112
}
107113

108-
static JSValue ObjectCallAsFunction (JSContext ctx, JSObject function, JSObject this, size_t argc, const JSValue argv[], JSValue * exception)
109-
{ g_print("Plugin Object Called as Function\n");
114+
static JSValue PluginObjectCallAsFunction (JSContext ctx, JSObject function, JSObject this, size_t argc, const JSValue argv[], JSValue * exception)
115+
{
116+
g_print("Plugin Object Called as Function\n");
110117
/*
111118
If your callback were invoked by the JavaScript expression
112119
'myObject.myFunction()', `function' would be set to myFunction, and
@@ -118,8 +125,9 @@ static JSValue ObjectCallAsFunction (JSContext ctx, JSObject function, JSObject
118125
return NULL;
119126
}
120127

121-
static JSObject ObjectCallAsConstructor (JSContext ctx, JSObject this, size_t argc, const JSValue argv[], JSValue * exception)
122-
{ g_print("Plugin Object Called as Constructor\n");
128+
static JSObject PluginObjectCallAsConstructor (JSContext ctx, JSObject this, size_t argc, const JSValue argv[], JSValue * exception)
129+
{
130+
g_print("Plugin Object Called as Constructor\n");
123131
/*
124132
If your callback were invoked by the JavaScript expression
125133
'new myConstructor()', `this' would be set to myConstructor.
@@ -130,8 +138,9 @@ static JSObject ObjectCallAsConstructor (JSContext ctx, JSObject this, size_t ar
130138
return NULL;
131139
}
132140

133-
static bool ObjectHasInstance(JSContext ctx, JSObject constructor, JSValue object, JSValue * exception)
134-
{ g_print("Called Plugin Object Has Instance\n");
141+
static bool PluginObjectHasInstance(JSContext ctx, JSObject constructor, JSValue object, JSValue * exception)
142+
{
143+
g_print("Called Plugin Object Has Instance\n");
135144
/*
136145
If your callback were invoked by the JavaScript expression
137146
'someValue instanceof myObject', `constructor' would be set to myObject
@@ -146,8 +155,9 @@ static bool ObjectHasInstance(JSContext ctx, JSObject constructor, JSValue objec
146155
return false;
147156
}
148157

149-
static JSValue ObjectConvertToType(JSContext ctx, JSObject object, JSType type, JSValue * exception)
150-
{ g_print("Called Plugin Object Convert to Type\n");
158+
static JSValue PluginObjectConvertToType(JSContext ctx, JSObject object, JSType type, JSValue * exception)
159+
{
160+
g_print("Called Plugin Object Convert to Type\n");
151161
/*
152162
If this function returns NULL, the conversion request forwards to
153163
object's parent class chain (which includes the default object class).
@@ -156,10 +166,13 @@ static JSValue ObjectConvertToType(JSContext ctx, JSObject object, JSType type,
156166
a string. An object converted to boolean is 'true.' An object converted
157167
to object is itself.
158168
*/
169+
if (type == kJSTypeNumber) return JSValueFromNumber(ctx, 0);
170+
if (type == kJSTypeString) return JSValueFromUtf8(ctx, PLUGIN_CLASS_NAME);
171+
159172
return NULL;
160173
}
161174

162-
static JSStaticValue ObjectValues[] = {
175+
static JSStaticValue PluginClassValues[] = {
163176

164177
/* GET follows the same prototype as getProperty */
165178
/* SET follows the same prototype as setProperty */
@@ -171,12 +184,12 @@ static JSStaticValue ObjectValues[] = {
171184
out useless errors.
172185
*/
173186

174-
{NULL, (void*) ObjectGetProperty, (void*) ObjectSetProperty, 0}, /* NAME, GET, SET, OBJECT ATTRIBUTES */
187+
//{MyProp, (void*) PluginObjectGetProperty, (void*) PluginObjectSetProperty, 0}, /* NAME, GET, SET, OBJECT ATTRIBUTES */
175188
{NULL, NULL, NULL, 0} /* DO NOT MODIFY, LEAVE LAST */
176189

177190
};
178191

179-
static JSStaticFunction ObjectFunctions[] = {
192+
static JSStaticFunction PluginClassFunctions[] = {
180193

181194
/* these functions follow the same prototype as callAsFunction */
182195

@@ -185,42 +198,48 @@ static JSStaticFunction ObjectFunctions[] = {
185198

186199
};
187200

188-
static JSClassDefinition ObjectDefinition = {
201+
static JSClassDefinition PluginClassDefinition = {
189202
0, /* Version, always 0 */
190203
/* ClassAttributes */
191204
kJSClassAttributeNoAutomaticPrototype,
192-
CLASSNAME, /* Class Name */
205+
PLUGIN_CLASS_NAME, /* Class Name */
193206
NULL, /* Parent Class */
194-
ObjectValues, /* Static Values */
195-
ObjectFunctions, /* Static Functions */
196-
(void*) ObjectInitialize, /* Object Initializer */
197-
(void*) ObjectFinalize, /* Object Finalizer */
198-
(void*) ObjectHasProperty, /* Object Has Property */
199-
(void*) ObjectGetProperty, /* Object Get Property */
200-
(void*) ObjectSetProperty, /* Object Set Property */
201-
(void*) ObjectDeleteProperty, /* Object Delete Property */
202-
(void*) ObjectGetPropertyNames, /* Object Get Property Names */
203-
(void*) ObjectCallAsFunction, /* new Object Call As Function */
204-
(void*) ObjectCallAsConstructor, /* new Object Call As Constructor */
205-
(void*) ObjectHasInstance, /* Has Instance */
206-
(void*) ObjectConvertToType /* Object Convert To Type */
207+
PluginClassValues, /* Static Values */
208+
PluginClassFunctions, /* Static Functions */
209+
(void*) PluginObjectInitialize, /* Object Initializer */
210+
(void*) PluginObjectFinalize, /* Object Finalizer */
211+
(void*) PluginObjectHasProperty, /* Object Has Property */
212+
(void*) PluginObjectGetProperty, /* Object Get Property */
213+
(void*) PluginObjectSetProperty, /* Object Set Property */
214+
(void*) PluginObjectDeleteProperty, /* Object Delete Property */
215+
(void*) PluginObjectGetPropertyNames, /* Object Get Property Names */
216+
(void*) PluginObjectCallAsFunction, /* new Object Call As Function */
217+
(void*) PluginObjectCallAsConstructor, /* new Object Call As Constructor */
218+
(void*) PluginObjectHasInstance, /* Has Instance */
219+
(void*) PluginObjectConvertToType /* Object Convert To Type */
207220
};
208221

209-
JSObject ClassObjectConstructor (JSContext ctx, JSObject constructor, size_t argc, const JSValue argv[], JSValue* exception)
210-
{ g_print("Called Plugin Class Object Constructor\n");
222+
static JSObject PluginClassConstructor (JSContext ctx, JSObject constructor, size_t argc, const JSValue argv[], JSValue* exception)
223+
{
224+
225+
g_print("Called Plugin Class Object Constructor\n");
226+
211227
return JSObjectMake(ctx, PluginClass, NULL);
212228
}
213229

214230
JSValue load(JSContext ctx, char * path, JSObject object, JSValue * exception)
215-
{ g_print("Called Plugin Load\n");
231+
{
232+
233+
g_print("Called Plugin Load\n");
234+
216235
if (!PluginClass)
217-
PluginClass = JSClassCreate(&ObjectDefinition);
236+
PluginClass = JSClassCreate(&PluginClassDefinition);
218237

219238
JSObject constructor = JSObjectCreateConstructor(
220-
ctx, PluginClass, ClassObjectConstructor
239+
ctx, PluginClass, PluginClassConstructor
221240
);
222241

223-
JSObjectSetUtf8Property(ctx, object, CLASSNAME,
242+
JSObjectSetUtf8Property(ctx, object, PLUGIN_CLASS_NAME,
224243
(JSValue) constructor, 0
225244
);
226245

@@ -231,7 +250,8 @@ JSValue load(JSContext ctx, char * path, JSObject object, JSValue * exception)
231250
}
232251

233252
void unload(JSContext ctx)
234-
{ g_print("Called Plugin Unload\n");
253+
{
254+
g_print("Called Plugin Unload\n");
235255
JSClassRelease(PluginClass);
236256
}
237257

0 commit comments

Comments
 (0)