Sometimes we need to call javascript function form a c++ xpcom. The following is a method for this.

1. Define your javascript callback interface in a IDL file.

1
2
3
interface JSCallback : nsISupports {
    boolean call(in PRUint32 bogus, in PRUint32 aData);
}

And another interface looks like:

1
2
3
interface TestJSCallback : nsISupports {
    void sum(in PRUint32 first, in PRUint32 second, in JSCallBack aCallback);
}

2. Complete the sum function’s cpp code.

1
2
3
4
5
6
7
8
NS_IMETHODIMP TestJSCallback::Sum(PRUint32 first, PRUint32 second, JSCallback *aCallback) {
    PRBool ret = true;
    nsCOMPtr<JSCallback> js_callback = aCallback;
 
    js_callback->Call(0, first+second, &ret);
 
    return NS_OK;
}

3. Interface call.

?View Code JAVASCRIPT
1
2
3
4
5
6
var test = function(sum) {
    alert(sum);
};
 
var com = Components.classes["XXXXXX"].getService(Components.interfaces.TestJSCallback);
com.sum(1, 2, test);

It will pop up a dialog shows “3″ … Cheers…

Tags: ,

放出个 JavaScript 代码语法高亮类, 是根据 Unnamed Blog 上的修改的(原文提供的下载后有不少错误…), 效率比我原来写的高了不少. 当然, 我自己最后估计还是拿 Lex 来做…

主要修改的地方首先是让整个 js 可运行(-_-!)…然后完善了一些功能, 比如对 & 和 < 的处理, 还有把其它语言的支持分离开来.

首先是 cpp 高亮的支持 js 文件

sh.Brushes.Cpp = function() {}

sh.Brushes.Cpp.aliases = ‘cpp’;

sh.Brushes.Cpp.regexList = [
{regex: new RegExp('//.*$', 'gm'), css: 'comment'}, // one line comments
{regex: new RegExp('/\\*[\\s\\S]*?\\*/’, ‘g’), css: ‘comment’}, // multi line comments
{regex: new RegExp(“\’(?:[^\\\\']|\\\\.)*\’|” +
“\”(?:[^\\\\\"]|\\\\.)*\”", ‘g’), css: ‘string’}, // string literals
{regex: new RegExp(‘^\\s*#.*’, ‘gm’), css: ‘preprocessor’}, // preprocessor directives
{regex: new RegExp(‘\\b(?:auto|break|case|char|const|continue|default|do’ +
‘double|else|enum|extern|float|for|goto|if|int|long|register’ +
‘return|short|signed|sizeof|static|struct|switch|typedef|union’ +
‘unsigned|void|volatile|while)\\b’, ‘gm’), css: ‘keyword’} // keyword
];

Read the rest of this entry »

Tags: ,