Merge: nitrpg: data management uses `mongodb` instead of `json_store`
authorJean Privat <jean@pryen.org>
Fri, 4 Dec 2015 20:40:25 +0000 (15:40 -0500)
committerJean Privat <jean@pryen.org>
Fri, 4 Dec 2015 20:40:25 +0000 (15:40 -0500)
Depends on #1759, #1760 and #1761.

Pull-Request: #1762
Reviewed-by: Jean Privat <jean@pryen.org>

39 files changed:
contrib/jwrapper/Makefile
contrib/jwrapper/src/model.nit
contrib/nitiwiki/examples/default/config.ini
contrib/nitiwiki/examples/default/templates/header.html
contrib/nitiwiki/examples/nitiwiki/templates/menu.html
contrib/nitiwiki/src/wiki_html.nit
contrib/nitiwiki/src/wiki_links.nit
lib/core/bytes.nit
lib/core/collection/array.nit
lib/core/collection/circular_array.nit [new file with mode: 0644]
lib/core/collection/collection.nit
lib/core/text/abstract_text.nit
lib/core/text/flat.nit
lib/gen_nit.nit
lib/nitcorn/examples/.gitignore [new file with mode: 0644]
lib/nitcorn/examples/Makefile
lib/nitcorn/examples/src/restful_annot.nit [new file with mode: 0644]
lib/nitcorn/restful.nit [new file with mode: 0644]
lib/pthreads/concurrent_collections.nit
lib/pthreads/examples/jointask_example.nit [new file with mode: 0644]
lib/pthreads/examples/threadpool_example.nit
lib/pthreads/threadpool.nit
share/man/nitrestful.md [new file with mode: 0644]
src/Makefile
src/astvalidation.nit
src/frontend/serialization_phase.nit
src/nitrestful.nit [new file with mode: 0644]
src/nitserial.nit
tests/bench_seq.nit [new file with mode: 0644]
tests/niti.skip
tests/nitrestful.args [new file with mode: 0644]
tests/nitvm.skip
tests/sav/bench_seq.res [new file with mode: 0644]
tests/sav/jointask_example.res [new file with mode: 0644]
tests/sav/nitrestful.res [new file with mode: 0644]
tests/sav/nitrestful_args1.res [new file with mode: 0644]
tests/sav/test_new_native_alt1.res
tests/sav/test_seq.res
tests/test_seq.nit

index 3173333..c1901bf 100644 (file)
@@ -12,7 +12,7 @@ src/javap_test_parser.nit: ../nitcc/src/nitcc grammar/javap.sablecc
        mv javap* gen/
 
 src/serial.nit: $(shell ../../bin/nitls -M src/jwrapper.nit)
-       ../../bin/nitserial -o src/serial.nit -d package src/jwrapper.nit
+       ../../bin/nitserial -o src/serial.nit src/jwrapper.nit
 
 bin/jwrapper: src/javap_test_parser.nit src/serial.nit $(shell ../../bin/nitls -M src/jwrapper.nit) ../../bin/nitc
        mkdir -p bin
index 9013bff..e642da4 100644 (file)
@@ -157,7 +157,7 @@ class NitType
        var identifier: String
 
        # If this NitType was found in `lib/android`, contains the module name to import
-       var mod: nullable NitModule
+       var mod: nullable NitModuleRef
 
        # Is this type known, wrapped and available in Nit?
        var is_known: Bool = true
@@ -183,7 +183,7 @@ class JavaClass
        var constructors = new Array[JavaConstructor]
 
        # Importations from this class
-       var imports = new HashSet[NitModule]
+       var imports = new HashSet[NitModuleRef]
 
        # Interfaces implemented by this class
        var implements = new HashSet[JavaType]
@@ -482,7 +482,7 @@ class JavaConstructor
 end
 
 # A Nit module, use to import the referenced extern classes
-class NitModule
+class NitModuleRef
        # Relative path to the module
        var path: String
 
@@ -490,7 +490,7 @@ class NitModule
        var name: String is lazy do return path.basename(".nit")
 
        redef fun to_s do return self.name
-       redef fun ==(other) do return other isa NitModule and self.path == other.path
+       redef fun ==(other) do return other isa NitModuleRef and self.path == other.path
        redef fun hash do return self.path.hash
 end
 
@@ -501,7 +501,7 @@ redef class Sys
        # * The value is the corresponding `NitType`.
        var find_extern_class: DefaultMap[String, nullable NitType] is lazy do
                var map = new DefaultMap[String, nullable NitType](null)
-               var modules = new HashMap[String, NitModule]
+               var modules = new HashMap[String, NitModuleRef]
 
                var lib_paths = opt_libs.value
                if lib_paths == null then lib_paths = new Array[String]
@@ -543,7 +543,7 @@ redef class Sys
 
                                var mod = modules.get_or_null(path)
                                if mod == null then
-                                       mod = new NitModule(path)
+                                       mod = new NitModuleRef(path)
                                        modules[path] = mod
                                end
 
index 7917052..9ac9824 100644 (file)
@@ -1,4 +1,3 @@
 wiki.name=MyWiki
 wiki.desc=proudly powered by nit
 wiki.logo=assets/logo.png
-wiki.root_dir=/full/path/to/your/wiki/root/dir
index d86aac9..298f8ab 100644 (file)
@@ -1,5 +1,5 @@
 <header>
-    <a href="#"><img src="%ROOT_URL%/%LOGO%" alt="logo"/></a>
+    <a href="%ROOT_URL%/index.html"><img src="%ROOT_URL%/%LOGO%" alt="logo"/></a>
     <h2>%SUBTITLE%</h2>
     <h1>%TITLE%</h1>
 </header>
index 1ff832e..ec20a0e 100644 (file)
@@ -8,7 +8,7 @@
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                        </button>
-                       <a class="navbar-brand" href="%ROOT_URL%index.html">%TITLE%</a>
+                       <a class="navbar-brand" href="%ROOT_URL%/index.html">%TITLE%</a>
                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
index 8042af6..ab79f09 100644 (file)
@@ -189,10 +189,7 @@ redef class WikiArticle
        fun load_template(template_file: String): TemplateString do
                var tpl = wiki.load_template(template_file)
                if tpl.has_macro("ROOT_URL") then
-                       var root_dir = href.dirname.relpath("")
-                       # Avoid issues if the macro is just followed by a `/` (as with url prefix)
-                       if root_dir == "" then root_dir = "."
-                       tpl.replace("ROOT_URL", root_dir)
+                       tpl.replace("ROOT_URL", root_href)
                end
                return tpl
        end
index 03f21ca..c1785b2 100644 (file)
@@ -100,12 +100,23 @@ redef class WikiEntry
        # Relative path to `self` from the target root_url
        fun href: String do return breadcrumbs.join("/")
 
