UuidPrototype.cs 1.4 KB

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