2012-05-12 03:03:32 +08:00
|
|
|
#include "simple_native_generated.hpp"
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSClass* S_AnotherClass::jsClass = NULL;
|
|
|
|
JSObject* S_AnotherClass::jsObject = NULL;
|
2012-05-12 03:03:32 +08:00
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_AnotherClass::jsConstructor(JSContext *cx, uint32_t argc, jsval *vp)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
2012-05-19 06:21:24 +08:00
|
|
|
JSObject *obj = JS_NewObject(cx, S_AnotherClass::jsClass, S_AnotherClass::jsObject, NULL);
|
|
|
|
S_AnotherClass *cobj = new S_AnotherClass(obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t));
|
|
|
|
pt->flags = 0; pt->data = cobj;
|
|
|
|
JS_SetPrivate(obj, pt);
|
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
void S_AnotherClass::jsFinalize(JSContext *cx, JSObject *obj)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
pointerShell_t *pt = (pointerShell_t *)JS_GetPrivate(obj);
|
|
|
|
if (pt) {
|
2012-05-19 06:21:24 +08:00
|
|
|
if (!(pt->flags & kPointerTemporary) && pt->data) delete (S_AnotherClass *)pt->data;
|
2012-05-12 03:03:32 +08:00
|
|
|
JS_free(cx, pt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_AnotherClass::jsPropertyGet(JSContext *cx, JSObject *obj, jsid _id, jsval *val)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
int32_t propId = JSID_TO_INT(_id);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_AnotherClass *cobj; JSGET_PTRSHELL(S_AnotherClass, cobj, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (!cobj) return JS_FALSE;
|
|
|
|
switch(propId) {
|
2012-05-19 06:21:24 +08:00
|
|
|
case kAPublicField:
|
|
|
|
do { jsval tmp; JS_NewNumberValue(cx, cobj->aPublicField, &tmp); JS_SET_RVAL(cx, val, tmp); } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
2012-05-19 06:21:24 +08:00
|
|
|
case kJustOneField:
|
|
|
|
do { jsval tmp; JS_NewNumberValue(cx, cobj->getJustOneField(), &tmp); JS_SET_RVAL(cx, val, tmp); } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_AnotherClass::jsPropertySet(JSContext *cx, JSObject *obj, jsid _id, JSBool strict, jsval *val)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
int32_t propId = JSID_TO_INT(_id);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_AnotherClass *cobj; JSGET_PTRSHELL(S_AnotherClass, cobj, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (!cobj) return JS_FALSE;
|
|
|
|
switch(propId) {
|
2012-05-19 06:21:24 +08:00
|
|
|
case kAPublicField:
|
|
|
|
do { uint32_t tmp; JS_ValueToECMAUint32(cx, *val, &tmp); cobj->aPublicField = tmp; } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
void S_AnotherClass::jsCreateClass(JSContext *cx, JSObject *globalObj, const char *name)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
jsClass = (JSClass *)calloc(1, sizeof(JSClass));
|
|
|
|
jsClass->name = name;
|
|
|
|
jsClass->addProperty = JS_PropertyStub;
|
|
|
|
jsClass->delProperty = JS_PropertyStub;
|
|
|
|
jsClass->getProperty = JS_PropertyStub;
|
|
|
|
jsClass->setProperty = JS_StrictPropertyStub;
|
|
|
|
jsClass->enumerate = JS_EnumerateStub;
|
|
|
|
jsClass->resolve = JS_ResolveStub;
|
|
|
|
jsClass->convert = JS_ConvertStub;
|
|
|
|
jsClass->finalize = jsFinalize;
|
|
|
|
jsClass->flags = JSCLASS_HAS_PRIVATE;
|
|
|
|
static JSPropertySpec properties[] = {
|
2012-05-19 06:21:24 +08:00
|
|
|
{"aPublicField", kAPublicField, JSPROP_PERMANENT | JSPROP_SHARED, S_AnotherClass::jsPropertyGet, S_AnotherClass::jsPropertySet},
|
|
|
|
{"justOneField", kJustOneField, JSPROP_PERMANENT | JSPROP_SHARED, S_AnotherClass::jsPropertyGet, S_AnotherClass::jsPropertySet},
|
2012-05-12 03:03:32 +08:00
|
|
|
{0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSFunctionSpec funcs[] = {
|
2012-05-19 06:21:24 +08:00
|
|
|
JS_FN("doSomethingSimple", S_AnotherClass::jsdoSomethingSimple, 0, JSPROP_PERMANENT | JSPROP_SHARED),
|
2012-05-12 03:03:32 +08:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSFunctionSpec st_funcs[] = {
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
jsObject = JS_InitClass(cx,globalObj,NULL,jsClass,S_AnotherClass::jsConstructor,0,properties,funcs,NULL,st_funcs);
|
2012-05-12 03:03:32 +08:00
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_AnotherClass::jsdoSomethingSimple(JSContext *cx, uint32_t argc, jsval *vp) {
|
2012-05-12 03:03:32 +08:00
|
|
|
JSObject* obj = (JSObject *)JS_THIS_OBJECT(cx, vp);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_AnotherClass* self = NULL; JSGET_PTRSHELL(S_AnotherClass, self, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (self == NULL) return JS_FALSE;
|
2012-05-19 06:21:24 +08:00
|
|
|
if (argc == 0) {
|
|
|
|
self->doSomethingSimple();
|
2012-05-12 03:03:32 +08:00
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_TRUE);
|
2012-05-12 03:03:32 +08:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_TRUE);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSClass* S_SimpleNativeClass::jsClass = NULL;
|
|
|
|
JSObject* S_SimpleNativeClass::jsObject = NULL;
|
2012-05-12 03:03:32 +08:00
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_SimpleNativeClass::jsConstructor(JSContext *cx, uint32_t argc, jsval *vp)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
2012-05-19 06:21:24 +08:00
|
|
|
JSObject *obj = JS_NewObject(cx, S_SimpleNativeClass::jsClass, S_SimpleNativeClass::jsObject, NULL);
|
|
|
|
S_SimpleNativeClass *cobj = new S_SimpleNativeClass(obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t));
|
|
|
|
pt->flags = 0; pt->data = cobj;
|
|
|
|
JS_SetPrivate(obj, pt);
|
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
void S_SimpleNativeClass::jsFinalize(JSContext *cx, JSObject *obj)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
pointerShell_t *pt = (pointerShell_t *)JS_GetPrivate(obj);
|
|
|
|
if (pt) {
|
2012-05-19 06:21:24 +08:00
|
|
|
if (!(pt->flags & kPointerTemporary) && pt->data) delete (S_SimpleNativeClass *)pt->data;
|
2012-05-12 03:03:32 +08:00
|
|
|
JS_free(cx, pt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_SimpleNativeClass::jsPropertyGet(JSContext *cx, JSObject *obj, jsid _id, jsval *val)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
int32_t propId = JSID_TO_INT(_id);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_SimpleNativeClass *cobj; JSGET_PTRSHELL(S_SimpleNativeClass, cobj, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (!cobj) return JS_FALSE;
|
|
|
|
switch(propId) {
|
2012-05-19 06:21:24 +08:00
|
|
|
case kSomeField:
|
|
|
|
do { jsval tmp; JS_NewNumberValue(cx, cobj->getSomeField(), &tmp); JS_SET_RVAL(cx, val, tmp); } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
2012-05-19 06:21:24 +08:00
|
|
|
case kSomeOtherField:
|
|
|
|
do { jsval tmp; JS_NewNumberValue(cx, cobj->getSomeOtherField(), &tmp); JS_SET_RVAL(cx, val, tmp); } while (0);
|
|
|
|
break;
|
|
|
|
case kAnotherMoreComplexField:
|
|
|
|
do { JSString *tmp = JS_NewStringCopyZ(cx, cobj->getAnotherMoreComplexField()); JS_SET_RVAL(cx, val, STRING_TO_JSVAL(tmp)); } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_SimpleNativeClass::jsPropertySet(JSContext *cx, JSObject *obj, jsid _id, JSBool strict, jsval *val)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
int32_t propId = JSID_TO_INT(_id);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_SimpleNativeClass *cobj; JSGET_PTRSHELL(S_SimpleNativeClass, cobj, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (!cobj) return JS_FALSE;
|
|
|
|
switch(propId) {
|
2012-05-19 06:21:24 +08:00
|
|
|
case kSomeField:
|
|
|
|
do { uint32_t tmp; JS_ValueToECMAUint32(cx, *val, &tmp); cobj->setSomeField(tmp); } while (0);
|
|
|
|
break;
|
|
|
|
case kSomeOtherField:
|
|
|
|
do { uint32_t tmp; JS_ValueToECMAUint32(cx, *val, &tmp); cobj->setSomeOtherField(tmp); } while (0);
|
|
|
|
break;
|
|
|
|
case kAnotherMoreComplexField:
|
|
|
|
do {
|
|
|
|
char *tmp = JS_EncodeString(cx, JSVAL_TO_STRING(*val));
|
|
|
|
if (tmp) { cobj->setAnotherMoreComplexField(tmp); }
|
|
|
|
} while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
void S_SimpleNativeClass::jsCreateClass(JSContext *cx, JSObject *globalObj, const char *name)
|
2012-05-12 03:03:32 +08:00
|
|
|
{
|
|
|
|
jsClass = (JSClass *)calloc(1, sizeof(JSClass));
|
|
|
|
jsClass->name = name;
|
|
|
|
jsClass->addProperty = JS_PropertyStub;
|
|
|
|
jsClass->delProperty = JS_PropertyStub;
|
|
|
|
jsClass->getProperty = JS_PropertyStub;
|
|
|
|
jsClass->setProperty = JS_StrictPropertyStub;
|
|
|
|
jsClass->enumerate = JS_EnumerateStub;
|
|
|
|
jsClass->resolve = JS_ResolveStub;
|
|
|
|
jsClass->convert = JS_ConvertStub;
|
|
|
|
jsClass->finalize = jsFinalize;
|
|
|
|
jsClass->flags = JSCLASS_HAS_PRIVATE;
|
|
|
|
static JSPropertySpec properties[] = {
|
2012-05-19 06:21:24 +08:00
|
|
|
{"someField", kSomeField, JSPROP_PERMANENT | JSPROP_SHARED, S_SimpleNativeClass::jsPropertyGet, S_SimpleNativeClass::jsPropertySet},
|
|
|
|
{"someOtherField", kSomeOtherField, JSPROP_PERMANENT | JSPROP_SHARED, S_SimpleNativeClass::jsPropertyGet, S_SimpleNativeClass::jsPropertySet},
|
|
|
|
{"anotherMoreComplexField", kAnotherMoreComplexField, JSPROP_PERMANENT | JSPROP_SHARED, S_SimpleNativeClass::jsPropertyGet, S_SimpleNativeClass::jsPropertySet},
|
2012-05-12 03:03:32 +08:00
|
|
|
{0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSFunctionSpec funcs[] = {
|
2012-05-19 06:21:24 +08:00
|
|
|
JS_FN("doSomeProcessing", S_SimpleNativeClass::jsdoSomeProcessing, 2, JSPROP_PERMANENT | JSPROP_SHARED),
|
2012-05-12 03:03:32 +08:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSFunctionSpec st_funcs[] = {
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
jsObject = JS_InitClass(cx,globalObj,NULL,jsClass,S_SimpleNativeClass::jsConstructor,0,properties,funcs,NULL,st_funcs);
|
2012-05-12 03:03:32 +08:00
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
JSBool S_SimpleNativeClass::jsdoSomeProcessing(JSContext *cx, uint32_t argc, jsval *vp) {
|
2012-05-12 03:03:32 +08:00
|
|
|
JSObject* obj = (JSObject *)JS_THIS_OBJECT(cx, vp);
|
2012-05-19 06:21:24 +08:00
|
|
|
S_SimpleNativeClass* self = NULL; JSGET_PTRSHELL(S_SimpleNativeClass, self, obj);
|
2012-05-12 03:03:32 +08:00
|
|
|
if (self == NULL) return JS_FALSE;
|
2012-05-19 06:21:24 +08:00
|
|
|
if (argc == 2) {
|
|
|
|
JSString *arg0;
|
|
|
|
JSString *arg1;
|
|
|
|
JS_ConvertArguments(cx, 2, JS_ARGV(cx, vp), "SS", &arg0, &arg1);
|
|
|
|
std::string narg0 = JS_EncodeString(cx, arg0);
|
|
|
|
std::string narg1 = JS_EncodeString(cx, arg1);
|
|
|
|
int ret = self->doSomeProcessing(narg0, narg1);
|
|
|
|
do { jsval tmp; JS_NewNumberValue(cx, ret, &tmp); JS_SET_RVAL(cx, vp, tmp); } while (0);
|
2012-05-12 03:03:32 +08:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_TRUE);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-19 06:21:24 +08:00
|
|
|
void register_enums_simple_native_generated(JSObject *global) {
|
|
|
|
JSContext *cx = ScriptingCore::getInstance().getGlobalContext();
|
|
|
|
JSObject *_someThingEnumerated_enum = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
do {
|
|
|
|
jsval _v_kValue1; JS_NewNumberValue(cx, 1, &_v_kValue1);
|
|
|
|
JS_SetProperty(cx, _someThingEnumerated_enum, "kValue1", &_v_kValue1);
|
|
|
|
jsval _v_kValue2; JS_NewNumberValue(cx, 2, &_v_kValue2);
|
|
|
|
JS_SetProperty(cx, _someThingEnumerated_enum, "kValue2", &_v_kValue2);
|
|
|
|
jsval _v_kValue3; JS_NewNumberValue(cx, 3, &_v_kValue3);
|
|
|
|
JS_SetProperty(cx, _someThingEnumerated_enum, "kValue3", &_v_kValue3);
|
|
|
|
jsval _v_kValue4; JS_NewNumberValue(cx, 4, &_v_kValue4);
|
|
|
|
JS_SetProperty(cx, _someThingEnumerated_enum, "kValue4", &_v_kValue4);
|
|
|
|
}while (0);
|
|
|
|
jsval _someThingEnumerated_enum_val = OBJECT_TO_JSVAL(_someThingEnumerated_enum);
|
|
|
|
JS_SetProperty(cx, global, "someThingEnumerated", &_someThingEnumerated_enum_val);
|
|
|
|
}
|