Skip to content

Commit

Permalink
Added JSProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Aug 30, 2024
1 parent 53d7f3f commit 9b3a7f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var p = new Proxy({}, {
get(t, p, receiver) {
return p + "." + p;
}
});

assert.strictEqual(p.a, "a.a");
2 changes: 1 addition & 1 deletion YantraJS.Core.Tests/YantraJS.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down
35 changes: 21 additions & 14 deletions YantraJS.Core/Core/Proxy/JSProxy.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using Yantra.Core;
using YantraJS.Core.Clr;
using YantraJS.Extensions;

namespace YantraJS.Core
{
public class JSProxy : JSObject
[JSBaseClass("Object")]
[JSFunctionGenerator("Proxy")]
public partial class JSProxy : JSObject
{
readonly JSObject target;
private readonly JSObject handler;

protected JSProxy(JSObject target, JSObject handler) : base((JSObject)null)
protected JSProxy((JSObject target, JSObject handler) p) : base(JSContext.Current.ObjectPrototype)
{
var (target, handler) = p;
if (target == null || handler == null)
{
throw JSContext.Current.NewTypeError("Cannot create proxy with a non-object as target or handler");
Expand All @@ -33,7 +38,8 @@ public override JSValue InvokeFunction(in Arguments a)
var fx = handler[KeyStrings.apply];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(a.OverrideThis(target));
var args = new JSArray(a.ToArray());
return fxFunction.Call(this, this.target, a.This, args);
}
return target.InvokeFunction(a);
}
Expand All @@ -43,7 +49,8 @@ public override JSValue CreateInstance(in Arguments a)
var fx = handler[KeyStrings.constructor];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(a.OverrideThis(target));
var args = new JSArray(a.ToArray());
return fxFunction.Call(this, this.target, args);
}
return target.CreateInstance(a);
}
Expand All @@ -53,7 +60,7 @@ public override JSValue DefineProperty(JSValue key, JSObject propertyDescription
var fx = handler[KeyStrings.defineProperty];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(new Arguments(target, key, propertyDescription));
return fxFunction.InvokeFunction(new Arguments(target, target, key, propertyDescription));
}
return target.DefineProperty(key, propertyDescription);
}
Expand All @@ -63,7 +70,7 @@ public override JSValue Delete(JSValue index)
var fx = handler[KeyStrings.deleteProperty];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(new Arguments(target, index));
return fxFunction.InvokeFunction(new Arguments(target, target, index));
}
return target.Delete(index);
}
Expand All @@ -73,7 +80,7 @@ internal protected override JSValue GetValue(JSSymbol key, JSValue receiver, boo
var fx = handler[KeyStrings.get];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(new Arguments(target, key, receiver));
return fxFunction.InvokeFunction(new Arguments(target, target, key, receiver));
}
return target.GetValue(key, receiver, throwError);
}
Expand All @@ -83,7 +90,7 @@ internal protected override JSValue GetValue(KeyString key, JSValue receiver, bo
var fx = handler[KeyStrings.get];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(new Arguments(target, key.ToJSValue(), receiver));
return fxFunction.InvokeFunction(new Arguments(target,target, key.ToJSValue(), receiver));
}
return target.GetValue(key, receiver, throwError);
}
Expand All @@ -93,7 +100,7 @@ internal protected override JSValue GetValue(uint key, JSValue receiver, bool th
var fx = handler[KeyStrings.get];
if (fx is JSFunction fxFunction)
{
return fxFunction.InvokeFunction(new Arguments(target, new JSNumber(key), receiver));
return fxFunction.InvokeFunction(new Arguments(target, target, new JSNumber(key), receiver));
}
return target.GetValue(key, receiver, throwError);
}
Expand All @@ -103,7 +110,7 @@ internal protected override bool SetValue(JSSymbol name, JSValue value, JSValue
var fx = handler[KeyStrings.set];
if (fx is JSFunction fxFunction)
{
fxFunction.InvokeFunction(new Arguments(target, name, receiver));
fxFunction.InvokeFunction(new Arguments(target, target, name, receiver));
return true;
}
return target.SetValue(name, value, receiver, false);
Expand All @@ -114,7 +121,7 @@ internal protected override bool SetValue(KeyString name, JSValue value, JSValue
var fx = handler[KeyStrings.set];
if (fx is JSFunction fxFunction)
{
fxFunction.InvokeFunction(new Arguments(target, name.ToJSValue(), receiver));
fxFunction.InvokeFunction(new Arguments(target, target, name.ToJSValue(), receiver));
return true;
}
return target.SetValue(name, value, receiver, false);
Expand All @@ -125,7 +132,7 @@ internal protected override bool SetValue(uint name, JSValue value, JSValue rece
var fx = handler[KeyStrings.set];
if (fx is JSFunction fxFunction)
{
fxFunction.InvokeFunction(new Arguments(target, new JSNumber(name), receiver));
fxFunction.InvokeFunction(new Arguments(target, target, new JSNumber(name), receiver));
return true;
}
return target.SetValue(name, value, receiver, false);
Expand Down Expand Up @@ -178,11 +185,11 @@ internal override PropertyKey ToKey(bool create = false)
return target.ToKey();
}

[Constructor]
[JSExport(IsConstructor = true)]
public new static JSValue Constructor(in Arguments a)
{
var (f, s) = a.Get2();
return new JSProxy(f as JSObject, s as JSObject);
return new JSProxy((f as JSObject, s as JSObject));
}
}
}
2 changes: 1 addition & 1 deletion YantraTests/YantraTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>

Expand Down

0 comments on commit 9b3a7f6

Please sign in to comment.