New nsIScriptable interface: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
 
No edit summary
Line 114: Line 114:
=== Use Cases ===
=== Use Cases ===


==== existing dialog widget ====
Consider the dialog widget, which allows you to have an "ondialogaccept" event handler.  This widget is implemented in JS.
Consider the dialog widget, which allows you to have an "ondialogaccept" event handler.  This widget is implemented in JS.
See dialog.xul, http://lxr.mozilla.org/seamonkey/source/xpfe/global/resources/content/bindings/dialog.xml#297
See dialog.xul, http://lxr.mozilla.org/seamonkey/source/xpfe/global/resources/content/bindings/dialog.xml#297
Line 133: Line 134:
         // Compile a function using the specified language.  Returns
         // Compile a function using the specified language.  Returns
         // an nsIScriptable that can be directly called (ie, by passing
         // an nsIScriptable that can be directly called (ie, by passing
         // DISPID_DEFAULT)
         // DISPID_DEFAULT).  |glob| must be the global window in
         nsIScriptable CompileFunction(PRUint32 langID,  
        // which the returned function will execute when called.
        // Generally you pass |this|.
         nsIScriptable CompileFunction(in nsISupports glob,
                                      in PRUint32 langID,  
                                       in DOMString aCode,  
                                       in DOMString aCode,  
                                       [array] in DOMString argNames);
                                       [array] in DOMString argNames);
Line 141: Line 145:
         void Exec(PRUint32 langID, in DOMString aCode);
         void Exec(PRUint32 langID, in DOMString aCode);
      
      
         // Takes a script-type ID, and nsISupports |glob| (which must be the  
         // Takes a script-type ID (which is presumably the ID for
         // global object for some (presumably other) language), and returns
        // a language other than the one being executed), and nsISupports  
        // an nsIScriptable, which is the namespace for the requested language.
        // |glob| (which must be the global object for the currently
         // executing language) and returns an nsIScriptable, which is  
        // the namespace for the requested language.
         nsIScriptable GetLanguageGlobal(in PRUint32 aRequestedLanguage, in nsISupports glob);
         nsIScriptable GetLanguageGlobal(in PRUint32 aRequestedLanguage, in nsISupports glob);
     };
     };
Line 152: Line 158:
     var s = components.classes["@mozilla.org/scriptobjectfactory"].getService();
     var s = components.classes["@mozilla.org/scriptobjectfactory"].getService();
      
      
     var func = s.CompileFunction(lang_id, handler, "event");
     var func = s.CompileFunction(this, lang_id, handler, "event");
     // |func| is an nsIScriptable - call it.
     // |func| is an nsIScriptable - call it.
     var returned = func(event)
     var returned = func(event)
==== Contrived dialog widget ====


Below is a contrived example that demonstrates GetLanguageGlobal. Let's assume the design of the dialog widget was such that it simply assumed a global function called "ondialogaccept" in the window namespace.
Below is a contrived example that demonstrates GetLanguageGlobal. Let's assume the design of the dialog widget was such that it simply assumed a global function called "ondialogaccept" in the window namespace.
Line 165: Line 173:
       <dialog>
       <dialog>


This could be implemented in a language agnostic fashion:
This could be implemented now in a JavaScript specific way simply by calling the function 'ondialogaccept' by name, in the assumption a <script> block created
such a function.
 
Assuming nsIScriptable, this could be implemented in a language agnostic fashion as follows:


     var namespace = s.GetLanguageGlobal(lang_id, window)
     var namespace = s.GetLanguageGlobal(lang_id, window)
Line 171: Line 182:
     var ret = func(event)
     var ret = func(event)


    // The first line returns an nsIScriptable object.  The JS support for this  
The first line returns an nsIScriptable object.  The JS support for this  
    // interface would then allow 'namespace.ondialogaccept' to invoke the  
interface would then allow 'namespace.ondialogaccept' to invoke the  
    // necessary nsIScriptable methods to lookup the attribute.  The attribute  
necessary nsIScriptable methods to lookup the attribute.  The attribute  
    // would be looked up as a simple property reference, and as a function
would be looked up as a simple property reference, and as a function
    // object would be found, |func| would itself be an nsIScriptable.
object would be found, |func| would itself be an nsIScriptable.
    // |func(event)| would then automatically call nsIScriptable::Invoke with a
|func(event)| would then automatically call nsIScriptable::Invoke with a
    // DISPID of DISPID_DEFAULT.
DISPID of DISPID_DEFAULT.
      
      
or the sake of completeness, the above could also obviously be written as:
or the sake of completeness, the above could also obviously be written as:
Line 184: Line 195:
     var namespace.ondialogaccept(event)
     var namespace.ondialogaccept(event)
      
      
    // In this case the use of nsIScriptable may be slightly different - the
In this case the use of nsIScriptable may be slightly different - the
    // initial Invoke for 'ondialogaccept' may be done as a method call, assuming
initial Invoke for 'ondialogaccept' may be done as a method call, assuming
    // the language is capable of detecting that situation.
the language is capable of detecting that situation.
32

edits

Navigation menu