我想从我正在开发的插件中的沙箱中的命名空间中执行一组函数,我做过类似的事情:
if (typeof namespace == "undefined") {
var namespace = {
foo:function(){return 0},
goo:function(){
var s = new Components.utils.Sandbox("http://code.google.com/p/headertool/");
//importing utility method inside the sandbox
s.importFunction(namespace.foo); //FAIL...
var result = Components.utils.evalInSandbox(code, s);
return result;
}
/**
* Constructor.
*/
(function() {
this.startup();
}).apply(namespace);
}
但是当我尝试导入该函数时,我收到了这个错误:
Error: uncaught exception: [Exception... "Illegal value" nsresult: "0x80070057
(NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://headertool/content
/overlay.js :: <TOP_LEVEL> :: line 321" data: no]
我认为这是相关的,因为我试图导入的函数不在当前上下文中,有一个更好的解决方案或解决问题的唯一方法是在主要上下文中将make function设为public: / p>
if (typeof namespace == "undefined") {
var namespace = {
goo:function(){
var s = new Components.utils.Sandbox("http://code.google.com/p/headertool/");
//importing utility method inside the sandbox
s.importFunction(namespace.foo); //OK...
var result = Components.utils.evalInSandbox(code, s);
return result;
}
/**
* Constructor.
*/
(function() {
this.startup();
} ).apply(namespace);
};
//Global namespace
function foo(){return 0};