Skip to content

Commit

Permalink
Added Function factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Aug 30, 2024
1 parent 9b3a7f6 commit 20cc5e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
54 changes: 30 additions & 24 deletions YantraJS.Core/Core/Function/JSFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSFunctionDelegate>(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<JSFunctionDelegate>(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<JSFunctionDelegate>(name, body, args);
return new JSFunction(lambda.Compile(), name);
var body = assign;
var lambda = Expression.Lambda<JSFunctionDelegate>(name, body, args);
return lambda.Compile();
}, name);
}
}
}
10 changes: 10 additions & 0 deletions YantraJS.Core/Core/Function/JSFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ public JSFunction(JSFunctionDelegate f)

}

public JSFunction(Func<JSFunctionDelegate> 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,
Expand Down

0 comments on commit 20cc5e5

Please sign in to comment.