slist.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <precomp.h>
  2. #include "slist.h"
  3. #include <api/script/script.h>
  4. #include <api/script/scriptmgr.h>
  5. ListScriptController _listController;
  6. ListScriptController *listController=&_listController;
  7. // -- Functions table -------------------------------------
  8. function_descriptor_struct ListScriptController::exportedFunction[] = {
  9. {L"addItem", 1, (void*)SList::script_vcpu_addItem },
  10. {L"removeItem", 1, (void*)SList::script_vcpu_removeItem },
  11. {L"enumItem", 1, (void*)SList::script_vcpu_enumItem },
  12. {L"findItem2", 2, (void*)SList::script_vcpu_findItem2 },
  13. {L"findItem", 1, (void*)SList::script_vcpu_findItem },
  14. {L"getNumItems", 0, (void*)SList::script_vcpu_getNumItems },
  15. {L"removeAll", 0, (void*)SList::script_vcpu_removeAll },
  16. };
  17. // --------------------------------------------------------
  18. const wchar_t *ListScriptController::getClassName() {
  19. return L"List";
  20. }
  21. const wchar_t *ListScriptController::getAncestorClassName() {
  22. return L"Object";
  23. }
  24. ScriptObject *ListScriptController::instantiate() {
  25. SList *l = new SList;
  26. ASSERT(l != NULL);
  27. return l->getScriptObject();
  28. }
  29. void ListScriptController::destroy(ScriptObject *o) {
  30. SList *obj = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  31. ASSERT(obj != NULL);
  32. delete obj;
  33. }
  34. void *ListScriptController::encapsulate(ScriptObject *o) {
  35. return NULL; // no encapsulation for list yet
  36. }
  37. void ListScriptController::deencapsulate(void *o) {
  38. }
  39. int ListScriptController::getNumFunctions() {
  40. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  41. }
  42. const function_descriptor_struct *ListScriptController::getExportedFunctions() {
  43. return exportedFunction;
  44. }
  45. GUID ListScriptController::getClassGuid() {
  46. return slistGuid;
  47. }
  48. //------------------------------------------------------------------------
  49. SList::SList() {
  50. getScriptObject()->vcpu_setInterface(slistGuid, (void *)static_cast<SList *>(this));
  51. getScriptObject()->vcpu_setClassName(L"List");
  52. getScriptObject()->vcpu_setController(listController);
  53. }
  54. SList::~SList() {
  55. }
  56. TList<scriptVar> *SList::getTList() {
  57. return &list;
  58. }
  59. scriptVar SList::script_vcpu_enumItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i) {
  60. SCRIPT_FUNCTION_INIT;
  61. ASSERT(SOM::isNumeric(&i));
  62. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  63. if (l) return l->getTList()->enumItem(SOM::makeInt(&i));
  64. RETURN_SCRIPT_ZERO;
  65. }
  66. scriptVar SList::script_vcpu_addItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar obj) {
  67. SCRIPT_FUNCTION_INIT;
  68. scriptVar dup;
  69. dup = SOM::makeVar(obj.type);
  70. SOM::assignPersistent(&dup, &obj);
  71. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  72. if (l) l->getTList()->addItem(dup);
  73. RETURN_SCRIPT_VOID;
  74. }
  75. scriptVar SList::script_vcpu_removeItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar i) {
  76. SCRIPT_FUNCTION_INIT;
  77. ASSERT(SOM::isNumeric(&i));
  78. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  79. if (l) {
  80. scriptVar dup = l->getTList()->enumItem(SOM::makeInt(&i));
  81. if (dup.type == SCRIPT_STRING) {
  82. if (dup.data.odata) FREE(dup.data.odata);
  83. }
  84. l->getTList()->delByPos(SOM::makeInt(&i));
  85. }
  86. RETURN_SCRIPT_VOID;
  87. }
  88. scriptVar SList::script_vcpu_getNumItems(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  89. SCRIPT_FUNCTION_INIT;
  90. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  91. if (l) return MAKE_SCRIPT_INT(l->getTList()->getNumItems());
  92. return MAKE_SCRIPT_INT(0);
  93. }
  94. scriptVar SList::script_vcpu_findItem2(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar obj, scriptVar start) {
  95. SCRIPT_FUNCTION_INIT;
  96. ASSERT(SOM::isNumeric(&start));
  97. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  98. if (l) {
  99. if (obj.type != SCRIPT_STRING) {
  100. for (int i=SOM::makeInt(&start);i<l->getTList()->getNumItems();i++) {
  101. if (!MEMCMP(&l->getTList()->enumItem(i), &obj, sizeof(scriptVar))) {
  102. return MAKE_SCRIPT_INT(i);
  103. }
  104. }
  105. } else {
  106. for (int i=SOM::makeInt(&start);i<l->getTList()->getNumItems();i++) {
  107. if (!wcscmp(l->getTList()->enumItem(i).data.sdata, obj.data.sdata)) {
  108. return MAKE_SCRIPT_INT(i);
  109. }
  110. }
  111. }
  112. }
  113. return MAKE_SCRIPT_INT(-1);
  114. }
  115. scriptVar SList::script_vcpu_findItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar obj) {
  116. SCRIPT_FUNCTION_INIT;
  117. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  118. if (l) {
  119. if (obj.type != SCRIPT_STRING) {
  120. for (int i=0;i<l->getTList()->getNumItems();i++) {
  121. if (!MEMCMP(&l->getTList()->enumItem(i), &obj, sizeof(scriptVar))) {
  122. return MAKE_SCRIPT_INT(i);
  123. }
  124. }
  125. } else {
  126. for (int i=0;i<l->getTList()->getNumItems();i++) {
  127. if (!wcscmp(l->getTList()->enumItem(i).data.sdata, obj.data.sdata)) {
  128. return MAKE_SCRIPT_INT(i);
  129. }
  130. }
  131. }
  132. }
  133. return MAKE_SCRIPT_INT(-1);
  134. }
  135. scriptVar SList::script_vcpu_removeAll(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  136. SCRIPT_FUNCTION_INIT;
  137. SList *l = static_cast<SList *>(o->vcpu_getInterface(slistGuid));
  138. if (l) {
  139. for (int i=0;i<l->getTList()->getNumItems();i++) {
  140. scriptVar v = l->getTList()->enumItem(i);
  141. if (v.type == SCRIPT_STRING)
  142. FREE((wchar_t *)v.data.sdata);
  143. }
  144. l->getTList()->removeAll();
  145. }
  146. RETURN_SCRIPT_VOID;
  147. }