I need to expose my component's functions to web document's
javascript. I achieved this in a javascript component, but I need it
in c++ and I don't understand why this code doesn't work

I'll put here all the code I've used. It's a minimal component that
only performs a sum of two numbers, but it's enough to try to
understand how yto expose that function to javascript.

I beg if someone could help me, please.

thanks in advance.

PS: Maybe should I create a C++ component with the functionality and
connect it to the javascript component that can expose its functions,
using it as a wrapper? may this be a good solution or not?  I will
appreciate your suggestions.


/**************  ImyCom.idl  *************/

#include "nsISupports.idl"

[scriptable, uuid(87f3699c-25aa-404b-b750-6b3752c65371)]
interface ImyCom : nsISupports
{
  long add(in long a, in long b);

};


/**************  myCom.cpp  *************/

#include "myCom.h"



NS_IMPL_ISUPPORTS1_CI(myCom, ImyCom)


myCom::myCom()
{
  /* member initializers and constructor code */
}

myCom::~myCom()
{
  /* destructor code */
}

/* long add (in long a, in long b); */
NS_IMETHODIMP myCom::add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
{
        *_retval = a + b;
        return NS_OK;
}


/**************  myCom.h  *************/

#ifndef _MYCOM_H_
#define _MYCOM_H_

#include "ImyCom.h"
#include "nsISupports.h"
#include "nsISupportsImpl.h"

#include "nsIDOMClassInfo.h"
#include "nsDOMCID.h"
#include "nsIScriptNameSpaceManager.h"


#define MYCOM_CONTRACTID "@clauer.uji.es/myCom;1"
#define MYCOM_CLASSNAME  "A XPCOM JS exposition sample"
#define MYCOM_CID_STR    "abfb30fd-26d3-4332-86e5-dc79fcf7bf25"
#define MYCOM_CID  { 0xabfb30fd, 0x26d3, 0x4332, \
{0x86, 0xe5, 0xdc, 0x79, 0xfc, 0xf7, 0xbf, 0x25 } }


/* Header file */
class myCom : public ImyCom
{
public:
  NS_DECL_ISUPPORTS

  NS_DECL_IMYCOM

  myCom();
  virtual ~myCom();
};

#endif //_MYCOM_H_




/**************  myComModule.cpp  *************/


#include "nsIGenericFactory.h"
#include "myCom.h"

NS_GENERIC_FACTORY_CONSTRUCTOR(myCom)




NS_DECL_DOM_CLASSINFO(myCom)

//Or should I use this one? NS_DECL_CLASSINFO(myCom)

NS_DOMCI_EXTENSION(myCom)
       static NS_DEFINE_CID(kmyComCID, MYCOM_CID);
       NS_DOMCI_EXTENSION_ENTRY_BEGIN(myCom)
         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(ImyCom)
       NS_DOMCI_EXTENSION_ENTRY_END_NO_PRIMARY_IF(myCom,PR_TRUE,
&kmyComCID)
NS_DOMCI_EXTENSION_END





static NS_METHOD
myComRegister(nsIComponentManager *aCompMgr,
                  nsIFile *aPath,
                  const char *registryLocation,
                  const char *componentType,
                  const nsModuleComponentInfo *info)
{
  nsresult rv = NS_OK;

  nsCOMPtr<nsICategoryManager> catman =
    do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);

  if (NS_FAILED(rv))
    return rv;

  nsXPIDLCString previous;


  rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
                                "myCom",
                                MYCOM_CONTRACTID,
                                PR_TRUE, PR_TRUE,
getter_Copies(previous));
  NS_ENSURE_SUCCESS(rv, rv);


  char* iidString = NS_GET_IID(ImyCom).ToString();
  if (!iidString)
    return NS_ERROR_OUT_OF_MEMORY;

  rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_INTERFACE,
                                "ImyCom",
                                iidString,
                                PR_TRUE, PR_TRUE,
getter_Copies(previous));
  nsCRT::free(iidString);
  NS_ENSURE_SUCCESS(rv, rv);

  return rv;
}





static nsModuleComponentInfo components[] =
{
    {
       MYCOM_CLASSNAME,
       MYCOM_CID,
       MYCOM_CONTRACTID,
       myComConstructor,
       myComRegister
    }
};




void PR_CALLBACK
myComModuleDestructor(nsIModule* self){
  NS_IF_RELEASE(NS_CLASSINFO_NAME(myCom));
}


NS_IMPL_NSGETMODULE_WITH_DTOR("myComModule", components,
myComModuleDestructor)


/************** testmycom.html  ************/
<HTML>
  <SCRIPT>
    function MyComTestGo() {
      try {
        var obj =  new myComFromJS();
        var res = obj.add(1,5);
        alert(res);
        res = obj.cat("hola","quetal");
        alert(res);

        //var res = myComFromJS.add(1,5);
        //alert(res);
      }
      catch (err) {
        alert(err);
        return;
      }
    }
  </SCRIPT>
  <BODY>
    Click to test t2.<br/>
    <BUTTON ONCLICK="MyComTestGo();">Go</BUTTON>
  </BODY>
</HTML>



This is all of it. I don't know if i forgot some declaration or
implementation that makes it able to expose the functions to
javascript, as long as the Javascript version of the component is
quite different to this one.

_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to