Subj : Using Return value in another script problem To : netscape.public.mozilla.jseng From : Laurent Marzullo Date : Fri May 28 2004 01:48 pm Hello, I've got problem, and I can't figure it out, while using previously JS script return value in another script (or even when returning from the executed scrip, just after JS_ExecuteScript return). Here what I want: // --- C++ // --- The following structure is the return type of a method. struct PrtyNameAttr { const char* mdnm; const char* srnm; const char* frnm; }; // --- C++ class PrtyFactory { public: PrtyNameAttr* GetDefaultPrtyNameAttr( long long ); }; I then want to be able to use those 2 class into JS. I've done: 1) // For the structure JSClass PrtyNameAttrClass = { "PrtyNameAttr" , 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; // JSClass PrtyFactoryClass = { "PrtyFactory" , 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; JSFunctionSpec PrtyFactoryMethodes[] = { { "GetDefaultPrtyNameAttr" , prtyFactory_GetDefaultPrtyNameAttr , 1 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 } }; 2) I've then initialise the context, define/create my object etc. 3) I then want to // --- C++ // Create a default PrtyFactoryClass object and set it as a property // of my global object: JSObject* o = JS_ConstructObject( cx , &PrtyFactoryClass , 0 , 0 ); // Result = Success bool t = JS_DefineProperty( cx , global , "PrtyFactory" , OBJECT_TO_JSVAL( o ) , 0 , 0 , JSPROP_READONLY ); // t = true jsval rval; JSBool ok = JS_ExecuteScript( cx , global , js , &rval ); // // The JAVA SCRIPT EXECUTED IS: // ----------------------------------------- // // PrtyFactory.GetDefaultPrtyNameAttr( 1 ); // // --- End of JS --------------------------- // PrtyFactory is a property of the global object defined above. if ( ! ok ) // JAVA SCRIPT ERROR else { if ( JSVAL_IS_INT( rval ) ) // JS return an INT else if ( JSVAL_IS_OBJECT( rval ) ) // JS Return an object } According to my trace, the method is called (The one that respond to the GetDefaultPrtyNameAttr method) and return an OBJECT. Here what I'm doing in the method: 1) Calling a CORBA Service to retrieve structure information from a data base (The structure and value contained are correct). 2) Creating a PrtNameAttr object. o = JS_ConstructObject( cx , &PrtyNameAttrClass , 0 , 0 ); if ( o == 0 ) std::cerr << " (Error)\n"; else std::cerr << " (done)\n"; 3) I then defining a property for every field in the structure. JS_DefineProperty( cx , o , "frnm" , STRING_TO_JSVAL( JS_NewString( cx , frnm, strlen( frnm ) ) ) , 0 , 0 , JSPROP_READONLY ); JS_DefineProperty( cx , o , "mdnm" , STRING_TO_JSVAL( JS_NewString( cx , mdnm , strlen( mdnm ) ) ) , 0 , 0 , JSPROP_READONLY ); JS_DefineProperty( cx , o , "srnm" , STRING_TO_JSVAL( JS_NewString( cx , srnm , strlen( srnm ) ) ) , 0 , 0 , JSPROP_READONLY ); 4) Printing the value of the property here give the correct value. 5) returning the new object *rval = OBJECT_TO_JSVAL( o ); return JS_TRUE; ******** ******** ******** Here the problem: When I'm using the return value in the C++ code, and try to print the one of the property, JS_GetProperty return true but the value printed is not correct (it's garbage character). I've tried to add the returned jsval to root with JS_AddRoot, but I always got the problem. Could someone help please ? Thanks you very much Laurent Marzullo .