Skip to content

Commit

Permalink
[NativeAOT-LLVM] Fix generating wasm export name for JSExport registr…
Browse files Browse the repository at this point in the history
…ation method (#2581)
  • Loading branch information
maraf authored May 9, 2024
1 parent 87b8674 commit 198b7eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
13 changes: 9 additions & 4 deletions src/mono/browser/runtime/managed-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ function bind_assembly_exports_naot (assembly: string) {
if (assemblyWithoutExtension.endsWith(".dll")) {
assemblyWithoutExtension = assemblyWithoutExtension.substring(0, assembly.length - 4);
}
const register = (Module as any)["_" + assemblyWithoutExtension + "__GeneratedInitializer" + "__Register_"];
mono_assert(register, `Missing wasm export for JSExport registration function in assembly ${assembly}`);
const exportName = fixup_wasm_export_name("_" + assemblyWithoutExtension + "__GeneratedInitializer" + "__Register_");
const register = (Module as any)[exportName];
mono_assert(register, `Missing wasm export '${exportName}' (for JSExport registration function in assembly '${assembly}')`);
register();
return Promise.resolve();
}
Expand All @@ -388,10 +389,10 @@ export const bind_assembly_exports: (assemblyName: string) => Promise<void> = Na
function get_method (method_name: string): MonoMethod {
if (NativeAOT) {
const fqn = runtimeHelpers.runtime_interop_namespace + "." + runtimeHelpers.runtime_interop_exports_classname + "." + method_name;
const exportName = `_${fqn.replace(/\./g, "_")}`;
const exportName = fixup_wasm_export_name("_" + fqn);
const exportFunc = (Module as any)[exportName];
return exportFunc ?? (() => {
throw new Error(`Wasm export ${fqn} not found`);
throw new Error(`Missing wasm export '${exportName}' (for ${fqn})`);
});
}

Expand All @@ -402,6 +403,10 @@ function get_method (method_name: string): MonoMethod {
return res;
}

function fixup_wasm_export_name (export_name: string): string {
return export_name.replace(/\./g, "_");
}

type ManagedExports = {
InstallMainSynchronizationContext: MonoMethod | undefined,
entry_point: MonoMethod,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/nativeaot/SmokeTests/DotnetJs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace DotnetJsApp;
namespace DotnetJs.App;

partial class Program
{
Expand Down
20 changes: 10 additions & 10 deletions src/tests/nativeaot/SmokeTests/DotnetJs/wwwroot/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dotnet, exit } from './dotnet.js'

const { runMain, setModuleImports, getAssemblyExports } = await dotnet
.withApplicationArguments("A", "B", "C")
.withMainAssembly("DotnetJs")
.withMainAssembly("DotnetJs.App")
.create();

setModuleImports('main.js', {
Expand All @@ -16,28 +16,28 @@ setModuleImports('main.js', {

let result = await runMain();

const exports = await getAssemblyExports("DotnetJs.dll");
const exports = await getAssemblyExports("DotnetJs.App.dll");

const square = exports.DotnetJsApp.Program.Interop.Square(5);
const square = exports.DotnetJs.App.Program.Interop.Square(5);
if (square != 25) {
result = 13;
}

try {
exports.DotnetJsApp.Program.Interop.Throw();
exports.DotnetJs.App.Program.Interop.Throw();
result = 14;
} catch (e) {
console.log(`Thrown expected exception: ${e}`);
}

const concat = exports.DotnetJsApp.Program.Interop.Concat("Aaa", "Bbb");
const concat = exports.DotnetJs.App.Program.Interop.Concat("Aaa", "Bbb");
if (concat != "AaaBbb") {
result = 15;
}

let isPromiseResolved = false;
let promise = new Promise(resolve => setTimeout(() => { console.log("Promise resolved"); isPromiseResolved = true; resolve(); }, 100));
let asyncResult = await exports.DotnetJsApp.Program.Interop.Async(promise);
let asyncResult = await exports.DotnetJs.App.Program.Interop.Async(promise);
console.log(`Async result: ${asyncResult}`);
if (!isPromiseResolved) {
result = 16;
Expand All @@ -49,7 +49,7 @@ if (asyncResult != 87) {
try {
isPromiseResolved = false;
promise = new Promise(resolve => setTimeout(() => { console.log("Promise resolved"); isPromiseResolved = true; resolve(); }, 100));
asyncResult = await exports.DotnetJsApp.Program.Interop.Async(promise, true);
asyncResult = await exports.DotnetJs.App.Program.Interop.Async(promise, true);
if (asyncResult != 87) {
console.log(`Unexpected async result: ${asyncResult}`);
result = 18;
Expand All @@ -62,14 +62,14 @@ try {
console.log(`Thrown expected exception: ${e}`);
}

const cancelResult = await exports.DotnetJsApp.Program.Interop.AsyncWithCancel();
const cancelResult = await exports.DotnetJs.App.Program.Interop.AsyncWithCancel();
if (cancelResult !== 0) {
console.log(`Unexpected result from AsyncWithCancel: ${cancelResult}`);
result = 21;
}

var jsObject = { x: 42 };
var jsObjectResult = exports.DotnetJsApp.Program.Interop.JSObject(jsObject);
var jsObjectResult = exports.DotnetJs.App.Program.Interop.JSObject(jsObject);
if (!jsObjectResult) {
console.log(`Unexpected result from JSObject: ${jsObjectResult}`);
result = 22;
Expand All @@ -81,7 +81,7 @@ if (jsObject.y != jsObject.x + 1) {
}

const msgs = [];
const csharpFunc = exports.DotnetJsApp.Program.Interop.DelegateMarshalling(() => "String from JavaScript", msg => { msgs.push(msg); console.log(`Message from C# '${msg}'`); });
const csharpFunc = exports.DotnetJs.App.Program.Interop.DelegateMarshalling(() => "String from JavaScript", msg => { msgs.push(msg); console.log(`Message from C# '${msg}'`); });
if (msgs.length !== 1 || msgs[0] !== "Wrapping value in C# 'String from JavaScript'") {
console.log(`Unexpected number of messages from Func: ${JSON.stringify(msgs)}`);
result = 24;
Expand Down

0 comments on commit 198b7eb

Please sign in to comment.