+       # Relative path to the directory `self` from the target root_url
+       fun dir_href: String do return href.dirname
+
+       # Relative path to the root url from `self`
+       fun root_href: String do
+               var root_dir = dir_href.relpath("")
+               # Avoid issues if used as a macro just followed by a `/` (as with url prefix)
+               if root_dir == "" then root_dir = "."
+               return root_dir
+       end
+
        # A relative `href` to `self` from the page `context`.
        #
        # Should be used to navigate between documents.
        fun href_from(context: WikiEntry): String
        do
-               var res = context.href.dirname.relpath(href)
+               var res = context.dir_href.relpath(href)
                return res
        end
 
@@ -174,6 +185,8 @@ redef class WikiSection
                end
                return new WikiSectionIndex(wiki, "index", self)
        end
+
+       redef fun dir_href do return href
 end
 
 redef class WikiArticle
@@ -212,6 +225,8 @@ class WikiSectionIndex
        redef fun title do return section.title
 
        redef fun href do return section.href
+
+       redef fun dir_href do return section.dir_href
 end
 
 # A MarkdownProcessor able to parse wiki links.
index a1dbfbe..af5b61b 100644 (file)
@@ -19,7 +19,39 @@ import kernel
 import collection::array
 intrude import text::flat
 
+# Any kind of entity which can be searched for in a Sequence of Byte
+interface BytePattern
+       # Return the first occurence of `self` in `b`, or -1 if not found
+       fun first_index_in(b: SequenceRead[Byte]): Int do return first_index_in_from(b, 0)
+
+       # Return the first occurence of `self` in `b` starting at `from`, or -1 if not found
+       fun first_index_in_from(b: SequenceRead[Byte], from: Int): Int is abstract
+
+       # Return the last occurence of `self` in `b`, or -1 if not found
+       fun last_index_in(b: SequenceRead[Byte]): Int do return last_index_in_from(b, b.length - 1)
+
+       # Return the last occurence of `self` in `b`, or -1 if not found
+       fun last_index_in_from(b: SequenceRead[Byte], from: Int): Int is abstract
+
+       # Returns the indexes of all the occurences of `self` in `b`
+       fun search_all_in(b: SequenceRead[Byte]): SequenceRead[Int] is abstract
+
+       # Length of the pattern
+       fun pattern_length: Int is abstract
+
+       # Appends `self` to `b`
+       fun append_to(b: Sequence[Byte]) is abstract
+
+       # Is `self` a prefix for `b` ?
+       fun is_prefix(b: SequenceRead[Byte]): Bool is abstract
+
+       # Is `self` a suffix for `b` ?
+       fun is_suffix(b: SequenceRead[Byte]): Bool is abstract
+end
+
 redef class Byte
+       super BytePattern
+
        # Write self as a string into `ns` at position `pos`
        private fun add_digest_at(ns: NativeString, pos: Int) do
                var tmp = (0xF0u8 & self) >> 4
@@ -72,6 +104,39 @@ redef class Byte
                # i.e. this abort is here to please the compiler
                abort
        end
