Merge: Clean stuff
authorJean Privat <jean@pryen.org>
Thu, 25 Sep 2014 13:10:33 +0000 (09:10 -0400)
committerJean Privat <jean@pryen.org>
Thu, 25 Sep 2014 13:10:33 +0000 (09:10 -0400)
A lot of small fixes and cleanups not really related.

Pull-Request: #768
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

27 files changed:
contrib/benitlux/src/benitlux_view.nit
examples/mnit_simple/src/complete_simple_android.nit
examples/mnit_simple/src/simple.nit
examples/mnit_simple/src/simple_linux.nit
examples/mnit_simple/src/test_data_store.nit [new file with mode: 0644]
lib/android/android.nit
lib/android/android_data_store.nit [new file with mode: 0644]
lib/app/app.nit [new file with mode: 0644]
lib/app/app_base.nit [moved from lib/app.nit with 90% similarity]
lib/app/data_store.nit [new file with mode: 0644]
lib/linux/linux.nit [new file with mode: 0644]
lib/linux/linux_data_store.nit [new file with mode: 0644]
lib/mnit_linux/linux_app.nit
lib/standard/re.nit
tests/nitg-sg.skip
tests/sav/dino_linux.res
tests/sav/friendz_linux.res
tests/sav/linux_app.res
tests/sav/linux_assets.res
tests/sav/mnit_linux.res
tests/sav/moles_linux.res
tests/sav/nitg-g/fixme/test_platform_android.res [new file with mode: 0644]
tests/sav/nitg-s/fixme/sensors.res [new file with mode: 0644]
tests/sav/nitg-sg/fixme/sensors.res [new file with mode: 0644]
tests/sav/opengles2_hello_triangle.res
tests/sav/shoot_linux.res
tests/sav/simple_linux.res

