static void
emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object);
+static void
+emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object, MonoMarshalNative string_encoding);
+
static void
mono_struct_delete_old (MonoClass *klass, char *ptr);
static MonoStringBuilder *
mono_string_utf16_to_builder2 (gunichar2 *text);
+static MonoString*
+mono_string_new_len_wrapper (const char *text, guint length);
+
static void
mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *eltype, guint32 elnum);
register_icall (mono_string_to_utf16, "mono_string_to_utf16", "ptr obj", FALSE);
register_icall (mono_string_from_utf16, "mono_string_from_utf16", "obj ptr", FALSE);
register_icall (mono_string_new_wrapper, "mono_string_new_wrapper", "obj ptr", FALSE);
+ register_icall (mono_string_new_len_wrapper, "mono_string_new_len_wrapper", "obj ptr int", FALSE);
register_icall (mono_string_to_utf8, "mono_string_to_utf8", "ptr obj", FALSE);
register_icall (mono_string_to_lpstr, "mono_string_to_lpstr", "ptr obj", FALSE);
register_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", "ptr object", FALSE);
*((gunichar2 *) dst + len) = 0;
}
+static MonoString*
+mono_string_new_len_wrapper (const char *text, guint length)
+{
+ return mono_string_new_len (mono_domain_get (), text, length);
+}
+
static int
mono_mb_emit_proxy_check (MonoMethodBuilder *mb, int branch_code)
{
return mono_mb_emit_branch (mb, branch_code);
}
+/*
+ * mono_mb_emit_exception_marshal_directive:
+ *
+ * This function assumes ownership of MSG, which should be malloc-ed.
+ */
static void
-mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, const char *msg)
+mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, char *msg)
{
- mono_mb_emit_exception_full (mb, "System.Runtime.InteropServices", "MarshalDirectiveException", msg);
+ char *s;
+
+ if (!mb->dynamic) {
+ s = mono_image_strdup (mb->method->klass->image, msg);
+ g_free (msg);
+ } else {
+ s = g_strdup (msg);
+ }
+ mono_mb_emit_exception_full (mb, "System.Runtime.InteropServices", "MarshalDirectiveException", s);
}
guint
char *msg = g_strdup_printf ("marshaling conversion %d not implemented", conv);
mono_mb_emit_exception_marshal_directive (mb, msg);
- g_free (msg);
break;
}
}
return mono_marshal_string_to_utf16;
case MONO_MARSHAL_CONV_LPWSTR_STR:
return mono_string_from_utf16;
- case MONO_MARSHAL_CONV_LPSTR_STR:
+ case MONO_MARSHAL_CONV_LPTSTR_STR:
#ifdef TARGET_WIN32
return mono_string_from_utf16;
#else
return mono_string_new_wrapper;
#endif
+ case MONO_MARSHAL_CONV_LPSTR_STR:
+ return mono_string_new_wrapper;
case MONO_MARSHAL_CONV_STR_LPTSTR:
#ifdef TARGET_WIN32
return mono_marshal_string_to_utf16;
}
static void
-emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
+emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object,
+ MonoMarshalNative string_encoding)
{
MonoMarshalType *info;
int i;
case MONO_TYPE_R8:
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, mono_type_to_ldind (ftype));
- mono_mb_emit_byte (mb, mono_type_to_stind (ftype));
+ if (t == MONO_TYPE_CHAR && ntype == MONO_NATIVE_U1 && string_encoding != MONO_NATIVE_LPWSTR) {
+ if (to_object) {
+ mono_mb_emit_byte (mb, CEE_LDIND_U1);
+ mono_mb_emit_byte (mb, CEE_STIND_I2);
+ } else {
+ mono_mb_emit_byte (mb, CEE_LDIND_U2);
+ mono_mb_emit_byte (mb, CEE_STIND_I1);
+ }
+ } else {
+ mono_mb_emit_byte (mb, mono_type_to_ldind (ftype));
+ mono_mb_emit_byte (mb, mono_type_to_stind (ftype));
+ }
break;
case MONO_TYPE_VALUETYPE: {
int src_var, dst_var;
}
}
+static void
+emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
+{
+ emit_struct_conv_full (mb, klass, to_object, -1);
+}
+
static void
emit_struct_free (MonoMethodBuilder *mb, MonoClass *klass, int struct_var)
{
case MONO_NATIVE_LPWSTR:
return MONO_MARSHAL_CONV_STR_LPWSTR;
case MONO_NATIVE_LPSTR:
+ case MONO_NATIVE_VBBYREFSTR:
return MONO_MARSHAL_CONV_STR_LPSTR;
case MONO_NATIVE_LPTSTR:
return MONO_MARSHAL_CONV_STR_LPTSTR;
*need_free = FALSE;
return MONO_MARSHAL_CONV_LPWSTR_STR;
case MONO_NATIVE_LPSTR:
+ case MONO_NATIVE_VBBYREFSTR:
return MONO_MARSHAL_CONV_LPSTR_STR;
case MONO_NATIVE_LPTSTR:
return MONO_MARSHAL_CONV_LPTSTR_STR;
res = newm;
new_key = g_new0 (SignatureMethodPair, 1);
*new_key = key;
+ if (static_method_with_first_arg_bound)
+ new_key->sig = signature_dup (del->method->klass->image, key.sig);
g_hash_table_insert (cache, new_key, res);
mono_marshal_set_wrapper_info (res, new_key);
mono_marshal_unlock ();
res->hasthis = FALSE;
for (i = sig->param_count - 1; i >= 0; i --)
res->params [i + 1] = sig->params [i];
- res->params [0] = &mono_ptr_class_get (&klass->byval_arg)->byval_arg;
+ res->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
return res;
}
csig->params [1] = &mono_defaults.int_class->byval_arg;
csig->params [2] = &mono_defaults.int_class->byval_arg;
csig->params [3] = &mono_defaults.int_class->byval_arg;
+ csig->pinvoke = 1;
+#if TARGET_WIN32
+ /* This is called from runtime code so it has to be cdecl */
+ csig->call_convention = MONO_CALL_C;
+#endif
name = mono_signature_to_name (callsig, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
mb = mono_mb_new (target_klass, name, MONO_WRAPPER_RUNTIME_INVOKE);
static MonoClass *ICustomMarshaler = NULL;
static MonoMethod *cleanup_native, *cleanup_managed;
static MonoMethod *marshal_managed_to_native, *marshal_native_to_managed;
- MonoMethod *get_instance;
+ MonoMethod *get_instance = NULL;
MonoMethodBuilder *mb = m->mb;
char *exception_msg = NULL;
guint32 loc1;
if (!ICustomMarshaler) {
ICustomMarshaler = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ICustomMarshaler");
- g_assert (ICustomMarshaler);
+ if (!ICustomMarshaler) {
+ exception_msg = g_strdup ("Current profile doesn't support ICustomMarshaler");
+ goto handle_exception;
+ }
cleanup_native = mono_class_get_method_from_name (ICustomMarshaler, "CleanUpNativeData", 1);
g_assert (cleanup_native);
if (!get_instance)
exception_msg = g_strdup_printf ("Custom marshaler '%s' does not implement a static GetInstance method that takes a single string parameter and returns an ICustomMarshaler.", mklass->name);
+handle_exception:
/* Throw exception and emit compensation code if neccesary */
if (exception_msg) {
switch (action) {
break;
}
- if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
+ if (encoding == MONO_NATIVE_VBBYREFSTR) {
+ static MonoMethod *m;
+
+ if (!m) {
+ m = mono_class_get_method_from_name_flags (mono_defaults.string_class, "get_Length", -1, 0);
+ g_assert (m);
+ }
+
+ /*
+ * Have to allocate a new string with the same length as the original, and
+ * copy the contents of the buffer pointed to by CONV_ARG into it.
+ */
+ g_assert (t->byref);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, conv_arg);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_LDIND_I);
+ mono_mb_emit_managed_call (mb, m, NULL);
+ mono_mb_emit_icall (mb, mono_string_new_len_wrapper);
+ mono_mb_emit_byte (mb, CEE_STIND_REF);
+ } else if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_icall (mb, conv_to_icall (conv));
break;
case MARSHAL_ACTION_PUSH:
- if (t->byref)
+ if (t->byref && encoding != MONO_NATIVE_VBBYREFSTR)
mono_mb_emit_ldloc_addr (mb, conv_arg);
else
mono_mb_emit_ldloc (mb, conv_arg);
if (t->data.klass->flags & TYPE_ATTRIBUTE_ABSTRACT){
mono_mb_emit_byte (mb, CEE_POP);
- mono_mb_emit_exception_marshal_directive (mb, "Returned SafeHandles should not be abstract");
+ mono_mb_emit_exception_marshal_directive (mb, g_strdup ("Returned SafeHandles should not be abstract"));
break;
}
*conv_arg_type = intptr_type;
if (t->byref){
- mono_mb_emit_exception_marshal_directive (mb,
- "HandleRefs can not be returned from unmanaged code (or passed by ref)");
+ char *msg = g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
+ mono_mb_emit_exception_marshal_directive (mb, msg);
break;
}
mono_mb_emit_ldarg_addr (mb, argnum);
}
case MARSHAL_ACTION_CONV_RESULT: {
- mono_mb_emit_exception_marshal_directive (mb,
- "HandleRefs can not be returned from unmanaged code (or passed by ref)");
+ char *msg = g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
+ mono_mb_emit_exception_marshal_directive (mb, msg);
break;
}
}
mono_mb_emit_ldloc (mb, conv_arg);
- mono_mb_emit_icall (mb, g_free);
+ mono_mb_emit_icall (mb, mono_marshal_free);
mono_mb_patch_branch (mb, pos2);
}
/* Free the pointer allocated by unmanaged code */
mono_mb_emit_ldloc (mb, loc);
- mono_mb_emit_icall (mb, g_free);
+ mono_mb_emit_icall (mb, mono_marshal_free);
mono_mb_patch_branch (mb, pos);
}
break;
return conv_arg;
}
+static gboolean
+mono_pinvoke_is_unicode (MonoMethodPInvoke *piinfo)
+{
+ switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
+ case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
+ return FALSE;
+ case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
+ return TRUE;
+ case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
+ default:
+#ifdef TARGET_WIN32
+ return TRUE;
+#else
+ return FALSE;
+#endif
+ }
+}
+
static int
emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
if (is_string)
esize = sizeof (gpointer);
+ else if (eklass == mono_defaults.char_class) /*can't call mono_marshal_type_size since it causes all sorts of asserts*/
+ esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
else
esize = mono_class_native_size (eklass, NULL);
mono_mb_emit_stloc (mb, 1);
/* emit valuetype conversion code */
- emit_struct_conv (mb, eklass, FALSE);
+ emit_struct_conv_full (mb, eklass, FALSE, eklass == mono_defaults.char_class ? encoding : -1);
}
mono_mb_emit_add_to_local (mb, index_var, 1);
eklass = klass->element_class;
if ((eklass == mono_defaults.stringbuilder_class) || (eklass == mono_defaults.string_class))
esize = sizeof (gpointer);
+ else if (eklass == mono_defaults.char_class)
+ esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
else
esize = mono_class_native_size (eklass, NULL);
src_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
mono_mb_emit_stloc (mb, 1);
/* emit valuetype conversion code */
- emit_struct_conv (mb, eklass, TRUE);
+ emit_struct_conv_full (mb, eklass, TRUE, eklass == mono_defaults.char_class ? encoding : -1);
}
if (need_free) {
if (is_string)
esize = sizeof (gpointer);
+ else if (eklass == mono_defaults.char_class)
+ esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
else
esize = mono_class_native_size (eklass, NULL);
return cached_methods [kind];
name = g_strdup_printf ("virt_stelemref_%s", strelemref_wrapper_name [kind]);
- mb = mono_mb_new_no_dup_name (mono_defaults.object_class, name, MONO_WRAPPER_STELEMREF);
+ mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_STELEMREF);
g_free (name);
if (!signature) {
res = mono_mb_create_method (mb, signature, 4);
res->flags |= METHOD_ATTRIBUTE_VIRTUAL;
mono_marshal_set_wrapper_info (res, GUINT_TO_POINTER (kind + 1));
- cached_methods [kind] = res;
+
+ mono_marshal_lock ();
+ if (!cached_methods [kind]) {
+ cached_methods [kind] = res;
+ mono_marshal_unlock ();
+ } else {
+ mono_marshal_unlock ();
+ mono_free_method (res);
+ }
mono_mb_free (mb);
- return res;
+ return cached_methods [kind];
}
MonoMethod*