I should have a more detailed explanation of the title: in C, a SpiderMonkey object that can be invoked as a function in JavaScript, in other words, is a non function object, Can be invoked as a function in JavaScript. For example, I have a hash object that, when I am in var h = new Hash (), can call H (key) directly, H is not a function object, but can get the value of key keys in this function call.
First, you must set the "Call" field in Jsclass to the appropriate function when you create the structure of this hash class, as follows:
static
JSBool call_hash(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval){
/* here the obj refers to the global object, not the callee itself */
*rval = JSVAL_NULL;
return JS_TRUE;
}
static
JSClass hash_class = {
"Hash", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
finalize_hash,
0,
0,
call_hash,
0,
0,
0,
0,
0
};
Now, the key question here is how to get the Call_hash function to know who the called object (callee) is when SpiderMonkey calls the Call_hash function. However, the Mozilla Official document does not provide any explanation for this. So I asked the question in my mailing list, and someone gave me a very special technique--when the engine called the call function, Argv[-2 was the caller itself.
After playing with SpiderMonkey for a while, I still intend to give up spidermonkey, although this is a very mature and powerful scripting engine, but his API is still a bit confusing, from this article of the solution to the problem can be seen.