Skip to content

Blazor JSImport JSObject identity #64327

@lskyum

Description

@lskyum

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

This is a bit of a strange and niche issue.

When using JSImport/JSExport for WASM Blazor, the identity of returned JSObject's are synchronized between the .NET (WASM) engine and the JavaScript engine. Meaning the same .NET JSObject is returned for the same JavaScript object. (Unless it has already been disposed or garbage collected).

This is a really nice feature, because it allows you to synchronize various things between the two worlds.

But this doesn't seem to work when the JavaScript object is a constructor, for example:

export function getDivElement() {
    return HTMLDivElement;
}

This means that code like:

element.constructor == HTMLDivElement

Doesn't work inside the .NET engine, because the JSObject from HTMLDivElement doesn't maintain it's instance correctly.

It's a bit hard to explain, so please see the attached BlazorJSImportBugReport.zip
BlazorJSImportBugReport.zip

Clicking "Test Regular Objects" shows that the JSObject instances works correctly with regular objects:
Image

Clicking "Test HTMLDivElement Constructor" shows that it doesn't work for constructor objects:
Image

Expected Behavior

I would expact any JavaScript object returned by a JSImport function to maintain it's identity, so the same JSObject is returned for the same JavaScript object. (Unless the JSObject has been disposed or garbage collected)

Steps To Reproduce

Use the attached demo project (See Home.razor, CallJavaScript1.cs and test.js)

Exceptions (if any)

No response

.NET Version

10.0.100 (But the problem is also in .NET 9 and probably also 8)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions