Subj : Another Property's Getter & Setter Question To : netscape.public.mozilla.jseng From : lyg Date : Wed Nov 24 2004 08:36 am I have a puzzle when using SpiderMonkey in my app. It need that all data should be managed by my app including storing, updating and so on.But When some data is the content of one property of JS object. I have to store the data's jsval to slots, otherwise the javal might be gc'ed. In order to express the positon clearly, i use the following c code. Since I have stored the people information in class_info, I don't want to save they jsval to the slots. when i need to get or set the people.name, I just get or set the value of class_info.name. When I set the attribute of the name and address to JSPROP_SHARE, it can realise the goal, but when i evalutate "people.print(people.name)", is it possible to be wrong? I means before calling the people.print, gc is called and the people.name have been finalized. it may be wrong! Is there some solution for my app's requirement? Thanks!! enum tagMY_PEOPLE { MY_NAME = 0, MY_ADDRESS, }; typedef struct{ char name[16]; char addr[64];}PeopleInfo; static PeopleInfo class_info={"className", "classAddress"}; static JSBool GetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool SetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp); /* People Class GETTER */ static JSBool GetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp) { if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { case MY_NAME: *vp = STRING_TO_JSVAL (JS_NewStringCopyZ (cx, class_info.name)); break; case MY_ADDRESS: *vp = STRING_TO_JSVAL (JS_NewStringCopyZ (cx, class_info.addr)); break; } } return JS_TRUE; } /* People Class SETTER */ static JSBool SetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp) { JSString *t1; char * t2; t1 = JS_ValueToString(cx, *vp); t2 = JS_GetStringBytes(t1); if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_I NT(id)) { case MY_NAME: strncpy (class_info.name, t2 , 15); break; case MY_ADDRESS: strncpy (class_info.addr, t2, 63); break; } } return JS_TRUE; } /* Define People Class */ static JSClass PeopleClass = { "people",0, JS_PropertyStub,JS_PropertyStub, GetPeopleProperty, SetPeopleProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; /* People PropertySpec */ static JSPropertySpec PeopleProperties[] = { {"name", MY_NAME, JSPROP_ENUMERATE | JSPROP_SHARED} {"address", MY_ADDRESS, JSPROP_ENUMERATE | JSPROP_SHARED}, {0} } ; .