In one A.cpp file, I have defined a static array of JSFunctionSpec,
like this:
static JSFunctionSpec JProfFunctions[] = {
{"JProfStartProfiling", JProfStartProfiling,
0, 0, 0
},
{"JProfStopProfiling", JProfStopProfiling,
0, 0, 0},
{nsnull, nsnull,
0, 0, 0}
};
Try this:
In A.cpp:
JSFunctionSpec * my_funcs()
{
static std::vector<JSFunctionSpec *> funcs;
if( funcs.empty() ){
... populate funcs and end with a trailing null
JSFunctionSpec because JS_DefinFunctions() likes
is that way ...
}
return &funcs[0];
]
Now add that function to your API (A.hpp) so you can reference it from
B.cpp.
[Then, since you're using SpiderMonkey from C++, take a look at my C++
toolkit for SpiderMonkey, which simplifies this type of thing:
http://SpiderApe.sf.net ]
Here's an example taken from the SpiderApe code:
typedef std::vector<JSFunctionSpec *> JSFuncVector;
/**
Used by MonkeyWrapper to get the default array of
script-callable functions.
You may use this vector with JS_DefineFunctions(), but
should follow this guideline:
<pre>
JSFuncVector myvec( standard_js_functions() );
myvec.push_back( 0 );
JS_DefineFunctions(cx,obj, myvec[0]);
</pre>
This ensures that the vector ends with a NULL element, as
required by JS_DefineFunctions(). Since arbitrary code can
add functions to standard_js_functions(), there is normally
no guaranty that the final element is null. It is poor
practice
to add a NULL element to standard_js_functions() because
elements
added after it would be ignored by JS_DefineFunctions().
On the first call to this function, the returned vector is
pre-populated with whatever functions the ape API provides
for script-side code, so this vector will always have at
least a few entries unless client code is foolish enough to
empty it.
*/
APE_PUBLIC_API JSFuncVector & standard_js_functions();
JSBool
init_functions( JSContext * cx, JSObject * obj )
{
/**
2006.05.30: we COPY standard_js_functions() because
older internal code used vector::reserve() and
operator[] to populate the vector. This led to an
incorrect value being returned from vector::size()
and an
uncertainty about whether or not the vector has a
trailing
NULL entry (needed by JS_DefineFunctions()). It is
a wonder
that it ever worked before.
*/
JSFuncVector funcs( standard_js_functions() );
funcs.push_back( 0 ); // ensure a trailing NULL entry
if( ! JS_DefineFunctions( cx, obj, funcs[0] ) )
{
JS_ReportError(cx,"ape::init_functions():
JS_DefineFunctions() failed!");
return JS_FALSE;
}
return JS_TRUE;
}
----- stephan