Default implementation for IV8FastHostObjectOperations#683
Default implementation for IV8FastHostObjectOperations#683AnsisMalins wants to merge 1 commit intoClearFoundry:masterfrom
Conversation
|
Hi @AnsisMalins, Thanks for posting this PR!
Here's why we're reluctant to merge this PR as is:
Thanks again! |
|
What's your prescription for implementing FastProxy compatibility for types who's "inheritance slot" is already taken? Just implement the interface? |
You could implement a "fast wrapper" for your class using Here's a simple superclass-subclass hierarchy in which both classes have FastProxy support: public class Foo : IV8FastHostObject {
public long Value { get; set; }
public double GetScaledValue(double scaleFactor) => Value * scaleFactor;
// FastProxy stuff
private static readonly V8FastHostObjectOperations<Foo> Operations = new();
IV8FastHostObjectOperations IV8FastHostObject.Operations => Operations;
protected static void Configure<T>(V8FastHostObjectConfiguration<T> configuration) where T : Foo {
configuration.AddPropertyAccessors(
nameof(Value),
static (T self, in V8FastResult value) => value.Set(self.Value),
static (T self, in V8FastArg value) => self.Value = value.GetInt64()
);
configuration.AddMethodGetter(
nameof(GetScaledValue),
static (T self, in V8FastArgs args, in V8FastResult result) => result.Set(self.GetScaledValue(args.GetDouble(0)))
);
}
static Foo() => Operations.Configure(static configuration => Configure(configuration));
}
public class Bar : Foo, IV8FastHostObject {
public double GetReducedValue() => Value / (2.0 * Math.PI);
// FastProxy stuff
private static readonly V8FastHostObjectOperations<Bar> Operations = new();
IV8FastHostObjectOperations IV8FastHostObject.Operations => Operations;
protected new static void Configure<T>(V8FastHostObjectConfiguration<T> configuration) where T : Bar {
Foo.Configure(configuration);
configuration.AddMethodGetter(
nameof(GetReducedValue),
static (T self, in V8FastArgs _, in V8FastResult result) => result.Set(self.GetReducedValue())
);
}
static Bar() => Operations.Configure(static configuration => Configure(configuration));
}With this setup, the subclass effectively inherits and extends its FastProxy configuration. Beyond the class-specific configuration code, FastProxy support amounts to just a few lines of boilerplate. |
This change adds a default implementation to IV8FastHostObjectOperations on runtimes that support it. The use case is objects exported to JavaScript that are part of an inheritance hierarchy. Without the default implementation, one has to laboriously implement every interface method every time.
I find it unlikely you will just merge this, but I'm hoping for some insightful feedback.
Usage: