B2G/gecko/testing/marionette/marionette-listener.js
/**
 * Execute the given script either as a function body (executeScript)
 * or directly (for 'mochitest' like JS Marionette tests)
 */
Execute the given script either as a function body (executeScript)
or directly (for 'mochitest' like JS Marionette tests)
Execute the given script either as a function body ( executeScript ) or directly ( for ' mochitest ' like JS Marionette tests )
function executeScript(msg, directInject) {
  asyncTestCommandId = msg.json.command_id;
  let script = msg.json.value;
  if (msg.json.newSandbox || !sandbox) {
    sandbox = createExecuteContentSandbox(curWindow,
                                          msg.json.timeout);
    if (!sandbox) {
      sendError("Could not create sandbox!", 500, null, asyncTestCommandId);
      return;
    }
  }
  else {
    sandbox.asyncTestCommandId = asyncTestCommandId;
  }
  try {
    if (directInject) {
      if (importedScripts.exists()) {
        let stream = Components.classes["@mozilla.org/network/file-input-stream;1"].  
                      createInstance(Components.interfaces.nsIFileInputStream);
        stream.init(importedScripts, -1, 0, 0);
        let data = NetUtil.readInputStreamToString(stream, stream.available());
        script = data + script;
      }
      let res = Cu.evalInSandbox(script, sandbox, "1.8", "dummy file" ,0);
      sendSyncMessage("Marionette:shareData",
                      {log: elementManager.wrapValue(marionetteLogObj.getLogs())});
      marionetteLogObj.clearLogs();
      if (res == undefined || res.passed == undefined) {
        sendError("Marionette.finish() not called", 17, null, asyncTestCommandId);
      }
      else {
        sendResponse({value: elementManager.wrapValue(res)}, asyncTestCommandId);
      }
    }
    else {
      try {
        sandbox.__marionetteParams = elementManager.convertWrappedArguments(
          msg.json.args, curWindow);
      }
      catch(e) {
        sendError(e.message, e.code, e.stack, asyncTestCommandId);
        return;
      }
      script = "let __marionetteFunc = function(){" + script + "};" +
                   "__marionetteFunc.apply(null, __marionetteParams);";
      if (importedScripts.exists()) {
        let stream = Components.classes["@mozilla.org/network/file-input-stream;1"].  
                      createInstance(Components.interfaces.nsIFileInputStream);
        stream.init(importedScripts, -1, 0, 0);
        let data = NetUtil.readInputStreamToString(stream, stream.available());
        script = data + script;
      }
      let res = Cu.evalInSandbox(script, sandbox, "1.8", "dummy file", 0);
      sendSyncMessage("Marionette:shareData",
                      {log: elementManager.wrapValue(marionetteLogObj.getLogs())});
      marionetteLogObj.clearLogs();
      sendResponse({value: elementManager.wrapValue(res)}, asyncTestCommandId);
    }
  }
  catch (e) {
    let error = createStackMessage(e,
                                   "execute_script",
                                   msg.json.filename,
                                   msg.json.line,
                                   script);
    sendError(error[0], 17, error[1], asyncTestCommandId);
  }
}
function executeScript ( msg , directInject ) { asyncTestCommandId = msg . json . command_id ; let script = msg . json . value ; if ( msg . json . newSandbox || ! sandbox ) { sandbox = createExecuteContentSandbox ( curWindow , msg . json . timeout ) ; if ( ! sandbox ) { sendError ( "Could not create sandbox!" , 500 , null , asyncTestCommandId ) ; return ; } } else { sandbox . asyncTestCommandId = asyncTestCommandId ; } try { if ( directInject ) { if ( importedScripts . exists ( ) ) { let stream = Components . classes [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Components . interfaces . nsIFileInputStream ) ; stream . init ( importedScripts , - 1 , 0 , 0 ) ; let data = NetUtil . readInputStreamToString ( stream , stream . available ( ) ) ; script = data + script ; } let res = Cu . evalInSandbox ( script , sandbox , "1.8" , "dummy file" , 0 ) ; sendSyncMessage ( "Marionette:shareData" , { log : elementManager . wrapValue ( marionetteLogObj . getLogs ( ) ) } ) ; marionetteLogObj . clearLogs ( ) ; if ( res == undefined || res . passed == undefined ) { sendError ( "Marionette.finish() not called" , 17 , null , asyncTestCommandId ) ; } else { sendResponse ( { value : elementManager . wrapValue ( res ) } , asyncTestCommandId ) ; } } else { try { sandbox . __marionetteParams = elementManager . convertWrappedArguments ( msg . json . args , curWindow ) ; } catch ( e ) { sendError ( e . message , e . code , e . stack , asyncTestCommandId ) ; return ; } script = "let __marionetteFunc = function(){" + script + "};" + "__marionetteFunc.apply(null, __marionetteParams);" ; if ( importedScripts . exists ( ) ) { let stream = Components . classes [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Components . interfaces . nsIFileInputStream ) ; stream . init ( importedScripts , - 1 , 0 , 0 ) ; let data = NetUtil . readInputStreamToString ( stream , stream . available ( ) ) ; script = data + script ; } let res = Cu . evalInSandbox ( script , sandbox , "1.8" , "dummy file" , 0 ) ; sendSyncMessage ( "Marionette:shareData" , { log : elementManager . wrapValue ( marionetteLogObj . getLogs ( ) ) } ) ; marionetteLogObj . clearLogs ( ) ; sendResponse ( { value : elementManager . wrapValue ( res ) } , asyncTestCommandId ) ; } } catch ( e ) { let error = createStackMessage ( e , "execute_script" , msg . json . filename , msg . json . line , script ) ; sendError ( error [ 0 ] , 17 , error [ 1 ] , asyncTestCommandId ) ; } }
Execute the given script either as a function body (executeScript)
 or directly (for 'mochitest' like JS Marionette tests)
Execute the given script either as a function body ( executeScript ) or directly ( for ' mochitest ' like JS Marionette tests )
// 17 = JavascriptException