-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Jint.Native; | ||
|
||
namespace Jint; | ||
|
||
/// <summary> | ||
/// https://tc39.es/ecma262/#sec-agents , still a work in progress, mostly placeholder | ||
/// </summary> | ||
internal sealed class Agent | ||
{ | ||
private List<JsValue> _keptAlive = new(); | ||
|
||
public void AddToKeptObjects(JsValue target) | ||
{ | ||
_keptAlive.Add(target); | ||
} | ||
|
||
public void ClearKeptObjects() | ||
{ | ||
_keptAlive.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Jint.Native.Function; | ||
using Jint.Native.Object; | ||
using Jint.Runtime; | ||
using Jint.Runtime.Descriptors; | ||
|
||
namespace Jint.Native.WeakRef; | ||
|
||
/// <summary> | ||
/// https://tc39.es/ecma262/#sec-weak-ref-constructor | ||
/// </summary> | ||
internal sealed class WeakRefConstructor : FunctionInstance, IConstructor | ||
{ | ||
private static readonly JsString _functionName = new("WeakRef"); | ||
|
||
internal WeakRefConstructor( | ||
Engine engine, | ||
Realm realm, | ||
FunctionPrototype functionPrototype, | ||
ObjectPrototype objectPrototype) | ||
: base(engine, realm, _functionName) | ||
{ | ||
_prototype = functionPrototype; | ||
PrototypeObject = new WeakRefPrototype(engine, realm, this, objectPrototype); | ||
_length = new PropertyDescriptor(1, PropertyFlag.Configurable); | ||
_prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden); | ||
} | ||
|
||
private WeakRefPrototype PrototypeObject { get; } | ||
|
||
protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments) | ||
{ | ||
ExceptionHelper.ThrowTypeError(_realm, "Constructor WeakRef requires 'new'"); | ||
return null; | ||
} | ||
|
||
ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) | ||
{ | ||
if (newTarget.IsUndefined()) | ||
{ | ||
ExceptionHelper.ThrowTypeError(_realm); | ||
} | ||
|
||
var target = arguments.At(0); | ||
|
||
if (target is not ObjectInstance) | ||
{ | ||
ExceptionHelper.ThrowTypeError(_realm, "WeakRef: target must be an object"); | ||
} | ||
|
||
var weakRef = OrdinaryCreateFromConstructor( | ||
newTarget, | ||
static intrinsics => intrinsics.WeakRef.PrototypeObject, | ||
static (Engine engine, Realm _, object? t) => new WeakRefInstance(engine, (ObjectInstance) t!), | ||
target); | ||
|
||
_engine.AddToKeptObjects(target); | ||
|
||
return weakRef; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Jint.Native.Object; | ||
|
||
namespace Jint.Native.WeakRef; | ||
|
||
/// <summary> | ||
/// https://tc39.es/ecma262/#sec-properties-of-weak-ref-instances | ||
/// </summary> | ||
internal sealed class WeakRefInstance : ObjectInstance | ||
{ | ||
private readonly WeakReference<ObjectInstance> _weakRefTarget; | ||
|
||
public WeakRefInstance(Engine engine, ObjectInstance target) : base(engine) | ||
{ | ||
_weakRefTarget = new WeakReference<ObjectInstance>(target); | ||
} | ||
|
||
public JsValue WeakRefDeref() | ||
{ | ||
if (_weakRefTarget.TryGetTarget(out var target)) | ||
{ | ||
_engine.AddToKeptObjects(target); | ||
return target; | ||
} | ||
|
||
return Undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Jint.Collections; | ||
using Jint.Native.Object; | ||
using Jint.Native.Symbol; | ||
using Jint.Runtime; | ||
using Jint.Runtime.Descriptors; | ||
using Jint.Runtime.Interop; | ||
|
||
namespace Jint.Native.WeakRef; | ||
|
||
/// <summary> | ||
/// https://tc39.es/ecma262/#sec-properties-of-the-weak-ref-prototype-object | ||
/// </summary> | ||
internal sealed class WeakRefPrototype : Prototype | ||
{ | ||
private readonly WeakRefConstructor _constructor; | ||
|
||
internal WeakRefPrototype( | ||
Engine engine, | ||
Realm realm, | ||
WeakRefConstructor constructor, | ||
ObjectPrototype prototype) : base(engine, realm) | ||
{ | ||
_prototype = prototype; | ||
_constructor = constructor; | ||
} | ||
|
||
protected override void Initialize() | ||
{ | ||
const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; | ||
var properties = new PropertyDictionary(5, checkExistingKeys: false) | ||
{ | ||
["constructor"] = new(_constructor, PropertyFlag.NonEnumerable), | ||
["deref"] = new(new ClrFunctionInstance(Engine, "deref", Deref, 0, PropertyFlag.Configurable), propertyFlags) | ||
}; | ||
SetProperties(properties); | ||
|
||
var symbols = new SymbolDictionary(1) | ||
{ | ||
[GlobalSymbolRegistry.ToStringTag] = new("WeakRef", false, false, true) | ||
}; | ||
SetSymbols(symbols); | ||
} | ||
|
||
private JsValue Deref(JsValue thisObj, JsValue[] arguments) | ||
{ | ||
var weakRef = thisObj as WeakRefInstance; | ||
if (weakRef is null) | ||
{ | ||
ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakRef"); | ||
} | ||
|
||
return weakRef.WeakRefDeref(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters