ref.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <generated/blog_channel_ncd_ref.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct refhere_instance {
  53. NCDModuleInst *i;
  54. LinkedList0 refs_list;
  55. };
  56. struct ref_instance {
  57. NCDModuleInst *i;
  58. struct refhere_instance *rh;
  59. LinkedList0Node refs_list_node;
  60. };
  61. static void ref_instance_free (struct ref_instance *o);
  62. static void refhere_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  63. {
  64. struct refhere_instance *o = vo;
  65. o->i = i;
  66. // check arguments
  67. if (!NCDVal_ListRead(params->args, 0)) {
  68. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  69. goto fail0;
  70. }
  71. // init refs list
  72. LinkedList0_Init(&o->refs_list);
  73. // signal up
  74. NCDModuleInst_Backend_Up(o->i);
  75. return;
  76. fail0:
  77. NCDModuleInst_Backend_SetError(i);
  78. NCDModuleInst_Backend_Dead(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_EMPTY_STRING_ID) {
  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_SetError(i);
  119. NCDModuleInst_Backend_Dead(i);
  120. }
  121. static void ref_func_new_from_refhere (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  122. {
  123. struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  124. return ref_func_new_templ(vo, i, params, rh);
  125. }
  126. static void ref_func_new_from_ref (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  127. {
  128. struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  129. return ref_func_new_templ(vo, i, params, ref->rh);
  130. }
  131. static void ref_instance_free (struct ref_instance *o)
  132. {
  133. // remove from refhere's reft list
  134. LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
  135. NCDModuleInst_Backend_Dead(o->i);
  136. }
  137. static void ref_func_die (void *vo)
  138. {
  139. struct ref_instance *o = vo;
  140. ref_instance_free(o);
  141. }
  142. static int ref_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  143. {
  144. struct ref_instance *o = vo;
  145. // We don't redirect methods, and there will never be an object
  146. // with empty name. Fail here so we don't report non-errors.
  147. if (objname == NCD_EMPTY_STRING_ID) {
  148. return 0;
  149. }
  150. return NCDModuleInst_Backend_GetObj(o->rh->i, objname, out_object);
  151. }
  152. static const struct NCDModule modules[] = {
  153. {
  154. .type = "refhere",
  155. .func_new2 = refhere_func_new,
  156. .func_die = refhere_func_die,
  157. .func_getobj = refhere_func_getobj,
  158. .alloc_size = sizeof(struct refhere_instance)
  159. }, {
  160. .type = "refhere::ref",
  161. .base_type = "ref",
  162. .func_new2 = ref_func_new_from_refhere,
  163. .func_die = ref_func_die,
  164. .func_getobj = ref_func_getobj,
  165. .alloc_size = sizeof(struct ref_instance)
  166. }, {
  167. .type = "ref::ref",
  168. .base_type = "ref",
  169. .func_new2 = ref_func_new_from_ref,
  170. .func_die = ref_func_die,
  171. .func_getobj = ref_func_getobj,
  172. .alloc_size = sizeof(struct ref_instance)
  173. }, {
  174. .type = NULL
  175. }
  176. };
  177. const struct NCDModuleGroup ncdmodule_ref = {
  178. .modules = modules
  179. };