1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # FFI concers common between the compilers and the interpreter.
18 # Offers services to compile modules using foreign code. Mainly allows
19 # to wrap foreign code in Nit methods.
26 intrude import ffi_base
28 import header_dependency
30 import c_compiler_options
34 import extra_java_files
36 intrude import light_ffi
39 # Complete the compilation of the FFI code
40 redef fun finalize_ffi_wrapper
(compdir
, mainmodule
)
42 for language
in ffi_callbacks
.keys
do
43 for callback
in ffi_callbacks
[language
] do
44 language
.compile_callback
(callback
, self, mainmodule
, ffi_ccu
.as(not null))
47 language
.compile_to_files
(self, compdir
)
50 # include dependancies FFI
51 for mod
in header_dependencies
do
52 if mod
.uses_ffi
then ffi_ccu
.header_custom
.add
("#include \"{mod.c_name}._ffi
.h\
"\n")
60 redef class VerifyNitniCallbacksPhase
61 redef fun process_npropdef
(npropdef
)
65 if not npropdef
isa AMethPropdef then return
67 var code_block
= npropdef
.n_extern_code_block
68 if code_block
== null then return
70 var lang
= code_block
.language
73 # Associate callbacks used by an extern method to its foreign language
74 for callback
in npropdef
.foreign_callbacks
.all
do
75 var map
= npropdef
.mpropdef
.mclassdef
.mmodule
.ffi_callbacks
76 if not map
.keys
.has
(lang
) then map
[lang
] = new HashSet[NitniCallback]
77 map
[lang
].add
(callback
)