UuidPrototype.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. {
  7. internal sealed class UuidPrototype : UuidInstance
  8. {
  9. private UuidPrototype(Engine engine) : base(engine)
  10. {
  11. }
  12. private UuidInstance EnsureUuidInstance(JsValue thisObject)
  13. {
  14. return thisObject.TryCast<UuidInstance>(value => throw new JavaScriptException(Engine.Realm.Intrinsics.TypeError, "Invalid Uuid"));
  15. }
  16. private JsValue ToGuidString(JsValue thisObject, JsValue[] arguments) => EnsureUuidInstance(thisObject).PrimitiveValue.ToString();
  17. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments) => EnsureUuidInstance(thisObject).PrimitiveValue;
  18. public static UuidPrototype CreatePrototypeObject(Engine engine, UuidConstructor ctor)
  19. {
  20. var obj = new UuidPrototype(engine)
  21. {
  22. PrimitiveValue = JsUuid.Empty,
  23. _prototype = engine.Realm.Intrinsics.Object.PrototypeObject,
  24. };
  25. obj.FastSetProperty("constructor", new PropertyDescriptor(ctor, false, false, true));
  26. return obj;
  27. }
  28. public void Configure()
  29. {
  30. FastSetProperty("toString", new PropertyDescriptor(new ClrFunction(Engine, "toString", ToGuidString), true, false, true));
  31. FastSetProperty("valueOf", new PropertyDescriptor(new ClrFunction(Engine, "valueOf", ValueOf), true, false, true));
  32. }
  33. }
  34. }