Bug 1265133 - Adds s-expr comments support for wasmTextToBinary. r=sunfish
authorYury Delendik <ydelendik@mozilla.com>
Fri, 15 Apr 2016 23:18:57 -0500
changeset 331725 f58b6c6a39f92e4b6d415a4d84ed5ceac0407b73
parent 331724 177d9f1a0f9a0864e0cda80dcf5f03e964237a2c
child 331726 0eb46e22a78285e7eba9abbfd76bc98148812ba5
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssunfish
bugs1265133
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1265133 - Adds s-expr comments support for wasmTextToBinary. r=sunfish
js/src/asmjs/WasmTextToBinary.cpp
js/src/jit-test/tests/wasm/comments.js
--- a/js/src/asmjs/WasmTextToBinary.cpp
+++ b/js/src/asmjs/WasmTextToBinary.cpp
@@ -1167,16 +1167,17 @@ class WasmTokenStream
     }
     WasmToken fail(const char16_t* begin) const {
         return WasmToken(begin);
     }
 
     WasmToken nan(const char16_t* begin);
     WasmToken literal(const char16_t* begin);
     WasmToken next();
+    void skipSpaces();
 
   public:
     WasmTokenStream(const char16_t* text, UniqueChars* error)
       : cur_(text),
         end_(text + js_strlen(text)),
         lineStart_(text),
         line_(1),
         lookaheadIndex_(0),
@@ -1345,25 +1346,58 @@ WasmTokenStream::literal(const char16_t*
 
     CheckedInt<uint32_t> index = u.value();
     if (index.isValid())
         return WasmToken(index.value(), begin, cur_);
 
     return WasmToken(u.value(), begin, cur_);
 }
 
+void
+WasmTokenStream::skipSpaces()
+{
+    while (cur_ != end_) {
+        char16_t ch = *cur_;
+        if (ch == ';' && consume(MOZ_UTF16(";;"))) {
+            // Skipping single line comment.
+            while (cur_ != end_ && !IsWasmNewLine(*cur_))
+                cur_++;
+        } else if (ch == '(' && consume(MOZ_UTF16("(;"))) {
+            // Skipping multi-line and possibly nested comments.
+            size_t level = 1;
+            while (cur_ != end_) {
+                char16_t ch = *cur_;
+                if (ch == '(' && consume(MOZ_UTF16("(;"))) {
+                    level++;
+                } else if (ch == ';' && consume(MOZ_UTF16(";)"))) {
+                    if (--level == 0)
+                        break;
+                } else {
+                    cur_++;
+                    if (IsWasmNewLine(ch)) {
+                        lineStart_ = cur_;
+                        line_++;
+                    }
+                }
+            }
+        } else if (IsWasmSpace(ch)) {
+            cur_++;
+            if (IsWasmNewLine(ch)) {
+                lineStart_ = cur_;
+                line_++;
+            }
+        } else
+            break; // non-whitespace found
+    }
+}
+
 WasmToken
 WasmTokenStream::next()
 {
-    while (cur_ != end_ && IsWasmSpace(*cur_)) {
-        if (IsWasmNewLine(*cur_++)) {
-            lineStart_ = cur_;
-            line_++;
-        }
-    }
+    skipSpaces();
 
     if (cur_ == end_)
         return WasmToken(WasmToken::EndOfFile, cur_, cur_);
 
     const char16_t* begin = cur_;
     switch (*begin) {
       case '"':
         cur_++;
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/wasm/comments.js
@@ -0,0 +1,33 @@
+load(libdir + "wasm.js");
+
+// sanity check
+assertErrorMessage(() => wasmEvalText(''), SyntaxError, /parsing wasm text/);
+
+// single line comment
+var o = wasmEvalText('(module (func)) ;; end');
+var o = wasmEvalText('(module (func)) ;; end\n');
+var o = wasmEvalText('(module (func))\n;; end');
+var o = wasmEvalText('(module (func))\n;; end');
+var o = wasmEvalText(';;start\n(module (func))');
+var o = wasmEvalText('(module (func ;; middle\n))');
+var o = wasmEvalText('(module (func) ;; middle\n (export "a" 0))');
+assertEq(Object.getOwnPropertyNames(o)[0], "a");
+
+// multi-line comments
+var o = wasmEvalText('(module (func))(; end ;)');
+var o = wasmEvalText('(module (func)) (; end\nmulti;)\n');
+var o = wasmEvalText('(module (func))\n(;;)');
+var o = wasmEvalText('(;start;)(module (func))');
+var o = wasmEvalText('(;start;)\n(module (func))');
+var o = wasmEvalText('(module (func (; middle\n multi\n;)))');
+var o = wasmEvalText('(module (func)(;middle;)(export "a" 0))');
+assertEq(Object.getOwnPropertyNames(o)[0], "a");
+
+// nested comments
+var o = wasmEvalText('(module (;nested(;comment;);)(func (;;;;)))');
+var o = wasmEvalText(';;;;;;;;;;\n(module ;;(;n \n(func (;\n;;;)))');
+
+assertErrorMessage(() => wasmEvalText(';; only comment'), SyntaxError, /parsing wasm text/);
+assertErrorMessage(() => wasmEvalText(';; only comment\n'), SyntaxError, /parsing wasm text/);
+assertErrorMessage(() => wasmEvalText('(; only comment ;)'), SyntaxError, /parsing wasm text/);
+assertErrorMessage(() => wasmEvalText(';; only comment\n'), SyntaxError, /parsing wasm text/);