diff --git a/YantraJS.Core.Tests/Generator/Files/es6/Syntax/proxy/proxy-get.js b/YantraJS.Core.Tests/Generator/Files/es6/Syntax/proxy/proxy-get.js new file mode 100644 index 00000000..cd41f6ea --- /dev/null +++ b/YantraJS.Core.Tests/Generator/Files/es6/Syntax/proxy/proxy-get.js @@ -0,0 +1,7 @@ +var p = new Proxy({}, { + get(t, p, receiver) { + return p + "." + p; + } +}); + +assert.strictEqual(p.a, "a.a"); diff --git a/YantraJS.Core.Tests/YantraJS.Core.Tests.csproj b/YantraJS.Core.Tests/YantraJS.Core.Tests.csproj index 7c2942b4..6808f85c 100644 --- a/YantraJS.Core.Tests/YantraJS.Core.Tests.csproj +++ b/YantraJS.Core.Tests/YantraJS.Core.Tests.csproj @@ -1,7 +1,7 @@  - net6 + net8.0 false true diff --git a/YantraJS.Core/Core/Proxy/JSProxy.cs b/YantraJS.Core/Core/Proxy/JSProxy.cs index c019a9ee..5310a504 100644 --- a/YantraJS.Core/Core/Proxy/JSProxy.cs +++ b/YantraJS.Core/Core/Proxy/JSProxy.cs @@ -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"); @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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)); } } } diff --git a/YantraTests/YantraTests.csproj b/YantraTests/YantraTests.csproj index f603d242..43749183 100644 --- a/YantraTests/YantraTests.csproj +++ b/YantraTests/YantraTests.csproj @@ -1,7 +1,7 @@  - net6 + net8.0 true false