ref.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/module_common.h>
  50. #include <generated/blog_channel_ncd_ref.h>
  51. struct refhere_instance {
  52. NCDModuleInst *i;
  53. LinkedList0 refs_list;
  54. };
  55. struct ref_instance {
  56. NCDModuleInst *i;
  57. struct refhere_instance *rh;
  58. LinkedList0Node refs_list_node;
  59. };
  60. static void ref_instance_free (struct ref_instance *o);
  61. static void refhere_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  62. {
  63. struct refhere_instance *o = vo;
  64. o->i = i;
  65. // check arguments
  66. if (!NCDVal_ListRead(params->args, 0)) {
  67. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  68. goto fail0;
  69. }
  70. // init refs list
  71. LinkedList0_Init(&o->refs_list);
  72. // signal up
  73. NCDModuleInst_Backend_Up(o->i);
  74. return;
  75. fail0:
  76. NCDModuleInst_Backend_DeadError(i);
  77. }
  78. static void refhere_func_die (void *vo)
  79. {
  80. struct refhere_instance *o = vo;
  81. // die refs
  82. while (!LinkedList0_IsEmpty(&o->refs_list)) {
  83. struct ref_instance *ref = UPPER_OBJECT(LinkedList0_GetFirst(&o->refs_list), struct ref_instance, refs_list_node);
  84. ASSERT(ref->rh == o)
  85. ref_instance_free(ref);
  86. }
  87. NCDModuleInst_Backend_Dead(o->i);
  88. }
  89. static int refhere_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  90. {
  91. struct refhere_instance *o = vo;
  92. // We don't redirect methods, and there will never be an object
  93. // with empty name. Fail here so we don't report non-errors.
  94. if (objname == NCD_STRING_EMPTY) {
  95. return 0;
  96. }
  97. return NCDModuleInst_Backend_GetObj(o->i, objname, out_object);
  98. }
  99. static void ref_func_new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, struct refhere_instance *rh)
  100. {
  101. struct ref_instance *o = vo;
  102. o->i = i;
  103. // check arguments
  104. if (!NCDVal_ListRead(params->args, 0)) {
  105. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  106. goto fail0;
  107. }
  108. // set refhere
  109. o->rh = rh;
  110. // add to refhere's refs list
  111. LinkedList0_Prepend(&o->rh->refs_list, &o->refs_list_node);
  112. // signal up
  113. NCDModuleInst_Backend_Up(o->i);
  114. return;
  115. fail0:
  116. NCDModuleInst_Backend_DeadError(i);
  117. }
  118. static void ref_func_new_from_refhere (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  119. {
  120. struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  121. return ref_func_new_templ(vo, i, params, rh);
  122. }
  123. static void ref_func_new_from_ref (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  124. {
  125. struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  126. return ref_func_new_templ(vo, i, params, ref->rh);
  127. }
  128. static void ref_instance_free (struct ref_instance *o)
  129. {
  130. // remove from refhere's reft list
  131. LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
  132. NCDModuleInst_Backend_Dead(o->i);
  133. }
  134. static void ref_func_die (void *vo)
  135. {
  136. struct ref_instance *o = vo;
  137. ref_instance_free(o);
  138. }
  139. static int ref_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
  140. {
  141. struct ref_instance *o = vo;
  142. // We don't redirect methods, and there will never be an object
  143. // with empty name. Fail here so we don't report non-errors.
  144. if (objname == NCD_STRING_EMPTY) {
  145. return 0;
  146. }
  147. return NCDModuleInst_Backend_GetObj(o->rh->i, objname, out_object);
  148. }
  149. static struct NCDModule modules[] = {
  150. {
  151. .type = "refhere",
  152. .func_new2 = refhere_func_new,
  153. .func_die = refhere_func_die,
  154. .func_getobj = refhere_func_getobj,
  155. .alloc_size = sizeof(struct refhere_instance)
  156. }, {
  157. .type = "refhere::ref",
  158. .base_type = "ref",
  159. .func_new2 = ref_func_new_from_refhere,
  160. .func_die = ref_func_die,
  161. .func_getobj = ref_func_getobj,
  162. .alloc_size = sizeof(struct ref_instance)
  163. }, {
  164. .type = "ref::ref",
  165. .base_type = "ref",
  166. .func_new2 = ref_func_new_from_ref,
  167. .func_die = ref_func_die,
  168. .func_getobj = ref_func_getobj,
  169. .alloc_size = sizeof(struct ref_instance)
  170. }, {
  171. .type = NULL
  172. }
  173. };
  174. const struct NCDModuleGroup ncdmodule_ref = {
  175. .modules = modules
  176. };