Search code examples
javascriptfirefox-addonsandboxfirefox-addon-sdk

Missing classes defined in window when eval in Sandbox


I have a problem trying to evaluate some source code in Sandbox. I added some classes to the window (DOM) object and I really need these classes to be included in Sandbox. Eg:

window.BaseClass = function(){this.someProperty = "bla bla";}

...

var ctx = new Components.utils.Sandbox(window);
    ctx.BaseClass = BaseClass; //This is neccesary

var src = "function SubClass(){}; SubClass.prototype = new BaseClass();";

var evaluation = Components.utils.evalInSandbox(src, ctx);
var res = new evaluation[className];

alert(res);

The problem is these new classes are not always the same, so I can't just to ctx.BaseClass = BaseClass; all the time. So is there a way to include in SandBox all the classes window has defined?


Solution

  • I was trying to get the new class from the evalInSandbox result. That was the problem: I had to do it from the context:

    Components.utils.evalInSandbox(src, ctx);
    var res = new ctx[className];