Subj : Re: Compacting JS source via ParseTree or Decompile To : netscape.public.mozilla.jseng From : Dan Libby Date : Tue Jun 08 2004 02:29 pm Okay, so clearly I do not yet understand how to iterate through a script's functions and variables. I came up with the following draft, which at least compiles, but exits at the first for loop in shorten_script_vars(). ida->length is always 0. The object is the Global class that was created for the context. I also saw an internal macro, OBJ_ENUMERATE() that looks interesting, but I figured it was best to try and use the public API first. // Iterate through functions void shorten_script_vars(JSContext* cx, JSObject* obj) { JSIdArray* ida = JS_Enumerate( cx, obj ); if( ida ) { jsval* val; int i; for(i = 0; i < ida->length; i++ ) { if( JS_TRUE == JS_GetElement(cx, obj, ida->vector[i], val) ) { if( JS_TypeOfValue(cx, *val) == JSTYPE_FUNCTION ) { JSFunction* fun = JS_ValueToFunction(cx, *val); shorten_function_vars( cx, fun ); } } } } } // Iterate through vars in function void shorten_function_vars(JSContext* cx, JSFunction* fun) { JSIdArray* ida = JS_Enumerate( cx, fun->object ); if( ida ) { jsval* val; int i; for(i = 0; i < ida->length; i++ ) { if( JS_TRUE == JS_GetElement(cx, fun->object, ida->vector[i], val) ) { JSType type = JS_TypeOfValue(cx, *val); switch( type ) { case JSTYPE_OBJECT: case JSTYPE_STRING: case JSTYPE_NUMBER: case JSTYPE_BOOLEAN: printf("in here\n\n"); break; default: break; } } } } } Brendan Eich wrote: > Brendan Eich wrote: > >> All you need to do is to rename the function's properties that have >> js_GetLocalVariable as their getter. Are you renaming arguments too? Yes, arguments could/should be renamed (assuming JS only supports positional arguments). > Why not rename *all* identifiers, including function names? Just map > through a unique/ugly identifier generator when walking the parse tree > (as recommended later in my reply). Well, if I rename global vars, class names, or function names, then we have the problem with external scripts or HTML events referencing those items using the original name. There are a few ways I see of dealing with this problem: 1) Simplest. Make it a command-line option, off by default. Leave it up to the user. 2) Process multiple files at once, so we update all references in like fashion. trickier. especially with HTML. 3) Allow user to [somehow] specify a list of externally referenced items that should not be changed. But none of that yet. crawl. walk. run. .