+
+       redef fun first_index_in_from(b, from) do
+               for i in [from .. b.length[ do if b[i] == self then return i
+               return -1
+       end
+
+       redef fun last_index_in_from(b, from) do
+               for i in [0 .. from].step(-1) do if b[i] == self then return i
+               return -1
+       end
+
+       redef fun search_all_in(b) do
+               var ret = new Array[Int]
+               var pos = 0
+               loop
+                       pos = first_index_in_from(b, pos)
+                       if pos == -1 then return ret
+                       ret.add pos
+                       pos += 1
+               end
+       end
+
+       redef fun pattern_length do return 1
+
+       redef fun append_to(b) do b.push self
+
+       #     assert 'b'.ascii.is_suffix("baqsdb".to_bytes)
+       #     assert not 'b'.ascii.is_suffix("baqsd".to_bytes)
+       redef fun is_suffix(b) do return b.length != 0 and b.last == self
+
+       #     assert 'b'.ascii.is_prefix("baqsdb".to_bytes)
+       #     assert not 'b'.ascii.is_prefix("aqsdb".to_bytes)
+       redef fun is_prefix(b) do return b.length != 0 and b.first == self
 end
 
 # A buffer containing Byte-manipulation facilities
@@ -79,6 +144,7 @@ end
 # Uses Copy-On-Write when persisted
 class Bytes
        super AbstractArray[Byte]
+       super BytePattern
 
        # A NativeString being a char*, it can be used as underlying representation here.
        var items: NativeString
@@ -107,7 +173,9 @@ class Bytes
                init(ns, 0, cap)
        end
 
-       redef fun is_empty do return length != 0
+       redef fun pattern_length do return length
+
+       redef fun is_empty do return length == 0
 
        #     var b = new Bytes.empty
        #     b.add 101u8
@@ -118,6 +186,71 @@ class Bytes
                return items[i]
        end
 
+       # Returns a copy of `self`
+       fun clone: Bytes do
+               var b = new Bytes.with_capacity(length)
+               b.append(self)
+               return b
+       end
+
+       # Trims off the whitespaces at the beginning and the end of `self`
+       #
+       #     var b = "102041426E6F1020" .hexdigest_to_bytes
+       #     assert b.trim.hexdigest == "41426E6F"
+       #
+       # NOTE: A whitespace is defined here as a byte whose value is <= 0x20
+       fun trim: Bytes do
+               var st = 0
+               while st < length do
+                       if self[st] > 0x20u8 then break
+                       st += 1
+               end
+               if st >= length then return new Bytes.empty
+               var ed = length - 1
+               while ed > 0 do
+                       if self[ed] > 0x20u8 then break
+                       ed -= 1
+               end
+               return slice(st, ed - st + 1)
+       end
+
+       # Returns a subset of the content of `self` starting at `from` and of length `count`
+       #
+       #     var b = "abcd".to_bytes
+       #     assert b.slice(1, 2).hexdigest == "6263"
+       #     assert b.slice(-1, 2).hexdigest == "61"
+       #     assert b.slice(1, 0).hexdigest == ""
+       #     assert b.slice(2, 5).hexdigest == "6364"
+       fun slice(from, count: Int): Bytes do
+               if count <= 0 then return new Bytes.empty
+
+               if from < 0 then
+                       count += from
+                       if count < 0 then count = 0
+                       from = 0
+               end
+
+               if (count + from) > length then count = length - from
+               if count <= 0 then return new Bytes.empty
+
+               var ret = new Bytes.with_capacity(count)
+
+               ret.append_ns(items.fast_cstring(from), count)
+               return ret
+       end
+
+       # Returns a copy of `self` starting at `from`
+       #
+       #     var b = "abcd".to_bytes
+       #     assert b.slice_from(1).hexdigest  == "626364"
+       #     assert b.slice_from(-1).hexdigest == "61626364"
+       #     assert b.slice_from(2).hexdigest  == "6364"
+       fun slice_from(from: Int): Bytes do
+               if from >= length then return new Bytes.empty
+               if from < 0 then from = 0
+               return slice(from, length)
+       end
+
        # Returns self as a hexadecimal digest
        fun hexdigest: String do
                var elen = length * 2
@@ -218,6 +351,15 @@ class Bytes
                length += ln
        end
 
+       # Appends the bytes of `s` to `selftextextt`
+       fun append_text(s: Text) do
+               for i in s.substrings do
+                       append_ns(i.fast_cstring, i.bytelen)
+               end
+       end
+
+       redef fun append_to(b) do b.append self
+
        redef fun enlarge(sz) do
                if capacity >= sz then return
                persisted = false
@@ -237,6 +379,157 @@ class Bytes
 
        redef fun iterator do return new BytesIterator.with_buffer(self)
 
+       redef fun first_index_in_from(b, from) do
+               if is_empty then return -1
+               var fst = self[0]
+               var bpos = fst.first_index_in_from(self, from)
+               for i in [0 .. length[ do
+                       if self[i] != b[bpos] then return first_index_in_from(b, bpos + 1)
+                       bpos += 1
+               end
+               return bpos
+       end
+
+       redef fun last_index_in_from(b, from) do
+               if is_empty then return -1
+               var lst = self[length - 1]
+               var bpos = lst.last_index_in_from(b, from)
+               for i in [0 .. length[.step(-1) do
+                       if self[i] != b[bpos] then return last_index_in_from(b, bpos - 1)
+                       bpos -= 1
+               end
+               return bpos
+       end
+
+       redef fun search_all_in(b) do
+               var ret = new Array[Int]
+               var pos = first_index_in_from(b, 0)
+               if pos == -1 then return ret
+               pos = pos + 1
+               ret.add pos
+               loop
+                       pos = first_index_in_from(b, pos)
+                       if pos == -1 then return ret
+                       ret.add pos
+                       pos += length
+               end
+       end
+
+       # Splits the content on self when encountering `b`
+       #
+       #     var a = "String is string".to_bytes.split_with('s'.ascii)
+       #     assert a.length == 3
+       #     assert a[0].hexdigest == "537472696E672069"
+       #     assert a[1].hexdigest == "20"
+       #     assert a[2].hexdigest == "7472696E67"
+       fun split_with(b: BytePattern): Array[Bytes] do
+               var fst = b.search_all_in(self)
+               if fst.is_empty then return [clone]
+               var retarr = new Array[Bytes]
+               var prev = 0
+               for i in fst do
+                       retarr.add(slice(prev, i - prev))
+                       prev = i + b.pattern_length
+               end
+               retarr.add slice_from(prev)
+               return retarr
+       end
+
+       # Splits `self` in two parts at the first occurence of `b`
+       #
+       #     var a = "String is string".to_bytes.split_once_on('s'.ascii)
+       #     assert a[0].hexdigest == "537472696E672069"
+       #     assert a[1].hexdigest == "20737472696E67"
+       fun split_once_on(b: BytePattern): Array[Bytes] do
+               var spl = b.first_index_in(self)
+               if spl == -1 then return [clone]
+               var ret = new Array[Bytes].with_capacity(2)
+               ret.add(slice(0, spl))
+               ret.add(slice_from(spl + b.pattern_length))
+               return ret
+       end
+
+       # Replaces all the occurences of `this` in `self` by `by`
+       #
+       #     var b = "String is string".to_bytes.replace(0x20u8, 0x41u8)
+       #     assert b.hexdigest == "537472696E6741697341737472696E67"
+       fun replace(pattern: BytePattern, bytes: BytePattern): Bytes do
+               if is_empty then return new Bytes.empty
+               var pos = pattern.search_all_in(self)
+               if pos.is_empty then return clone
+               var ret = new Bytes.with_capacity(length)
+               var prev = 0
+               for i in pos do
+                       ret.append_ns(items.fast_cstring(prev), i - prev)
+                       bytes.append_to ret
+                       prev = i + pattern.pattern_length
+               end
+               ret.append(slice_from(pos.last + pattern.pattern_length))
+               return ret
+       end
+
+       # Decode `self` from percent (or URL) encoding to a clear string
+       #
+       # Replace invalid use of '%' with '?'.
+       #
+       #     assert "aBc09-._~".to_bytes.from_percent_encoding == "aBc09-._~".to_bytes
+       #     assert "%25%28%29%3c%20%3e".to_bytes.from_percent_encoding == "%()< >".to_bytes
+       #     assert ".com%2fpost%3fe%3dasdf%26f%3d123".to_bytes.from_percent_encoding == ".com/post?e=asdf&f=123".to_bytes
+       #     assert "%25%28%29%3C%20%3E".to_bytes.from_percent_encoding == "%()< >".to_bytes
+       #     assert "incomplete %".to_bytes.from_percent_encoding == "incomplete ?".to_bytes
+       #     assert "invalid % usage".to_bytes.from_percent_encoding == "invalid ? usage".to_bytes
+       #     assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".to_bytes.from_percent_encoding == "éあいう".to_bytes
+       fun from_percent_encoding: Bytes do
+               var tmp = new Bytes.with_capacity(length)
+               var pos = 0
+               while pos < length do
+                       var b = self[pos]
+                       if b != '%'.ascii then
+                               tmp.add b
+                               pos += 1
+                               continue
+                       end
+                       if length - pos < 2 then
+                               tmp.add '?'.ascii
+                               pos += 1
+                               continue
+                       end
+                       var bn = self[pos + 1]
+                       var bnn = self[pos + 2]
+                       if not bn.is_valid_hexdigit or not bnn.is_valid_hexdigit then
+                               tmp.add '?'.ascii
+                               pos += 1
+                               continue
+                       end
+                       tmp.add((bn.hexdigit_to_byteval << 4) + bnn.hexdigit_to_byteval)
+                       pos += 3
+               end
+               return tmp
+       end
+
+       # Is `b` a prefix of `self` ?
+       fun has_prefix(b: BytePattern): Bool do return b.is_prefix(self)
+
+       # Is `b` a suffix of `self` ?
+       fun has_suffix(b: BytePattern): Bool do return b.is_suffix(self)
+
+       redef fun is_suffix(b) do
+               if length > b.length then return false
+               var j = b.length - 1
+               var i = length - 1
+               while i > 0 do
+                       if self[i] != b[j] then return false
+                       i -= 1
+                       j -= 1
+               end
+               return true
+       end
+
+       redef fun is_prefix(b) do
+               if length > b.length then return false
+               for i in [0 .. length[ do if self[i] != b[i] then return false
+               return true
+       end
 end
 
 private class BytesIterator
@@ -397,3 +690,20 @@ redef class NativeString
                return new Bytes(nns, len, len)
        end
 end
+
+# Joins an array of bytes `arr` separated by `sep`
+#
+#     assert join_bytes(["String".to_bytes, "is".to_bytes, "string".to_bytes], ' '.ascii).hexdigest == "537472696E6720697320737472696E67"
+fun join_bytes(arr: Array[Bytes], sep: nullable BytePattern): Bytes do
+       if arr.is_empty then return new Bytes.empty
+       sep = sep or else new Bytes.empty
+       var endln = sep.pattern_length * (arr.length - 1)
+       for i in arr do endln += i.length
+       var ret = new Bytes.with_capacity(endln)
+       ret.append(arr.first)
+       for i in  [1 .. arr.length[ do
+               sep.append_to(ret)
+               ret.append arr[i]
+       end
+       return ret
+end
index b65f282..d196333 100644 (file)
@@ -111,11 +111,18 @@ abstract class AbstractArrayRead[E]
        #     assert b      ==  [10, 20, 2, 3, 50]
        fun copy_to(start: Int, len: Int, dest: AbstractArray[E], new_start: Int)
        do
-               # TODO native one
-               var i = len
-               while i > 0 do
-                       i -= 1
-                       dest[new_start+i] = self[start+i]
+               if start < new_start then
+                       var i = len
+                       while i > 0 do
+                               i -= 1
+                               dest[new_start+i] = self[start+i]
+                       end
+               else
+                       var i = 0
+                       while i < len do
+                               dest[new_start+i] = self[start+i]
+                               i += 1
+                       end
                end
        end
 
@@ -220,22 +227,18 @@ abstract class AbstractArray[E]
        do
                assert not_empty: not is_empty
                var r = first
-               var i = 1
-               var l = length
-               while i < l do
-                       self[i-1] = self[i]
-                       i += 1
-               end
-               _length = l - 1
+               var l = length-1
+               copy_to(1, l, self, 0)
+               _length = l
                return r
        end
 
        redef fun unshift(item)
        do
-               var i = length - 1
-               while i >= 0 do
-                       self[i+1] = self[i]
-                       i -= 1
+               var l = length
+               if l > 0 then
+                       enlarge(l + 1)
+                       copy_to(0, l, self, 1)
                end
                self[0] = item
        end
@@ -369,6 +372,32 @@ class Array[E]
                _length = nl
        end
 
+       redef fun copy_to(start, len, dest, new_start)
+       do
+               # Fast code when source and destination are two arrays
+
+               if not dest isa Array[E] then
+                       super
+                       return
+               end
+
+               # Enlarge dest if required
+               var dest_len = new_start + len
+               if dest_len > dest.length then
+                       dest.enlarge(dest_len)
+                       dest.length = dest_len
+               end
+
+               # Get underlying native arrays
+               var items = self.items
+               if items == null then return
+               var dest_items = dest.items
+               assert dest_items != null
+
+               # Native copy
+               items.memmove(start, len, dest_items, new_start)
+       end
+
        redef fun enlarge(cap)
        do
                var c = _capacity
@@ -962,6 +991,25 @@ universal NativeArray[E]
 
        # Copy `length` items to `dest`.
        fun copy_to(dest: NativeArray[E], length: Int) is intern
+
+       # Copy `length` items to `dest` starting from `dest`.
+       fun memmove(start: Int, length: Int, dest: NativeArray[E], dest_start: Int) do
+               # TODO native one
+               if start < dest_start then
+                       var i = length
+                       while i > 0 do
+                               i -= 1
+                               dest[dest_start+i] = self[start+i]
+                       end
+               else
+                       var i = 0
+                       while i < length do
+                               dest[dest_start+i] = self[start+i]
+                               i += 1
+                       end
+               end
+       end
+
        #fun =(o: NativeArray[E]): Bool is intern
        #fun !=(o: NativeArray[E]): Bool is intern
 end
diff --git a/lib/core/collection/circular_array.nit b/lib/core/collection/circular_array.nit
new file mode 100644 (file)
index 0000000..9419853
--- /dev/null
@@ -0,0 +1,258 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Efficient data structure to access both end of the sequence.
+module circular_array
+
+import array
+
+# Efficient data structure to access both end of the sequence.
+#
+# A circular array offers efficient random access,
+# efficient manipulation for both ends of the structure (push, pop, ) and
+# automatic amortized growth.
+#
+# Therefore it can be used as is or as and efficient queue (FIFO/LIFO)
+class CircularArray[E]
+       super Sequence[E]
+
+       # The low-level storage of the items.
+       #
+       # Internally, there is two main configurations
+       #
+       # One part: from `head` to `tail` (inclusive)
+       #
+       # ~~~raw
+       # ...0123...
+       #    h  t
+       # ~~~
+       #
+       # Two parts: from `head` to `capacity-1`,
+       # then from `0` to `tail` (both inclusive)
+       # Test with `head > tail`
+       #
+       # ~~~raw
+       # 345....012
+       #   t    h
+       # ~~~
+       #
+       # For consistency, *length* and *index* are used in the context of the sequence (self) and
+       # *capacity* and *offset* are used in the context of the native array.
+       #
+       # Initially the native is not allocated, the first one is done with `enlarge`
+       private var native: NativeArray[E] is noautoinit
+
+       # The offset of the `first` item in `native`
+       private var head = 0
+
+       # The offset of the `last` item in `native`
+       private var tail: Int = -1
+
+       redef var length = 0
+
+       # Transform an index into an offset.
+       #
+       # The method takes care of the initial gap and the possible wrapping.
+       #
+       # REQUIRE: `0 <= index and index < length`
+       private fun offset(index: Int): Int
+       do
+               assert index >= 0
+               var head = self._head
+               var tail = self._tail
+               var offset = head + index
+
+               if head > tail then
+                       # Two parts
+                       var capacity = native.length
+                       if offset < capacity then
+                               return offset
+                       end
+                       offset -= capacity
+               end
+
+               assert offset <= tail
+               return offset
+       end
+
+       redef fun [](index) do return native[offset(index)]
+
+       redef fun []=(index, item) do
+               var l = length
+               if index == l then
+                       push(item)
+                       return
+               end
+               native[offset(index)] = item
+       end
+
+       redef fun push(item) do
+               var l = length + 1
+               enlarge(l)
+               length = l
+
+               var native = _native
+               var cap = native.length
+               var t = tail + 1
+               if t >= cap then t -= cap
+
+               native[t] = item
+               tail = t
+       end
+
+       redef fun add_all(items) do
+               enlarge length + items.length
+               super
+       end
+
+       redef fun pop do
+               var l = length - 1
+               assert l >= 0
+               length = l
+
+               var native = _native
+               var t = tail
+               var res = native[t]
+
+               t -= 1
+               if t < 0 then t += native.length
+               tail = t
+
+               return res
+       end
+
+       redef fun unshift(item) do
+               var l = length + 1
+               enlarge(l)
+               length = l
+
+               var native = _native
+               var h = head - 1
+               if h < 0 then h += native.length
+
+               native[h] = item
+               head = h
+       end
+
+       redef fun shift do
+               var l = length - 1
+               assert l >= 0
+               length = l
+
+               var native = _native
+               var h = head
+               var res = native[h]
+
+               h += 1
+               var cap = native.length
+               if h >= cap then h -= cap
+               head = h
+
+               return res
+       end
+
+       # Ensure at least a given capacity
+       #
+       # If the current capacity is enough, then no-op.
+       fun enlarge(capacity: Int)
+       do
+               # First allocation
+               if not isset _native then
+                       var new_c = 8
+                       while new_c < capacity do new_c *= 2
+                       native = new NativeArray[E](new_c)
+                       return
+               end
+
+               # Compute more capacity
+               var c = native.length
+               if capacity <= c then return
+               var new_c = c
+               while new_c < capacity do new_c *= 2
+
+               var new_native = new NativeArray[E](new_c)
+
+               # Reallocation: just realign the parts on 0
+               if head > tail then
+                       # Two parts
+                       native.memmove(head, c-head, new_native, 0)
+                       native.memmove(0, tail+1, new_native, c-head)
+               else
+                       # One part
+                       native.memmove(head, length, new_native, 0)
+               end
+               head = 0
+               tail = length - 1
+               native = new_native
+       end
+
+       redef fun insert(item, index)
+       do
+               # Special insertion at the end (is push)
+               if index >= length then
+                       assert index == length
+                       push(item)
+                       return
+               end
+               assert index >= 0
+
+               var new_len = length + 1
+
+               # TODO be more efficient:
+               # Here, we just allocate a new native and copy everything.
+
+               # Allocate a new native array
+               var c = native.length
+               while c < new_len do c *= 2
+               var new_native = new NativeArray[E](c)
+
+               # Copy everything
+               var i = 0
+               while i < index do
+                       new_native[i] = self[i]
+                       i += 1
+               end
+               new_native[index] = item
+               var l = length
+               while i < l do
+                       new_native[i+1] = self[i]
+                       i += 1
+               end
+
+               # Use the new native array
+               length = new_len
+               head = 0
+               tail = new_len - 1
+               native = new_native
+       end
+
+       redef fun clear do
+               length = 0
+               head = 0
+               tail = -1
+       end
+
+       redef fun iterator do return new CircularArrayIterator[E](self)
+end
+
+private class CircularArrayIterator[E]
+       super IndexedIterator[E]
+
+       var array: CircularArray[E]
+
+       redef var index = 0
+       redef fun is_ok do return index < array.length
+       redef fun item do return array[index]
+       redef fun next do index += 1
+end
index 0701cc7..2475b68 100644 (file)
@@ -16,6 +16,7 @@ module collection
 import range
 import list
 import array
+import circular_array
 import sorter
 import hash_collection
 import union_find
index e114d35..76823e2 100644 (file)
@@ -998,7 +998,7 @@ abstract class Text
 end
 
 # All kinds of array-based text representations.
-private abstract class FlatText
+abstract class FlatText
        super Text
 
        # Underlying C-String (`char*`)
index 4d35126..c3332d0 100644 (file)
@@ -36,9 +36,11 @@ end
 
 redef class FlatText
 
-       fun first_byte: Int do return 0
+       # First byte of the NativeString
+       protected fun first_byte: Int do return 0
 
-       fun last_byte: Int do return _bytelen - 1
+       # Last byte of the NativeString
+       protected fun last_byte: Int do return _bytelen - 1
 
        # Cache of the latest position (char) explored in the string
        var position: Int = 0
index ea410a2..a9dc0ed 100644 (file)
@@ -15,6 +15,8 @@
 # Support to generate and otherwise manipulate Nit code
 module gen_nit
 
+import template
+
 redef class Sys
        # Reserved keywords in the Nit language
        var keywords: Set[String] is lazy do return new HashSet[String].from([
@@ -39,3 +41,37 @@ redef class Sys
        var methods_in_pointer: Array[String] is lazy do return methods_in_object + [
                "free"]
 end
+
+# Template of a Nit module to generate Nit code
+class NitModule
+       super Template
+
+       # Header on top of the module, usually the documentation
+       var header: nullable Writable = null is writable
+
+       # The module's name
+       var name: Writable is writable
+
+       # Imports from this module
+       var imports = new Array[Writable]
+
+       # Main content of this module
+       var content = new Array[Writable]
+
+       redef fun rendering
+       do
+               var header = header
+               if header != null then add header
+
+               var name = name
+               add "module {name}\n\n"
+
+               for i in imports do add "import {i}\n"
+               add "\n"
+
+               for l in content do
+                       add l
+                       add "\n"
+               end
+       end
+end
diff --git a/lib/nitcorn/examples/.gitignore b/lib/nitcorn/examples/.gitignore
new file mode 100644 (file)
index 0000000..c704121
--- /dev/null
@@ -0,0 +1 @@
+src/restful_annot_gen.nit
index 9597941..d1bdeae 100644 (file)
@@ -1,7 +1,15 @@
-all:
+all: bin/restful_annot
        mkdir -p bin/
        ../../../bin/nitc --dir bin src/nitcorn_hello_world.nit src/simple_file_server.nit
 
 xymus.net:
        mkdir -p bin/
        ../../../bin/nitc --dir bin/ src/xymus_net.nit
+
+pre-build: src/restful_annot_gen.nit
+src/restful_annot_gen.nit:
+       ../../../bin/nitrestful -o $@ src/restful_annot.nit
+
+bin/restful_annot: src/restful_annot_gen.nit
+       mkdir -p bin/
+       ../../../bin/nitc -o $@ src/restful_annot_gen.nit
diff --git a/lib/nitcorn/examples/src/restful_annot.nit b/lib/nitcorn/examples/src/restful_annot.nit
new file mode 100644 (file)
index 0000000..3657031
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import nitcorn::restful
+
+class MyAction
+       super RestfulAction
+
+       # Method answering requests like `foo?s=some_string&i=42&b=true`
+       fun foo(s: String, i: Int, b: Bool): HttpResponse
+       is restful do
+               var resp = new HttpResponse(200)
+               resp.body = "foo {s} {i} {b}"
+               return resp
+       end
+
+       # Method answering requests like `bar?s=these_arguments_are_optional`
+       fun bar(s: nullable String, i: nullable Int, b: nullable Bool): HttpResponse
+       is restful do
+               var resp = new HttpResponse(200)
+               resp.body = "bar {s or else "null"} {i or else "null"} {b or else "null"}"
+               return resp
+       end
+end
+
+var vh = new VirtualHost("localhost:8080")
+
+# Serve everything with our restful action
+vh.routes.add new Route(null, new MyAction)
+
+# Avoid executing when running tests
+if "NIT_TESTING".environ == "true" then exit 0
+
+var factory = new HttpFactory.and_libevent
+factory.config.virtual_hosts.add vh
+factory.run
diff --git a/lib/nitcorn/restful.nit b/lib/nitcorn/restful.nit
new file mode 100644 (file)
index 0000000..bccfd2c
--- /dev/null
@@ -0,0 +1,45 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Support module for the `nitrestful` tool and the `restful` annotation
+module restful is new_annotation(restful)
+
+import nitcorn
+import json::serialization
+
+# Action with `restful` methods
+class RestfulAction
+       super Action
+
+       redef fun answer(request, truncated_uri) do return new HttpResponse(400)
+
+       # Service to deserialize arguments from JSON
+       #
+       # Accepts `nullable String` for convenience, but returns `null` when `val == null`.
+       #
+       # This method is called by the code generated by `nitrestful`.
+       # It can be specialized to customize its behavior.
+       protected fun deserialize_arg(val: nullable String): nullable Object
+       do
+               if val == null then return null
+
+               var deserializer = new JsonDeserializer(val)
+               if deserializer.errors.not_empty then
+                       print_error deserializer.errors.join("\n")
+                       return null
+               end
+
+               return deserializer.deserialize
+       end
+end
index 67e6765..d55bad0 100644 (file)
@@ -499,4 +499,12 @@ class ConcurrentList[E]
                real_collection.push(e)
                mutex.unlock
        end
+
+       redef fun shift
+       do
+               mutex.lock
+               var value = real_collection.shift
+               mutex.unlock
+               return value
+       end
 end
diff --git a/lib/pthreads/examples/jointask_example.nit b/lib/pthreads/examples/jointask_example.nit
new file mode 100644 (file)
index 0000000..3d12f6f
--- /dev/null
@@ -0,0 +1,52 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Simple example of joinable task using threadpool
+module jointask_example
+
+import threadpool
+
+# Task computing a string
+class StringTask
+       super JoinTask
+
+       # Sleeping time
+       var sec: Int
+
+       # result of `self` execution
+       var value: String
+
+       # ID for printing
+       var id: Int
+
+       redef fun main do
+               nanosleep(sec, 0)
+               value += " id: {id}"
+       end
+end
+
+var tp = new ThreadPool
+var t0 = new StringTask(10, "First, long task", 0)
+var tasks = new Array[StringTask]
+for i in 5.times do
+       tasks.add(new StringTask(1, "Small task", i + 1))
+end
+tp.execute(t0)
+for t in tasks do tp.execute(t)
+for t in tasks do
+       t.join
+       print t.value
+end
+t0.join
+print t0.value
index f7fafd7..fec1ae2 100644 (file)
@@ -19,7 +19,7 @@ import threadpool
 
 # Task printing "hello world" on standard output
 class HWTask
-       super Task
+       super JoinTask
 
        # Sleeping time
        var sec: Int
index 42931d3..02042ba 100644 (file)
@@ -20,7 +20,7 @@ import concurrent_collections
 
 # A simple ThreadPool implemented with an array
 class ThreadPool
-       private var queue = new ConcurrentList[Task].wrap(new List[Task])
+       private var queue = new ConcurrentList[JoinTask].wrap(new List[JoinTask])
        private var mutex = new Mutex
        private var cond = new NativePthreadCond
 
@@ -37,7 +37,7 @@ class ThreadPool
        private fun set_nb_threads(nb: nullable Int) is autoinit do nb_threads = nb or else 5
 
        # Adds a Task into the queue
-       fun execute(task: Task) do
+       fun execute(task: JoinTask) do
                queue.push(task)
                cond.signal
        end
@@ -47,20 +47,49 @@ end
 private class PoolThread
        super Thread
 
-       var queue: ConcurrentList[Task]
+       var queue: ConcurrentList[JoinTask]
        var mutex: Mutex
        var cond : NativePthreadCond
 
        redef fun main do
                loop
-                       var t: nullable Task = null
+                       var t: nullable JoinTask = null
                        mutex.lock
                        if queue.is_empty then cond.wait(mutex.native.as(not null))
                        if not queue.is_empty then
-                               t = queue.pop
+                               t = queue.shift
                        end
                        mutex.unlock
-                       if t != null then t.main
+                       if t != null then
+                               t.main
+                               t.mutex.lock
+                               t.is_done = true
+                               var tcond = t.cond
+                               if tcond != null then tcond.signal
+                               t.mutex.unlock
+                       end
+               end
+       end
+end
+
+# A Task which is joinable, meaning it can return a value and if the value is not set yet, it blocks the execution
+class JoinTask
+       super Task
+
+       # Is `self` done ?
+       var is_done = false
+
+       private var mutex = new Mutex
+       private var cond: nullable NativePthreadCond = null
+
+       # Return immediatly if the task terminated, or block waiting for `self` to terminate
+       fun join do
+               mutex.lock
+               if not is_done then
+                       var cond = new NativePthreadCond
+                       self.cond = cond
+                       cond.wait(mutex.native.as(not null))
                end
+               mutex.unlock
        end
 end
diff --git a/share/man/nitrestful.md b/share/man/nitrestful.md
new file mode 100644 (file)
index 0000000..0968830
--- /dev/null
@@ -0,0 +1,19 @@
+# NAME
+
+nitrestful - generates boilerplate code to relay RESTful request to static Nit methods
+
+# SYNOPSIS
+
+nitrestful [*options*]... FILE
+
+# OPTIONS
+
+### `-o`, `--output`
+Output file (can also be 'stdout').
+
+### `--dir`
+Output directory.
+
+# SEE ALSO
+
+The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org>
index 6b9866a..0a5b909 100644 (file)
@@ -16,7 +16,7 @@
 
 NITCOPT=--semi-global
 OLDNITCOPT=--semi-global
-OBJS=nitc nitpick nit nitdoc nitls nitunit nitpretty nitmetrics nitx nitlight nitdbg_client nitserial
+OBJS=nitc nitpick nit nitdoc nitls nitunit nitpretty nitmetrics nitx nitlight nitdbg_client nitserial nitrestful
 SRCS=$(patsubst %,%.nit,$(OBJS))
 BINS=$(patsubst %,../bin/%,$(OBJS))
 
index 183b4b2..cf3da76 100644 (file)
@@ -24,7 +24,7 @@ class ASTValidationVisitor
        do
                node.accept_ast_validation(self)
        end
-       private var path = new List[ANode]
+       private var path = new CircularArray[ANode]
        private var seen = new HashSet[ANode]
 end
 
index 3a70e66..358f211 100644 (file)
@@ -56,7 +56,6 @@ redef class ADefinition
        end
 end
 
-# TODO add annotations on attributes (volatile, sensitive or do_not_serialize?)
 private class SerializationPhasePreModel
        super Phase
 
diff --git a/src/nitrestful.nit b/src/nitrestful.nit
new file mode 100644 (file)
index 0000000..22ffd1d
--- /dev/null
@@ -0,0 +1,252 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Tool generating boilerplate code linking RESTful actions to Nit methods
+module nitrestful
+
+import gen_nit
+
+import frontend
+
+private class RestfulPhase
+       super Phase
+
+       # Classes with methods marked with the `restful` annotation
+       var restful_classes = new HashSet[MClass]
+
+       redef fun process_annotated_node(node, nat)
+       do
+               # Skip if we are not interested
+               var text = nat.n_atid.n_id.text
+               if text != "restful" then return
+
+               if not node isa AMethPropdef then
+                       toolcontext.error(nat.location,
+                               "Syntax Error: `restful` can only be applied on method definitions")
+                       return
+               end
+
+               var mpropdef = node.mpropdef
+               if mpropdef == null then return
+
+               var mproperty = mpropdef.mproperty
+               var mclassdef = mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+
+               # Test subclass of `RestfulAction`
+               var sup_class_name = "RestfulAction"
+               var sup_class = toolcontext.modelbuilder.try_get_mclass_by_name(
+                       nat, mmodule, sup_class_name)
+               var in_hierarchy = mclassdef.in_hierarchy
+               if in_hierarchy == null or sup_class == null then return
+               var sup_classes = in_hierarchy.greaters
+               if not sup_classes.has(sup_class.intro) then
+                       toolcontext.error(nat.location,
+                               "Syntax Error: `restful` is only valid within subclasses of `{sup_class_name}`")
+                       return
+               end
+
+               # Register the property
+               var mclass = mclassdef.mclass
+               mclass.restful_methods.add mproperty
+               restful_classes.add mclass
+       end
+end
+
+redef class MClass
+
+       # Methods with the `restful` annotation in this class
+       private var restful_methods = new Array[MMethod]
+end
+
+redef class ToolContext
+       # Generate serialization and deserialization methods on `auto_serializable` annotated classes.
+       var restful_phase: Phase = new RestfulPhase(self, [modelize_class_phase])
+
+       # Where do we put a single result?
+       var opt_output: OptionString = new OptionString("Output file (can also be 'stdout')", "-o", "--output")
+
+       # Where do we put the result?
+       var opt_dir: OptionString = new OptionString("Output directory", "--dir")
+
+       redef init
+       do
+               option_context.add_option(opt_output, opt_dir)
+               super
+       end
+end
+
+redef class MType
+       # Write code in `template` to parse the argument `arg_name` to this parameter type
+       private fun gen_arg_convert(template: Template, arg_name: String)
+       do
+               if self.name == "String" or self.name == "nullable String" then
+                       # String are used as is
+                       template.add """
+                       var out_{{{arg_name}}} = in_{{{arg_name}}}
+"""
+               else
+                       # Deserialize everything else
+                       template.add """
+                       var out_{{{arg_name}}} = deserialize_arg(in_{{{arg_name}}})
+"""
+               end
+       end
+
+       # Does this parameter type needs to be checked before calling the method?
+       #
+       # Some nullable types do not need to be check as `null` values are acceptable.
+       private fun needs_type_check: Bool do return true
+end
+
+redef class MNullableType
+       redef fun needs_type_check do return name != "nullable String" and name != "nullable Object"
+end
+
+var toolcontext = new ToolContext
+toolcontext.tooldescription = """
+Usage: nitrestful [OPTION] module.nit [other_module.nit [...]]
+Generates the boilerplate code to link RESTful request to static Nit methods."""
+
+toolcontext.process_options args
+var arguments = toolcontext.option_context.rest
+
+# Check options
+if toolcontext.opt_output.value != null and toolcontext.opt_dir.value != null then
+       print "Error: cannot use both --dir and --output"
+       exit 1
+end
+if arguments.length > 1 and toolcontext.opt_output.value != null then
+       print "Error: --output needs a single source file. Do you prefer --dir?"
+       exit 1
+end
+
+var model = new Model
+var modelbuilder = new ModelBuilder(model, toolcontext)
+
+var mmodules = modelbuilder.parse(arguments)
+modelbuilder.run_phases
+var first_mmodule = mmodules.first
+
+# Name of the support module
+var module_name
+
+# Path to the support module
+var module_path = toolcontext.opt_output.value
+if module_path == null then
+       module_name = "{first_mmodule.name}_rest"
+       module_path = "{module_name}.nit"
+
+       var dir = toolcontext.opt_dir.value
+       if dir != null then module_path = dir.join_path(module_path)
+else if module_path == "stdout" then
+       module_name = "{first_mmodule.name}_rest"
+       module_path = null
+else if module_path.has_suffix(".nit") then
+       module_name = module_path.basename(".nit")
+else
+       module_name = module_path.basename
+       module_path += ".nit"
+end
+
+var nit_module = new NitModule(module_name)
+nit_module.header = """
+# This file is generated by nitrestful
+# Do not modify, instead refine the generated services.
+"""
+
+for mmod in mmodules do
+       nit_module.imports.add mmod.name
+end
+
+var phase = toolcontext.restful_phase
+assert phase isa RestfulPhase
+
+for mclass in phase.restful_classes do
+
+       var t = new Template
+       nit_module.content.add t
+
+       t.add """
+redef class {{{mclass}}}
+       redef fun answer(request, truncated_uri)
+       do
+               var verbs = truncated_uri.split("/")
+               if verbs.not_empty and verbs.first.is_empty then verbs.shift
+
+               if verbs.length != 1 then return super
+               var verb = verbs.first
+
+"""
+       var methods = mclass.restful_methods
+       for i in methods.length.times, method in methods do
+               var msig = method.intro.msignature
+               if msig == null then continue
+
+               t.add "         "
+               if i != 0 then t.add "else "
+
+               t.add """if verb == "{{{method.name}}}" then
+"""
+
+               var args = new Array[String]
+               var isas = new Array[String]
+               for param in msig.mparameters do
+
+                       t.add """
+                       var in_{{{param.name}}} = request.string_arg("{{{param.name}}}")
+"""
+
+                       var mtype = param.mtype
+                       mtype.gen_arg_convert(t, param.name)
+
+                       var arg = "out_{param.name}"
+                       args.add arg
+
+                       if mtype.needs_type_check then
+                               isas.add "{arg} isa {mtype.name}"
+                       end
+
+                       t.add "\n"
+               end
+
+               if isas.not_empty then t.add """
+                       if not {{{isas.join(" or not ")}}} then
+                               return super
+                       end
+"""
+
+               var sig = ""
+               if args.not_empty then sig = "({args.join(", ")})"
+
+               t.add """
+                       return {{{method.name}}}{{{sig}}}
+"""
+       end
+
+       t.add """
+               end
+               return super
+       end
+end"""
+end
+
+# Write support module
+if module_path != null then
+       # To file
+       nit_module.write_to_file module_path
+else
+       # To stdout
+       nit_module.write_to stdout
+end
index 1c24d69..9c8a07b 100644 (file)
 # generate and include its own serialization support module.
 module nitserial
 
-import frontend
-import rapid_type_analysis
 import template
+import gen_nit
 
-# A Nit module
-#
-# TODO add more features and move to lib
-class NitModule
-       super Template
-
-       var header: nullable Writable = null
-
-       # The module's name
-       var name: Writable
-
-       # Imports from this module
-       var imports = new Array[Writable]
-
-       # Main content of this module
-       var content = new Array[Writable]
-
-       redef fun rendering
-       do
-               var header = header
-               if header != null then add header
-
-               var name = name
-               add "module {name}\n\n"
-
-               for i in imports do add "import {i}\n"
-               add "\n"
-
-               for l in content do add "{l}\n"
-       end
-end
+import frontend
+import rapid_type_analysis
 
 redef class ToolContext
        # Where do we put a single result?
diff --git a/tests/bench_seq.nit b/tests/bench_seq.nit
new file mode 100644 (file)
index 0000000..8e83868
--- /dev/null
@@ -0,0 +1,42 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2005-2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import more_collections
+
+fun test_list(l: Sequence[Int], n: Int)
+do
+       for i in [0..n[ do l.push i
+       print "{l.length} {l.first} {l.last}"
+       for i in [0..n[ do l.unshift -i-1
+       print "{l.length} {l.first} {l.last}"
+       for i in [0..n[ do l.unshift l.pop
+       print "{l.length} {l.first} {l.last}"
+       for i in [0..n[ do l.push l.shift
+       print "{l.length} {l.first} {l.last}"
+       for i in [0..n[ do l[i] = l[n+i]
+       print "{l.length} {l.first} {l.last}"
+       for i in [0..n] do l.insert(i*10, i*2)
+       print "{l.length} {l.first} {l.last}"
+       print ""
+end
+
+var nb = 100
+if args.not_empty then nb = args.first.to_i
+
+test_list(new Array[Int], nb)
+test_list(new List[Int], nb)
+test_list(new UnrolledList[Int], nb)
+test_list(new CircularArray[Int], nb)
index 2fc5a83..8c7c00e 100644 (file)
@@ -17,6 +17,7 @@ test_docdown_args
 pep8analysis
 emscripten
 nitserial_args
+nitrestful_args
 nitunit_args
 nitpretty_args
 hamming_number
diff --git a/tests/nitrestful.args b/tests/nitrestful.args
new file mode 100644 (file)
index 0000000..054e93a
--- /dev/null
@@ -0,0 +1 @@
+-o stdout ../lib/nitcorn/examples/src/restful_annot.nit
index 2fc5a83..8c7c00e 100644 (file)
@@ -17,6 +17,7 @@ test_docdown_args
 pep8analysis
 emscripten
 nitserial_args
+nitrestful_args
 nitunit_args
 nitpretty_args
 hamming_number
diff --git a/tests/sav/bench_seq.res b/tests/sav/bench_seq.res
new file mode 100644 (file)
index 0000000..d9c755c
--- /dev/null
@@ -0,0 +1,28 @@
+100 0 99
+200 -100 99
+200 0 -1
+200 -100 99
+200 0 99
+301 0 99
+
+100 0 99
+200 -100 99
+200 0 -1
+200 -100 99
+200 0 99
+301 0 99
+
+100 0 99
+200 -100 99
+200 0 -1
+200 -100 99
+200 0 99
+301 0 99
+
+100 0 99
+200 -100 99
+200 0 -1
+200 -100 99
+200 0 99
+301 0 99
+
diff --git a/tests/sav/jointask_example.res b/tests/sav/jointask_example.res
new file mode 100644 (file)
index 0000000..a0f7bca
--- /dev/null
@@ -0,0 +1,6 @@
+Small task id: 1
+Small task id: 2
+Small task id: 3
+Small task id: 4
+Small task id: 5
+First, long task id: 0
diff --git a/tests/sav/nitrestful.res b/tests/sav/nitrestful.res
new file mode 100644 (file)
index 0000000..c1bc443
--- /dev/null
@@ -0,0 +1,3 @@
+Usage: nitrestful [OPTION] module.nit [other_module.nit [...]]
+Generates the boilerplate code to link RESTful request to static Nit methods.
+Use --help for help
diff --git a/tests/sav/nitrestful_args1.res b/tests/sav/nitrestful_args1.res
new file mode 100644 (file)
index 0000000..e2947d2
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is generated by nitrestful
+# Do not modify, instead refine the generated services.
+module restful_annot_rest
+
+import restful_annot
+
+redef class MyAction
+       redef fun answer(request, truncated_uri)
+       do
+               var verbs = truncated_uri.split("/")
+               if verbs.not_empty and verbs.first.is_empty then verbs.shift
+
+               if verbs.length != 1 then return super
+               var verb = verbs.first
+
+               if verb == "foo" then
+                       var in_s = request.string_arg("s")
+                       var out_s = in_s
+
+                       var in_i = request.string_arg("i")
+                       var out_i = deserialize_arg(in_i)
+
+                       var in_b = request.string_arg("b")
+                       var out_b = deserialize_arg(in_b)
+
+                       if not out_s isa String or not out_i isa Int or not out_b isa Bool then
+                               return super
+                       end
+                       return foo(out_s, out_i, out_b)
+               else if verb == "bar" then
+                       var in_s = request.string_arg("s")
+                       var out_s = in_s
+
+                       var in_i = request.string_arg("i")
+                       var out_i = deserialize_arg(in_i)
+
+                       var in_b = request.string_arg("b")
+                       var out_b = deserialize_arg(in_b)
+
+                       if not out_i isa nullable Int or not out_b isa nullable Bool then
+                               return super
+                       end
+                       return bar(out_s, out_i, out_b)
+               end
+               return super
+       end
+end
index 458de3c..9e5ceab 100644 (file)
@@ -1,4 +1,4 @@
-Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/core/collection/array.nit:960)
+Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/core/collection/array.nit:989)
 NativeString
 0x4e
 Nit
index 1de5fa0..c55a08b 100644 (file)
@@ -14,3 +14,7 @@ true
 true
 true
 
+true
+true
+true
+
index ff77897..40cfa68 100644 (file)
@@ -29,3 +29,4 @@ test_seq(new List[Int], new List[Int])
 test_seq(new Array[Int], new Array[Int])
 test_seq(new List[Int], new Array[Int])
 test_seq(new Array[Int], new List[Int])
+test_seq(new Array[Int], new CircularArray[Int])