Open herki18 opened 1 day ago
Hello @herki18,
It appears that the use of JavaScript proxies is breaking the inheritance chain for host-bound objects
Not exactly. In the expression document.appendChild(div)
, the problem is div
. As a JavaScript proxy, it's a script object and therefore an invalid argument for AppendChild
.
On the JavaScript side, ClearScript host objects are exotic in the sense that they have internal properties that are inaccessible via the script language. Those properties allow host objects to be transformed into their .NET counterparts when passed back to the host, but proxies can only expose normal JavaScript properties.
We've toyed with the idea of switching to normal properties, perhaps keyed by symbols, but we abandoned the project. Full support for proxied host objects is likely infeasible due to the radical differences between the .NET and JavaScript type systems.
Unfortunately, the error message is misleading here, and that's due to the use of case-insensitive member resolution. You're using V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding
, right?
In any case, let's take a step back. What issues are motivating the wrapping of host objects within JavaScript proxies? It would appear that event handler syntax is one such issue. Is there something else?
Hello @ClearScriptLib ,
Yes, the event handler syntax is the main issue motivating our use of JavaScript proxies. We're trying to maintain the standard event handler syntax (e.g., element.onclick = function() {...}
) without requiring modifications to third-party frameworks like React, which would otherwise require .connect
for event binding.
Aside from that, there are no other major issues driving the use of proxies at the moment. We’re using a CustomAttributeLoader
to simplify integration between JavaScript and our backend (customized AngleSharp in Unity), but the main workaround we're trying to achieve is for event handling without modifying external frameworks.
Main Problem:
I want to enable the use of the standard JavaScript event handler assignment, such as onclick = function() { ... }
Description
I'm integrating ClearScript into my project to expose C# backend objects as a DOM-like environment for JavaScript frameworks to interact with. To facilitate event handling and maintain standard DOM method access, I'm attempting to wrap these host objects using JavaScript proxies. However, this approach is causing inherited methods like
appendChild
to become inaccessible, resulting in runtime errors.Objective
onclick = ...
) instead of exclusively relying on methods likeconnect
.Approach Taken
Proxy Implementation:
document
object and created elements using JavaScript proxies to intercept event property assignments (e.g.,onclick
).Proxy Handlers:
Document Proxy Handler:
Element Proxy Handler:
Usage Example:
Problem Encountered
When wrapping elements in proxies, methods like
appendChild
, leading to runtime errors such as:This issue does not occur when interacting with
documentBackend
directly without proxies. It seems that the proxy interferes with ClearScript's method binding for inherited methods.What I've Tried
Removing Conditional Property Checks:
if (property in target)
in theget
trap, which I removed to ensure all properties are accessible.Binding Methods:
get
trap, if the property is a function, I bind it to the target object to preserve the correctthis
context.Direct Property Access:
Reflect.get
andReflect.set
without additional logic, except for event properties.Event Handling Adjustments:
set
trap to connect and disconnect event handlers appropriately based on the presence ofconnect
anddisconnect
methods on C# events.Testing Without Proxies:
appendChild
work correctly without proxies, confirming that the issue is proxy-related.Additional Information
C# Backend:
JavaScript Environment:
Request for Assistance
Best Practices:
appendChild
?Proxy Configuration:
get
andset
traps that prevent interference with ClearScript's method binding?Event Handling Alternatives:
connect
methods for event handling?onclick = ...
) instead of relying onconnect
?Known Limitations:
Alternative Approaches:
Update / Additional Information
Inheritance Issue:
It appears that the use of JavaScript proxies is breaking the inheritance chain for host-bound objects (such as those created in C# using AngleSharp). Specifically, methods like
appendChild
are inherited from parent classes (e.g.,Node
in the DOM hierarchy), but the proxy seems to prevent JavaScript from correctly resolving and accessing these inherited methods. When interacting with thedocumentBackend
directly (without proxies), the inherited methods are accessible as expected, but once a proxy is applied, those methods are no longer available, causing runtime errors.It seems that ClearScript's method binding is affected by the proxy, particularly when resolving methods along the prototype chain. Any suggestions or workarounds to ensure that inherited methods remain accessible when using proxies would be greatly appreciated.