Call this extern method

Property definitions

nitc :: naive_interpreter $ AMethPropdef :: call_extern
	# Call this extern method
	protected fun call_extern(v: NaiveInterpreter, mpropdef: MMethodDef, arguments: Array[Instance], f: Frame): nullable Instance
	do
		return v.error_instance
	end
src/interpreter/naive_interpreter.nit:972,2--976,4

nitc :: dynamic_loading_ffi $ AMethPropdef :: call_extern
	redef fun call_extern(v, mpropdef, args, frame)
	do
		# Fallback the default error if this method is not supported
		if not supported_by_dynamic_ffi then return super

		var entry = foreign_entry_cache
		if entry == null then
			# Get handle to foreign code lib
			var amodule = v.modelbuilder.mmodule2node(mpropdef.mclassdef.mmodule)
			assert amodule != null

			var lib = amodule.foreign_code_lib(v)
			if lib == null then return v.error_instance

			# Get handle to implementation function
			entry = lib.dlsym(mpropdef.foreign_lib_entry_cname.to_cstring)
			if entry.address_is_null then
				print mpropdef.foreign_lib_entry_cname
				v.fatal "FFI Error: Cannot find method {mpropdef.name} in foreign code library."
				return v.error_instance
			end

			foreign_entry_cache = entry
		end

		# Prepare to send args to foreign code lib
		var is_init = mpropdef.mproperty.is_init
		if is_init then args.shift
		var native_args_length = args.length

		var native_args = new CallArg(args.length)
		var a = 0
		if not is_init then
			var arg = args[a]
			var native_arg = native_args[a]
			native_arg.from_static_type(arg, mpropdef.mclassdef.mclass.mclass_type)
			a += 1
		end
		for param in mpropdef.msignature.mparameters do
			var arg = args[a]
			var native_arg = native_args[a]
			native_arg.from_static_type(arg, param.mtype)
			a += 1
		end

		# Allocate memory for the return value
		var native_return = new CallArg(1)
		var error = entry.call(native_args_length, native_args, native_return)

		if error then
			v.fatal "FFI Error: Native code library reported an error"
			return null
		end

		# Get the result
		var return_mtype = mpropdef.msignature.return_mtype
		if is_init then return_mtype = mpropdef.mclassdef.mclass.mclass_type

		var return_value
		if return_mtype == null then
			return_value = null
		else
			return_value = native_return.to_instance(return_mtype, v)
		end

		native_args.free
		native_return.free

		return return_value
	end
src/interpreter/dynamic_loading_ffi/dynamic_loading_ffi.nit:253,2--322,4