1998-06-04 03:32:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-06-04 03:32:26 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-06-04 03:32:26 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-06-04 03:32:26 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 23:49:15 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1998-06-04 03:32:26 +04:00
|
|
|
*/
|
|
|
|
|
1999-03-06 03:33:35 +03:00
|
|
|
#ifndef XP_MAC
|
1998-06-04 03:32:26 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1999-03-06 03:33:35 +03:00
|
|
|
#else
|
|
|
|
#include <stat.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#endif
|
|
|
|
|
1999-06-27 13:34:52 +04:00
|
|
|
#if !defined XP_UNIX && !defined XP_MAC && ! defined XP_BEOS
|
1998-06-04 03:32:26 +04:00
|
|
|
#include <direct.h>
|
1998-12-15 06:24:59 +03:00
|
|
|
#endif
|
1999-03-06 03:33:35 +03:00
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
#include <fstream.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "nsIPtr.h"
|
|
|
|
#include "plhash.h"
|
|
|
|
#include "JSStubGen.h"
|
|
|
|
#include "Exceptions.h"
|
|
|
|
#include "IdlSpecification.h"
|
|
|
|
#include "IdlInterface.h"
|
|
|
|
#include "IdlVariable.h"
|
|
|
|
#include "IdlAttribute.h"
|
|
|
|
#include "IdlFunction.h"
|
|
|
|
#include "IdlParameter.h"
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kFilePrefix[] = "nsJS";
|
|
|
|
static const char kFileSuffix[] = "cpp";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
JSStubGen::JSStubGen()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStubGen::~JSStubGen()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-08-04 04:08:29 +04:00
|
|
|
JSStubGen::Generate(char *aFileName,
|
1998-06-04 03:32:26 +04:00
|
|
|
char *aOutputDirName,
|
1998-07-03 04:48:06 +04:00
|
|
|
IdlSpecification &aSpec,
|
|
|
|
int aIsGlobal)
|
1998-06-04 03:32:26 +04:00
|
|
|
{
|
1998-08-04 04:08:29 +04:00
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(0);
|
|
|
|
|
|
|
|
if (!OpenFile(iface->GetName(), aOutputDirName, kFilePrefix, kFileSuffix)) {
|
1998-06-04 03:32:26 +04:00
|
|
|
throw new CantOpenFileException(aFileName);
|
|
|
|
}
|
|
|
|
|
1998-07-03 04:48:06 +04:00
|
|
|
mIsGlobal = aIsGlobal;
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
GenerateNPL();
|
|
|
|
GenerateIncludes(aSpec);
|
|
|
|
GenerateIIDDefinitions(aSpec);
|
1999-06-12 01:49:41 +04:00
|
|
|
#ifndef USE_COMPTR
|
1998-06-04 03:32:26 +04:00
|
|
|
GenerateDefPtrs(aSpec);
|
1999-06-12 01:49:41 +04:00
|
|
|
#endif
|
1998-06-04 03:32:26 +04:00
|
|
|
GeneratePropertySlots(aSpec);
|
|
|
|
GeneratePropertyFunc(aSpec, PR_TRUE);
|
|
|
|
GeneratePropertyFunc(aSpec, PR_FALSE);
|
2000-02-23 01:08:36 +03:00
|
|
|
GenerateCustomPropertyFuncs(aSpec);
|
1998-06-04 03:32:26 +04:00
|
|
|
GenerateFinalize(aSpec);
|
|
|
|
GenerateEnumerate(aSpec);
|
|
|
|
GenerateResolve(aSpec);
|
|
|
|
GenerateMethods(aSpec);
|
|
|
|
GenerateJSClass(aSpec);
|
|
|
|
GenerateClassProperties(aSpec);
|
|
|
|
GenerateClassFunctions(aSpec);
|
|
|
|
GenerateConstructor(aSpec);
|
|
|
|
GenerateInitClass(aSpec);
|
|
|
|
GenerateNew(aSpec);
|
|
|
|
CloseFile();
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIncludeDefaultsStr[] = "\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"#include \"jsapi.h\"\n"
|
2000-05-11 00:12:44 +04:00
|
|
|
"#include \"jsnum.h\"\n"
|
1998-12-03 03:33:00 +03:00
|
|
|
"#include \"nsJSUtils.h\"\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
"#include \"nsDOMError.h\"\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"#include \"nscore.h\"\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
"#include \"nsIServiceManager.h\"\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"#include \"nsIScriptContext.h\"\n"
|
1999-05-27 01:43:28 +04:00
|
|
|
"#include \"nsIScriptSecurityManager.h\"\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"#include \"nsIJSScriptObject.h\"\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"#include \"nsIScriptObjectOwner.h\"\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"#include \"nsIScriptGlobalObject.h\"\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
"#include \"nsCOMPtr.h\"\n"
|
1999-11-20 10:28:34 +03:00
|
|
|
"#include \"nsDOMPropEnums.h\"\n"
|
1999-08-27 10:06:24 +04:00
|
|
|
#ifndef USE_COMPTR
|
1998-06-04 03:32:26 +04:00
|
|
|
"#include \"nsIPtr.h\"\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
#endif
|
1998-06-23 22:43:06 +04:00
|
|
|
"#include \"nsString.h\"\n";
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIncludeStr[] = "#include \"nsIDOM%s.h\"\n";
|
|
|
|
static const char kXPIDLIncludeStr[] = "#include \"%s.h\"\n";
|
|
|
|
static const char kIncludeConstructorStr[] =
|
1998-12-23 01:03:20 +03:00
|
|
|
"#include \"nsIScriptNameSpaceManager.h\"\n"
|
1999-03-29 02:19:42 +04:00
|
|
|
"#include \"nsIComponentManager.h\"\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
"#include \"nsIJSNativeInitializer.h\"\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
"#include \"nsDOMCID.h\"\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
static PRIntn
|
|
|
|
IncludeEnumerator(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
1999-03-10 23:15:23 +03:00
|
|
|
switch ((Type)(int)(he->value)) {
|
|
|
|
case TYPE_OBJECT:
|
1999-03-29 02:19:42 +04:00
|
|
|
case TYPE_FUNC:
|
1999-03-10 23:15:23 +03:00
|
|
|
sprintf(buf, kIncludeStr, (char *)he->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
sprintf(buf, kXPIDLIncludeStr, (char *)he->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// uh oh...
|
|
|
|
break;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
ofstream *file = (ofstream *)arg;
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateIncludes(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
ofstream *file = GetFile();
|
1998-09-17 05:53:52 +04:00
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
*file << kIncludeDefaultsStr;
|
|
|
|
EnumerateAllObjects(aSpec, (PLHashEnumerator)IncludeEnumerator,
|
1998-08-04 04:08:29 +04:00
|
|
|
file, PR_FALSE);
|
1998-09-17 05:53:52 +04:00
|
|
|
if (HasConstructor(*primary_iface, NULL)) {
|
|
|
|
*file << kIncludeConstructorStr;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << "\n";
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIIDDefaultStr[] = "\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);\n"
|
|
|
|
"static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);\n";
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIIDStr[] = "static NS_DEFINE_IID(kI%sIID, %s);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
PRIntn
|
|
|
|
JSStubGen_IIDEnumerator(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
char iid_buf[256];
|
|
|
|
JSStubGen *me = (JSStubGen *)arg;
|
|
|
|
ofstream *file = me->GetFile();
|
1999-03-10 23:15:23 +03:00
|
|
|
|
|
|
|
switch ((Type)(int)(he->value)) {
|
|
|
|
case TYPE_OBJECT:
|
1999-03-29 02:19:42 +04:00
|
|
|
case TYPE_FUNC:
|
1999-03-10 23:15:23 +03:00
|
|
|
me->GetInterfaceIID(iid_buf, (char *)he->key);
|
|
|
|
sprintf(buf, kIIDStr, (char *)he->key, iid_buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
{
|
|
|
|
// This sucks. I know.
|
|
|
|
char* p = (char*) he->key;
|
|
|
|
if (p[0] == 'n' && p[1] == 's' && p[2] == 'I')
|
|
|
|
p += 3;
|
|
|
|
|
|
|
|
me->GetXPIDLInterfaceIID(iid_buf, p);
|
|
|
|
sprintf(buf, kIIDStr, p, iid_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateIIDDefinitions(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
|
|
|
|
*file << kIIDDefaultStr;
|
|
|
|
EnumerateAllObjects(aSpec, (PLHashEnumerator)JSStubGen_IIDEnumerator,
|
|
|
|
this, PR_FALSE);
|
|
|
|
*file << "\n";
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kDefPtrStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"NS_DEF_PTR(nsIDOM%s);\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kDefXPIDLPtrStr[] =
|
1999-03-10 23:15:23 +03:00
|
|
|
"NS_DEF_PTR(%s);\n";
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
PRIntn
|
|
|
|
JSStubGen_DefPtrEnumerator(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
JSStubGen *me = (JSStubGen *)arg;
|
|
|
|
ofstream *file = me->GetFile();
|
1999-03-10 23:15:23 +03:00
|
|
|
|
|
|
|
switch ((Type)(int)(he->value)) {
|
|
|
|
case TYPE_OBJECT:
|
1999-03-29 02:19:42 +04:00
|
|
|
case TYPE_FUNC:
|
1999-03-10 23:15:23 +03:00
|
|
|
sprintf(buf, kDefPtrStr, (char *)he->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
sprintf(buf, kDefXPIDLPtrStr, (char *)he->key);
|
|
|
|
break;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateDefPtrs(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
|
|
|
|
EnumerateAllObjects(aSpec, (PLHashEnumerator)JSStubGen_DefPtrEnumerator,
|
|
|
|
this, PR_FALSE);
|
|
|
|
*file << "\n";
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropEnumStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"//\n"
|
|
|
|
"// %s property ids\n"
|
|
|
|
"//\n"
|
|
|
|
"enum %s_slots {\n";
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropSlotStr[] = " %s_%s = -%d";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GeneratePropertySlots(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
1998-09-11 22:05:11 +04:00
|
|
|
int any_props = 0;
|
1998-09-25 04:49:44 +04:00
|
|
|
int prop_counter = 0;
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
strcpy(iface_name, iface->GetName());
|
|
|
|
StrUpr(iface_name);
|
|
|
|
int a, acount = iface->AttributeCount();
|
|
|
|
for (a = 0; a < acount; a++) {
|
|
|
|
IdlAttribute *attr = iface->GetAttributeAt(a);
|
|
|
|
char attr_name[128];
|
|
|
|
|
1999-05-28 04:17:38 +04:00
|
|
|
if (attr->GetIsNoScript()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the first time...
|
|
|
|
if (!any_props) {
|
1998-06-04 03:32:26 +04:00
|
|
|
sprintf(buf, kPropEnumStr, iface->GetName(), iface->GetName());
|
|
|
|
*file << buf;
|
|
|
|
}
|
1999-05-28 04:17:38 +04:00
|
|
|
// If this is the first time for this interface...
|
1998-09-11 22:05:11 +04:00
|
|
|
else if (a == 0) {
|
|
|
|
*file << ",\n";
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-05-28 04:17:38 +04:00
|
|
|
any_props = 1;
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
strcpy(attr_name, attr->GetName());
|
|
|
|
StrUpr(attr_name);
|
|
|
|
|
1998-09-25 04:49:44 +04:00
|
|
|
sprintf(buf, kPropSlotStr, iface_name, attr_name, ++prop_counter);
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
1998-09-11 22:05:11 +04:00
|
|
|
if (a != acount-1) {
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << ",\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-09-11 22:05:11 +04:00
|
|
|
if (any_props) {
|
|
|
|
*file << "\n};\n";
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncBeginStr[] = "\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"/***********************************************************************/\n"
|
|
|
|
"//\n"
|
|
|
|
"// %s Properties %ster\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s%sProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)\n"
|
|
|
|
"{\n"
|
1999-06-12 02:38:35 +04:00
|
|
|
" nsIDOM%s *a = (nsIDOM%s*)nsJSUtils::nsGetNativeThis(cx, obj);\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"\n"
|
|
|
|
" // If there's no private data, this must be the prototype, so ignore\n"
|
|
|
|
" if (nsnull == a) {\n"
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
" }\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
"\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIntCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsresult rv = NS_OK;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" if (JSVAL_IS_INT(id)) {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsIScriptSecurityManager *secMan = nsJSUtils::nsGetSecurityManager(cx, obj);\n"
|
|
|
|
" if (!secMan)\n"
|
|
|
|
" return PR_FALSE;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" switch(JSVAL_TO_INT(id)) {\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIntCaseNamedItemStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
" PRBool checkNamedItem = PR_TRUE;\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsresult rv = NS_OK;\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
" if (JSVAL_IS_INT(id)) {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsIScriptSecurityManager *secMan = nsJSUtils::nsGetSecurityManager(cx, obj);\n"
|
|
|
|
" if (!secMan)\n"
|
|
|
|
" return PR_FALSE;\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
" checkNamedItem = PR_FALSE;\n"
|
|
|
|
" switch(JSVAL_TO_INT(id)) {\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncDefaultStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" default:\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsCallJSScriptObject%sProperty(a, cx, obj, id, vp);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
1998-07-15 03:07:26 +04:00
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncDefaultNamedItemStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
" default:\n"
|
|
|
|
" checkNamedItem = PR_TRUE;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncDefaultItemStr[] =
|
1998-07-15 03:07:26 +04:00
|
|
|
" default:\n"
|
|
|
|
" {\n"
|
|
|
|
" %s prop;\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = a->Item(JSVAL_TO_INT(id), %sprop);\n"
|
|
|
|
" if (NS_SUCCEEDED(rv)) {\n"
|
1998-07-15 03:07:26 +04:00
|
|
|
"%s"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-12-22 04:48:47 +03:00
|
|
|
static const char kPropFuncDefaultItemEllipsisStr[] =
|
|
|
|
" default:\n"
|
|
|
|
" {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = a->Item(cx, &id, 1, vp);\n"
|
1999-12-22 04:48:47 +03:00
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncDefaultItemNonPrimaryStr[] =
|
1998-08-08 03:01:52 +04:00
|
|
|
" default:\n"
|
|
|
|
" {\n"
|
|
|
|
" %s prop;\n"
|
|
|
|
" nsIDOM%s* b;\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
1999-09-09 23:16:12 +04:00
|
|
|
" nsresult result = NS_OK;\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = b->Item(JSVAL_TO_INT(id), %sprop);\n"
|
|
|
|
" if (NS_SUCCEEDED(rv)) {\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
"%s"
|
|
|
|
" }\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" NS_RELEASE(b);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = NS_ERROR_DOM_WRONG_TYPE_ERR;\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-12-22 04:48:47 +03:00
|
|
|
static const char kPropFuncDefaultItemEllipsisNonPrimaryStr[] =
|
|
|
|
" default:\n"
|
|
|
|
" {\n"
|
|
|
|
" nsIDOM%s* b;\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = b->Item(cx, &id, 1, vp);\n"
|
1999-12-22 04:48:47 +03:00
|
|
|
" NS_RELEASE(b);\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = NS_ERROR_DOM_WRONG_TYPE_ERR;\n"
|
1999-12-22 04:48:47 +03:00
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncEndStr[] =
|
1998-07-15 00:19:28 +04:00
|
|
|
" else {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsCallJSScriptObject%sProperty(a, cx, obj, id, vp);\n"
|
1998-07-15 00:19:28 +04:00
|
|
|
" }\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" if (NS_FAILED(rv))\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, rv);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" return PR_TRUE;\n"
|
|
|
|
"}\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncNamedItemStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
"\n"
|
|
|
|
" if (checkNamedItem) {\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
" %s prop;\n"
|
|
|
|
" nsAutoString name;\n"
|
1999-09-09 23:16:12 +04:00
|
|
|
" nsresult result = NS_OK;\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
"\n"
|
|
|
|
" JSString *jsstring = JS_ValueToString(cx, id);\n"
|
|
|
|
" if (nsnull != jsstring) {\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" name.Assign(JS_GetStringChars(jsstring));\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" name.SetLength(0);\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
" }\n"
|
|
|
|
"\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
" result = a->NamedItem(name, %sprop);\n"
|
|
|
|
" if (NS_SUCCEEDED(result)) {\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" if (NULL != prop) {\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
"%s"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
1998-08-13 23:07:45 +04:00
|
|
|
" else {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsCallJSScriptObject%sProperty(a, cx, obj, id, vp);\n"
|
1998-08-13 23:07:45 +04:00
|
|
|
" }\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
1998-08-04 04:08:29 +04:00
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-12-22 04:48:47 +03:00
|
|
|
static const char kPropFuncNamedItemEllipsisStr[] =
|
|
|
|
"\n"
|
|
|
|
" if (checkNamedItem) {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = a->NamedItem(cx, &id, 1, vp);\n"
|
1999-12-22 04:48:47 +03:00
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropFuncNamedItemNonPrimaryStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
"\n"
|
|
|
|
" if (checkNamedItem) {\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" %s prop;\n"
|
|
|
|
" nsIDOM%s* b;\n"
|
|
|
|
" nsAutoString name;\n"
|
|
|
|
"\n"
|
|
|
|
" JSString *jsstring = JS_ValueToString(cx, id);\n"
|
|
|
|
" if (nsnull != jsstring) {\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" name.Assign(JS_GetStringChars(jsstring));\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" name.SetLength(0);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
1999-09-09 23:16:12 +04:00
|
|
|
" nsresult result = NS_OK;\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
" result = b->NamedItem(name, %sprop);\n"
|
|
|
|
" if (NS_SUCCEEDED(result)) {\n"
|
1998-08-13 23:07:45 +04:00
|
|
|
" NS_RELEASE(b);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" if (NULL != prop) {\n"
|
|
|
|
"%s"
|
|
|
|
" }\n"
|
1998-08-13 23:07:45 +04:00
|
|
|
" else {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsCallJSScriptObject%sProperty(a, cx, obj, id, vp);\n"
|
1998-08-13 23:07:45 +04:00
|
|
|
" }\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" NS_RELEASE(b);\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_WRONG_TYPE_ERR);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-12-22 04:48:47 +03:00
|
|
|
static const char kPropFuncNamedItemEllipsisNonPrimaryStr[] =
|
|
|
|
"\n"
|
|
|
|
" if (checkNamedItem) {\n"
|
|
|
|
" nsIDOM%s* b;\n"
|
|
|
|
" nsresult result = NS_OK;\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
|
|
|
" result = b->NamedItem(cx, &id, 1, vp);\n"
|
|
|
|
" NS_RELEASE(b);\n"
|
|
|
|
" if (NS_FAILED(result)) {\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_WRONG_TYPE_ERR);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n";
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
#define JSGEN_GENERATE_PROPFUNCBEGIN(buffer, op, className) \
|
|
|
|
sprintf(buffer, kPropFuncBeginStr, className, op, op, className, className, className)
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_PROPFUNCEND(buffer, op) \
|
1998-07-15 03:07:26 +04:00
|
|
|
sprintf(buffer, kPropFuncEndStr, op)
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_PROPFUNCDEFAULT(buffer, op) \
|
|
|
|
sprintf(buffer, kPropFuncDefaultStr, op)
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropCaseBeginStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" case %s_%s:\n"
|
1999-05-27 01:43:28 +04:00
|
|
|
" {\n"
|
2000-02-02 03:22:58 +03:00
|
|
|
" rv = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_%s_%s, %s);\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" if (NS_SUCCEEDED(rv)) {\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropCaseEndStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" }\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" break;\n"
|
|
|
|
" }\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kNoAttrStr[] = " case 0:\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GeneratePropertyFunc(IdlSpecification &aSpec, PRBool aIsGetter)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
PRBool any = PR_FALSE;
|
|
|
|
|
|
|
|
JSGEN_GENERATE_PROPFUNCBEGIN(buf, aIsGetter ? "Get" : "Set", primary_iface->GetName());
|
|
|
|
*file << buf;
|
|
|
|
|
1999-06-12 01:49:41 +04:00
|
|
|
IdlFunction *item_func = NULL;
|
|
|
|
IdlFunction *named_item_func = NULL;
|
|
|
|
IdlInterface *item_iface = NULL;
|
|
|
|
IdlInterface *named_item_iface = NULL;
|
1999-12-22 04:48:47 +03:00
|
|
|
PRBool named_item_has_ellipsis = PR_FALSE;
|
|
|
|
PRBool item_has_ellipsis = PR_FALSE;
|
1999-06-12 01:49:41 +04:00
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
int i, icount = aSpec.InterfaceCount();
|
1999-06-12 01:49:41 +04:00
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
|
|
|
|
int m, mcount = iface->FunctionCount();
|
|
|
|
for (m = 0; m < mcount; m++) {
|
|
|
|
IdlFunction *func = iface->GetFunctionAt(m);
|
|
|
|
|
|
|
|
if (strcmp(func->GetName(), "item") == 0) {
|
|
|
|
item_func = func;
|
|
|
|
item_iface = iface;
|
1999-12-22 04:48:47 +03:00
|
|
|
item_has_ellipsis = func->GetHasEllipsis();
|
1999-06-12 01:49:41 +04:00
|
|
|
}
|
|
|
|
else if (strcmp(func->GetName(), "namedItem") == 0) {
|
|
|
|
named_item_func = func;
|
|
|
|
named_item_iface = iface;
|
1999-12-22 04:48:47 +03:00
|
|
|
named_item_has_ellipsis = func->GetHasEllipsis();
|
1999-06-12 01:49:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == named_item_func) {
|
|
|
|
*file << kIntCaseStr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*file << kIntCaseNamedItemStr;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
strcpy(iface_name, iface->GetName());
|
|
|
|
StrUpr(iface_name);
|
|
|
|
|
|
|
|
int a, acount = iface->AttributeCount();
|
|
|
|
for (a = 0; a < acount; a++) {
|
|
|
|
IdlAttribute *attr = iface->GetAttributeAt(a);
|
|
|
|
char attr_name[128];
|
|
|
|
|
|
|
|
strcpy(attr_name, attr->GetName());
|
|
|
|
StrUpr(attr_name);
|
|
|
|
|
2000-02-23 01:08:36 +03:00
|
|
|
if (attr->GetIsNoScript() || attr->GetReplaceable() || (!aIsGetter && (attr->GetReadOnly()))) {
|
1998-06-04 03:32:26 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
any = PR_TRUE;
|
|
|
|
|
1999-11-20 10:28:34 +03:00
|
|
|
char upr_attr_name[128];
|
|
|
|
strcpy(upr_attr_name, attr_name);
|
|
|
|
StrUpr(upr_attr_name);
|
1999-05-27 01:43:28 +04:00
|
|
|
|
1999-11-20 10:28:34 +03:00
|
|
|
char upr_iface_name[128];
|
|
|
|
strcpy(upr_iface_name, iface_name);
|
|
|
|
StrUpr(upr_iface_name);
|
1999-05-27 01:43:28 +04:00
|
|
|
|
1999-08-31 18:23:54 +04:00
|
|
|
if (aIsGetter) {
|
1999-11-20 10:28:34 +03:00
|
|
|
sprintf(buf, kPropCaseBeginStr, iface_name, attr_name, upr_iface_name, upr_attr_name, "PR_FALSE");
|
1999-08-31 18:23:54 +04:00
|
|
|
}
|
|
|
|
else {
|
1999-11-20 10:28:34 +03:00
|
|
|
sprintf(buf, kPropCaseBeginStr, iface_name, attr_name, upr_iface_name, upr_attr_name, "PR_TRUE");
|
1999-08-31 18:23:54 +04:00
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
if (aIsGetter) {
|
1998-07-15 03:07:26 +04:00
|
|
|
GeneratePropGetter(file, *iface, *attr,
|
|
|
|
iface == primary_iface ? JSSTUBGEN_PRIMARY : JSSTUBGEN_NONPRIMARY);
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GeneratePropSetter(file, *iface, *attr, iface == primary_iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(buf, kPropCaseEndStr);
|
1998-07-15 03:07:26 +04:00
|
|
|
*file << buf;
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!any) {
|
|
|
|
*file << kNoAttrStr;
|
|
|
|
}
|
1998-07-15 03:07:26 +04:00
|
|
|
|
1998-08-04 04:08:29 +04:00
|
|
|
if (aIsGetter) {
|
1998-07-15 03:07:26 +04:00
|
|
|
if (NULL != item_func) {
|
|
|
|
IdlVariable *rval = item_func->GetReturnValue();
|
1999-12-22 04:48:47 +03:00
|
|
|
if (item_has_ellipsis) {
|
|
|
|
GeneratePropGetter(file, *item_iface, *rval,
|
|
|
|
item_iface == primary_iface ?
|
|
|
|
JSSTUBGEN_DEFAULT_ELLIPSIS : JSSTUBGEN_DEFAULT_NONPRIMARY_ELLIPSIS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GeneratePropGetter(file, *item_iface, *rval,
|
|
|
|
item_iface == primary_iface ?
|
|
|
|
JSSTUBGEN_DEFAULT : JSSTUBGEN_DEFAULT_NONPRIMARY);
|
|
|
|
}
|
1998-07-15 03:07:26 +04:00
|
|
|
}
|
1999-06-12 01:49:41 +04:00
|
|
|
else if (NULL != named_item_func) {
|
|
|
|
*file << kPropFuncDefaultNamedItemStr;
|
|
|
|
}
|
1998-07-15 03:07:26 +04:00
|
|
|
else {
|
|
|
|
JSGEN_GENERATE_PROPFUNCDEFAULT(buf, aIsGetter ? "Get" : "Set");
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSGEN_GENERATE_PROPFUNCDEFAULT(buf, aIsGetter ? "Get" : "Set");
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
1998-08-04 04:08:29 +04:00
|
|
|
if (aIsGetter && (NULL != named_item_func)) {
|
|
|
|
IdlVariable *rval = named_item_func->GetReturnValue();
|
1999-12-22 04:48:47 +03:00
|
|
|
if (named_item_has_ellipsis) {
|
|
|
|
GeneratePropGetter(file, *named_item_iface, *rval,
|
|
|
|
named_item_iface == primary_iface ?
|
|
|
|
JSSTUBGEN_NAMED_ITEM_ELLIPSIS : JSSTUBGEN_NAMED_ITEM_NONPRIMARY_ELLIPSIS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GeneratePropGetter(file, *named_item_iface, *rval,
|
|
|
|
named_item_iface == primary_iface ?
|
|
|
|
JSSTUBGEN_NAMED_ITEM : JSSTUBGEN_NAMED_ITEM_NONPRIMARY);
|
|
|
|
}
|
1998-08-04 04:08:29 +04:00
|
|
|
}
|
|
|
|
|
1998-12-23 01:03:20 +03:00
|
|
|
if (aIsGetter) {
|
|
|
|
JSGEN_GENERATE_PROPFUNCEND(buf, "Get");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSGEN_GENERATE_PROPFUNCEND(buf, "Set");
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" %s prop;\n"
|
|
|
|
" rv = a->Get%s(%sprop);\n"
|
|
|
|
" if (NS_SUCCEEDED(rv)) {\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"%s"
|
2000-03-11 09:32:42 +03:00
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kGetCaseNonPrimaryStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" %s prop;\n"
|
|
|
|
" nsIDOM%s* b;\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
|
|
|
" rv = b->Get%s(%sprop);\n"
|
|
|
|
" if(NS_SUCCEEDED(rv)) {\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"%s"
|
2000-03-11 09:32:42 +03:00
|
|
|
" }\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" NS_RELEASE(b);\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = NS_ERROR_DOM_WRONG_TYPE_ERR;\n"
|
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kObjectGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" // get the js object\n"
|
|
|
|
" nsJSUtils::nsConvertObjectToJSVal((nsISupports *)prop, cx, obj, vp);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kXPIDLObjectGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" // get the js object; n.b., this will do a release on 'prop'\n"
|
|
|
|
" nsJSUtils::nsConvertXPCObjectToJSVal(prop, NS_GET_IID(%s), cx, obj, vp);\n";
|
1999-03-10 23:15:23 +03:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kStringGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsJSUtils::nsConvertStringToJSVal(prop, cx, vp);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIntGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" *vp = INT_TO_JSVAL(prop);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
2000-05-11 00:12:44 +04:00
|
|
|
static const char kFloatGetCaseStr[] =
|
|
|
|
" *vp = DOUBLE_TO_JSVAL(js_NewDouble(cx, prop));\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kBoolGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" *vp = BOOLEAN_TO_JSVAL(prop);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-27 10:06:24 +04:00
|
|
|
static const char kJSValGetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" *vp = prop;\n";
|
1999-08-27 10:06:24 +04:00
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
void
|
|
|
|
JSStubGen::GeneratePropGetter(ofstream *file,
|
|
|
|
IdlInterface &aInterface,
|
1998-07-15 03:07:26 +04:00
|
|
|
IdlVariable &aAttribute,
|
|
|
|
PRInt32 aType)
|
1998-06-04 03:32:26 +04:00
|
|
|
{
|
1998-08-08 03:01:52 +04:00
|
|
|
char buf[2048];
|
1999-03-10 23:15:23 +03:00
|
|
|
char buf2[1024];
|
1998-06-04 03:32:26 +04:00
|
|
|
char attr_type[128];
|
|
|
|
char attr_name[128];
|
|
|
|
const char *case_str;
|
|
|
|
|
|
|
|
GetVariableTypeForLocal(attr_type, aAttribute);
|
1998-08-04 04:08:29 +04:00
|
|
|
if ((JSSTUBGEN_PRIMARY == aType) || (JSSTUBGEN_NONPRIMARY == aType)) {
|
1998-07-15 03:07:26 +04:00
|
|
|
GetCapitalizedName(attr_name, aAttribute);
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
switch (aAttribute.GetType()) {
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
case_str = kBoolGetCaseStr;
|
|
|
|
break;
|
|
|
|
case TYPE_LONG:
|
|
|
|
case TYPE_SHORT:
|
|
|
|
case TYPE_ULONG:
|
|
|
|
case TYPE_USHORT:
|
|
|
|
case TYPE_CHAR:
|
|
|
|
case TYPE_INT:
|
|
|
|
case TYPE_UINT:
|
|
|
|
case_str = kIntGetCaseStr;
|
|
|
|
break;
|
2000-05-11 00:12:44 +04:00
|
|
|
case TYPE_FLOAT:
|
|
|
|
case_str = kFloatGetCaseStr;
|
|
|
|
break;
|
1999-08-27 10:06:24 +04:00
|
|
|
case TYPE_JSVAL:
|
|
|
|
case_str = kJSValGetCaseStr;
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
case TYPE_STRING:
|
|
|
|
case_str = kStringGetCaseStr;
|
|
|
|
break;
|
|
|
|
case TYPE_OBJECT:
|
|
|
|
case_str = kObjectGetCaseStr;
|
|
|
|
break;
|
1999-03-10 23:15:23 +03:00
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
case_str = buf2;
|
1999-04-24 06:38:05 +04:00
|
|
|
sprintf(buf2, kXPIDLObjectGetCaseStr, aAttribute.GetTypeName(), aAttribute.GetTypeName());
|
1999-03-10 23:15:23 +03:00
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
default:
|
|
|
|
// XXX Fail for other cases
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-07-15 03:07:26 +04:00
|
|
|
if (JSSTUBGEN_PRIMARY == aType) {
|
1998-06-04 03:32:26 +04:00
|
|
|
sprintf(buf, kGetCaseStr, attr_type, attr_name,
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
|
|
|
case_str);
|
|
|
|
}
|
1998-07-15 03:07:26 +04:00
|
|
|
else if (JSSTUBGEN_NONPRIMARY == aType) {
|
1998-06-04 03:32:26 +04:00
|
|
|
sprintf(buf, kGetCaseNonPrimaryStr, attr_type,
|
|
|
|
aInterface.GetName(), aInterface.GetName(),
|
|
|
|
attr_name,
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
1999-09-09 03:14:30 +04:00
|
|
|
case_str);
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
1998-08-04 04:08:29 +04:00
|
|
|
else if (JSSTUBGEN_DEFAULT == aType) {
|
1998-07-15 03:07:26 +04:00
|
|
|
sprintf(buf, kPropFuncDefaultItemStr, attr_type,
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
|
|
|
case_str);
|
|
|
|
}
|
1998-08-08 03:01:52 +04:00
|
|
|
else if (JSSTUBGEN_DEFAULT_NONPRIMARY == aType) {
|
1999-06-16 03:41:30 +04:00
|
|
|
sprintf(buf, kPropFuncDefaultItemNonPrimaryStr, attr_type,
|
1998-08-08 03:01:52 +04:00
|
|
|
aInterface.GetName(), aInterface.GetName(),
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
1999-09-09 03:14:30 +04:00
|
|
|
case_str);
|
1998-08-08 03:01:52 +04:00
|
|
|
}
|
1999-12-22 04:48:47 +03:00
|
|
|
else if (JSSTUBGEN_DEFAULT_ELLIPSIS == aType) {
|
|
|
|
sprintf(buf, kPropFuncDefaultItemEllipsisStr);
|
|
|
|
}
|
|
|
|
else if (JSSTUBGEN_DEFAULT_NONPRIMARY_ELLIPSIS == aType) {
|
|
|
|
sprintf(buf, kPropFuncDefaultItemEllipsisNonPrimaryStr,
|
|
|
|
aInterface.GetName(), aInterface.GetName());
|
|
|
|
}
|
1998-08-08 03:01:52 +04:00
|
|
|
else if (JSSTUBGEN_NAMED_ITEM == aType) {
|
1998-08-04 04:08:29 +04:00
|
|
|
sprintf(buf, kPropFuncNamedItemStr, attr_type,
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
1998-08-13 23:07:45 +04:00
|
|
|
case_str, "Get");
|
1998-08-04 04:08:29 +04:00
|
|
|
}
|
1998-08-08 03:01:52 +04:00
|
|
|
else if (JSSTUBGEN_NAMED_ITEM_NONPRIMARY == aType) {
|
|
|
|
sprintf(buf, kPropFuncNamedItemNonPrimaryStr, attr_type,
|
|
|
|
aInterface.GetName(), aInterface.GetName(),
|
|
|
|
aAttribute.GetType() == TYPE_STRING ? "" : "&",
|
1999-09-09 03:14:30 +04:00
|
|
|
case_str, "Get");
|
1998-08-08 03:01:52 +04:00
|
|
|
}
|
1999-12-22 04:48:47 +03:00
|
|
|
else if (JSSTUBGEN_NAMED_ITEM_ELLIPSIS == aType) {
|
|
|
|
sprintf(buf, kPropFuncNamedItemEllipsisStr);
|
|
|
|
}
|
|
|
|
else if (JSSTUBGEN_NAMED_ITEM_NONPRIMARY_ELLIPSIS == aType) {
|
|
|
|
sprintf(buf, kPropFuncNamedItemEllipsisNonPrimaryStr,
|
|
|
|
aInterface.GetName(), aInterface.GetName());
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" %s prop;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"%s \n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" rv = a->Set%s(prop);\n"
|
|
|
|
" %s\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kSetCaseNonPrimaryStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" %s prop;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"%s \n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsIDOM%s *b;\n"
|
|
|
|
" if (NS_OK == a->QueryInterface(kI%sIID, (void **)&b)) {\n"
|
|
|
|
" b->Set%s(prop);\n"
|
|
|
|
" NS_RELEASE(b);\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" %s\n"
|
|
|
|
" rv = NS_ERROR_DOM_WRONG_TYPE_ERR;\n"
|
|
|
|
" }\n"
|
|
|
|
" %s\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kObjectSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" if (PR_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)&prop,\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" kI%sIID, NS_ConvertASCIItoUCS2(\"%s\"),\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" cx, *vp)) {\n"
|
|
|
|
" rv = NS_ERROR_DOM_NOT_OBJECT_ERR;\n"
|
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kXPIDLObjectSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" if (PR_FALSE == nsJSUtils::nsConvertJSValToXPCObject((nsISupports **) &prop,\n"
|
|
|
|
" kI%sIID, cx, *vp)) {\n"
|
|
|
|
" rv = NS_ERROR_DOM_NOT_XPC_OBJECT_ERR;\n"
|
|
|
|
" }\n";
|
1999-03-10 23:15:23 +03:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kObjectSetCaseEndStr[] = "NS_IF_RELEASE(prop);";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-05-04 07:32:12 +04:00
|
|
|
static const char* kXPIDLObjectSetCaseEndStr = kObjectSetCaseEndStr;
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kStringSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsJSUtils::nsConvertJSValToString(prop, cx, *vp);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kIntSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" int32 temp;\n"
|
|
|
|
" if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) {\n"
|
|
|
|
" prop = (%s)temp;\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" rv = NS_ERROR_DOM_NOT_NUMBER_ERR;\n"
|
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kBoolSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" if (PR_FALSE == nsJSUtils::nsConvertJSValToBool(&prop, cx, *vp)) {\n"
|
|
|
|
" rv = NS_ERROR_DOM_NOT_BOOLEAN_ERR;\n"
|
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-27 10:06:24 +04:00
|
|
|
static const char kJSValSetCaseStr[] =
|
2000-03-11 09:32:42 +03:00
|
|
|
" prop = *vp;\n";
|
1999-08-27 10:06:24 +04:00
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
void
|
|
|
|
JSStubGen::GeneratePropSetter(ofstream *file,
|
|
|
|
IdlInterface &aInterface,
|
|
|
|
IdlAttribute &aAttribute,
|
|
|
|
PRBool aIsPrimary)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char attr_type[128];
|
|
|
|
char attr_name[128];
|
|
|
|
char case_buf[1024];
|
|
|
|
const char *end_str;
|
|
|
|
|
|
|
|
GetVariableTypeForLocal(attr_type, aAttribute);
|
|
|
|
GetCapitalizedName(attr_name, aAttribute);
|
|
|
|
|
|
|
|
switch (aAttribute.GetType()) {
|
|
|
|
case TYPE_BOOLEAN:
|
1998-12-03 03:33:00 +03:00
|
|
|
sprintf(case_buf, kBoolSetCaseStr);
|
1998-06-04 03:32:26 +04:00
|
|
|
break;
|
|
|
|
case TYPE_LONG:
|
|
|
|
case TYPE_SHORT:
|
|
|
|
case TYPE_ULONG:
|
|
|
|
case TYPE_USHORT:
|
|
|
|
case TYPE_CHAR:
|
|
|
|
case TYPE_INT:
|
|
|
|
case TYPE_UINT:
|
|
|
|
sprintf(case_buf, kIntSetCaseStr, attr_type);
|
|
|
|
break;
|
1999-08-27 10:06:24 +04:00
|
|
|
case TYPE_JSVAL:
|
|
|
|
strcpy(case_buf, kJSValSetCaseStr);
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
case TYPE_STRING:
|
|
|
|
strcpy(case_buf, kStringSetCaseStr);
|
|
|
|
break;
|
|
|
|
case TYPE_OBJECT:
|
1998-07-09 03:44:46 +04:00
|
|
|
sprintf(case_buf, kObjectSetCaseStr, aAttribute.GetTypeName(), aAttribute.GetTypeName());
|
1998-06-04 03:32:26 +04:00
|
|
|
break;
|
1999-03-10 23:15:23 +03:00
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
// Yeah, this sucks. That's life.
|
|
|
|
{
|
|
|
|
char* p = aAttribute.GetTypeName();
|
|
|
|
if (p[0] == 'n' && p[1] == 's' && p[2] == 'I')
|
|
|
|
p += 3;
|
|
|
|
|
1999-04-27 10:14:08 +04:00
|
|
|
sprintf(case_buf, kXPIDLObjectSetCaseStr, p);
|
1999-03-10 23:15:23 +03:00
|
|
|
}
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
default:
|
|
|
|
// XXX Fail for other cases
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-05-04 07:32:12 +04:00
|
|
|
switch (aAttribute.GetType()) {
|
|
|
|
case TYPE_OBJECT: end_str = kObjectSetCaseEndStr; break;
|
|
|
|
case TYPE_XPIDL_OBJECT: end_str = kXPIDLObjectSetCaseEndStr; break;
|
|
|
|
default: end_str = ""; break;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
if (aIsPrimary) {
|
|
|
|
sprintf(buf, kSetCaseStr, attr_type, case_buf, attr_name, end_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, kSetCaseNonPrimaryStr, attr_type, case_buf,
|
|
|
|
aInterface.GetName(), aInterface.GetName(), attr_name,
|
1999-09-09 03:14:30 +04:00
|
|
|
end_str, end_str);
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
2000-02-23 01:08:36 +03:00
|
|
|
static const char kCustomPropFuncBeginStr[] = "\n"
|
|
|
|
"/***********************************************************************/\n"
|
|
|
|
"//\n"
|
|
|
|
"// %s Property %ster\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s%s%ster(JSContext *cx, JSObject *obj, jsval id, jsval *vp)\n"
|
|
|
|
"{\n"
|
|
|
|
" nsIDOM%s *a = (nsIDOM%s*)nsJSUtils::nsGetNativeThis(cx, obj);\n"
|
|
|
|
"\n"
|
|
|
|
" // If there's no private data, this must be the prototype, so ignore\n"
|
|
|
|
" if (nsnull == a) {\n"
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" nsresult rv;\n"
|
2000-03-11 09:32:42 +03:00
|
|
|
" nsIScriptSecurityManager *secMan = nsJSUtils::nsGetSecurityManager(cx, obj);\n"
|
|
|
|
" if (!secMan)\n"
|
|
|
|
" return PR_FALSE;\n"
|
2000-02-23 01:08:36 +03:00
|
|
|
" rv = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_%s_%s, %s);\n"
|
|
|
|
" if (NS_FAILED(rv)) {\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, rv);\n"
|
|
|
|
" }\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
static const char kCustomPropGetterFuncEndStr[] = "\n"
|
|
|
|
" return PR_TRUE;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char kCustomPropSetterFuncEndStr[] = "\n"
|
|
|
|
" JS_DefineProperty(cx, obj, \"%s\", *vp, nsnull, nsnull, JSPROP_ENUMERATE);\n"
|
|
|
|
" return PR_TRUE;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateCustomPropertyFuncs(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
PRBool any = PR_FALSE;
|
|
|
|
static char* get_str = "Get";
|
|
|
|
static char* set_str = "Set";
|
|
|
|
|
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
char iface_name[128];
|
|
|
|
char iface_name_upper[128];
|
|
|
|
|
|
|
|
strcpy(iface_name, iface->GetName());
|
|
|
|
strcpy(iface_name_upper, iface->GetName());
|
|
|
|
StrUpr(iface_name_upper);
|
|
|
|
|
|
|
|
int a, acount = iface->AttributeCount();
|
|
|
|
for (a = 0; a < acount; a++) {
|
|
|
|
IdlAttribute *attr = iface->GetAttributeAt(a);
|
|
|
|
|
|
|
|
if (attr->GetReplaceable()) {
|
|
|
|
char attr_name[128];
|
|
|
|
char attr_name_upper[128];
|
|
|
|
|
|
|
|
strcpy(attr_name, attr->GetName());
|
|
|
|
strcpy(attr_name_upper, attr->GetName());
|
|
|
|
StrUpr(attr_name_upper);
|
|
|
|
|
|
|
|
sprintf(buf, kCustomPropFuncBeginStr, attr_name,
|
|
|
|
get_str, iface_name, attr_name, get_str,
|
|
|
|
iface_name, iface_name, iface_name_upper, attr_name_upper,
|
|
|
|
"PR_FALSE");
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
GeneratePropGetter(file, *iface, *attr,
|
|
|
|
iface == primary_iface ? JSSTUBGEN_PRIMARY : JSSTUBGEN_NONPRIMARY);
|
|
|
|
*file << kCustomPropGetterFuncEndStr;
|
|
|
|
|
|
|
|
sprintf(buf, kCustomPropFuncBeginStr, attr_name,
|
|
|
|
set_str, iface_name, attr_name, set_str,
|
|
|
|
iface_name, iface_name, iface_name_upper, attr_name_upper,
|
|
|
|
"PR_TRUE");
|
|
|
|
*file << buf;
|
|
|
|
sprintf(buf, kCustomPropSetterFuncEndStr, attr_name);
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kFinalizeStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s finalizer\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(void)\n"
|
|
|
|
"Finalize%s(JSContext *cx, JSObject *obj)\n"
|
|
|
|
"{\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
" nsJSUtils::nsGenericFinalize(cx, obj);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateFinalize(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(0);
|
|
|
|
|
1999-06-15 07:17:11 +04:00
|
|
|
sprintf(buf, kFinalizeStr, iface->GetName(), iface->GetName());
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kEnumerateStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s enumerate\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"Enumerate%s(JSContext *cx, JSObject *obj)\n"
|
|
|
|
"{\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
" return nsJSUtils::nsGenericEnumerate(cx, obj);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_ENUMERATE(buf, className) \
|
1998-12-03 03:33:00 +03:00
|
|
|
sprintf(buf, kEnumerateStr, className, className);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateEnumerate(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(0);
|
|
|
|
char *name = iface->GetName();
|
|
|
|
|
|
|
|
JSGEN_GENERATE_ENUMERATE(buf, name);
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kResolveStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s resolve\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"Resolve%s(JSContext *cx, JSObject *obj, jsval id)\n"
|
|
|
|
"{\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
"%s"
|
1998-06-04 03:32:26 +04:00
|
|
|
"}\n";
|
|
|
|
|
1998-12-23 01:03:20 +03:00
|
|
|
static const char* kGenericResolveStr =
|
|
|
|
" return nsJSUtils::nsGenericResolve(cx, obj, id);\n";
|
|
|
|
|
|
|
|
static const char* kGlobalResolveStr =
|
|
|
|
" return nsJSUtils::nsGlobalResolve(cx, obj, id);\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_RESOLVE(buf, className, str) \
|
|
|
|
sprintf(buf, kResolveStr, className, className, str);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateResolve(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(0);
|
|
|
|
char *name = iface->GetName();
|
|
|
|
|
1998-12-23 01:03:20 +03:00
|
|
|
if (mIsGlobal) {
|
|
|
|
JSGEN_GENERATE_RESOLVE(buf, name, kGlobalResolveStr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSGEN_GENERATE_RESOLVE(buf, name, kGenericResolveStr);
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBeginStr[] = "\n\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"//\n"
|
|
|
|
"// Native method %s\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s%s(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)\n"
|
|
|
|
"{\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
" nsIDOM%s *nativeThis = (nsIDOM%s*)nsJSUtils::nsGetNativeThis(cx, obj);\n"
|
|
|
|
" nsresult result = NS_OK;\n";
|
1998-06-13 02:34:39 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBeginNonPrimaryStr[] = "\n\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
"//\n"
|
|
|
|
"// Native method %s\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s%s(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)\n"
|
|
|
|
"{\n"
|
1999-12-17 03:26:08 +03:00
|
|
|
" nsIDOM%s *privateThis = (nsIDOM%s*)nsJSUtils::nsGetNativeThis(cx, obj);\n"
|
1999-12-17 03:11:03 +03:00
|
|
|
#ifdef USE_COMPTR
|
|
|
|
" nsCOMPtr<nsIDOM%s> nativeThis;\n"
|
|
|
|
#else
|
|
|
|
" nsIDOM%sPtr nativeThis = nsnull;\n"
|
|
|
|
#endif
|
|
|
|
" nsresult result = NS_OK;\n"
|
|
|
|
#ifdef USE_COMPTR
|
|
|
|
" if (NS_OK != privateThis->QueryInterface(kI%sIID, getter_AddRefs(nativeThis))) {\n"
|
|
|
|
#else
|
|
|
|
" if (NS_OK != privateThis->QueryInterface(kI%sIID, (void **)&nativeThis)) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
#endif
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_WRONG_TYPE_ERR);\n"
|
1998-08-08 03:01:52 +04:00
|
|
|
" }\n"
|
1999-07-07 03:38:33 +04:00
|
|
|
"\n";
|
1998-08-08 03:01:52 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodReturnStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" %s nativeRet;\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodParamStr[] = " %s b%d;\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
2000-03-11 09:32:42 +03:00
|
|
|
static const char kMethodBodyBeginStr[] =
|
|
|
|
" *rval = JSVAL_NULL;\n"
|
|
|
|
" nsIScriptSecurityManager *secMan = nsJSUtils::nsGetSecurityManager(cx, obj);\n"
|
|
|
|
" if (!secMan)\n"
|
|
|
|
" return PR_FALSE;\n"
|
|
|
|
" result = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_%s_%s, PR_FALSE);\n"
|
|
|
|
" if (NS_FAILED(result)) {\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
|
|
|
" }\n";
|
1999-12-18 23:29:29 +03:00
|
|
|
|
|
|
|
static const char kMethodCheckNullStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" // If there's no private data, this must be the prototype, so ignore\n"
|
|
|
|
" if (nsnull == nativeThis) {\n"
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
1999-08-10 10:59:03 +04:00
|
|
|
" {\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-12-18 23:29:29 +03:00
|
|
|
static const char kMethodCheckNullNonPrimaryStr[] =
|
|
|
|
" // If there's no private data, this must be the prototype, so ignore\n"
|
|
|
|
#ifdef USE_COMPTR
|
|
|
|
" if (!nativeThis) {\n"
|
|
|
|
#else
|
|
|
|
" if (nativeThis.IsNull()) {\n"
|
|
|
|
#endif
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" {\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodObjectParamStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
#ifdef USE_COMPTR
|
|
|
|
" if (JS_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)(void**)getter_AddRefs(b%d),\n"
|
|
|
|
#else
|
1998-12-23 01:03:20 +03:00
|
|
|
" if (JS_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)&b%d,\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
#endif
|
1998-12-03 03:33:00 +03:00
|
|
|
" kI%sIID,\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" NS_ConvertASCIItoUCS2(\"%s\"),\n"
|
1998-12-03 03:33:00 +03:00
|
|
|
" cx,\n"
|
|
|
|
" argv[%d])) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_OBJECT_ERR);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_OBJECTPARAM(buffer, paramNum, paramType) \
|
1998-12-03 03:33:00 +03:00
|
|
|
sprintf(buffer, kMethodObjectParamStr, paramNum, paramType, \
|
|
|
|
paramType, paramNum)
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodXPIDLObjectParamStr[] =
|
1999-06-12 01:49:41 +04:00
|
|
|
#ifdef USE_COMPTR
|
2000-01-18 04:01:27 +03:00
|
|
|
" if (JS_FALSE == nsJSUtils::nsConvertJSValToXPCObject(getter_AddRefs(b%d),\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
#else
|
1999-07-23 01:57:29 +04:00
|
|
|
" if (JS_FALSE == nsJSUtils::nsConvertJSValToXPCObject((nsISupports**) &b%d,\n"
|
1999-06-12 01:49:41 +04:00
|
|
|
#endif
|
1999-04-27 10:14:08 +04:00
|
|
|
" kI%sIID, cx, argv[%d])) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_XPC_OBJECT_ERR);\n"
|
1999-04-27 10:14:08 +04:00
|
|
|
" }\n";
|
1999-03-10 23:15:23 +03:00
|
|
|
|
|
|
|
#define JSGEN_GENERATE_XPIDL_OBJECTPARAM(buffer, paramNum, paramType) \
|
1999-04-27 10:14:08 +04:00
|
|
|
sprintf(buffer, kMethodXPIDLObjectParamStr, paramNum, \
|
|
|
|
(((paramType)[0] == 'n' && (paramType)[1] == 's' && (paramType)[2] == 'I') \
|
|
|
|
? ((paramType) + 3) : (paramType)), \
|
|
|
|
paramNum)
|
1999-03-10 23:15:23 +03:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodStringParamStr[] =
|
1998-12-23 01:03:20 +03:00
|
|
|
" nsJSUtils::nsConvertJSValToString(b%d, cx, argv[%d]);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
#define JSGEN_GENERATE_STRINGPARAM(buffer, paramNum) \
|
1998-12-03 03:33:00 +03:00
|
|
|
sprintf(buffer, kMethodStringParamStr, paramNum, paramNum)
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBoolParamStr[] =
|
1998-12-23 01:03:20 +03:00
|
|
|
" if (!nsJSUtils::nsConvertJSValToBool(&b%d, cx, argv[%d])) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_BOOLEAN_ERR);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_BOOLPARAM(buffer, paramNum) \
|
|
|
|
sprintf(buffer, kMethodBoolParamStr, paramNum, paramNum)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodIntParamStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" if (!JS_ValueToInt32(cx, argv[%d], (int32 *)&b%d)) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_NUMBER_ERR);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_INTPARAM(buffer, paramNum) \
|
|
|
|
sprintf(buffer, kMethodIntParamStr, paramNum, paramNum)
|
|
|
|
|
2000-05-11 00:12:44 +04:00
|
|
|
static const char kMethodFloatParamStr[] =
|
|
|
|
" if (!JS_ValueToNumber(cx, argv[%d], &b%d)) {\n"
|
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_NUMBER_ERR);\n"
|
|
|
|
" }\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_FLOATPARAM(buffer, paramNum) \
|
|
|
|
sprintf(buffer, kMethodFloatParamStr, paramNum, paramNum)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodFuncParamStr[] =
|
1999-11-18 23:38:43 +03:00
|
|
|
#ifdef USE_COMPTR
|
|
|
|
" if (!nsJSUtils::nsConvertJSValToFunc(getter_AddRefs(b%d),\n"
|
|
|
|
#else
|
|
|
|
" if (!nsJSUtils::nsConvertJSValToFunc((nsIDOMEventListener**)(nsISupports**) &b%d,\n"
|
|
|
|
#endif
|
1999-03-29 02:19:42 +04:00
|
|
|
" cx,\n"
|
|
|
|
" obj,\n"
|
|
|
|
" argv[%d])) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_NOT_FUNCTION_ERR);\n"
|
1999-03-29 02:19:42 +04:00
|
|
|
" }\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_FUNCPARAM(buffer, paramNum, paramType) \
|
|
|
|
sprintf(buffer, kMethodFuncParamStr, paramNum, paramNum)
|
|
|
|
|
1999-08-27 10:06:24 +04:00
|
|
|
static const char kMethodJSValParamStr[] =
|
|
|
|
" b%d = argv[%d];\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_JSVALPARAM(buffer, paramNum) \
|
|
|
|
sprintf(buffer, kMethodJSValParamStr, paramNum, paramNum)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodParamListStr[] = "b%d";
|
|
|
|
static const char kMethodParamListDelimiterStr[] = ", ";
|
|
|
|
static const char kMethodParamEllipsisStr[] = "cx, argv+%d, argc-%d";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBodyMiddleStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
" result = nativeThis->%s(%s%snativeRet);\n"
|
|
|
|
" if (NS_FAILED(result)) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
|
|
|
"\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBodyMiddleNoReturnStr[] =
|
1998-06-13 02:34:39 +04:00
|
|
|
"\n"
|
1999-09-09 03:14:30 +04:00
|
|
|
" result = nativeThis->%s(%s);\n"
|
|
|
|
" if (NS_FAILED(result)) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, result);\n"
|
1998-06-13 02:34:39 +04:00
|
|
|
" }\n"
|
|
|
|
"\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodObjectRetStr[] =
|
1999-12-18 23:29:29 +03:00
|
|
|
" nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, obj, rval);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodXPIDLObjectRetStr[] =
|
1999-05-04 07:32:12 +04:00
|
|
|
" // n.b., this will release nativeRet\n"
|
2000-01-11 23:49:15 +03:00
|
|
|
" nsJSUtils::nsConvertXPCObjectToJSVal(nativeRet, NS_GET_IID(%s), cx, obj, rval);\n";
|
1999-03-10 23:15:23 +03:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodStringRetStr[] =
|
1998-12-23 01:03:20 +03:00
|
|
|
" nsJSUtils::nsConvertStringToJSVal(nativeRet, cx, rval);\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodIntRetStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" *rval = INT_TO_JSVAL(nativeRet);\n";
|
|
|
|
|
2000-05-11 00:12:44 +04:00
|
|
|
static const char kMethodFloatRetStr[] =
|
|
|
|
" *rval = DOUBLE_TO_JSVAL(js_NewDouble(cx, nativeRet));\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBoolRetStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" *rval = BOOLEAN_TO_JSVAL(nativeRet);\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodVoidRetStr[] =
|
1998-06-13 02:34:39 +04:00
|
|
|
" *rval = JSVAL_VOID;\n";
|
|
|
|
|
1999-08-27 10:06:24 +04:00
|
|
|
static const char kMethodJSValRetStr[] =
|
|
|
|
" *rval = nativeRet;\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kMethodBadParamStr[] =
|
|
|
|
" if (argc < %d) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_TOO_FEW_PARAMETERS_ERR);\n"
|
1999-08-10 10:59:03 +04:00
|
|
|
" }\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
static const char kMethodEndStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateMethods(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
ofstream *file = GetFile();
|
1998-08-08 03:01:52 +04:00
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
1998-09-11 22:05:11 +04:00
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
GetCapitalizedName(iface_name, *iface);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
int m, mcount = iface->FunctionCount();
|
|
|
|
for (m = 0; m < mcount; m++) {
|
|
|
|
IdlFunction *func = iface->GetFunctionAt(m);
|
|
|
|
IdlVariable *rval = func->GetReturnValue();
|
|
|
|
char method_name[128];
|
|
|
|
char return_type[128];
|
|
|
|
int p, pcount = func->ParameterCount();
|
|
|
|
|
1999-05-28 04:17:38 +04:00
|
|
|
if (func->GetIsNoScript()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
GetCapitalizedName(method_name, *func);
|
1998-09-17 05:53:52 +04:00
|
|
|
// If this is a constructor don't have a method for it
|
|
|
|
if (strcmp(method_name, iface_name) == 0) {
|
1998-09-11 22:05:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
GetVariableTypeForLocal(return_type, *rval);
|
1998-08-08 03:01:52 +04:00
|
|
|
if (i == 0) {
|
|
|
|
sprintf(buf, kMethodBeginStr, method_name, iface->GetName(),
|
|
|
|
method_name, iface->GetName(), iface->GetName());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, kMethodBeginNonPrimaryStr, method_name, iface->GetName(),
|
|
|
|
method_name, primary_iface->GetName(), primary_iface->GetName(),
|
|
|
|
iface->GetName(), iface->GetName(), iface->GetName());
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
1998-06-13 02:34:39 +04:00
|
|
|
if (rval->GetType() != TYPE_VOID) {
|
|
|
|
sprintf(buf, kMethodReturnStr, return_type);
|
|
|
|
*file << buf;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
for (p = 0; p < pcount; p++) {
|
|
|
|
IdlParameter *param = func->GetParameterAt(p);
|
|
|
|
|
|
|
|
GetVariableTypeForMethodLocal(return_type, *param);
|
|
|
|
sprintf(buf, kMethodParamStr, return_type, p);
|
|
|
|
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
1999-11-20 10:28:34 +03:00
|
|
|
char upr_method_name[128];
|
|
|
|
strcpy(upr_method_name, method_name);
|
|
|
|
StrUpr(upr_method_name);
|
1999-05-27 01:43:28 +04:00
|
|
|
|
1999-11-20 10:28:34 +03:00
|
|
|
char upr_iface_name[128];
|
|
|
|
strcpy(upr_iface_name, iface->GetName());
|
|
|
|
StrUpr(upr_iface_name);
|
1999-05-27 01:43:28 +04:00
|
|
|
|
1999-11-20 10:28:34 +03:00
|
|
|
sprintf(buf, kMethodBodyBeginStr, upr_iface_name,
|
|
|
|
upr_method_name);
|
1999-12-18 23:29:29 +03:00
|
|
|
if (i == 0) {
|
|
|
|
*file << kMethodCheckNullStr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*file << kMethodCheckNullNonPrimaryStr;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
1999-12-18 23:29:29 +03:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
if (pcount > 0) {
|
1999-12-18 23:29:29 +03:00
|
|
|
sprintf(buf, kMethodBadParamStr, pcount);
|
|
|
|
*file << buf;
|
1999-08-10 10:59:03 +04:00
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
for (p = 0; p < pcount; p++) {
|
|
|
|
IdlParameter *param = func->GetParameterAt(p);
|
|
|
|
|
|
|
|
switch(param->GetType()) {
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
JSGEN_GENERATE_BOOLPARAM(buf, p);
|
|
|
|
break;
|
|
|
|
case TYPE_LONG:
|
|
|
|
case TYPE_SHORT:
|
|
|
|
case TYPE_ULONG:
|
|
|
|
case TYPE_USHORT:
|
|
|
|
case TYPE_CHAR:
|
|
|
|
case TYPE_INT:
|
|
|
|
case TYPE_UINT:
|
|
|
|
JSGEN_GENERATE_INTPARAM(buf, p);
|
|
|
|
break;
|
2000-05-11 00:12:44 +04:00
|
|
|
case TYPE_FLOAT:
|
|
|
|
JSGEN_GENERATE_FLOATPARAM(buf, p);
|
|
|
|
break;
|
1999-08-27 10:06:24 +04:00
|
|
|
case TYPE_JSVAL:
|
|
|
|
JSGEN_GENERATE_JSVALPARAM(buf, p);
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
case TYPE_STRING:
|
|
|
|
JSGEN_GENERATE_STRINGPARAM(buf, p);
|
|
|
|
break;
|
|
|
|
case TYPE_OBJECT:
|
|
|
|
JSGEN_GENERATE_OBJECTPARAM(buf, p, param->GetTypeName());
|
|
|
|
break;
|
1999-03-10 23:15:23 +03:00
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
JSGEN_GENERATE_XPIDL_OBJECTPARAM(buf, p, param->GetTypeName());
|
|
|
|
break;
|
1999-03-29 02:19:42 +04:00
|
|
|
case TYPE_FUNC:
|
|
|
|
JSGEN_GENERATE_FUNCPARAM(buf, p, param->GetTypeName());
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
default:
|
|
|
|
// XXX Fail for other cases
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char param_buf[512];
|
|
|
|
char *param_ptr = param_buf;
|
|
|
|
param_buf[0] = '\0';
|
|
|
|
for (p = 0; p < pcount; p++) {
|
1998-06-13 02:34:39 +04:00
|
|
|
if (p > 0) {
|
|
|
|
strcpy(param_ptr, kMethodParamListDelimiterStr);
|
|
|
|
param_ptr += strlen(param_ptr);
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
sprintf(param_ptr, kMethodParamListStr, p);
|
|
|
|
param_ptr += strlen(param_ptr);
|
|
|
|
}
|
|
|
|
|
1998-07-09 03:44:46 +04:00
|
|
|
if (func->GetHasEllipsis()) {
|
1998-06-23 22:43:06 +04:00
|
|
|
if (pcount > 0) {
|
|
|
|
strcpy(param_ptr, kMethodParamListDelimiterStr);
|
1998-07-09 03:44:46 +04:00
|
|
|
param_ptr += strlen(param_ptr);
|
|
|
|
}
|
|
|
|
sprintf(param_ptr, kMethodParamEllipsisStr, pcount, pcount);
|
|
|
|
param_ptr += strlen(param_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rval->GetType() != TYPE_VOID) {
|
|
|
|
if ((pcount > 0) || func->GetHasEllipsis()) {
|
|
|
|
strcpy(param_ptr, kMethodParamListDelimiterStr);
|
1998-06-23 22:43:06 +04:00
|
|
|
}
|
1998-06-13 02:34:39 +04:00
|
|
|
sprintf(buf, kMethodBodyMiddleStr, method_name, param_buf,
|
|
|
|
rval->GetType() == TYPE_STRING ? "" : "&");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, kMethodBodyMiddleNoReturnStr, method_name, param_buf);
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
switch(rval->GetType()) {
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
*file << kMethodBoolRetStr;
|
|
|
|
break;
|
|
|
|
case TYPE_LONG:
|
|
|
|
case TYPE_SHORT:
|
|
|
|
case TYPE_ULONG:
|
|
|
|
case TYPE_USHORT:
|
|
|
|
case TYPE_CHAR:
|
|
|
|
case TYPE_INT:
|
|
|
|
case TYPE_UINT:
|
|
|
|
*file << kMethodIntRetStr;
|
|
|
|
break;
|
2000-05-11 00:12:44 +04:00
|
|
|
case TYPE_FLOAT:
|
|
|
|
*file << kMethodFloatRetStr;
|
|
|
|
break;
|
1999-08-27 10:06:24 +04:00
|
|
|
case TYPE_JSVAL:
|
|
|
|
*file << kMethodJSValRetStr;
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
case TYPE_STRING:
|
|
|
|
*file << kMethodStringRetStr;
|
|
|
|
break;
|
|
|
|
case TYPE_OBJECT:
|
|
|
|
*file << kMethodObjectRetStr;
|
|
|
|
break;
|
1999-03-10 23:15:23 +03:00
|
|
|
case TYPE_XPIDL_OBJECT:
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
sprintf(buf, kMethodXPIDLObjectRetStr, rval->GetTypeName());
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
break;
|
1998-06-13 02:34:39 +04:00
|
|
|
case TYPE_VOID:
|
|
|
|
*file << kMethodVoidRetStr;
|
|
|
|
break;
|
1998-06-04 03:32:26 +04:00
|
|
|
default:
|
|
|
|
// XXX Fail for other cases
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
*file << kMethodEndStr;
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kJSClassStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n/***********************************************************************/\n"
|
|
|
|
"//\n"
|
|
|
|
"// class for %s\n"
|
|
|
|
"//\n"
|
|
|
|
"JSClass %sClass = {\n"
|
|
|
|
" \"%s\", \n"
|
1999-05-12 06:05:48 +04:00
|
|
|
" JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS,\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" JS_PropertyStub,\n"
|
|
|
|
" JS_PropertyStub,\n"
|
|
|
|
" Get%sProperty,\n"
|
|
|
|
" Set%sProperty,\n"
|
|
|
|
" Enumerate%s,\n"
|
|
|
|
" Resolve%s,\n"
|
|
|
|
" JS_ConvertStub,\n"
|
2000-02-02 03:22:58 +03:00
|
|
|
" Finalize%s,\n"
|
|
|
|
" nsnull,\n"
|
|
|
|
" nsJSUtils::nsCheckAccess\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"};\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_JSCLASS(buf, className) \
|
|
|
|
sprintf(buf, kJSClassStr, className, className, className, className, \
|
|
|
|
className, className, className, className);
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateJSClass(IdlSpecification &aSpec)
|
|
|
|
{
|
2000-02-02 03:22:58 +03:00
|
|
|
char buf[1024];
|
1998-06-04 03:32:26 +04:00
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(0);
|
|
|
|
char *name = iface->GetName();
|
|
|
|
|
|
|
|
JSGEN_GENERATE_JSCLASS(buf, name);
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropSpecBeginStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s class properties\n"
|
|
|
|
"//\n"
|
|
|
|
"static JSPropertySpec %sProperties[] =\n"
|
|
|
|
"{\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropSpecEntryStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" {\"%s\", %s_%s, JSPROP_ENUMERATE%s},\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropSpecReadOnlyStr[] = " | JSPROP_READONLY";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
2000-02-23 01:08:36 +03:00
|
|
|
static const char kPropSpecEntryReplaceableStr[] =
|
|
|
|
" {\"%s\", %s_%s, JSPROP_ENUMERATE, %s%sGetter, %s%sSetter},\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kPropSpecEndStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" {0}\n"
|
|
|
|
"};\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateClassProperties(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
|
|
|
|
sprintf(buf, kPropSpecBeginStr, primary_iface->GetName(),
|
|
|
|
primary_iface->GetName());
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
strcpy(iface_name, iface->GetName());
|
|
|
|
StrUpr(iface_name);
|
|
|
|
int a, acount = iface->AttributeCount();
|
|
|
|
for (a = 0; a < acount; a++) {
|
|
|
|
IdlAttribute *attr = iface->GetAttributeAt(a);
|
|
|
|
char attr_name[128];
|
|
|
|
|
1999-05-28 04:17:38 +04:00
|
|
|
if (attr->GetIsNoScript()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
strcpy(attr_name, attr->GetName());
|
|
|
|
StrUpr(attr_name);
|
|
|
|
|
2000-02-23 01:08:36 +03:00
|
|
|
if (attr->GetReplaceable()) {
|
|
|
|
sprintf(buf, kPropSpecEntryReplaceableStr, attr->GetName(),
|
|
|
|
iface_name, attr_name, iface->GetName(), attr->GetName(),
|
|
|
|
iface->GetName(), attr->GetName());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, kPropSpecEntryStr, attr->GetName(),
|
|
|
|
iface_name, attr_name,
|
|
|
|
attr->GetReadOnly() ? kPropSpecReadOnlyStr : "");
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*file << kPropSpecEndStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kFuncSpecBeginStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s class methods\n"
|
|
|
|
"//\n"
|
|
|
|
"static JSFunctionSpec %sMethods[] = \n"
|
|
|
|
"{\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kFuncSpecEntryStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" {\"%s\", %s%s, %d},\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kFuncSpecEndStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
" {0}\n"
|
|
|
|
"};\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateClassFunctions(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
|
|
|
|
sprintf(buf, kFuncSpecBeginStr, primary_iface->GetName(),
|
|
|
|
primary_iface->GetName());
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
1998-09-11 22:05:11 +04:00
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
GetCapitalizedName(iface_name, *iface);
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
int m, mcount = iface->FunctionCount();
|
|
|
|
for (m = 0; m < mcount; m++) {
|
|
|
|
char method_name[128];
|
|
|
|
IdlFunction *func = iface->GetFunctionAt(m);
|
|
|
|
|
1999-05-28 04:17:38 +04:00
|
|
|
if (func->GetIsNoScript()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
GetCapitalizedName(method_name, *func);
|
1998-09-17 05:53:52 +04:00
|
|
|
// If this is a constructor don't have a method for it.
|
|
|
|
if (strcmp(method_name, iface_name) == 0) {
|
1998-09-11 22:05:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
sprintf(buf, kFuncSpecEntryStr, func->GetName(),
|
|
|
|
iface->GetName(), method_name,
|
|
|
|
func->ParameterCount());
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*file << kFuncSpecEndStr;
|
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kEmptyConstructorStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s constructor\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)\n"
|
|
|
|
"{\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" return JS_FALSE;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"}\n";
|
|
|
|
|
1998-09-17 05:53:52 +04:00
|
|
|
#define JSGEN_GENERATE_EMPTYCONSTRUCTOR(buf, className) \
|
|
|
|
sprintf(buf, kEmptyConstructorStr, className, className);
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kConstructorBeginStr[] =
|
1998-09-17 05:53:52 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s constructor\n"
|
|
|
|
"//\n"
|
|
|
|
"PR_STATIC_CALLBACK(JSBool)\n"
|
|
|
|
"%s(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)\n"
|
|
|
|
"{\n"
|
|
|
|
" nsresult result;\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
" nsIID classID;\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" nsIDOM%s *nativeThis;\n"
|
|
|
|
" nsIScriptObjectOwner *owner = nsnull;\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
" nsIJSNativeInitializer* initializer = nsnull;\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
"\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
" static NS_DEFINE_IID(kIDOM%sIID, NS_IDOM%s_IID);\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
" static NS_DEFINE_IID(kIJSNativeInitializerIID, NS_IJSNATIVEINITIALIZER_IID);\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
"\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" nsCOMPtr<nsIScriptContext> scriptCX;\n"
|
|
|
|
" nsJSUtils::nsGetStaticScriptContext(cx, obj, getter_AddRefs(scriptCX));\n"
|
|
|
|
" if (!scriptCX) {\n"
|
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" nsCOMPtr<nsIScriptNameSpaceManager> manager;\n"
|
|
|
|
" scriptCX->GetNameSpaceManager(getter_AddRefs(manager));\n"
|
|
|
|
" if (!manager) {\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
2000-04-22 01:29:19 +04:00
|
|
|
" result = manager->LookupName(NS_ConvertASCIItoUCS2(\"%s\"), PR_TRUE, classID);\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" if (NS_OK != result) {\n"
|
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
1999-03-09 12:44:27 +03:00
|
|
|
" result = nsComponentManager::CreateInstance(classID,\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" nsnull,\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
" kIDOM%sIID,\n"
|
|
|
|
" (void **)&nativeThis);\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" if (NS_OK != result) {\n"
|
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
" result = nativeThis->QueryInterface(kIJSNativeInitializerIID, (void **)&initializer);\n"
|
1999-05-07 23:37:21 +04:00
|
|
|
" if (NS_OK == result) {\n"
|
1999-12-18 23:29:29 +03:00
|
|
|
" result = initializer->Initialize(cx, obj, argc, argv);\n"
|
1999-05-07 23:37:21 +04:00
|
|
|
" NS_RELEASE(initializer);\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
"\n"
|
1999-05-07 23:37:21 +04:00
|
|
|
" if (NS_OK != result) {\n"
|
|
|
|
" NS_RELEASE(nativeThis);\n"
|
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
1999-05-05 00:48:50 +04:00
|
|
|
" }\n"
|
1998-12-23 01:03:20 +03:00
|
|
|
"\n"
|
1998-09-17 05:53:52 +04:00
|
|
|
" result = nativeThis->QueryInterface(kIScriptObjectOwnerIID, (void **)&owner);\n"
|
|
|
|
" if (NS_OK != result) {\n"
|
|
|
|
" NS_RELEASE(nativeThis);\n"
|
|
|
|
" return JS_FALSE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" owner->SetScriptObject((void *)obj);\n"
|
|
|
|
" JS_SetPrivate(cx, obj, nativeThis);\n"
|
|
|
|
"\n"
|
|
|
|
" NS_RELEASE(owner);\n"
|
|
|
|
" return JS_TRUE;\n"
|
|
|
|
"}";
|
1998-12-23 01:03:20 +03:00
|
|
|
|
|
|
|
#define JSGEN_GENERATE_CONSTRUCTOR(buf, className, caps) \
|
|
|
|
sprintf(buf, kConstructorBeginStr, className, className, className, \
|
1999-12-18 23:29:29 +03:00
|
|
|
className, caps, className, className)
|
1998-12-23 01:03:20 +03:00
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
void
|
|
|
|
JSStubGen::GenerateConstructor(IdlSpecification &aSpec)
|
|
|
|
{
|
1999-12-18 23:29:29 +03:00
|
|
|
char buf[4096];
|
1998-06-04 03:32:26 +04:00
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
1998-09-17 05:53:52 +04:00
|
|
|
IdlFunction *constructor;
|
|
|
|
char *name = primary_iface->GetName();
|
|
|
|
char caps_name[128];
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1998-09-17 05:53:52 +04:00
|
|
|
strcpy(caps_name, name);
|
|
|
|
StrUpr(caps_name);
|
|
|
|
|
|
|
|
if (HasConstructor(*primary_iface, &constructor)) {
|
1998-12-23 01:03:20 +03:00
|
|
|
JSGEN_GENERATE_CONSTRUCTOR(buf, name, caps_name);
|
1998-09-17 05:53:52 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
1999-09-24 03:52:09 +04:00
|
|
|
else if (!mIsGlobal) {
|
1998-09-17 05:53:52 +04:00
|
|
|
JSGEN_GENERATE_EMPTYCONSTRUCTOR(buf, name);
|
|
|
|
*file << buf;
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
}
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kGlobalInitClassStr[] =
|
1998-07-03 04:48:06 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s class initialization\n"
|
|
|
|
"//\n"
|
|
|
|
"nsresult NS_Init%sClass(nsIScriptContext *aContext, \n"
|
|
|
|
" nsIScriptGlobalObject *aGlobal)\n"
|
|
|
|
"{\n"
|
|
|
|
" JSContext *jscontext = (JSContext *)aContext->GetNativeContext();\n"
|
|
|
|
" JSObject *global = JS_GetGlobalObject(jscontext);\n"
|
|
|
|
"\n"
|
|
|
|
" JS_DefineProperties(jscontext, global, %sProperties);\n"
|
|
|
|
" JS_DefineFunctions(jscontext, global, %sMethods);\n"
|
|
|
|
"\n"
|
|
|
|
" return NS_OK;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_GLOBALINITCLASS(buffer, className) \
|
|
|
|
sprintf(buffer, kGlobalInitClassStr, className, className, className, \
|
|
|
|
className)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitClassBeginStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// %s class initialization\n"
|
|
|
|
"//\n"
|
1999-02-17 11:32:14 +03:00
|
|
|
"extern \"C\" NS_DOM nsresult NS_Init%sClass(nsIScriptContext *aContext, void **aPrototype)\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"{\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" JSContext *jscontext = (JSContext *)aContext->GetNativeContext();\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" JSObject *proto = nsnull;\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" JSObject *constructor = nsnull;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" JSObject *parent_proto = nsnull;\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" JSObject *global = JS_GetGlobalObject(jscontext);\n"
|
|
|
|
" jsval vp;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" if ((PR_TRUE != JS_LookupProperty(jscontext, global, \"%s\", &vp)) ||\n"
|
|
|
|
" !JSVAL_IS_OBJECT(vp) ||\n"
|
|
|
|
" ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) ||\n"
|
|
|
|
" (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), \"prototype\", &vp)) || \n"
|
|
|
|
" !JSVAL_IS_OBJECT(vp)) {\n\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
#define JSGEN_GENERATE_INITCLASSBEGIN(buffer, className) \
|
|
|
|
sprintf(buffer, kInitClassBeginStr, className, className, className)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kGetParentProtoStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" if (NS_OK != NS_Init%sClass(aContext, (void **)&parent_proto)) {\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
" }\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitClassBodyStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" proto = JS_InitClass(jscontext, // context\n"
|
|
|
|
" global, // global object\n"
|
|
|
|
" parent_proto, // parent proto \n"
|
|
|
|
" &%sClass, // JSClass\n"
|
|
|
|
" %s, // JSNative ctor\n"
|
|
|
|
" 0, // ctor args\n"
|
|
|
|
" %sProperties, // proto props\n"
|
|
|
|
" %sMethods, // proto funcs\n"
|
|
|
|
" nsnull, // ctor props (static)\n"
|
|
|
|
" nsnull); // ctor funcs (static)\n"
|
|
|
|
" if (nsnull == proto) {\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_INITCLASSBODY(buffer, className) \
|
|
|
|
sprintf(buffer, kInitClassBodyStr, className, className, \
|
1998-06-23 22:43:06 +04:00
|
|
|
className, className)
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kAliasConstructorStr[] =
|
1998-09-11 22:05:11 +04:00
|
|
|
" JS_AliasProperty(jscontext, global, \"%s\", \"%s\");\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitStaticBeginStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" if ((PR_TRUE == JS_LookupProperty(jscontext, global, \"%s\", &vp)) &&\n"
|
|
|
|
" JSVAL_IS_OBJECT(vp) &&\n"
|
|
|
|
" ((constructor = JSVAL_TO_OBJECT(vp)) != nsnull)) {\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitStaticEntryStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" vp = INT_TO_JSVAL(nsIDOM%s::%s);\n"
|
|
|
|
" JS_SetProperty(jscontext, constructor, \"%s\", &vp);\n"
|
|
|
|
"\n";
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitStaticEndStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" }\n"
|
|
|
|
"\n";
|
1998-06-04 03:32:26 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kInitClassEndStr[] =
|
1998-06-23 22:43:06 +04:00
|
|
|
" }\n"
|
|
|
|
" else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) {\n"
|
|
|
|
" proto = JSVAL_TO_OBJECT(vp);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
" }\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"\n"
|
|
|
|
" if (aPrototype) {\n"
|
|
|
|
" *aPrototype = proto;\n"
|
|
|
|
" }\n"
|
|
|
|
" return NS_OK;\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateInitClass(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[2048];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
char *primary_class = primary_iface->GetName();
|
|
|
|
|
1998-07-03 04:48:06 +04:00
|
|
|
if (mIsGlobal) {
|
|
|
|
JSGEN_GENERATE_GLOBALINITCLASS(buf, primary_class);
|
|
|
|
*file << buf;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
JSGEN_GENERATE_INITCLASSBEGIN(buf, primary_class);
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
if (primary_iface->BaseClassCount() > 0) {
|
|
|
|
char *base_class = primary_iface->GetBaseClassAt(0);
|
|
|
|
|
|
|
|
sprintf(buf, kGetParentProtoStr, base_class);
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSGEN_GENERATE_INITCLASSBODY(buf, primary_class);
|
|
|
|
*file << buf;
|
|
|
|
|
1998-09-11 22:05:11 +04:00
|
|
|
int i, icount = aSpec.InterfaceCount();
|
|
|
|
for (i = 0; i < icount; i++) {
|
|
|
|
IdlInterface *iface = aSpec.GetInterfaceAt(i);
|
|
|
|
char iface_name[128];
|
|
|
|
|
|
|
|
GetCapitalizedName(iface_name, *iface);
|
|
|
|
|
|
|
|
int m, mcount = iface->FunctionCount();
|
|
|
|
for (m = 0; m < mcount; m++) {
|
|
|
|
char method_name[128];
|
|
|
|
IdlFunction *func = iface->GetFunctionAt(m);
|
|
|
|
|
|
|
|
GetCapitalizedName(method_name, *func);
|
|
|
|
// If this is a constructor defined in a non-primary interface
|
|
|
|
// don't have a method for it...we'll alias it to the constructor
|
|
|
|
// for the primary interface.
|
|
|
|
if ((strcmp(method_name, iface_name) == 0) &&
|
|
|
|
(iface != primary_iface)) {
|
|
|
|
sprintf(buf, kAliasConstructorStr, primary_class, method_name);
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int c, ccount = primary_iface->ConstCount();
|
1998-06-23 22:43:06 +04:00
|
|
|
if (ccount > 0) {
|
|
|
|
sprintf(buf, kInitStaticBeginStr, primary_iface->GetName());
|
|
|
|
*file << buf;
|
|
|
|
|
|
|
|
for (c = 0; c < ccount; c++) {
|
|
|
|
IdlVariable *var = primary_iface->GetConstAt(c);
|
|
|
|
|
|
|
|
if (NULL != var) {
|
|
|
|
sprintf(buf, kInitStaticEntryStr, primary_iface->GetName(),
|
|
|
|
var->GetName(), var->GetName());
|
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*file << kInitStaticEndStr;
|
|
|
|
}
|
|
|
|
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << kInitClassEndStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kNewGlobalJSObjectStr[] =
|
1998-07-03 04:48:06 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// Method for creating a new %s JavaScript object\n"
|
|
|
|
"//\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
"extern \"C\" NS_DOM nsresult NS_NewScript%s(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn)\n"
|
1998-07-03 04:48:06 +04:00
|
|
|
"{\n"
|
|
|
|
" NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, \"null arg\");\n"
|
|
|
|
" JSContext *jscontext = (JSContext *)aContext->GetNativeContext();\n"
|
|
|
|
"\n"
|
|
|
|
" JSObject *global = ::JS_NewObject(jscontext, &%sClass, NULL, NULL);\n"
|
|
|
|
" if (global) {\n"
|
|
|
|
" // The global object has a to be defined in two step:\n"
|
|
|
|
" // 1- create a generic object, with no prototype and no parent which\n"
|
|
|
|
" // will be passed to JS_InitStandardClasses. JS_InitStandardClasses \n"
|
|
|
|
" // will make it the global object\n"
|
|
|
|
" // 2- define the global object to be what you really want it to be.\n"
|
|
|
|
" //\n"
|
|
|
|
" // The js runtime is not fully initialized before JS_InitStandardClasses\n"
|
|
|
|
" // is called, so part of the global object initialization has to be moved \n"
|
|
|
|
" // after JS_InitStandardClasses\n"
|
|
|
|
"\n"
|
1999-06-15 07:17:11 +04:00
|
|
|
" // assign \"this\" to the js object\n"
|
1998-07-03 04:48:06 +04:00
|
|
|
" ::JS_SetPrivate(jscontext, global, aSupports);\n"
|
1999-06-15 07:17:11 +04:00
|
|
|
" NS_ADDREF(aSupports);\n"
|
1998-07-03 04:48:06 +04:00
|
|
|
"\n"
|
1998-09-03 05:19:58 +04:00
|
|
|
" JS_DefineProperties(jscontext, global, %sProperties);\n"
|
|
|
|
" JS_DefineFunctions(jscontext, global, %sMethods);\n"
|
|
|
|
"\n"
|
1998-07-03 04:48:06 +04:00
|
|
|
" *aReturn = (void*)global;\n"
|
|
|
|
" return NS_OK;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_NEWGLOBALJSOBJECT(buffer, className) \
|
1998-09-05 03:03:16 +04:00
|
|
|
sprintf(buffer, kNewGlobalJSObjectStr, className, \
|
1998-09-03 05:19:58 +04:00
|
|
|
className, className, className, className)
|
1998-07-03 04:48:06 +04:00
|
|
|
|
1999-08-10 10:59:03 +04:00
|
|
|
static const char kNewJSObjectStr[] =
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n\n//\n"
|
|
|
|
"// Method for creating a new %s JavaScript object\n"
|
|
|
|
"//\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
"extern \"C\" NS_DOM nsresult NS_NewScript%s(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn)\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"{\n"
|
|
|
|
" NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, \"null argument to NS_NewScript%s\");\n"
|
|
|
|
" JSObject *proto;\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" JSObject *parent;\n"
|
|
|
|
" nsIScriptObjectOwner *owner;\n"
|
|
|
|
" JSContext *jscontext = (JSContext *)aContext->GetNativeContext();\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
" nsresult result = NS_OK;\n"
|
|
|
|
" nsIDOM%s *a%s;\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
"\n"
|
|
|
|
" if (nsnull == aParent) {\n"
|
|
|
|
" parent = nsnull;\n"
|
|
|
|
" }\n"
|
|
|
|
" else if (NS_OK == aParent->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) {\n"
|
|
|
|
" if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) {\n"
|
|
|
|
" NS_RELEASE(owner);\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
" }\n"
|
|
|
|
" NS_RELEASE(owner);\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" if (NS_OK != NS_Init%sClass(aContext, (void **)&proto)) {\n"
|
1998-06-16 00:35:45 +04:00
|
|
|
" return NS_ERROR_FAILURE;\n"
|
|
|
|
" }\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
"\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
" result = aSupports->QueryInterface(kI%sIID, (void **)&a%s);\n"
|
|
|
|
" if (NS_OK != result) {\n"
|
|
|
|
" return result;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" // create a js object for this class\n"
|
1998-06-23 22:43:06 +04:00
|
|
|
" *aReturn = JS_NewObject(jscontext, &%sClass, proto, parent);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" if (nsnull != *aReturn) {\n"
|
|
|
|
" // connect the native object to the js object\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
" JS_SetPrivate(jscontext, (JSObject *)*aReturn, a%s);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
1998-09-05 03:03:16 +04:00
|
|
|
" NS_RELEASE(a%s);\n"
|
1998-06-04 03:32:26 +04:00
|
|
|
" return NS_ERROR_FAILURE; \n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return NS_OK;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
#define JSGEN_GENERATE_NEWJSOBJECT(buffer, className) \
|
|
|
|
sprintf(buffer, kNewJSObjectStr, className, className, \
|
1998-09-05 03:03:16 +04:00
|
|
|
className, className, className, className, \
|
|
|
|
className, className, className, className, className)
|
1998-06-04 03:32:26 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
JSStubGen::GenerateNew(IdlSpecification &aSpec)
|
|
|
|
{
|
|
|
|
char buf[2048];
|
|
|
|
ofstream *file = GetFile();
|
|
|
|
IdlInterface *primary_iface = aSpec.GetInterfaceAt(0);
|
|
|
|
char *primary_class = primary_iface->GetName();
|
|
|
|
|
1998-07-03 04:48:06 +04:00
|
|
|
if (mIsGlobal) {
|
|
|
|
JSGEN_GENERATE_NEWGLOBALJSOBJECT(buf, primary_class);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSGEN_GENERATE_NEWJSOBJECT(buf, primary_class);
|
|
|
|
}
|
1998-06-04 03:32:26 +04:00
|
|
|
*file << buf;
|
|
|
|
}
|
|
|
|
|