ref.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @file ref.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * References module.
  32. *
  33. * Synopsis:
  34. * refhere()
  35. * Variables:
  36. * Exposes variables and objects as seen from this refhere() statement.
  37. *
  38. * Synopsis:
  39. * ref refhere::ref()
  40. * ref ref::ref()
  41. * Variables:
  42. * Exposes variables and objects as seen from the corresponding refhere()
  43. * statement.
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <misc/offset.h>
  48. #include <structure/LinkedList0.h>
  49. #include <ncd/NCDModule.h>
  50. #include <ncd/static_strings.h>
  51. #include <generated/blog_channel_ncd_ref.h>
  52. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  53. struct refhere_instance {
  54. NCDModuleInst *i;
  55. LinkedList0 refs_list;
  56. };
  57. struct ref_instance {
  58. NCDModuleInst *i;
  59. struct refhere_instance *rh;
  60. LinkedList0Node refs_list_node;
  61. };
  62. static void ref_instance_free (struct ref_instance *o);
  63. static void refhere_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  64. {
  65. struct refhere_instance *o = vo;
  66. o->i = i;
  67. // check arguments
  68. if (!NCDVal_ListRead(params->args, 0)) {
  69. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  70. goto fail0;
  71. }
  72. // init refs list
  73. LinkedList0_Init(&o->refs_list);
  74. // signal up
  75. NCDModuleInst_Backend_Up(o->i);
  76. return;
  77. fail0:
  78. NCDModuleInst_Backend_DeadError(i);
  79. }
  80. static void refhere_func_die (void *vo)
  81. {
  82. struct refhere_instance *o = vo;
  83. // die refs
  84. while (!LinkedList0_IsEmpty(&o->refs_list)) {
  85. struct ref_instance *ref = UPPER_OBJECT(LinkedList0_GetFirst(&o->refs_list), struct ref_instance, refs_list_node);
  86. ASSERT(ref->rh == o)
  87. ref_instance_free(ref);
  88. }
  89. NCDModuleInst_Backend_Dead(o->i);
  90. }
  91. static int refhere_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  92. {
  93. struct refhere_instance *o = vo;
  94. // We don't redirect methods, and there will never be an object
  95. // with empty name. Fail here so we don't report non-errors.
  96. if (objname == NCD_STRING_EMPTY) {
  97. return 0;
  98. }
  99. return NCDModuleInst_Backend_GetObj(o->i, objname, out_object);
  100. }
  101. static void ref_func_new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, struct refhere_instance *rh)
  102. {
  103. struct ref_instance *o = vo;
  104. o->i = i;
  105. // check arguments
  106. if (!NCDVal_ListRead(params->args, 0)) {
  107. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  108. goto fail0;
  109. }
  110. // set refhere
  111. o->rh = rh;
  112. // add to refhere's refs list
  113. LinkedList0_Prepend(&o->rh->refs_list, &o->refs_list_node);
  114. // signal up
  115. NCDModuleInst_Backend_Up(o->i);
  116. return;
  117. fail0:
  118. NCDModuleInst_Backend_DeadError(i);
  119. }
  120. static void ref_func_new_from_refhere (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  121. {
  122. struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  123. return ref_func_new_templ(vo, i, params, rh);
  124. }
  125. static void ref_func_new_from_ref (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  126. {
  127. struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  128. return ref_func_new_templ(vo, i, params, ref->rh);
  129. }
  130. static void ref_instance_free (struct ref_instance *o)
  131. {
  132. // remove from refhere's reft list
  133. LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
  134. NCDModuleInst_Backend_Dead(o->i);
  135. }
  136. static void ref_func_die (void *vo)
  137. {
  138. struct ref_instance *o = vo;
  139. ref_instance_free(o);
  140. }
  141. static int ref_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  142. {
  143. struct ref_instance *o = vo;
  144. // We don't redirect methods, and there will never be an object
  145. // with empty name. Fail here so we don't report non-errors.
  146. if (objname == NCD_STRING_EMPTY) {
  147. return 0;
  148. }
  149. return NCDModuleInst_Backend_GetObj(o->rh->i, objname, out_object);
  150. }
  151. static struct NCDModule modules[] = {
  152. {
  153. .type = "refhere",
  154. .func_new2 = refhere_func_new,
  155. .func_die = refhere_func_die,
  156. .func_getobj = refhere_func_getobj,
  157. .alloc_size = sizeof(struct refhere_instance)
  158. }, {
  159. .type = "refhere::ref",
  160. .base_type = "ref",
  161. .func_new2 = ref_func_new_from_refhere,
  162. .func_die = ref_func_die,
  163. .func_getobj = ref_func_getobj,
  164. .alloc_size = sizeof(struct ref_instance)
  165. }, {
  166. .type = "ref::ref",
  167. .base_type = "ref",
  168. .func_new2 = ref_func_new_from_ref,
  169. .func_die = ref_func_die,
  170. .func_getobj = ref_func_getobj,
  171. .alloc_size = sizeof(struct ref_instance)
  172. }, {
  173. .type = NULL
  174. }
  175. };
  176. const struct NCDModuleGroup ncdmodule_ref = {
  177. .modules = modules
  178. };