custom.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Definitions for custom, native types.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <new>
  14. #include "custom.hpp"
  15. #include "memory.hpp"
  16. #include "str.hpp"
  17. #include "symbol.hpp"
  18. #include "types.hpp"
  19. #include "thread.hpp"
  20. KP_DECLS_BEGIN
  21. custom_base* custom_base::alloc_raw (size_t size)
  22. {
  23. /* We cannot use 'alloch', because it allocates and initializes a varobj,
  24. * disregarding any adjustments that must be made by a class with virtual
  25. * methods. So we inline the needed part here. */
  26. auto ret = (custom_base *)ensure_mask_impl (xmalloc (size), TYPE_SHIFT);
  27. ret->vo_size = (uint32_t)size;
  28. return (ret);
  29. }
  30. static result<void>
  31. check_custom_type (interpreter *interp, object *tp,
  32. object name, custom_base *bp)
  33. {
  34. if (!tp)
  35. return (interp->raise ("arg-error", "type descriptor must not be null"));
  36. object ty = *tp;
  37. valref parents (interp, NIL), slots (interp, NIL);
  38. if (ty != 0)
  39. {
  40. check:
  41. if (type_name (ty) != name)
  42. return (interp->raise ("arg-error", "different names used for "
  43. "the same custom type"));
  44. return (0);
  45. }
  46. bp->fill_type (interp, *parents, *slots);
  47. object rt = KP_TRY (type (interp, name, *parents, *slots));
  48. if (singlethr_p ())
  49. *tp = rt;
  50. else if (!atomic_cas_bool ((atomic_t *)tp, ty, rt))
  51. goto check;
  52. return (0);
  53. }
  54. result<custom_base*> custom_base::xalloc (interpreter *interp, object *tp,
  55. const char *name, custom_base *bp,
  56. constructor cfn)
  57. {
  58. valref tname = KP_TRY (intern (interp, name));
  59. return (custom_base::xalloc (interp, tp, *tname, bp, cfn));
  60. }
  61. result<custom_base*> custom_base::xalloc (interpreter *interp, object *tp,
  62. const char *name, uint32_t len,
  63. custom_base *bp, constructor cfn)
  64. {
  65. valref tname = KP_TRY (intern (interp, name, len));
  66. return (custom_base::xalloc (interp, tp, *tname, bp, cfn));
  67. }
  68. result<custom_base*> custom_base::xalloc (interpreter *interp, object *tp,
  69. object name, custom_base *ret,
  70. constructor cfn)
  71. {
  72. if (!symbol_p (name))
  73. return (interp->raise ("arg-error",
  74. "type name must be a string or symbol"));
  75. try
  76. {
  77. cfn (ret);
  78. }
  79. catch (...)
  80. {
  81. destroy (ret);
  82. xfree (ret);
  83. return (interp->raise ("runtime-error",
  84. "failed to construct custom object"));
  85. }
  86. auto rv = check_custom_type (interp, tp, name, ret);
  87. if (rv.error_p ())
  88. {
  89. destroy (ret);
  90. xfree (ret);
  91. return (exception ());
  92. }
  93. ret->vo_full = 0;
  94. ret->vo_type = typecode::CUSTOM;
  95. ret->fini = nullptr;
  96. ret->type_ptr = tp;
  97. interp->alval = ret->as_obj ();
  98. gc_register (interp, ret, ret->vo_size);
  99. return (ret);
  100. }
  101. KP_DECLS_END