-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
Copy pathUuidConstructor.cs
70 lines (54 loc) · 2.35 KB
/
UuidConstructor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using Jint.Native;
using Jint.Native.Function;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
namespace Jint.Tests.Runtime.Domain
{
internal sealed class UuidConstructor : FunctionInstance, IConstructor
{
private static readonly JsString _functionName = new JsString("Uuid");
private UuidConstructor(Engine engine) : base(engine, _functionName)
{
}
private JsValue Parse(JsValue @this, JsValue[] arguments)
{
switch (arguments.At(0))
{
case JsUuid uid:
return Construct(uid);
case JsValue js when Guid.TryParse(js.AsString(), out var res):
return Construct(res);
}
return Undefined;
}
protected override ObjectInstance GetPrototypeOf() => _prototype;
internal ObjectInstance _prototype;
public UuidPrototype PrototypeObject { get; private set; }
public static UuidConstructor CreateUuidConstructor(Engine engine)
{
var obj = new UuidConstructor(engine)
{
// The value of the [[Prototype]] internal property of the Uuid constructor is the Function prototype object
_prototype = engine.Function.PrototypeObject
};
obj.PrototypeObject = UuidPrototype.CreatePrototypeObject(engine, obj);
// The initial value of Uuid.prototype is the Date prototype object
obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, false, false, false));
engine.SetValue("Uuid", obj);
obj.Configure();
obj.PrototypeObject.Configure();
return obj;
}
public override JsValue Call(JsValue thisObject, JsValue[] arguments) => Construct(arguments, null);
public void Configure()
{
FastAddProperty("parse", new ClrFunctionInstance(Engine, "parse", Parse), true, false, true);
FastAddProperty("Empty", JsUuid.Empty, true, false, true);
}
public UuidInstance Construct(JsUuid uuid) => new UuidInstance(Engine) { PrimitiveValue = uuid, _prototype = PrototypeObject };
public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget) => Construct(Guid.NewGuid());
}
}