objref.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @file objref.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. * Synopsis:
  32. * objref(list(string) target)
  33. * objref_arg(list(string) target)
  34. */
  35. #include <misc/debug.h>
  36. #include <misc/balloc.h>
  37. #include <ncd/module_common.h>
  38. #include <generated/blog_channel_ncd_objref.h>
  39. struct instance {
  40. NCDModuleInst *i;
  41. NCDObjRef ref;
  42. };
  43. static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_arg)
  44. {
  45. ASSERT(is_arg == 0 || is_arg == 1)
  46. struct instance *o = vo;
  47. o->i = i;
  48. NCDValRef target_arg;
  49. if (!NCDVal_ListRead(params->args, 1, &target_arg)) {
  50. ModuleLog(i, BLOG_ERROR, "wrong arity");
  51. goto fail0;
  52. }
  53. if (!NCDVal_IsList(target_arg)) {
  54. ModuleLog(i, BLOG_ERROR, "argument is not a list");
  55. goto fail0;
  56. }
  57. size_t num_names = NCDVal_ListCount(target_arg);
  58. if (num_names == 0) {
  59. ModuleLog(i, BLOG_ERROR, "target list is empty");
  60. goto fail0;
  61. }
  62. size_t total_names = is_arg + num_names;
  63. NCD_string_id_t *names = BAllocArray(total_names, sizeof(*names));
  64. if (!names) {
  65. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  66. goto fail0;
  67. }
  68. if (is_arg) {
  69. names[0] = NCD_STRING_CALLER;
  70. }
  71. for (size_t j = 0; j < num_names; j++) {
  72. NCDValRef name_val = NCDVal_ListGet(target_arg, j);
  73. if (!NCDVal_IsString(name_val)) {
  74. ModuleLog(i, BLOG_ERROR, "target component is not a string");
  75. goto fail1;
  76. }
  77. NCD_string_id_t name_id = ncd_get_string_id(name_val, i->params->iparams->string_index);
  78. if (name_id < 0) {
  79. ModuleLog(i, BLOG_ERROR, "ncd_get_string_id failed");
  80. goto fail1;
  81. }
  82. names[is_arg + j] = name_id;
  83. }
  84. NCDObject first_obj;
  85. if (!NCDModuleInst_Backend_GetObj(i, names[0], &first_obj)) {
  86. ModuleLog(i, BLOG_ERROR, "first target object not found");
  87. goto fail1;
  88. }
  89. NCDObject obj;
  90. if (!NCDObject_ResolveObjExprCompact(&first_obj, names + 1, total_names - 1, &obj)) {
  91. ModuleLog(i, BLOG_ERROR, "non-first target object not found");
  92. goto fail1;
  93. }
  94. NCDPersistentObj *pobj = NCDObject_Pobj(&obj);
  95. if (!pobj) {
  96. ModuleLog(i, BLOG_ERROR, "object does not support references");
  97. goto fail1;
  98. }
  99. NCDObjRef_Init(&o->ref, pobj);
  100. BFree(names);
  101. NCDModuleInst_Backend_Up(i);
  102. return;
  103. fail1:
  104. BFree(names);
  105. fail0:
  106. NCDModuleInst_Backend_DeadError(i);
  107. }
  108. static void func_new_objref (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  109. {
  110. func_new_common(vo, i, params, 0);
  111. }
  112. static void func_new_objref_arg (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  113. {
  114. func_new_common(vo, i, params, 1);
  115. }
  116. static void func_die (void *vo)
  117. {
  118. struct instance *o = vo;
  119. NCDObjRef_Free(&o->ref);
  120. NCDModuleInst_Backend_Dead(o->i);
  121. }
  122. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  123. {
  124. struct instance *o = vo;
  125. NCDObject obj;
  126. if (!NCDObjRef_Deref(&o->ref, &obj)) {
  127. ModuleLog(o->i, BLOG_ERROR, "object reference has been broken");
  128. return 0;
  129. }
  130. if (name == NCD_STRING_EMPTY) {
  131. *out_object = obj;
  132. return 1;
  133. }
  134. return NCDObject_GetObj(&obj, name, out_object);
  135. }
  136. static struct NCDModule modules[] = {
  137. {
  138. .type = "objref",
  139. .func_new2 = func_new_objref,
  140. .func_die = func_die,
  141. .func_getobj = func_getobj,
  142. .alloc_size = sizeof(struct instance)
  143. }, {
  144. .type = "objref_arg",
  145. .func_new2 = func_new_objref_arg,
  146. .func_die = func_die,
  147. .func_getobj = func_getobj,
  148. .alloc_size = sizeof(struct instance)
  149. }, {
  150. .type = NULL
  151. }
  152. };
  153. const struct NCDModuleGroup ncdmodule_objref = {
  154. .modules = modules
  155. };