if (xpc) { rv = xpc->WrapNative(cx, ::JS_GetGlobalObject(cx), aTargetObject, NS_GET_IID(nsISupports), getter_AddRefs(holder)); if (holder) { rv = holder->GetJSObject(&scriptObject); }
nsCOMPtr<nsIXPConnectJSObjectHolder> holder; nsContentUtils::XPConnect()->WrapNative(cx, ::JS_GetGlobalObject(cx), aTargetObject, NS_GET_IID(nsISupports), getter_AddRefs(holder)); if (holder) { holder->GetJSObject(&scriptObject);
nsHTMLScriptEventHandler::Invoke(nsISupports *aTargetObject,                                 void *aArgs,                                 PRUint32 aArgCount){  nsresult rv;  nsAutoString scriptBody;  // Initial sanity checking  //  // It's 'ok' for aArgCount to be different from mArgNames.Count().  // This just means that the number of args being passed in is  // different from the number it is compiled with...  if (!aTargetObject || (aArgCount && !aArgs) ) {    return NS_ERROR_FAILURE;  }  // Get the text of the script to execute...  rv = mOuter->GetText(scriptBody);  if (NS_FAILED(rv)) {    return rv;  }  // Get the line number of the script (used when compiling)  PRUint32 lineNumber = 0;  nsCOMPtr<nsIScriptElement> sele(do_QueryInterface(mOuter));  if (sele) {    lineNumber = sele->GetScriptLineNumber();  }  // Get the script context...  nsCOMPtr<nsIDOMDocument> domdoc;  nsCOMPtr<nsIScriptContext> scriptContext;  mOuter->GetOwnerDocument(getter_AddRefs(domdoc));  nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));  if (doc) {    nsIScriptGlobalObject *sgo = doc->GetScriptGlobalObject();    if (sgo) {      scriptContext = sgo->GetContext();    }  }  // Fail if is no script context is available...  if (!scriptContext) {    return NS_ERROR_FAILURE;  }  // wrap the target object...  nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID()));  nsCOMPtr<nsIXPConnectJSObjectHolder> holder;  JSContext *cx = (JSContext *)scriptContext->GetNativeContext();  JSObject *scriptObject = nsnull;  if (xpc) {    rv = xpc->WrapNative(cx,                         ::JS_GetGlobalObject(cx),                         aTargetObject,                         NS_GET_IID(nsISupports),                         getter_AddRefs(holder));    if (holder) {      rv = holder->GetJSObject(&scriptObject);    }  }  // Fail if wrapping the native object failed...  if (!scriptObject) {    return NS_ERROR_FAILURE;  }  // Build up the array of argument names...  //  // Since this array is temporary (and const) the 'argument name' strings  // are NOT copied.  Instead each element points into the underlying buffer  // of the corresponding element in the mArgNames array...  //  // Remember, this is the number of arguments to compile the function with...  // So, use mArgNames.Count()  //  const int kMaxArgsOnStack = 10;  PRInt32 argc, i;  const char** args;  const char*  stackArgs[kMaxArgsOnStack];  args = stackArgs;  argc = mArgNames.Count();  // If there are too many arguments then allocate the array from the heap  // otherwise build it up on the stack...  if (argc >= kMaxArgsOnStack) {    args = new const char*[argc+1];    if (!args) return NS_ERROR_OUT_OF_MEMORY;  }  for(i=0; i<argc; i++) {    args[i] = mArgNames[i]->get();  }  // Null terminate for good luck ;-)  args[i] = nsnull;  // Compile the event handler script...  void* funcObject = nsnull;  NS_NAMED_LITERAL_CSTRING(funcName, "anonymous");  rv = scriptContext->CompileFunction(scriptObject,                                      funcName,   // method name                                      argc,       // no of arguments                                      args,       // argument names                                      scriptBody, // script text                                      nsnull,     // XXX: URL                                      lineNumber, // line no (for errors)                                      PR_FALSE,   // shared ?                                      &funcObject);  // Free the argument names array if it was heap allocated...  if (args != stackArgs) {    delete [] args;  }  // Fail if there was an error compiling the script.  if (NS_FAILED(rv)) {    return rv;  }  // Invoke the event handler script...  jsval dummy;  return scriptContext->CallEventHandler(scriptObject, (JSObject *)funcObject,                                         aArgCount, (jsval *)aArgs, &dummy);}