UuidPrototype.cs 1.5 KB

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