Subj : Re: Rhino, extracting functions from a script? To : netscape.public.mozilla.jseng From : Igor Bukanov Date : Sun Aug 15 2004 06:13 pm Laurens wrote: > Hi, > > > Is it possible to extract Functions from a given Script? > > I know it's possible to compile source code to a Function using > Context.compileFunction. What I want, however, is to compile a > JavaScript file containing multiple functions and reuse the Functions > instances across different Scopes in my application. More specifically, > I want to offer my users the ability to create a custom script with > common utility functions. These functions should be available whenever a > script runs. Context.compileString or Context.compileReader compile script and all function definition it contains to internal form. When you run this script via Script.exec(cx, scope), it will wrap the internal presentations into Function objects (fast operation) and put then into scope. Then it will execute the rest of script code if any. For example, if you have a script containing just function definitions like in: function f1() { ... } function f2() { ... } ... function f3() { ... } then whenever you execute Script.exec(cx, scope), Rhino will create properties named "f1", "f2", ..., "fN" in the scope object with proper Function objects. So if you simply run Script.exec before executing the rest of code, that should allow to access precompiled functions. Regards, Igor .