index 6c5d53b..d35d6d9 100644 (file)
@@ -43,6 +43,7 @@ class BenitluxDocument
        do
                add """
 <!DOCTYPE html>
+<html>
 <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
index 2f4f467..8700ab2 100644 (file)
@@ -24,4 +24,5 @@ import test_audio
 import test_shared_preferences
 import test_assets_and_resources
 import test_target_api
+import test_data_store
 import test_intent
index 6bfc9b1..9a3212d 100644 (file)
@@ -39,9 +39,9 @@ redef class App
                img = load_image( "fighter.png" )
        end
 
-       var r: Float = 0.0
-       var g: Float = 0.0
-       var b: Float = 0.0
+       var r = 0.0 is writable
+       var g = 0.0 is writable
+       var b = 0.0 is writable
        redef fun frame_core( display )
        do
                b = b + 0.01
index 1a3c5fd..c0d8d0f 100644 (file)
@@ -19,4 +19,6 @@ module simple_linux
 import simple
 import mnit_linux
 
+import test_data_store
+
 super
diff --git a/examples/mnit_simple/src/test_data_store.nit b/examples/mnit_simple/src/test_data_store.nit
new file mode 100644 (file)
index 0000000..4abd9a6
--- /dev/null
@@ -0,0 +1,52 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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.
+
+# Test for the shared_preferences module of App.nit framework
+module test_data_store
+
+import simple
+import app::data_store
+
+redef class App
+       redef fun window_created
+       do
+               super
+
+               # load colors from data store
+               var r = data_store["r"]
+               if r isa Float then
+                       self.r = r
+                       print "r {r}"
+               else assert r == null
+
+               var g = data_store["g"]
+               if g isa Float then
+                       self.g = g
+                       print "g {g}"
+               else assert g == null
+       end
+
+       redef fun input(ie)
+       do
+               if ie isa PointerEvent then
+                       # save color for next execution
+                       data_store["r"] = r
+                       data_store["g"] = g
+               end
+
+               return super
+       end
+end
index 3fc1a09..5523f30 100644 (file)
@@ -28,6 +28,7 @@ import platform
 import native_app_glue
 import dalvik
 private import log
+private import android_data_store
 
 # Uses Android logs to print everything
 redef fun print(text) do log_write(priority_info, app.log_prefix.to_cstring, text.to_s.to_cstring)
diff --git a/lib/android/android_data_store.nit b/lib/android/android_data_store.nit
new file mode 100644 (file)
index 0000000..57eb281
--- /dev/null
@@ -0,0 +1,38 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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.
+
+# Implements `app::data_store` using `shared_preferences`
+#
+# We use the shared preferences named "data_store" to store the data.
+module android_data_store
+
+import app::data_store
+private import shared_preferences
+
+redef class App
+       redef var data_store = new SharedPreferenceView
+end
+
+private class SharedPreferenceView
+       super DataStore
+
+       # The `SharedPreferences` used to implement the `DataStore`
+       var shared_preferences = new SharedPreferences.privately(app, "data_store") is lazy
+
+       redef fun [](key) do return shared_preferences[key]
+
+       redef fun []=(key, value) do shared_preferences[key] = value
+end
diff --git a/lib/app/app.nit b/lib/app/app.nit
new file mode 100644 (file)
index 0000000..79cbb38
--- /dev/null
@@ -0,0 +1,23 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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.
+
+# app.nit is a framework to create cross-platform applications
+#
+# The features offered by this modules are common to all platforms, but
+# may not be available on all devices.
+module app
+
+import app_base
similarity index 90%
rename from lib/app.nit
rename to lib/app/app_base.nit
index 5d0a1fd..a528d57 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# app.nit is a framework to create cross-platform applications
-#
-# The features offered by this modules are common to all platforms, but
-# may not be available on all devices.
-module app is
+module app_base is
        new_annotation app_name
        new_annotation app_version
 end
diff --git a/lib/app/data_store.nit b/lib/app/data_store.nit
new file mode 100644 (file)
index 0000000..afb6a1d
--- /dev/null
@@ -0,0 +1,38 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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 data storage services
+#
+# The implementation varies per platform.
+module data_store
+
+import app_base
+import serialization
+
+redef class App
+       # Services to store and load data
+       fun data_store: DataStore is abstract
+end
+
+# Simple data storage facility
+interface DataStore
+
+       # Get the object stored at `key`, or null if nothing is available
+       fun [](key: String): nullable Object is abstract
+
+       # Store `value` at `key`
+       fun []=(key: String, value: Serializable) is abstract
+end
diff --git a/lib/linux/linux.nit b/lib/linux/linux.nit
new file mode 100644 (file)
index 0000000..b106d62
--- /dev/null
@@ -0,0 +1,21 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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.
+
+# Implementation of app.nit for the Linux platform
+module linux
+
+import app
+private import linux_data_store
diff --git a/lib/linux/linux_data_store.nit b/lib/linux/linux_data_store.nit
new file mode 100644 (file)
index 0000000..b7cedea
--- /dev/null
@@ -0,0 +1,108 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# 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.
+
+module linux_data_store
+
+import app::data_store
+private import xdg_basedir
+private import sqlite3
+private import json_serialization
+
+redef class App
+       redef var data_store = new LinuxStore
+end
+
+private class LinuxStore
+       super DataStore
+
+       # File path of the Sqlite3 DB file
+       fun db_file: String do return "data_store.db"
+
+       # Sqlite3 table used
+       fun db_table: String do return "data_store"
+
+       private var db_cache: nullable Sqlite3DB = null
+
+       # Database to use to implement the `DataStore`
+       fun db: nullable Sqlite3DB
+       do
+               var db = db_cache
+               if db != null then return db
+
+               # Find DB path
+               var config_home = xdg_basedir.config_home.to_s
+               var config_dir = config_home.join_path(sys.program_name.basename(""))
+               if not config_dir.file_exists then config_dir.mkdir
+               var db_path = config_dir.join_path(db_file)
+
+               # Open DB connection
+               db = new Sqlite3DB.open(db_path)
+               if not db.is_open then
+                       app.log_error "Data store unavaible, cannot load/save data. (at '{db_path}' with '{db.error or else "unknown"}')"
+                       return null
+               end
+
+               # Create DB table
+               db.create_table "IF NOT EXISTS {db_table} (key TEXT PRIMARY KEY, value TEXT)"
+
+               db_cache = db
+               return db
+       end
+
+       redef fun [](key)
+       do
+               # Get DB
+               var db = self.db
+               if db == null then return null
+
+               # Prepare SELECT statement
+               var stmt = db.select("* FROM {db_table} WHERE key == {key.to_sql_string}")
+
+               # Execute statment
+               for row in stmt do
+                       # Get from statement
+                       var serialized = row[1].to_s
+                       stmt.close
+
+                       # Deserialize
+                       var deserializer = new JsonDeserializer(serialized)
+                       var deserialized = deserializer.deserialize
+
+                       return deserialized
+               end
+
+               stmt.close
+               return null
+       end
+
+       redef fun []=(key, value)
+       do
+               # Get DB
+               var db = self.db
+               if db == null then return
+
+               # Serialize
+               var stream = new StringOStream
+               var serializer = new JsonSerializer(stream)
+               serializer.serialize value
+               var serialized = stream.to_s
+
+               # Save in DB
+               db.execute "BEGIN TRANSACTION"
+               db.insert "OR REPLACE INTO {db_table} VALUES({key.to_sql_string}, {serialized.to_sql_string})"
+               db.execute "COMMIT"
+       end
+end
index e1d2e73..440187a 100644 (file)
@@ -19,6 +19,7 @@ module linux_app
 import mnit
 import sdl
 import linux_opengles1
+import linux
 
 in "C" `{
        #include <EGL/egl.h>
index 7baabd2..97cb7cb 100644 (file)
@@ -180,7 +180,7 @@ class Regex
        private var native: nullable NativeRegex = null
 
        # Cache of a single `regmatch_t` to prevent many calls to `malloc`
-       private var native_match = new NativeMatchArray.malloc(0) is lazy
+       private var native_match = new NativeMatchArray.malloc(1) is lazy
 
        # `cflags` of the last successful `compile`
        private var cflags_cache = 0
index c782ec0..5f17ed8 100644 (file)
@@ -1,2 +1,3 @@
 init_inherit
 init_linext
+android
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
diff --git a/tests/sav/nitg-g/fixme/test_platform_android.res b/tests/sav/nitg-g/fixme/test_platform_android.res
new file mode 100644 (file)
index 0000000..4ad3dc3
--- /dev/null
@@ -0,0 +1 @@
+UNDEFINED
diff --git a/tests/sav/nitg-s/fixme/sensors.res b/tests/sav/nitg-s/fixme/sensors.res
new file mode 100644 (file)
index 0000000..4ad3dc3
--- /dev/null
@@ -0,0 +1 @@
+UNDEFINED
diff --git a/tests/sav/nitg-sg/fixme/sensors.res b/tests/sav/nitg-sg/fixme/sensors.res
new file mode 100644 (file)
index 0000000..4ad3dc3
--- /dev/null
@@ -0,0 +1 @@
+UNDEFINED
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.
index 92405d4..c1dad34 100644 (file)
@@ -1,2 +1,2 @@
-../lib/mnit_linux/linux_app.nit:28,16--31: Redef Error: a virtual type cannot be refined.
-../lib/mnit_linux/linux_app.nit:29,16--29: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:29,16--31: Redef Error: a virtual type cannot be refined.
+../lib/mnit_linux/linux_app.nit:30,16--29: Redef Error: a virtual type cannot be refined.