UuidPrototype.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Jint.Native;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Tests.Runtime.Domain;
  6. internal sealed class UuidPrototype : UuidInstance
  7. {
  8. private UuidPrototype(Engine engine) : base(engine)
  9. {
  10. }
  11. private UuidInstance EnsureUuidInstance(JsValue thisObject)
  12. {
  13. return thisObject.TryCast<UuidInstance>(value => throw new JavaScriptException(Engine.Realm.Intrinsics.TypeError, "Invalid Uuid"));
  14. }
  15. private JsValue ToGuidString(JsValue thisObject, JsValue[] arguments) => EnsureUuidInstance(thisObject).PrimitiveValue.ToString();
  16. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments) => EnsureUuidInstance(thisObject).PrimitiveValue;
  17. public static UuidPrototype CreatePrototypeObject(Engine engine, UuidConstructor ctor)
  18. {
  19. var obj = new UuidPrototype(engine)
  20. {
  21. PrimitiveValue = JsUuid.Empty,
  22. _prototype = engine.Realm.Intrinsics.Object.PrototypeObject,
  23. };
  24. obj.FastSetProperty("constructor", new PropertyDescriptor(ctor, false, false, true));
  25. return obj;
  26. }
  27. public void Configure()
  28. {
  29. FastSetProperty("toString", new PropertyDescriptor(new ClrFunction(Engine, "toString", ToGuidString), true, false, true));
  30. FastSetProperty("valueOf", new PropertyDescriptor(new ClrFunction(Engine, "valueOf", ValueOf), true, false, true));
  31. }
  32. }