Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ public static unsafe JSFunctionBinding BindManagedFunction(string fullyQualified
{
var (assemblyName, nameSpace, shortClassName, methodName) = ParseFQN(fullyQualifiedName);

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(assemblyName),
Marshal.StringToCoTaskMemUTF8(nameSpace),
Marshal.StringToCoTaskMemUTF8(shortClassName),
Marshal.StringToCoTaskMemUTF8(methodName),
Expand Down
21 changes: 14 additions & 7 deletions src/mono/browser/runtime/invoke-cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ function bind_fn_0V (closure: BindingClosure) {
// call C# side
invoke_sync_jsexport(method, args);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand All @@ -147,7 +148,8 @@ function bind_fn_1V (closure: BindingClosure) {
// call C# side
invoke_sync_jsexport(method, args);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down Expand Up @@ -175,7 +177,8 @@ function bind_fn_1R (closure: BindingClosure) {
const js_result = res_converter(args);
return js_result;
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down Expand Up @@ -208,7 +211,8 @@ function bind_fn_1RA (closure: BindingClosure) {

return promise;
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down Expand Up @@ -238,7 +242,8 @@ function bind_fn_2R (closure: BindingClosure) {
const js_result = res_converter(args);
return js_result;
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down Expand Up @@ -273,7 +278,8 @@ function bind_fn_2RA (closure: BindingClosure) {

return promise;
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down Expand Up @@ -325,7 +331,8 @@ function bind_fn (closure: BindingClosure) {
}
return js_result;
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

endMeasure(mark, MeasuredBlock.callCsFunction, fqn);
}
};
Expand Down
24 changes: 16 additions & 8 deletions src/mono/browser/runtime/managed-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export function call_entry_point (main_assembly_name: string, program_args: stri

return promise;
} finally {
Module.stackRestore(sp); // synchronously
// synchronously
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);
}
}

Expand All @@ -90,7 +91,8 @@ export function load_satellite_assembly (dll: Uint8Array): void {
marshal_array_to_cs(arg1, dll, MarshalerType.Byte);
invoke_sync_jsexport(managedExports.LoadSatelliteAssembly, args);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand All @@ -109,7 +111,8 @@ export function load_lazy_assembly (dll: Uint8Array, pdb: Uint8Array | null): vo
marshal_array_to_cs(arg2, pdb, MarshalerType.Byte);
invoke_sync_jsexport(managedExports.LoadLazyAssembly, args);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand All @@ -132,7 +135,8 @@ export function release_js_owned_object_by_gc_handle (gc_handle: GCHandle) {
invoke_async_jsexport(runtimeHelpers.ioThreadTID, managedExports.ReleaseJSOwnedObjectByGCHandle, args, size);
}
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand All @@ -157,7 +161,8 @@ export function complete_task (holder_gc_handle: GCHandle, error?: any, data?: a
}
invoke_async_jsexport(runtimeHelpers.ioThreadTID, managedExports.CompleteTask, args, size);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand Down Expand Up @@ -203,7 +208,8 @@ export function call_delegate (callback_gc_handle: GCHandle, arg1_js: any, arg2_
return res_converter(res);
}
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand All @@ -223,7 +229,8 @@ export function get_managed_stack_trace (exception_gc_handle: GCHandle) {
const res = get_arg(args, 1);
return marshal_string_to_js(res);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand Down Expand Up @@ -341,7 +348,8 @@ export function bind_assembly_exports (assemblyName: string): Promise<void> {
}
return promise;
} finally {
Module.stackRestore(sp); // synchronously
// synchronously
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/mono/browser/runtime/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { MemOffset, NumberOrPointer } from "./types/internal";
import { VoidPtr, CharPtr } from "./types/emscripten";
import cwraps, { I52Error } from "./cwraps";
import { Module, mono_assert, runtimeHelpers } from "./globals";
import { loaderHelpers, Module, mono_assert, runtimeHelpers } from "./globals";
import { utf8ToString } from "./strings";
import { mono_log_warn, mono_log_error } from "./logging";

Expand Down Expand Up @@ -327,7 +327,8 @@ export function withStackAlloc<T1, T2, T3, TResult> (bytesWanted: number, f: (pt
try {
return f(ptr, ud1, ud2, ud3);
} finally {
Module.stackRestore(sp);
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System;
using System.Text.Json;
using System.Runtime.InteropServices.JavaScript;
using System.Diagnostics.CodeAnalysis;

public partial class LazyLoadingTest
{
[JSExport]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, "LazyLibrary.Foo", "LazyLibrary")]
public static void Run()
{
// System.Text.Json is marked as lazy loaded in the csproj ("BlazorWebAssemblyLazyLoad"), this method can be called only after the assembly is lazy loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
<ItemGroup>
<ProjectReference Include="..\Json\Json.csproj" />
<ProjectReference Include="..\ResourceLibrary\ResourceLibrary.csproj" />
<ProjectReference Include="..\LazyLibrary\LazyLibrary.csproj" />
<WasmEnvironmentVariable Include="baz" Value="boo" />
</ItemGroup>

<ItemGroup Condition="'$(TestLazyLoading)' == 'true'">
<BlazorWebAssemblyLazyLoad Include="Json$(WasmAssemblyExtension)" />
<BlazorWebAssemblyLazyLoad Include="LazyLibrary$(WasmAssemblyExtension)" />
<WasmExtraFilesToDeploy Include="profiler.js" />
</ItemGroup>
</Project>
11 changes: 9 additions & 2 deletions src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,16 @@ try {
}

await INTERNAL.loadLazyAssembly(`Json${lazyAssemblyExtension}`);
exports.LazyLoadingTest.Run();
await INTERNAL.loadLazyAssembly(`LazyLibrary${lazyAssemblyExtension}`);
const { LazyLibrary } = await getAssemblyExports("LazyLibrary");
const resLazy = LazyLibrary.Foo.Bar();
exit(resLazy == 42 ? 0 : 1);
}
else {
exports.LazyLoadingTest.Run();
exit(0);
}
exports.LazyLoadingTest.Run();
exit(0);
break;
case "LibraryInitializerTest":
exit(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices.JavaScript;

[assembly:System.Runtime.Versioning.SupportedOSPlatform("browser")]

namespace LazyLibrary;

public partial class Foo
{
[JSExport]
public static int Bar()
{
Console.WriteLine("Hello from Foo.Bar!");
return 42;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<OutputType>Library</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private void WriteBootConfig(string entryAssemblyName)
endLineNumber: 0,
endColumnNumber: 0,
message: message,
string.Join(";", LazyLoadedAssemblies.Select(a => a.ItemSpec)));
string.Join(";", remainingLazyLoadAssemblies.Select(a => a.ItemSpec)));

return;
}
Expand Down
Loading