Generating callable objects in SpiderMonkey

Source: Internet
Author: User
Tags hash

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.