JavaScript ã¨ã³ã¸ã³ V8 ã使ã£ã¦ã©ã¤ãã©ãªã®éçºã«ã¦ããããã¹ããå©ç¨ããã
大ããªããã°ã©ã ã«ãªã㨠JavaScript ã¨ããã©ããã©ã¦ã¶ããæä½ãã¦ãã¹ããã以å¤ã«ããåä½ã§ã³ãã³ãã©ã¤ã³ãããã¹ãããããªãã¾ããã
JsUnit ã¯ã©ããªã®ã
JavaScript ã®ã¦ããããã¹ããã¬ã¼ã ã¯ã¼ã¯ã¨ããã¨ãxUnit ã® JsUnit ãæãæµ®ãã³ã¾ãã使ã£ããã¨ããªãã£ãã®ã§ãã¡ãã£ã¨èª¿ã¹ã¦ã¿ã¾ããã
- http://hisasann.com/housetect/2008/04/javascriptunitjsunit.html
- http://bobchin.ddo.jp/wiki/index.php?Javascript%2FJsUnit
ã©ãã JsUnit 㯠html ãã¡ã¤ã«ãéãã¦ãã©ã¦ã¶ãã¼ã¹ã§ãã¹ãå®è¡ããã¿ããã§ããå ±åã§ãã¹ãã«ä½¿ãããµã¼ãã¼ã³ã³ãã¼ãã³ããããããã§ãããå°ç¨ã® HTTP ãµã¼ãã¹ãç«ã¡ä¸ãã¦ä½¿ãããã§ãã
ãã£ã±ã CUI ã§ä½¿ããã¤ã³ã¿ããªã¿ã¨ãã·ã§ã«ãã¼ã¹ã§ã ã¨ä¾¿å©ã§ãã
V8 ã使ããªãã®ã
以å id:amachang ããã® Google Chrome の JavaScript エンジン V8 を試す - IT戦記 ãã¿ã¦ãV8 ã® shell ã試ãã«ã³ã³ãã¤ã«ãã¦ãã¾ããã
v8/sample/shell.cc ãããã£ã¨è¦ã¦ã¿ãã¨ã
// Bind the global 'print' function to the C++ Print callback. global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); // Bind the global 'load' function to the C++ Load callback. global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load));
㨠print, load ããã¤ãã£ã颿°ã¨ãã¦çµã¿è¾¼ã¾ãã¦ãããã¨ããããã¾ãã
ã ãã JavaScript ã§ã® Ruby ã® irb ã¿ããã«ä½¿ããã®ã§ãã
試ãã¿ã
/tmp/lib.js ã
function bold(text){ return "<b>" + text + "</b>" }
ã¨ä½ããèªã¿è¾¼ã¾ãã¦ä½¿ã£ã¦ã¿ãã
% ./shell
V8 version 0.4.6
> load("/tmp/lib.js");
> var b = bold("Title");
> print(b);
<b>Title</b>
> quit();ã¨ãããããload ã使ã£ã¦ä»ã® js ãã¡ã¤ã«ããã¼ãã§ãã¾ãããããªãã¦ããããã¹ããå¯è½ã§ããã
ã¦ããããã¹ã
ã¦ããããã¹ãã¨ããã°ãå®è¡çµæã assertXXXX ãªé¢æ°ã§ç §åããã®ãä¸è¬çã§ããv8 ã«ã¯ãã¨ã㨠JavaScript ã®ã³ã¢æ©è½ã®ã¦ããããã¹ããå ¥ã£ã¦ãã¾ãã v8/test/mjsunit/*.js ã«ããããããã¾ãã
mjsunit.js
ãã®ãã¡ã¤ã«ãè¦ãã¨ã¢ãµã¼ã颿°ãä¸éãå®ç¾©ããã¦ããã®ã§ãã®ã¾ã¾ä½¿ãããã§ãã
// Copyright 2008 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. function MjsUnitAssertionError(message) { this.message = message; } MjsUnitAssertionError.prototype.toString = function () { return this.message; } /* * This file is included in all mini jsunit test cases. The test * framework expects lines that signal failed tests to start with * the f-word and ignore all other lines. */ function fail(expected, found, name_opt) { var start; if (name_opt) { // Fix this when we ditch the old test runner. start = "Fail" + "ure (" + name_opt + "): "; } else { start = "Fail" + "ure:"; } throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">"); } function assertEquals(expected, found, name_opt) { if (expected != found) { fail(expected, found, name_opt); } } function assertArrayEquals(expected, found, name_opt) { var start = ""; if (name_opt) { start = name_opt + " - "; } assertEquals(expected.length, found.length, start + "array length"); if (expected.length == found.length) { for (var i = 0; i < expected.length; ++i) { assertEquals(expected[i], found[i], start + "array element at index " + i); } } } function assertTrue(value, name_opt) { assertEquals(true, value, name_opt); } function assertFalse(value, name_opt) { assertEquals(false, value, name_opt); } function assertNaN(value, name_opt) { if (!isNaN(value)) { fail("NaN", value, name_opt); } } function assertThrows(code) { var threwException = true; try { eval(code); threwException = false; } catch (e) { // Do nothing. } if (!threwException) assertTrue(false, "did not throw exception"); } function assertInstanceof(obj, type) { if (!(obj instanceof type)) { assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">"); } } function assertDoesNotThrow(code) { try { eval(code); } catch (e) { assertTrue(false, "threw an exception"); } } function assertUnreachable(name_opt) { // Fix this when we ditch the old test runner. var message = "Fail" + "ure: unreachable" if (name_opt) { message += " - " + name_opt; } throw new MjsUnitAssertionError(message); }
ãã¹ããã¦ã¿ãã
å ã»ã©ã® lib.js ã® bold 颿°ããã¹ããã¦ã¿ã¾ãããã
test.js
ãã¹ãç¨ã® js ãã¡ã¤ã«ã§ãã /usr/local/v8 ã« V8 ã¯ãã§ãã¯ã¢ã¦ãããã³ã³ã³ãã¤ã«ãã¦ã¾ãã
// load mjsunit load("/usr/local/v8/test/mjsunit/mjsunit.js"); // load target js file load("/tmp/lib.js"); /* * Test Start */ var a = "ã«ã»ãã"; var b = bold(a); assertEquals(b, "<b>ã«ã»ãã</b>");
å®è¡ãã¦ã¿ã¾ãããã
% ./shell test.js % echo $? 0
ããã¨å¤±æããã¦ã¿ã¾ãã
assertEquals(b, "<b>FAIL</b>");
ã test.js ã«è¿½è¨ãã¦ãå®è¡ãã¦ã¿ã¾ãã
% /usr/local/v8/shell start.js /usr/local/v8/test/mjsunit/mjsunit.js:50: Failure: expected <<b>ã«ã»ãã</b>> found <<b>FAIL</b>> throw new MjsUnitAssertionError(start + " expected <" + expected + "> found ^ % echo $? 1
çµäºã³ã¼ããã¡ãã㨠1 ã«å¤ãã£ã¦ãã¾ããããããªãã¨ã©ã¼ãææã§ãã¾ãã
ã¾ã¨ã
V8 ãç¨ãã¦ã©ã¤ãã©ãªã®ã¦ããããã¹ããè¡ãããã¨ããããã¾ããã
ï¼å®è¡ã®ããã®ã©ããã¹ã¯ãªãããæ¸ããã£ã¨â¦ï¼