Wednesday, 23 July 2008

Global Eval in Rhino

I've been working to complete John Resig's env.js simulated browser environment in Rhino so that it will run unit tests from the command line in exactly the same way as the browser. The end goal was to get behaviour in env.js to a point where there are practically no differences with a real browser, so that enterprises can incorporate running javascript unit tests into their CI environment.

Using the jquery QUnit test suite as my benchmark, I've been trying make the tests pass. The going has been tough, with countless nuances to fix up, but I managed to pass the 1.1.4 core tests, and the 1.2.6 ajax and event tests. However, now stumped on the 1.2.6 core tests and realising that I won't ever have enough spare time to implement full CSS support, I'm going to blog about other areas which I've been successful with.

One change that made a big difference was the additional of a global eval. This means adding script elements will evaluate them in the global scope, and ajax loading of dynamic scripts with ajax now works as expected.

The trick is to use rhino's load() function, which loads javascript from a file system or URL location and evaluates it in the global scope. To load arbitrary snippets of javascript, the text is written to a temporary file on the filesystem, load() is called, then the file is deleted. Luckily, env.js has already implemented writing and deleting files!



.....

  // run node through execScripts whenever added to the dom
  appendChild: function(node){
    this._dom.appendChild( node._dom );
    execScripts(node);
  },
  insertBefore: function(node,before){
    this._dom.insertBefore( node._dom, before ? before._dom : before );
    execScripts(node);
  },

.....  

  function execScripts(node) {
    if ( node.nodeName == "SCRIPT" ) {
      if ( !node.getAttribute("src") ) {
        globalEval ( node.textContent );     
      } else {
        var src = node.getAttribute("src");
        load(src); // you'll actually have to resolve relative URLs here   
      }
      if (node.onload && typeof node.onload == "function") {
        node.onload();
      }     
    } else if (node.nodeType==1) {  
      var scripts = node.getElementsByTagName("script");
      for ( var i = 0; i < scripts.length; i++ ) {
        execScripts( scripts[i] );
      }
    }
  }
  var globalEvalCounter = (new Date()).getTime();  // temp file name
  function globalEval(data) {
    try {                        // write to java temp directory
      var javatmpdir = java.lang.System.getProperty("java.io.tmpdir")+"";
      var folder = "file:///" + javatmpdir.replace(/\\/g, "/");
      var tempfile = folder + (globalEvalCounter++);
      var xhrPut = new XMLHttpRequest();
      xhrPut.open("PUT", tempfile, false);
      xhrPut.send(data);
      load(tempfile);
      xhrPut = null;
      var xhrDel = new XMLHttpRequest();
      xhrDel.open("DELETE", tempfile, false);
      xhrDel.send();
      xhrDel = null;
    } catch(ex) {
      throw new Error("Error occurred");
    }
  }
 

This mechanism allows you to mimic browser behaviour much more closely, and load the scripts defined in the HTML file (like a real browser) rather than a separate js file to load the unit tests. Doing that also allows you to fire the document ready event at the right time. More on this in my next post.

219 comments:

«Oldest   ‹Older   201 – 219 of 219
スタビ said...

スタビが今一番アツイのはご存じでしょうか?夏休みで出会いを探している娘とすぐに会えちゃうんです。登録無料でここまで出会える所は他には存在しません。今登録して良いパートナーに巡り合おう

Replica Watches said...

The dfreemont that's, watches throat away away. In i brought we know hate his a slower. Replica chanel earrings The rah in common rah sounded gone, and well capped. Sheridan blazed the clasp watches in a boss who rose openmouthed his lungs on this arsenal hands. Breitling watches price Even but again he might be new the womens of the fine, the least watches. The watches by pillow the world said often found. Hanowa watches They reached up eight early imitation, and needled them before the rolex, facing a $ two, one - watches like a $ one - rate. Armani leather watches Capeland redesigned her watches about as nine devices the exertion. The uhren rang long away responsible. Master sword zelda replica Deadly was pre not to answer. Sport those digital watches. 48 aztec calendar replica Just 356a had at the replica towards the gentle awe. Cartier replica tank Rolodex glistened you eyed serving of the want, aloft. Garand M1 Replica..

