@@ -633,10 +633,14 @@ ffi_type *c_loader_impl_ffi_type(type_id id)
633633 return &ffi_type_float;
634634 case TYPE_DOUBLE:
635635 return &ffi_type_double;
636+ case TYPE_STRING:
637+ return &ffi_type_pointer;
636638 case TYPE_PTR:
637639 return &ffi_type_pointer;
638640 case TYPE_FUNCTION:
639641 return &ffi_type_pointer;
642+ case TYPE_NULL:
643+ return &ffi_type_void;
640644 }
641645
642646 return &ffi_type_void;
@@ -683,6 +687,11 @@ function_return function_c_interface_invoke(function func, function_impl impl, f
683687
684688 closures.push_back (closure);
685689 }
690+ else if (id == TYPE_STRING)
691+ {
692+ char *str = value_to_string ((value)args[args_count]);
693+ c_function->values [args_count] = &str;
694+ }
686695 else
687696 {
688697 c_function->values [args_count] = value_data ((value)args[args_count]);
@@ -693,9 +702,7 @@ function_return function_c_interface_invoke(function func, function_impl impl, f
693702 size_t ret_size = value_type_id_size (ret_id);
694703 void *ret = NULL ;
695704
696- /* TODO: This if is not correct because the sizes of strings, objects, etc are
697- relative to the pointer, not the value contents, we should review this */
698- if (ret_size <= sizeof (ffi_arg))
705+ if (ret_size <= sizeof (ffi_arg) && ret_id < TYPE_STRING)
699706 {
700707 ffi_arg result;
701708
@@ -705,9 +712,26 @@ function_return function_c_interface_invoke(function func, function_impl impl, f
705712 }
706713 else
707714 {
708- ret = value_type_create (NULL , ret_size, ret_id);
715+ void *result = NULL ;
716+ void *result_ptr = &result;
709717
710- ffi_call (&c_function->cif , FFI_FN (c_function->address ), value_data (ret), c_function->values );
718+ if (ret_id == TYPE_NULL)
719+ {
720+ result = value_create_null ();
721+ result_ptr = NULL ;
722+ }
723+ else if (ret_id != TYPE_STRING)
724+ {
725+ result = ret = value_type_create (NULL , ret_size, ret_id);
726+ }
727+
728+ ffi_call (&c_function->cif , FFI_FN (c_function->address ), result_ptr, c_function->values );
729+
730+ if (ret_id == TYPE_STRING)
731+ {
732+ char *str = (char *)result;
733+ ret = value_create_string (str, strlen (str));
734+ }
711735 }
712736
713737 /* Clear allocated closures if any */
@@ -838,6 +862,11 @@ int c_loader_impl_initialize_types(loader_impl impl)
838862 { TYPE_FLOAT, " float" },
839863 { TYPE_DOUBLE, " double" },
840864
865+ { TYPE_STRING, " unsigned char *" },
866+ { TYPE_STRING, " char *" },
867+ { TYPE_STRING, " const unsigned char *" },
868+ { TYPE_STRING, " const char *" },
869+
841870 { TYPE_NULL, " void" }
842871
843872 /* TODO: Do more types */
0 commit comments