nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
242 stars 67 forks source link

broken niti FFI for NativeArray #1899

Closed privat closed 8 years ago

privat commented 8 years ago

Exhibited by #1898 , the following code breaks the interpreter:

redef class NativeArray[E]
        fun foo `{ puts("Great Success"); `}
end

var a = new NativeArray[Object](1)
a.foo

This outputs na_ex.nit:2,2--27: Runtime error: FFI Error: Cannot load foreign code library for na_ex: nit_compile/na_ex.so: undefined symbol: na_ex___NativeArray_of_E_foo___impl

note: you need 8a0d0496177b59434e23c from #1898 else you will fail earlier because _Abstract method mangled_cname called on MParameterType_

xymus commented 8 years ago

Is there a common internal type to represent NativeArray between the compiler and the interpreter?

privat commented 8 years ago

No, native array are truly native, there isn't even a common representation between the separate and the global compiler. My best guess is to handle them as an opaque type (void* ?) for the moment.

xymus commented 8 years ago

Ok, so the only practical usage is to write a temporary extern implementation of a method until the intern version is integrated in c_src? So the only target engine should be the compiler? Because if the opaque type changes from engine to engine the C code would not be portable between the different engines.

Unless you have a use case of a method in NativeArray that does not use the state of the receiver?

I may simply catch the error sooner in the interpreter. Because its internal representation of NativeArray does not make much sense in C.

In the compiler, I can mark it as equivalent to a void*.

privat commented 8 years ago

My main issue is that the reported error does not make sense. My second issue is that the interpreter behavior differs form the compiler ones. If you look at the full example, the interpreter can handle the case with the class A without being more able to do anything with self anyway.

privat commented 8 years ago

Ohh.. I seems that the issue is possibly not related with NativeArray but with generics.

This one has the same behavior with nitc, nit and nith:

class A
    fun foo `{ puts("foo"); `}
end
var a = new A
a.foo

This one does not and fails with nit and nith:

class A[E]
    fun foo `{ puts("foo"); `}
end

var a = new A[Int]
a.foo

the error with nit:

x.nit:2,2--40: Runtime error: FFI Error: Cannot load foreign code library for x: nit_compile/x.so: undefined symbol: x___A_of_E_foo___impl

the error with nith:

x._ffi.c:14:43: error: unknown type name ‘A_of_nullable_Object
xymus commented 8 years ago

Heu... what's nith?

xymus commented 8 years ago

Hooo.. right, nith.