From b7a5db73943f5cbeab07efee0cec2d3b6662a9d8 Mon Sep 17 00:00:00 2001 From: Akash Kava <39438041+ackava@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:54:31 +0530 Subject: [PATCH] Source re allocation fixed in Function.toString --- Test262Runner/Program.cs | 2 +- YantraJS.Core.Tests/ModuleBuilderTests.cs | 4 ++-- YantraJS.Core/Core/Array/JSArrayPrototype.cs | 10 +++++----- .../Array/Typed/JSTypedArray.prototype.cs | 8 ++++---- YantraJS.Core/Core/Boolean/JSBoolean.cs | 5 +++++ YantraJS.Core/Core/Date/JSDatePrototype.cs | 4 ++-- YantraJS.Core/Core/Function/JSFunction.cs | 19 ++++++++++++++----- YantraJS.Core/Core/Generator/JSGenerator.cs | 2 +- YantraJS.Core/Core/JSArguments.cs | 2 +- YantraJS.Core/Core/JSContext.cs | 2 +- YantraJS.Core/Core/Number/JSNumber.cs | 4 ++++ YantraJS.Core/Core/Objects/JSReflect.cs | 12 ++++++------ YantraJS.Core/Core/Primitive/JSNull.cs | 5 +++++ YantraJS.Core/Core/Proxy/JSProxy.cs | 2 +- YantraJS.Core/Core/String/JSString.cs | 5 +++++ YantraJS.Core/FastParser/Ast/AstBlock.cs | 3 ++- .../FastParser/Parser/FastParser.Import.cs | 5 +++-- .../GeneratorsV2/GeneratorRewriter.cs | 4 ++-- YantraJS.ModuleExtensions/ModuleBuilder.cs | 4 ++-- YantraJS.Network/FetchResponse.cs | 2 ++ YantraJS.Network/KeyValueStore.cs | 6 +++--- 21 files changed, 71 insertions(+), 39 deletions(-) diff --git a/Test262Runner/Program.cs b/Test262Runner/Program.cs index 0acfa80d..56ee5742 100644 --- a/Test262Runner/Program.cs +++ b/Test262Runner/Program.cs @@ -118,7 +118,7 @@ private static async Task RunJSTest(string filePath) CoreScript.Evaluate(code, filePath); throw new Exception($"Exception not thrown"); } - catch (JSException ex) + catch (JSException) { return; } diff --git a/YantraJS.Core.Tests/ModuleBuilderTests.cs b/YantraJS.Core.Tests/ModuleBuilderTests.cs index e81cc202..f4aa447b 100644 --- a/YantraJS.Core.Tests/ModuleBuilderTests.cs +++ b/YantraJS.Core.Tests/ModuleBuilderTests.cs @@ -88,14 +88,14 @@ public async Task ExportValueShouldWork() } [TestMethod] - public async Task ImportModuleThrowException() + public void ImportModuleThrowException() { JSModuleContext context = new JSModuleContext(); Assert.ThrowsException((() => context.ImportModule("test"))); } [TestMethod] - public async Task ImportModuleFindModule() + public void ImportModuleFindModule() { JSModuleContext context = new JSModuleContext(); context.CreateModule("test", x => x.ExportType()); diff --git a/YantraJS.Core/Core/Array/JSArrayPrototype.cs b/YantraJS.Core/Core/Array/JSArrayPrototype.cs index 98b0b2c4..16a3d83b 100644 --- a/YantraJS.Core/Core/Array/JSArrayPrototype.cs +++ b/YantraJS.Core/Core/Array/JSArrayPrototype.cs @@ -18,7 +18,7 @@ public partial class JSArray { [JSExport(IsConstructor = true, Length = 1)] - public static JSValue Constructor(in Arguments a) + public new static JSValue Constructor(in Arguments a) { // throw JSContext.Current.NewTypeError("Not supported"); var @this = a.This; @@ -181,7 +181,7 @@ public static JSValue Every(in Arguments a) [JSPrototypeMethod][JSExport("entries")] - public static JSValue Entries(in Arguments a) + public new static JSValue Entries(in Arguments a) { var array = a.This as JSArray; @@ -453,7 +453,7 @@ public static JSValue Join(in Arguments a) } [JSPrototypeMethod][JSExport("keys")] - public static JSValue Keys(in Arguments a) + public new static JSValue Keys(in Arguments a) { var @this = a.This; @@ -1112,7 +1112,7 @@ internal static JSValue ToLocaleString(in Arguments a) } [JSPrototypeMethod][JSExport("toString")] - internal static JSValue ToString(in Arguments args) { + internal new static JSValue ToString(in Arguments args) { if(args.This.IsArray) return Join(in args); return args.This.InvokeMethod(KeyStrings.join,in args); @@ -1125,7 +1125,7 @@ internal static JSValue ToLocaleString(in Arguments a) [JSPrototypeMethod][JSExport("values", Length = 2)] [Symbol("@@iterator")] - public static JSValue Values(in Arguments a) + public new static JSValue Values(in Arguments a) { return new JSGenerator(a.This.GetElementEnumerator(), "Array Iterator"); } diff --git a/YantraJS.Core/Core/Array/Typed/JSTypedArray.prototype.cs b/YantraJS.Core/Core/Array/Typed/JSTypedArray.prototype.cs index da0e8873..0531fb16 100644 --- a/YantraJS.Core/Core/Array/Typed/JSTypedArray.prototype.cs +++ b/YantraJS.Core/Core/Array/Typed/JSTypedArray.prototype.cs @@ -10,7 +10,7 @@ namespace YantraJS.Core.Typed partial class JSTypedArray { [JSExport("toString")] - private JSValue ToString(in Arguments a) + private new JSValue ToString(in Arguments a) { return new JSString(ToString()); } @@ -70,7 +70,7 @@ public JSValue CopyWithin(in Arguments a) [JSExport("entries")] - public JSValue Entries(in Arguments a) + public new JSValue Entries(in Arguments a) { return new JSGenerator(GetEntries(), "Array Iterator"); } @@ -282,7 +282,7 @@ public JSValue Join(in Arguments a) } [JSExport("keys", Length = 0)] - public JSValue Keys(in Arguments a) + public new JSValue Keys(in Arguments a) { return this.GetKeys(); @@ -616,7 +616,7 @@ public JSValue SubArray(in Arguments a) } [JSExport("values", Length = 2)] - public JSValue Values(in Arguments a) + public new JSValue Values(in Arguments a) { return new JSGenerator(GetElementEnumerator(), "Array Iterator"); } diff --git a/YantraJS.Core/Core/Boolean/JSBoolean.cs b/YantraJS.Core/Core/Boolean/JSBoolean.cs index 470219c3..9dc0c151 100644 --- a/YantraJS.Core/Core/Boolean/JSBoolean.cs +++ b/YantraJS.Core/Core/Boolean/JSBoolean.cs @@ -96,6 +96,11 @@ public override string ToString() return _value ? "true" : "false"; } + public override int GetHashCode() + { + return _value ? 1 : 0; + } + public override bool Equals(object obj) { if (obj is JSBoolean b) diff --git a/YantraJS.Core/Core/Date/JSDatePrototype.cs b/YantraJS.Core/Core/Date/JSDatePrototype.cs index 5bfe0930..ae083114 100644 --- a/YantraJS.Core/Core/Date/JSDatePrototype.cs +++ b/YantraJS.Core/Core/Date/JSDatePrototype.cs @@ -890,7 +890,7 @@ internal JSValue ToLocaleTimeString(in Arguments a) [JSExport("toString", Length = 0)] - internal JSValue ToString(in Arguments a) + internal new JSValue ToString(in Arguments a) { if (this.value == JSDate.InvalidDate) @@ -944,7 +944,7 @@ internal JSValue ToUTCString(in Arguments a) [JSExport("valueOf", Length = 0)] - internal JSValue ValueOf(in Arguments a) + internal new JSValue ValueOf(in Arguments a) { if (this.value == DateTimeOffset.MinValue) diff --git a/YantraJS.Core/Core/Function/JSFunction.cs b/YantraJS.Core/Core/Function/JSFunction.cs index b17ad4d1..feec488a 100644 --- a/YantraJS.Core/Core/Function/JSFunction.cs +++ b/YantraJS.Core/Core/Function/JSFunction.cs @@ -37,7 +37,7 @@ public partial class JSFunction : JSObject [EditorBrowsable(EditorBrowsableState.Never)] public JSObject prototype; - readonly StringSpan source; + private StringSpan source; internal JSFunction constructor; @@ -247,7 +247,7 @@ public override JSValue InvokeFunction(in Arguments a) } [JSPrototypeMethod][JSExport("valueOf", Length = 1)] - public static JSValue ValueOf(in Arguments a) + public new static JSValue ValueOf(in Arguments a) { return a.This; } @@ -293,11 +293,20 @@ public static JSValue Call(in Arguments a) } [JSPrototypeMethod][JSExport("toString", Length = 0)] - public static JSValue ToString(in Arguments a) + public new static JSValue ToString(in Arguments a) { if (!(a.This is JSFunction fx)) throw JSContext.Current.NewTypeError($"Function.prototype.toString cannot be called with non function"); - return new JSString(fx.source); + var source = fx.source; + if (source.IsEmpty) + { + return new JSString(string.Empty); + } + if (source.Source.Length != source.Length || source.Offset != 0) + { + source = source.Value; + } + return new JSString(source.Source); } [EditorBrowsable(EditorBrowsableState.Never)] @@ -311,7 +320,7 @@ public static JSValue InvokeSuperConstructor(JSValue newTarget, JSValue super, i } [JSExport(IsConstructor = true)] - internal static JSValue Constructor(in Arguments args) + internal new static JSValue Constructor(in Arguments args) { var len = args.Length; diff --git a/YantraJS.Core/Core/Generator/JSGenerator.cs b/YantraJS.Core/Core/Generator/JSGenerator.cs index e3fdab00..4f235e34 100644 --- a/YantraJS.Core/Core/Generator/JSGenerator.cs +++ b/YantraJS.Core/Core/Generator/JSGenerator.cs @@ -43,7 +43,7 @@ public override string ToString() } [JSExport("toString")] - public JSValue ToString(in Arguments a) + public new JSValue ToString(in Arguments a) { return new JSString(ToString()); } diff --git a/YantraJS.Core/Core/JSArguments.cs b/YantraJS.Core/Core/JSArguments.cs index 70e1e17e..3c1211bc 100644 --- a/YantraJS.Core/Core/JSArguments.cs +++ b/YantraJS.Core/Core/JSArguments.cs @@ -15,7 +15,7 @@ public JSValue Callee(in Arguments a) throw JSContext.Current.NewTypeError($"Cannot access callee in strict mode"); } - public JSValue Values(in Arguments a) + public new JSValue Values(in Arguments a) { return new JSGenerator(this.GetElementEnumerator(), "Arguments"); } diff --git a/YantraJS.Core/Core/JSContext.cs b/YantraJS.Core/Core/JSContext.cs index 2da8f335..d9eeef2a 100644 --- a/YantraJS.Core/Core/JSContext.cs +++ b/YantraJS.Core/Core/JSContext.cs @@ -200,7 +200,7 @@ public void Dispose() // public readonly JSObject NumberPrototype; - public readonly JSObject ObjectPrototype; + public new readonly JSObject ObjectPrototype; // public readonly JSObject ArrayPrototype; diff --git a/YantraJS.Core/Core/Number/JSNumber.cs b/YantraJS.Core/Core/Number/JSNumber.cs index a95cc7b5..30001be0 100644 --- a/YantraJS.Core/Core/Number/JSNumber.cs +++ b/YantraJS.Core/Core/Number/JSNumber.cs @@ -288,6 +288,10 @@ public override JSValue AddValue(string value) // return new JSString(this.value.ToString() + value); //} + public override int GetHashCode() + { + return (int)value; + } public override bool Equals(object obj) { if (obj is JSNumber n) diff --git a/YantraJS.Core/Core/Objects/JSReflect.cs b/YantraJS.Core/Core/Objects/JSReflect.cs index 9c8ac66b..f5cfef55 100644 --- a/YantraJS.Core/Core/Objects/JSReflect.cs +++ b/YantraJS.Core/Core/Objects/JSReflect.cs @@ -29,7 +29,7 @@ public static JSValue Construct(in Arguments a) } [JSExport( Length = 3)] - public static JSValue DefineProperty(in Arguments a) + public new static JSValue DefineProperty(in Arguments a) { var (target, propertyKey, attributes) = a.Get3(); if (!(target is JSObject targetObject)) @@ -91,7 +91,7 @@ public static JSValue Get(in Arguments a) } [JSExport( Length = 2)] - public static JSValue GetOwnPropertyDescriptor(in Arguments a) + public new static JSValue GetOwnPropertyDescriptor(in Arguments a) { var (target, propertyKey) = a.Get2(); if (!(target is JSObject @object)) @@ -119,7 +119,7 @@ public static JSValue GetOwnPropertyDescriptor(in Arguments a) } [Static("getPrototypeOf")] - public static JSValue GetPrototypeOf(in Arguments a) + public new static JSValue GetPrototypeOf(in Arguments a) { var target = a.Get1(); if(!(target is JSObject)) @@ -159,7 +159,7 @@ public static JSValue Has(in Arguments a) } [JSExport( Length = 1)] - public static JSValue IsExtensible(in Arguments a) + public new static JSValue IsExtensible(in Arguments a) { var target = a.Get1(); if(!(target is JSObject @object)) @@ -195,7 +195,7 @@ public static JSValue OwnKeys(in Arguments a) } [JSExport( Length = 1)] - public static JSValue PreventExtensions(in Arguments a) + public new static JSValue PreventExtensions(in Arguments a) { var target = a.Get1(); if (!(target is JSObject @object)) @@ -255,7 +255,7 @@ public static JSValue Set(in Arguments a) } [JSExport] - public static JSValue SetPrototypeOf(in Arguments a) + public new static JSValue SetPrototypeOf(in Arguments a) { var (target,p) = a.Get2(); if (!(target is JSObject)) diff --git a/YantraJS.Core/Core/Primitive/JSNull.cs b/YantraJS.Core/Core/Primitive/JSNull.cs index 5ffdaf98..9269d487 100644 --- a/YantraJS.Core/Core/Primitive/JSNull.cs +++ b/YantraJS.Core/Core/Primitive/JSNull.cs @@ -111,6 +111,11 @@ public override IElementEnumerator GetElementEnumerator() // return new JSString("null" + value); //} + public override int GetHashCode() + { + return 0; + } + public override bool Equals(JSValue value) { if (value.IsNull) diff --git a/YantraJS.Core/Core/Proxy/JSProxy.cs b/YantraJS.Core/Core/Proxy/JSProxy.cs index b9b9214b..c019a9ee 100644 --- a/YantraJS.Core/Core/Proxy/JSProxy.cs +++ b/YantraJS.Core/Core/Proxy/JSProxy.cs @@ -179,7 +179,7 @@ internal override PropertyKey ToKey(bool create = false) } [Constructor] - public static JSValue Constructor(in Arguments a) + public new static JSValue Constructor(in Arguments a) { var (f, s) = a.Get2(); return new JSProxy(f as JSObject, s as JSObject); diff --git a/YantraJS.Core/Core/String/JSString.cs b/YantraJS.Core/Core/String/JSString.cs index 8fac995d..3730196c 100644 --- a/YantraJS.Core/Core/String/JSString.cs +++ b/YantraJS.Core/Core/String/JSString.cs @@ -246,6 +246,11 @@ public override IElementEnumerator GetAllKeys(bool showEnumerableOnly = true, bo [JSExport] public override int Length => value.Length; + public override int GetHashCode() + { + return value.GetHashCode(); + } + public override bool Equals(object obj) { if (obj is JSString v) diff --git a/YantraJS.Core/FastParser/Ast/AstBlock.cs b/YantraJS.Core/FastParser/Ast/AstBlock.cs index 355b9d63..7106cd8f 100644 --- a/YantraJS.Core/FastParser/Ast/AstBlock.cs +++ b/YantraJS.Core/FastParser/Ast/AstBlock.cs @@ -1,4 +1,5 @@ -namespace YantraJS.Core.FastParser +#nullable enable +namespace YantraJS.Core.FastParser { public class AstBlock : AstStatement { diff --git a/YantraJS.Core/FastParser/Parser/FastParser.Import.cs b/YantraJS.Core/FastParser/Parser/FastParser.Import.cs index 61b97405..64a2dfc4 100644 --- a/YantraJS.Core/FastParser/Parser/FastParser.Import.cs +++ b/YantraJS.Core/FastParser/Parser/FastParser.Import.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Text; @@ -26,7 +27,7 @@ bool Import(FastToken token, out AstStatement statement) return true; } - AstIdentifier all = null; + AstIdentifier? all = null; IFastEnumerable<(StringSpan, StringSpan)>? names = null; if(Identitifer(out id)) { diff --git a/YantraJS.Core/LinqExpressions/GeneratorsV2/GeneratorRewriter.cs b/YantraJS.Core/LinqExpressions/GeneratorsV2/GeneratorRewriter.cs index 76c6f7ec..e5efddaa 100644 --- a/YantraJS.Core/LinqExpressions/GeneratorsV2/GeneratorRewriter.cs +++ b/YantraJS.Core/LinqExpressions/GeneratorsV2/GeneratorRewriter.cs @@ -25,7 +25,7 @@ public class GeneratorRewriter: YExpressionMapVisitor private readonly ParameterExpression nextJump; private readonly ParameterExpression nextValue; private readonly ParameterExpression exception; - private readonly YFieldExpression StackItem; + // private readonly YFieldExpression StackItem; private readonly YFieldExpression Context; // private readonly YFieldExpression ScriptInfo; // private readonly YFieldExpression Closures; @@ -39,7 +39,7 @@ public class GeneratorRewriter: YExpressionMapVisitor private readonly ParameterExpression replaceArgs; // private readonly ParameterExpression replaceStackItem; private readonly ParameterExpression replaceContext; - private readonly ParameterExpression replaceScriptInfo; + // private readonly ParameterExpression replaceScriptInfo; private Sequence<(LabelTarget label, int id)> jumps = new Sequence<(LabelTarget label, int id)>(); public GeneratorRewriter( diff --git a/YantraJS.ModuleExtensions/ModuleBuilder.cs b/YantraJS.ModuleExtensions/ModuleBuilder.cs index 2c409e25..a4cf54c1 100644 --- a/YantraJS.ModuleExtensions/ModuleBuilder.cs +++ b/YantraJS.ModuleExtensions/ModuleBuilder.cs @@ -8,7 +8,7 @@ public class ModuleBuilder private List<(string name,object value)> exportedObjects = new List<(string name, object value)>(); private string _moduleName; - public ModuleBuilder ExportType(string name = null) + public ModuleBuilder ExportType(string? name = null) { exportedObjects.Add((name ?? typeof(T).Name, typeof(T))); return this; @@ -19,7 +19,7 @@ public ModuleBuilder(string moduleName) _moduleName = moduleName; } - public ModuleBuilder ExportType(Type type, string name = null) + public ModuleBuilder ExportType(Type type, string? name = null) { exportedObjects.Add((type.Name, type)); return this; diff --git a/YantraJS.Network/FetchResponse.cs b/YantraJS.Network/FetchResponse.cs index e2172675..b851076c 100644 --- a/YantraJS.Network/FetchResponse.cs +++ b/YantraJS.Network/FetchResponse.cs @@ -13,7 +13,9 @@ public partial class FetchResponse : JSObject { private readonly HttpResponseMessage message; +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. private FetchResponse(in Arguments a): base(a.NewPrototype) +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { } diff --git a/YantraJS.Network/KeyValueStore.cs b/YantraJS.Network/KeyValueStore.cs index e1f1c45e..3d0ecd4b 100644 --- a/YantraJS.Network/KeyValueStore.cs +++ b/YantraJS.Network/KeyValueStore.cs @@ -51,7 +51,7 @@ internal KeyValueStore(JSValue? first, JSObject? prototype = null) : this(protot var ve = new PropertySequence.ValueEnumerator(@object, true); while (ve.MoveNext(out var value, out var p)) { - headers[p.Value.Value] = value.ToString(); + headers[p.Value.Value!] = value.ToString(); } return; } @@ -102,7 +102,7 @@ public void Delete(string name) } [JSExport] - public IEnumerable Entries() + public new IEnumerable Entries() { if (headers == null) yield break; @@ -151,7 +151,7 @@ public JSValue Has(string name) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name)); - if (headers.ContainsKey(name.ToLower())) + if (headers!.ContainsKey(name.ToLower())) { return JSBoolean.True; }