spidermonkey note

/* spidermonkey 3 basic element
1.runtime
2.context
3.global object
*/

/*
	usefull apis
    1.获取参数: jsval *argv = JS_ARGV(cx, vp);
	2.获得调用该方法的对象object:JSObject*obj = JS_THIS_OBJECT(cx,vp);
    3.设置私有数据
    	3.1 JS_SetPrivate(jsObj,void*)
        	reqiure JSClass.flags 存在 ‘JSCLASS_HAS_PRIVATE‘
        3.2 void JS_SetReservedSlot(JSObject *obj, uint32_t index, jsval v);
        	要求 JSClass.flags has ‘JSCLASS_HAS_RESERVED_SLOTS(n)‘
    4. 	用于回调js代码里函数:
    	JS_CallFunctionValue
    	JS_CallFunction
        JS_CallFunctionValue */
可以通过3.中的方法,将js对应的对象和方法保存起来,通过C代码回调(4中的函数)来调用js代码。
// 下面的代码将js对象和对象的方法通过JS_SetReservedSlot()保存起来
static void addCallBackAndThis(JSObject *obj, jsval callback, jsval &thisObj)
{
    if(callback != JSVAL_VOID) {
        ScriptingCore::getInstance()->setReservedSpot(0, obj, callback);
    }
    if(thisObj != JSVAL_VOID) {
        ScriptingCore::getInstance()->setReservedSpot(1, obj, thisObj);
    }
}

JSBool ScriptingCore::setReservedSpot(uint32_t i, JSObject *obj, jsval value) {
    JS_SetReservedSlot(obj, i, value);
    return JS_TRUE;
}

// 下面则通过JS_CallFunctionXXX 系列函数产生js的回调。

int ScriptingCore::handleMenuClickedEvent(void* data)
{
    if (NULL == data)
        return 0;

    BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
    if (NULL == basicScriptData->nativeObject)
        return 0;

    MenuItem* menuItem = static_cast<MenuItem*>(basicScriptData->nativeObject);

    js_proxy_t * p = jsb_get_native_proxy(menuItem);
    if (!p) return 0;
    jsval retval;
    jsval dataVal;
    js_proxy_t *proxy = jsb_get_native_proxy(menuItem);
    dataVal = (proxy ? OBJECT_TO_JSVAL(proxy->obj) : JSVAL_NULL);
    executeJSFunctionFromReservedSpot(this->_cx, p->obj, dataVal, retval);
    return 1;
}
static void executeJSFunctionFromReservedSpot(JSContext *cx, JSObject *obj,
                                              jsval &dataVal, jsval &retval) {
    jsval func = JS_GetReservedSlot(obj, 0);
    if (func == JSVAL_VOID) { return; }
    jsval thisObj = JS_GetReservedSlot(obj, 1);
    JSAutoCompartment ac(cx, obj);

    if (thisObj == JSVAL_VOID) {
        JS_CallFunctionValue(cx, obj, func, 1, &dataVal, &retval);
    } else {
        assert(!JSVAL_IS_PRIMITIVE(thisObj));
        JS_CallFunctionValue(cx, JSVAL_TO_OBJECT(thisObj), func, 1, &dataVal, &retval);
    }
}
时间: 2024-10-15 13:50:44

spidermonkey note的相关文章

383.判断一个字符串是否能够包含另外一个字符串 Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the

thinking in java ----reading note (1)

# thinking in java 4th# reading note# victor# 2016.02.10 chapter 1 对象入门 1.1 抽象的进步    (1) 所有东西都是对象.    (2) 程序是一大堆对象的组合,对象间通过消息联系.    (3) 通过封装现有对象,可制作出新型对象.    (4) 每个对象都有一种类型(某个类的实例).    (5) 同一类的所有对象都能接受相同的消息.    1.2 对象的接口 & 1.3 实现方法的隐藏     接口规定了可对一个特定

Mozilla正在SpiderMonkey中测试JavaScript并行计算

Mozilla正致力于实现JavaScript“并行(parallelism)计算”,以便充分利用硬件性能.Dave Herman是Mozilla Research的首席研究员和策略总监.近日,他在一篇博文中提到,他们正在其JavaScript引擎SpiderMonkey中进行相关实验. 他们试图通过一种底层的.渐进的方法为JavaScript添加更加灵活和强大的并行计算原语.他首先指出: 我正在谈论的不是“并发(concurrency)”……JavaScript的异步并发模型很受欢迎,也取得了

三星note手机上html5画面模糊解决方案

最近测试发现,html5游戏在三星note手机上初次加载都会模糊,刷新之后就不模糊了,不知道是什么原因,本人对底层的东西也不熟,后来问了个同事,找到个解决方案,现记录一下. <meta name="viewport" content="width=device-width,target-densitydpi=device-dpi,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no&

Kali linux learning note

from:http://blog.sina.com.cn/s/blog_40983e5e0101dhz0.html 因为kali linux基于debian 7,当然要把这台Acer 4736z原有的debian 7删掉装kali啦,哈哈,这下不必为了BT5装虚拟机了,对于本子里60G的SSD来说还是好事一桩.要把kali当做桌面使用,就必须给kali添加一些软件,修改一些设置才好用,下面记录一下备忘,随时更新. 安装方法,官方文档,硬盘安装Kali Linux 把apt源设为官方提供的国内镜像

Beginning Scala study note(8) Scala Type System

1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the hierarchy and the type Nothing at the bottom of the hierarchy. All Scala types inherit from Any. # Using Any, Book extends AnyRef, and x is an Int that

[Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors

手机客户端向服务器提交Http请求时,Tomcat抛出错误: 十二月 31, 2014 2:32:45 下午 org.apache.coyote.http11.AbstractHttp11Processor process信息: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. 经过调试后发现是Tomcat

configure: error: Cannot find libmysqlclient under /usr Note that the MySQL client library is not bundled anymore! 报错解决

错误说明 今天在centos 6.3 64位版本上安装PHP5.4.3时在./configure 步骤的时候出现了下面错误configure: error: Cannot find libmysqlclient under /usr.Note that the MySQL client library is not bundled anymore! 原因分析与解决 通过查找libmysqlclient,发现是在/usr/lib64/mysql/目录内的libmysqlclient.so.15.0

Note of sed

refers to <Linux Command Line and Shell Scripting Bible> date:Sep 17 2016 format for using sed command: set options script file command of 's',the s command substitutes a second text string for the first text string pattern specified between the for