From 20cc5e50a1ae42a6699d7414ef425124a853d31c Mon Sep 17 00:00:00 2001 From: Akash Kava <39438041+ackava@users.noreply.github.com> Date: Fri, 30 Aug 2024 23:21:25 +0530 Subject: [PATCH] Added Function factory --- YantraJS.Core/Core/Function/JSFieldInfo.cs | 54 ++++++++++++---------- YantraJS.Core/Core/Function/JSFunction.cs | 10 ++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/YantraJS.Core/Core/Function/JSFieldInfo.cs b/YantraJS.Core/Core/Function/JSFieldInfo.cs index 8d7b326e..04411db7 100644 --- a/YantraJS.Core/Core/Function/JSFieldInfo.cs +++ b/YantraJS.Core/Core/Function/JSFieldInfo.cs @@ -23,41 +23,47 @@ public JSFieldInfo(ClrMemberNamingConvention namingConvention, FieldInfo field) public JSFunction GenerateFieldGetter() { - var args = Expression.Parameter(typeof(Arguments).MakeByRefType()); - Expression convertedThis = field.IsStatic - ? null - : JSValueToClrConverter.Get(ArgumentsBuilder.This(args), field.DeclaringType, "this"); - var body = - ClrProxyBuilder.Marshal( - Expression.Field( - convertedThis, field)); var name = $"get {Name}"; - var lambda = Expression.Lambda(name, body, args); - return new JSFunction(lambda.Compile(), name); + var field = this.field; + return new JSFunction(() => { + var args = Expression.Parameter(typeof(Arguments).MakeByRefType()); + Expression convertedThis = field.IsStatic + ? null + : JSValueToClrConverter.Get(ArgumentsBuilder.This(args), field.DeclaringType, "this"); + var body = + ClrProxyBuilder.Marshal( + Expression.Field( + convertedThis, field)); + var lambda = Expression.Lambda(name, body, args); + return lambda.Compile(); + }, name); } public JSFunction GenerateFieldSetter() { - var args = Expression.Parameter(typeof(Arguments).MakeByRefType()); - var a1 = ArgumentsBuilder.Get1(args); - var convert = field.IsStatic - ? null - : JSValueToClrConverter.Get(ArgumentsBuilder.This(args), field.DeclaringType, "this"); + var name = $"set {Name}"; + var field = this.field; + return new JSFunction(() => { + var args = Expression.Parameter(typeof(Arguments).MakeByRefType()); + var a1 = ArgumentsBuilder.Get1(args); + var convert = field.IsStatic + ? null + : JSValueToClrConverter.Get(ArgumentsBuilder.This(args), field.DeclaringType, "this"); - var clrArg1 = JSValueToClrConverter.Get(a1, field.FieldType, "value"); + var clrArg1 = JSValueToClrConverter.Get(a1, field.FieldType, "value"); - var fieldExp = Expression.Field(convert, field); + var fieldExp = Expression.Field(convert, field); - // todo - // not working for `char` - var assign = Expression.Assign(fieldExp, clrArg1).ToJSValue(); + // todo + // not working for `char` + var assign = Expression.Assign(fieldExp, clrArg1).ToJSValue(); - var body = assign; - var name = $"set {Name}"; - var lambda = Expression.Lambda(name, body, args); - return new JSFunction(lambda.Compile(), name); + var body = assign; + var lambda = Expression.Lambda(name, body, args); + return lambda.Compile(); + }, name); } } } diff --git a/YantraJS.Core/Core/Function/JSFunction.cs b/YantraJS.Core/Core/Function/JSFunction.cs index feec488a..80b2ec24 100644 --- a/YantraJS.Core/Core/Function/JSFunction.cs +++ b/YantraJS.Core/Core/Function/JSFunction.cs @@ -121,6 +121,16 @@ public JSFunction(JSFunctionDelegate f) } + public JSFunction(Func fx, in StringSpan name) + : this(empty, in name, StringSpan.Empty) + { + this.f = (in Arguments a) => + { + this.f = fx(); + return this.f(in a); + }; + } + public JSFunction( JSFunctionDelegate f, in StringSpan name,