Skip to content

Commit

Permalink
Remove interpolated string from jsinterop startup
Browse files Browse the repository at this point in the history
  • Loading branch information
kg committed Apr 17, 2024
1 parent a1542b6 commit 6b97da5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ internal static unsafe partial class Runtime
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void BindAssemblyExports(IntPtr assemblyNamePtr);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void GetAssemblyExport(IntPtr assemblyNamePtr, IntPtr namespacePtr, IntPtr classnamePtr, IntPtr methodNamePtr, IntPtr* monoMethodPtrPtr);
public static extern void GetAssemblyExport(IntPtr assemblyNamePtr, IntPtr namespacePtr, IntPtr classnamePtr, IntPtr methodNamePtr, int signatureHash, IntPtr* monoMethodPtrPtr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,21 @@ public static unsafe Task BindAssemblyExports(string? assemblyName)
public static unsafe JSFunctionBinding BindManagedFunction(string fullyQualifiedName, int signatureHash, ReadOnlySpan<JSMarshalerType> signatures)
{
var (assemblyName, nameSpace, shortClassName, methodName) = ParseFQN(fullyQualifiedName);
var wrapper_name = $"__Wrapper_{methodName}_{signatureHash}";

var dllName = assemblyName + ".dll";

IntPtr monoMethod;
Interop.Runtime.GetAssemblyExport(
// FIXME: Pass UTF-16 through directly so C can work with it, doing the conversion
// in C# pulls in a bunch of dependencies we don't need this early in startup.
// I tested removing the UTF8 conversion from this specific call, but other parts
// of startup I can't identify still pull in UTF16->UTF8 conversion, so it's not
// worth it to do that yet.
Marshal.StringToCoTaskMemUTF8(dllName),
Marshal.StringToCoTaskMemUTF8(nameSpace),
Marshal.StringToCoTaskMemUTF8(shortClassName),
Marshal.StringToCoTaskMemUTF8(wrapper_name),
Marshal.StringToCoTaskMemUTF8(methodName),
signatureHash,
&monoMethod);

if (monoMethod == IntPtr.Zero)
Expand Down Expand Up @@ -330,7 +336,7 @@ public static RuntimeMethodHandle GetMethodHandleFromIntPtr(IntPtr ptr)
// The BCL implementations of IndexOf/LastIndexOf/Trim are vectorized & fast,
// but they pull in a bunch of code that is otherwise not necessarily
// useful during early app startup, so we use simple scalar implementations
static int SmallIndexOf (string s, char ch, int direction = 1) {
private static int SmallIndexOf (string s, char ch, int direction = 1) {
if (s.Length < 1)
return -1;
int start_index = (direction > 0) ? 0 : s.Length - 1,
Expand All @@ -342,10 +348,9 @@ static int SmallIndexOf (string s, char ch, int direction = 1) {
return -1;
}

static string SmallTrim (string s) {
private static string SmallTrim (string s) {
if (s.Length < 1)
return s;
bool done = false;
int head = 0, tail = s.Length - 1;
while (head < s.Length) {
if (s[head] == ' ')
Expand Down
11 changes: 8 additions & 3 deletions src/mono/browser/runtime/corebindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef void (*background_job_cb)(void);

void mono_wasm_bind_assembly_exports (char *assembly_name);
void mono_wasm_assembly_get_entry_point (char *assembly_name, int auto_insert_breakpoint, MonoMethod **method_out);
void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *classname, char *methodname, MonoMethod **method_out);
void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *classname, char *methodname, int signature_hash, MonoMethod **method_out);

#ifndef DISABLE_THREADS
void mono_wasm_release_cs_owned_object_post (pthread_t target_tid, int js_handle);
Expand Down Expand Up @@ -230,13 +230,14 @@ void mono_wasm_bind_assembly_exports (char *assembly_name)
}
}

void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *classname, char *methodname, MonoMethod **method_out)
void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *classname, char *methodname, int signature_hash, MonoMethod **method_out)
{
MonoError error;
MonoAssembly* assembly;
MonoImage *image;
MonoClass *klass;
MonoMethod *method=NULL;
char real_method_name_buffer[4096];
*method_out = NULL;

assert (assembly_name);
Expand All @@ -247,10 +248,14 @@ void mono_wasm_get_assembly_export (char *assembly_name, char *namespace, char *

klass = mono_class_from_name (image, namespace, classname);
assert (klass);
method = mono_class_get_method_from_name (klass, methodname, -1);

snprintf(real_method_name_buffer, 4096, "__Wrapper_%s_%d", methodname, signature_hash);

method = mono_class_get_method_from_name (klass, real_method_name_buffer, -1);
assert (method);

*method_out = method;
// FIXME: free (assembly_name)?
free (namespace);
free (classname);
free (methodname);
Expand Down

0 comments on commit 6b97da5

Please sign in to comment.