Subj : Re: Newb; script library To : netscape.public.mozilla.jseng From : Shanti Rao Date : Mon Oct 18 2004 11:48 am Try this. Lyndon wrote: > void load_js_files() > { > extern char *sz_jsdir; > jsval rval; > char tfnam[MAX_STR_LEN]; > DIR *d; > FILE *f; > char *b; > struct dirent *de; > struct stat sb; > JSScript *jsp; > > if ( NULL != ( d = opendir( sz_jsdir ) ) ) > { > while( NULL != ( de = readdir( d ) ) ) > { > if ( 0 == strcmp( &de->d_name[de->d_namlen-3], > ".js" ) ) > { > sprintf( (char *)&tfnam, "%s/%s%c", > sz_jsdir, de->d_name, 0 ); > stat( tfnam, &sb ); > if ( NULL != ( f = fopen( tfnam, "r" ) ) ) > { > if ( NULL != ( b = (char *)malloc( > sb.st_size + 1 ) ) ) > { > memset( b, 0, sb.st_size + > 1 ); > fread( b, sb.st_size, 1, > f ); if (!JS_EvaluateScript(jscx,jsglob,b,sb.st_size,de->d_name,0)) printf("Oops. %s\n",b); > free( b ); > b = NULL; > } > fclose( f ); > } > } > } > closedir( d ); > } > } > > //------------------------------- > > > For each datum I encounter, I add it as a variable in the global. This > allows me to reference data using the tagnames in the original XML... e.g. > > Lyndon > > would, in the below snippit, produce > "var myname = \"Lyndon\";" > //------------------------------- > char jstmp[MAX_STR_LEN]; > jsval rval; if (!JS_SetProperty(jscx,jsglob,tag,JS_NewStringCopyZ(tbuf))) > { > fprintf( stderr, "Error creating variable %s with value > %s\n", tag, tbuf ); > } > > //------------------------------- > > > > Of course I have a Processing Instructions file, something like a > stylesheet. For the above example, it may > do something like this: > > ucase( myname ); > > When I encounter this tag, I call into the Monkey: > //------------------------------- > case SEGMENT_TYPE_JSFUNC: > /* javascript function */ > if ( JS_TRUE == JS_EvaluateScript( jscx, > jsglob, segments[seg_i].data, segments[seg_i].size, segments[seg_i].data, 0, > &rval ) ) > { > JS_ConvertValue( jscx, rval, > JSTYPE_STRING, &rval ); > rstr = JS_ValueToString(jscx, rval); > strncat( mbuf, JS_GetStringBytes( > rstr ), JS_GetStringLength( rstr ) ); > } > break; > > //------------------------------- > > > So, that all runs through fine, and I was actually pretty pleased with how > easy it was to add this limited support that I have. To the point, though, > if I have another JS file in my library (or even another function in the > same file), that function fails to execute properly. To wit: > > Lyndonbilliards > > > > Name: ucase( > hobby ) lcase( firstname ) > > > Output should be: > Name: LYNDON billiards > You mean Name: BILLIARDS lyndon .