Subj : Re: Compacting JS source via ParseTree or Decompile To : Dan Libby From : Brendan Eich Date : Tue Jun 08 2004 07:48 pm Dan Libby wrote: > 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. JS_Enumerate is for enumerable properties only. Also, until you execute the script (or at least its prolog, see JS_ExecuteScriptPart), you won't see any properties defined by it in the object you passed in as the obj param when compiling it. > // 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); You don't really need the value -- you rather want to munge the id. And you will be using friend and (until we make more friend APIs) private APIs to do this. So, you don't want JS_Enumerate, again. You should instead loop from OBJ_SCOPE(fun->object)->lastProp following parent links until null, testing for getter == js_GetLocalVariable || getter == js_GetArgument. > Yes, arguments could/should be renamed (assuming JS only supports > positional arguments). Right, JS2 adds named parameter passing. /be .