Skip to content

Commit

Permalink
Source re allocation fixed in Function.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Nov 9, 2023
1 parent e30f424 commit b7a5db7
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Test262Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions YantraJS.Core.Tests/ModuleBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public async Task ExportValueShouldWork()
}

[TestMethod]
public async Task ImportModuleThrowException()
public void ImportModuleThrowException()
{
JSModuleContext context = new JSModuleContext();
Assert.ThrowsException<ArgumentException>((() => context.ImportModule("test")));
}

[TestMethod]
public async Task ImportModuleFindModule()
public void ImportModuleFindModule()
{
JSModuleContext context = new JSModuleContext();
context.CreateModule("test", x => x.ExportType<TestClass>());
Expand Down
10 changes: 5 additions & 5 deletions YantraJS.Core/Core/Array/JSArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
Expand Down
8 changes: 4 additions & 4 deletions YantraJS.Core/Core/Array/Typed/JSTypedArray.prototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
Expand Down
5 changes: 5 additions & 0 deletions YantraJS.Core/Core/Boolean/JSBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions YantraJS.Core/Core/Date/JSDatePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions YantraJS.Core/Core/Function/JSFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public partial class JSFunction : JSObject
[EditorBrowsable(EditorBrowsableState.Never)]
public JSObject prototype;

readonly StringSpan source;
private StringSpan source;

internal JSFunction constructor;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)]
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/Generator/JSGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/JSArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/JSContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void Dispose()

// public readonly JSObject NumberPrototype;

public readonly JSObject ObjectPrototype;
public new readonly JSObject ObjectPrototype;

// public readonly JSObject ArrayPrototype;

Expand Down
4 changes: 4 additions & 0 deletions YantraJS.Core/Core/Number/JSNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions YantraJS.Core/Core/Objects/JSReflect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions YantraJS.Core/Core/Primitive/JSNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/Proxy/JSProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions YantraJS.Core/Core/String/JSString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion YantraJS.Core/FastParser/Ast/AstBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace YantraJS.Core.FastParser
#nullable enable
namespace YantraJS.Core.FastParser
{
public class AstBlock : AstStatement
{
Expand Down
5 changes: 3 additions & 2 deletions YantraJS.Core/FastParser/Parser/FastParser.Import.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Text;

Expand Down Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions YantraJS.ModuleExtensions/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string name = null)
public ModuleBuilder ExportType<T>(string? name = null)
{
exportedObjects.Add((name ?? typeof(T).Name, typeof(T)));
return this;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions YantraJS.Network/FetchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{

}
Expand Down

0 comments on commit b7a5db7

Please sign in to comment.