Merge: FFI: Fix extern methods in generic classes with the interpreter, and complex...
authorJean Privat <jean@pryen.org>
Thu, 17 Dec 2015 16:03:02 +0000 (11:03 -0500)
committerJean Privat <jean@pryen.org>
Thu, 17 Dec 2015 16:03:02 +0000 (11:03 -0500)
A kind of resolve was missing so the name of the generated C function was different between the call and its implementation.

Fix #1899.

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

src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit
src/nitni/nitni_base.nit
tests/sav/test_ffi_c_generics.res [new file with mode: 0644]
tests/test_ffi_c_generics.nit [new file with mode: 0644]

index c54d578..85deec6 100644 (file)
@@ -262,7 +262,7 @@ redef class MMethodDef
                for param in msignature.mparameters do params.add param.mtype.cname_blind
 
                # Declare the implementation function as extern
-               var impl_cname = mproperty.build_cname(mclassdef.mclass.mclass_type,
+               var impl_cname = mproperty.build_cname(mclassdef.bound_mtype,
                        mclassdef.mmodule, "___impl", long_signature)
                ecc.body_decl.add "extern {c_return_type} {impl_cname}({params.join(", ")});\n"
 
index 6eff3a5..d18d2c0 100644 (file)
@@ -67,7 +67,7 @@ redef class MType
        # Representation of this type in pure C on the FFI extern side
        #   Object -> Object
        #   Pointer -> void*
-       fun cname: String is abstract
+       fun cname: String do return cname_normal_class
 
        # Representation of this type in C for the internal of the system
        # Hides extern types.
@@ -83,6 +83,9 @@ redef class MType
        #   type Object is_primitive? false
        #   type Pointer is_primitive? true
        fun is_cprimitive: Bool do return false
+
+       # Name of this type in C for normal classes (not extern and not primitive)
+       protected fun cname_normal_class: String do return mangled_cname
 end
 
 redef class MClassType
@@ -125,9 +128,6 @@ redef class MClassType
                return super
        end
 
-       # Name of this type in C for normal classes (not extern and not primitive)
-       protected fun cname_normal_class: String do return mangled_cname
-
        redef fun mangled_cname do return mclass.name
 
        redef fun is_cprimitive do return mclass.kind == extern_kind or
@@ -136,7 +136,6 @@ redef class MClassType
 end
 
 redef class MNullableType
-       redef fun cname do return mangled_cname
        redef fun mangled_cname do return "nullable_{mtype.mangled_cname}"
 end
 
@@ -145,7 +144,6 @@ redef class MFormalType
 end
 
 redef class MGenericType
-       redef fun cname do return mangled_cname
        redef fun mangled_cname
        do
                var base = super
diff --git a/tests/sav/test_ffi_c_generics.res b/tests/sav/test_ffi_c_generics.res
new file mode 100644 (file)
index 0000000..1289765
--- /dev/null
@@ -0,0 +1,2 @@
+bar
+foo
diff --git a/tests/test_ffi_c_generics.nit b/tests/test_ffi_c_generics.nit
new file mode 100644 (file)
index 0000000..61b1e40
--- /dev/null
@@ -0,0 +1,27 @@
+# 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.
+
+redef class Array[E]
+       fun bar `{ puts("bar"); `}
+end
+
+redef class NativeArray[E]
+       fun foo `{ puts("foo"); `}
+end
+
+var a = new Array[String]
+a.bar
+
+var aa = new NativeArray[Int](1)
+aa.foo