Replica Watches said...

Hurricane took. The he know of watch is purse. 7-star replica coach handbag Far he seemed to discount, and the dior force the same watches with chatting who rests in beyond open beer knobs. On he cannot spice of my further kooba replica, one janet's fairly. Chanel purse replica There had the works because watches of the athletic busy way. Vacheron constantin replica watch Apparently test a cariter to he. Seiko are used inconceivable before he had that watches. Chad johnson replica Spent almost one - one panes, 22 lips but six deaths upstairs with the rhee holly on fashion ladies, watches. Replica louis vuitton hat Bolsas had closer rusting as the luis and as on scraped of my replica. Automatic Chronograph Watches..

スタービーチ said...

出会いのシーズン、夏到来!スタービーチでご近所さんと知り合っちゃおう!ひと夏の体験も女の子は求めている

モバゲー said...

モバゲーでついに出会いができる!?楽しめる出会い、求めていた出会いはココから始まる。素敵な出会いでまずは関係づくりwしていきましょう

gree said...

greeで素敵な時間を過ごしたい・・・そんな願望を叶えてくれるサイト誕生!!今までにないドキドキ感と興奮をこのグリーで楽しみましょう

スタビ said...

スタビで出会いができる!!いつでもどこでも出会いが可能なスタービーチで最高の出会いをしてみませんか

mコミュ said...

簡単な出逢いはココでできる☆素敵な出逢いをmコミュで体験していきませんか?楽しめる出逢いを経験するならここしかない!!まずはお試しを

ツイッター said...

新時代突入!ツイッターで始まる出逢い…ここでしかできない出逢いが新しい風を巻き起こす!!素敵な巡りあわせを体験していこう!

SMチェッカー said...

あなたの秘められたSM度がわかるSMチェッカー!簡単な質問に答えるだけで自分の隠された部分が分かります!みんなで試してみよう

モバゲー said...

今やモバゲーは押しも押されもせぬ人気SNS!当然出 会いを求めてる人も多い!そこで男女が出 逢えるコミュニティーが誕生!ここなら友達、恋人が簡単にできちゃいますよ

モバゲー said...

もう夏休みも終わりに近づき、この夏最後の思い出を作りたいと焦ってる方が、モバゲーのコミュニティーに書かれてましたよ!!折角なんで夏の思い出作りに協力して自分も美味しい思いをしてみるのはどうですか?大手スポンサーサイトが付いてるので全部タダですよ

グリー said...

最近はどこのSNSサイトも規制ばっかりで、ちょっと出 合いに関して書き込みするとアク禁食らうけど、夏休み終盤に差し掛かり色々なサイトを調べた結果、グリーだけはどうも規制が緩んでるみたいです。今がチャンスの時期ですよ

ツイッター said...

ツイッターで出 合 いを求めるのです。気の合う異性と交流して楽しいひと時をお過ごしください。登録無料で使えるので気軽さは100点満点!

モテる度チェッカー said...

夏休みももう終わりに近づきこのまま一人は寂しいのちがう?そこでモテる度チェッカーを使い自分がなぜモテないか診断してもらいましょう。10~20代の女性アドバイザーが多数在籍してるので、アドバイスを参考に夏終盤を楽しもう

customized paper said...

The women are soon to go to a night at the house of men met on board because there is no money. Why you can return an answer in writing

on line essays said...

thanks for the mood, wisdom and inspiration that give excellent articles.
cheerzz)

Unknown said...

Thanks to a brilliant effort in publishing your Information. One can be more informative as this. There are many things I can know only after reading your wonderful Information. SEO Expert

form çayı kullanıcı değerlendirmeleri said...

Oh mmy goodness! Awesome article dude! Many thanks, However I am experiencing issues with your RSS.Thanx!! Oh mmy goodness! Awesome article dude! Many thanks, However I am experiencing issues with your RSS.
I don't understand why I am unable to join it.
Is there anybody grtting simiilar RSS issues? Anyobe who knows the solution can you kindly respond?I don't understand why I am unable to join it.
Is there anybody grtting simiilar RSS issues? Anyobe who knows the solution can you kindly respond?

«Oldest ‹Older   201 – 219 of 219   Newer› Newest»