SayoriOS 0.3.3

jse_array.c
1 #include "portability.h"
3 #include <io/ports.h>
4 #include "io/tty.h"
5 #include "lib/stdio.h"
6
7 #include "../elk_config.h"
8 #include "../elk.h"
9
10 JSE_ARRAY JSE_GLOBAL_ARRAY;
11
17 JSE_ARRAY jse_array_create() {
18 JSE_ARRAY* arr = (JSE_ARRAY*)calloc(1, sizeof(JSE_ARRAY));
19 arr->pairs = (JSE_ARRAY_DATA*)calloc(1, JSE_EXT_ARRAY_INITIAL_SIZE * sizeof(JSE_ARRAY_DATA));
20 arr->size = 0;
21 arr->capacity = JSE_EXT_ARRAY_INITIAL_SIZE;
22 return arr[0];
23 }
24
25
31 JSE_ARRAY* jse_array_link_create() {
32 JSE_ARRAY* arr = (JSE_ARRAY*) calloc(1, sizeof(JSE_ARRAY));
33 arr->pairs = (JSE_ARRAY_DATA*) calloc(1, JSE_EXT_ARRAY_INITIAL_SIZE * sizeof(JSE_ARRAY_DATA));
34 arr->size = 0;
35 arr->capacity = JSE_EXT_ARRAY_INITIAL_SIZE;
36 return arr;
37 }
44 void jse_array_dump(JSE_ARRAY arr, int space){
45 char* sp = (char*) calloc(1, (sizeof(char) * (space + 1)));
46 memset(sp,' ', space);
47 for (int i = 0; i < arr.size; i++) {
48 if (arr.pairs[i].type == JSE_ARRAY_TYPE_INT) {
49 printf("%s[%d] Key: %s, Value (INT): %d\n",sp, i, arr.pairs[i].key, arr.pairs[i].value.int_value);
50 } else if (arr.pairs[i].type == JSE_ARRAY_TYPE_STRING) {
51 printf("%s[%d] Key: %s, Value (STRING): %s\n",sp, i, arr.pairs[i].key, arr.pairs[i].value.str_value);
52 } else if (arr.pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
53 printf("%s[%d] Key: %s, Value (ARRAY): Size %d\n",sp, i, arr.pairs[i].key, arr.pairs[i].value.array_value->size);
54 jse_array_dump(arr.pairs[i].value.array_value[0],space + 4);
55 }
56 }
57 free(sp);
58 }
59
66 void jse_array_link_dump(JSE_ARRAY* arr, int space){
67 char* sp = (char*)calloc(1, sizeof(char) * (space + 1));
68 memset(sp,' ', space);
69 for (int i = 0; i < arr->size; i++) {
70 if (arr->pairs[i].type == JSE_ARRAY_TYPE_INT) {
71 printf("%s[%d] Key: %s, Value (INT): %d\n",sp, i, arr->pairs[i].key, arr->pairs[i].value.int_value);
72 } else if (arr->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
73 printf("%s[%d] Key: %s, Value (STRING): %s\n",sp, i, arr->pairs[i].key, arr->pairs[i].value.str_value);
74 } else if (arr->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
75 printf("%s[%d] Key: %s, Value (ARRAY): Size %d\n",sp, i, arr->pairs[i].key, arr->pairs[i].value.array_value->size);
76 jse_array_dump(arr->pairs[i].value.array_value[0],space + 4);
77 }
78 }
79 free(sp);
80 }
81
88 void jse_array_value_dump(JSE_ARRAY_DATA data, int space){
89 if (data.type == JSE_ARRAY_TYPE_NULL) {
90 printf("[JSE] [Array] [DUMP] [VALUE] Invalid data\n");
91 return;
92 }
93 char* sp = (char*) calloc(1, sizeof(char) * (space + 1));
94 memset(sp,' ', space);
95 printf("[JSE] [Array] [DUMP] [VALUE] Success!\n%s Key: %s\n", sp, data.key);
96 printf("%s |--- Type: %d\n",sp, data.type);
97 if (data.type == JSE_ARRAY_TYPE_INT){
98 printf("%s |--- Value: %d\n",sp, data.value.int_value);
99 } else if (data.type == JSE_ARRAY_TYPE_STRING){
100 printf("%s |--- Value: %s\n",sp, data.value.str_value);
101 } else if (data.type == JSE_ARRAY_TYPE_ARRAY){
102 printf("%s |--- Value: (Array)\n",sp);
103 jse_array_dump(data.value.array_value[0],space + 4);
104
105 }
106 free(sp);
107 }
108
117 void jse_array_push(JSE_ARRAY* arr, const char* key, JSE_ARRAY_VALUE value, JSE_ARRAY_TYPE type) {
118 if (arr->size >= arr->capacity) {
119 arr->capacity *= 2;
120 arr->pairs = (JSE_ARRAY_DATA*)realloc(arr->pairs, arr->capacity * sizeof(JSE_ARRAY_DATA));
121 }
122 arr->pairs[arr->size].key = jse_strdup(key);
123 arr->pairs[arr->size].value = value;
124 arr->pairs[arr->size].type = type;
125 arr->size++;
126 }
127
128
137 void jse_array_editByID(JSE_ARRAY* arr, int Index, JSE_ARRAY_VALUE value, JSE_ARRAY_TYPE type) {
138 if (Index >= 0 && Index < arr->size) {
139 JSE_ARRAY_VALUE val = arr->pairs[Index].value;
140 if (arr->pairs[Index].type == JSE_ARRAY_TYPE_ARRAY){
141 jse_array_destroZ(val.array_value, 0);
142 } else if (arr->pairs[Index].type == JSE_ARRAY_TYPE_STRING){
143 free(val.str_value);
144 }
145 arr->pairs[Index].value = value;
146 arr->pairs[Index].type = type;
147 } else {
148 qemu_err("Error: Index out of bounds\n");
149 }
150 }
151
160 JSE_ARRAY_DATA jse_array_getByKey(JSE_ARRAY* arr, const char* key) {
161 for (int i = 0; i < arr->size; i++) {
162 if (strcmp(arr->pairs[i].key, key) == 0) {
163 //jse_array_value_dump(arr->pairs[i], 4);
164 return arr->pairs[i];
165 //return arr->pairs[i].value;
166 }
167 }
168 JSE_ARRAY_DATA jse_arr = {};
169 return jse_arr; // Возврат, если ключ не найден
170 }
171
180 JSE_ARRAY_DATA jse_array_getByIndex(JSE_ARRAY* arr, int index) {
181 if (index >= 0 && index < arr->size) {
182 return arr->pairs[index];
183 }
184
185 JSE_ARRAY_DATA jse_arr = {};
186 return jse_arr;
187 }
188
194 void jse_array_free(JSE_ARRAY* arr) {
195 for (int i = 0; i < arr->size; i++) {
196 //qemu_note("[Array] 1.Free %x",arr->pairs[i].key);
197 free(arr->pairs[i].key);
198 if (arr->pairs[i].type == JSE_ARRAY_TYPE_NULL ||
199 arr->pairs[i].type != JSE_ARRAY_TYPE_STRING ||
200 arr->pairs[i].type != JSE_ARRAY_TYPE_INT ||
201 arr->pairs[i].type != JSE_ARRAY_TYPE_ARRAY
202 ){
203 qemu_log("[Array] Skipping data");
204 continue;
205 }
206 if (arr->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
207 free(arr->pairs[i].value.str_value);
208 } else if (arr->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
209 // Дополнительная логика для освобождения памяти массива
210 qemu_note("[Array] In %x",arr->pairs[i].value.array_value);
211 jse_array_free(arr->pairs[i].value.array_value);
212 }
213 }
214 qemu_note("[Array] 2.Free %x",arr->pairs);
215 free(arr->pairs);
216 arr->size = 0;
217 arr->capacity = 0;
218 qemu_note("[Array] 3.Free %x",arr);
219 free(arr);
220 }
230 JSE_ARRAY jse_array_change_key_case(const JSE_ARRAY* orig, int uppercase) {
231 JSE_ARRAY new_arr = jse_array_create();
232
233 for (int i = 0; i < orig->size; i++) {
234 char* new_key = jse_strdup(orig->pairs[i].key);
235 (uppercase ?jse_func_toupper(new_key):jse_func_tolower(new_key));
236 jse_array_push(&new_arr, new_key, orig->pairs[i].value, orig->pairs[i].type);
237 }
238
239 return new_arr;
240 }
251 void jse_array_fill(JSE_ARRAY* arr, int start_index, int count, JSE_ARRAY_VALUE value, JSE_ARRAY_TYPE type) {
252 for (int i = start_index; i < start_index + count; i++) {
253 if (i < arr->size) {
254 if (arr->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
255 free(arr->pairs[i].value.str_value);
256 } else if (arr->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
257 jse_array_free(arr->pairs[i].value.array_value);
258 }
259 arr->pairs[i].value = value;
260 arr->pairs[i].type = type;
261 } else {
262 if (arr->size >= arr->capacity) {
263 arr->capacity *= 2;
264 arr->pairs = (JSE_ARRAY_DATA*)realloc(arr->pairs, arr->capacity * sizeof(JSE_ARRAY_DATA));
265 }
266 arr->pairs[i].key = jse_strdup("");
267 arr->pairs[i].value = value;
268 arr->pairs[i].type = type;
269 arr->size++;
270 }
271 }
272 }
282 JSE_ARRAY jse_array_diff(JSE_ARRAY* array, JSE_ARRAY* other_array) {
283 JSE_ARRAY diff_array = jse_array_create(); // Создаем массив для результатов
284
285 for (int i = 0; i < array->size; i++) {
286 int found_in_other = 0;
287 for (int j = 0; j < other_array->size; j++) {
288 if (array->pairs[i].type == other_array->pairs[j].type) {
289 if (array->pairs[i].type == JSE_ARRAY_TYPE_INT) {
290 if (array->pairs[i].value.int_value == other_array->pairs[j].value.int_value) {
291 found_in_other = 1;
292 break;
293 }
294 } else if (array->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
295 if (strcmp(array->pairs[i].value.str_value, other_array->pairs[j].value.str_value) == 0) {
296 found_in_other = 1;
297 break;
298 }
299 } else if (array->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
300 // Рекурсивное сравнение для массивов
301 JSE_ARRAY nested_diff = jse_array_diff(array->pairs[i].value.array_value, other_array->pairs[j].value.array_value);
302 if (nested_diff.size > 0) {
303 found_in_other = 1;
304 jse_array_push(&diff_array, array->pairs[i].key, array->pairs[i].value, array->pairs[i].type);
305 }
306 }
307 }
308 }
309 if (!found_in_other) {
310 jse_array_push(&diff_array, array->pairs[i].key, array->pairs[i].value, array->pairs[i].type);
311 }
312 }
313
314 return diff_array;
315 }
320 void jse_array_test(){
321 // Создаем два массива для сравнения
322 JSE_ARRAY array1 = jse_array_create();
323 jse_array_push(&array1, "age", (JSE_ARRAY_VALUE){.int_value = 30}, JSE_ARRAY_TYPE_INT);
324 jse_array_push(&array1, "name", (JSE_ARRAY_VALUE){.str_value = jse_strdup("John")}, JSE_ARRAY_TYPE_STRING);
325
326
327 // Первый вложенный массив
328 JSE_ARRAY nested_array1 = jse_array_create();
329 jse_array_push(&nested_array1, "item1", (JSE_ARRAY_VALUE){.int_value = 10}, JSE_ARRAY_TYPE_INT);
330 jse_array_push(&nested_array1, "item2", (JSE_ARRAY_VALUE){.str_value = jse_strdup("nested")}, JSE_ARRAY_TYPE_STRING);
331 jse_array_push(&array1, "nested", (JSE_ARRAY_VALUE){.array_value = &nested_array1}, JSE_ARRAY_TYPE_ARRAY);
332
333 JSE_ARRAY array2 = jse_array_create();
334 jse_array_push(&array2, "age", (JSE_ARRAY_VALUE){.int_value = 30}, JSE_ARRAY_TYPE_INT);
335 jse_array_push(&array2, "name", (JSE_ARRAY_VALUE){.str_value = jse_strdup("Jane")}, JSE_ARRAY_TYPE_STRING);
336
337 // Первый вложенный массив
338 JSE_ARRAY nested_array2 = jse_array_create();
339 jse_array_push(&nested_array2, "item1", (JSE_ARRAY_VALUE){.int_value = 10}, JSE_ARRAY_TYPE_INT);
340 jse_array_push(&nested_array2, "item2", (JSE_ARRAY_VALUE){.str_value = jse_strdup("nested")}, JSE_ARRAY_TYPE_STRING);
341 jse_array_push(&nested_array2, "item3", (JSE_ARRAY_VALUE){.str_value = jse_strdup("new")}, JSE_ARRAY_TYPE_STRING);
342 jse_array_push(&array2, "nested", (JSE_ARRAY_VALUE){.array_value = &nested_array2}, JSE_ARRAY_TYPE_ARRAY);
343
344 jse_array_value_dump(jse_array_getByKey(&array2, "nested"),4);
345 jse_array_value_dump(jse_array_getByKey(&array2, "i5ufjk4uj"),8);
346
347 // Выполняем сравнение массивов
348 JSE_ARRAY diff = jse_array_diff(&array1, &array2);
349
350 // Выводим результаты сравнения
351 printf("Difference between array1 and array2:\n");
352 jse_array_dump(diff,4);
353
354 // Освобождаем память
355 jse_array_free(&array1);
356 jse_array_free(&array2);
357 jse_array_free(&diff);
358 }
359
360
361 jsval_t jse_ext_array(struct js *js, jsval_t *args, int nargs) {
362
363 qemu_log("JGA size: %d ",JSE_GLOBAL_ARRAY.size);
364
365 // Создаем буфер
366 JSE_ARRAY* arr = (JSE_ARRAY*) calloc(1, sizeof(JSE_ARRAY));
367 arr->pairs = (JSE_ARRAY_DATA*) calloc(1, JSE_EXT_ARRAY_INITIAL_SIZE * sizeof(JSE_ARRAY_DATA));
368 arr->size = 0;
369 arr->capacity = JSE_EXT_ARRAY_INITIAL_SIZE;
370
371 qemu_log("Created array: %x ",arr);
372 jse_array_push(&JSE_GLOBAL_ARRAY, "__GLOBAL__", (JSE_ARRAY_VALUE){.array_value = arr}, JSE_ARRAY_TYPE_ARRAY);
373
374 qemu_log("JGA size: %d ",JSE_GLOBAL_ARRAY.size);
375 qemu_log("ARR size: %d ",arr->size);
376
377 return js_mknum(JSE_GLOBAL_ARRAY.size - 1);
378 }
379
380 jsval_t jse_ext_array_push(struct js *js, jsval_t *args, int nargs) {
381 //qemu_note("[NOTE] jse_ext_array_push");
382
383 if (nargs < 3) return js_mkerr(js,"%n args are required.", 3);
384 int Index = jse_getInt(js,args[0]);
385 const char* Key = js_str(js,args[1]);
386 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
387
388 if (data.type != JSE_ARRAY_TYPE_ARRAY){
389 return js_mkerr(js,"This element is not an array.");
390 }
391 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
392
393 JSE_ARRAY_VALUE val = data.value;
394 JSE_ARRAY* arr = val.array_value;
395
396 //qemu_note("[NOTE] Test dump! Size:%d | Capacity:%d", arr->size, arr->capacity);
397
398 int val_type = js_type(args[2]);
399
400 if (val_type == JS_NUM){
401 jse_array_push(arr, Key, (JSE_ARRAY_VALUE){.int_value = js_getnum(args[2])}, JSE_ARRAY_TYPE_INT);
402 } else if (val_type == JS_STR) {
403 const char* Value = js_str(js,args[2]);
404 jse_array_push(arr, Key, (JSE_ARRAY_VALUE){.str_value = jse_strdup(Value)}, JSE_ARRAY_TYPE_STRING);
405 } else if (val_type == JS_UNDEF || val_type == JS_FALSE || val_type == JS_NULL){
406 jse_array_push(arr, Key, (JSE_ARRAY_VALUE){.int_value = 0}, JSE_ARRAY_TYPE_INT);
407 } else if (val_type == JS_TRUE){
408 jse_array_push(arr, Key, (JSE_ARRAY_VALUE){.int_value = 1}, JSE_ARRAY_TYPE_INT);
409 } else {
410 qemu_note("Undetected type: %x", val_type);
411 }
412
413 return js_mknum(arr->size - 1);
414 }
415
416
417 jsval_t jse_ext_array_editByID(struct js *js, jsval_t *args, int nargs) {
418 //qemu_note("[NOTE] jse_ext_array_push");
419
420 if (nargs < 3) return js_mkerr(js,"%n args are required.", 3);
421 int Index = jse_getInt(js,args[0]);
422 int Key = jse_getInt(js,args[1]);
423 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
424
425 if (data.type != JSE_ARRAY_TYPE_ARRAY){
426 return js_mkerr(js,"This element is not an array.");
427 }
428 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
429
430 JSE_ARRAY_VALUE val = data.value;
431 JSE_ARRAY* arr = val.array_value;
432
433 //qemu_note("[NOTE] Test dump! Size:%d | Capacity:%d", arr->size, arr->capacity);
434
435 int val_type = js_type(args[2]);
436
437 if (val_type == JS_NUM){
438 jse_array_editByID(arr, Key, (JSE_ARRAY_VALUE){.int_value = js_getnum(args[2])}, JSE_ARRAY_TYPE_INT);
439 } else if (val_type == JS_STR) {
440 const char* Value = js_str(js,args[2]);
441 jse_array_editByID(arr, Key, (JSE_ARRAY_VALUE){.str_value = jse_strdup(Value)}, JSE_ARRAY_TYPE_STRING);
442 } else if (val_type == JS_UNDEF || val_type == JS_FALSE || val_type == JS_NULL){
443 jse_array_editByID(arr, Key, (JSE_ARRAY_VALUE){.int_value = 0}, JSE_ARRAY_TYPE_INT);
444 } else if (val_type == JS_TRUE){
445 jse_array_editByID(arr, Key, (JSE_ARRAY_VALUE){.int_value = 1}, JSE_ARRAY_TYPE_INT);
446 } else {
447 qemu_note("Undetected type: %x", val_type);
448 }
449
450 return js_mkundef();
451 }
452
453
454 jsval_t jse_ext_array_getByKey(struct js *js, jsval_t *args, int nargs) {
455 //qemu_note("[NOTE] jse_ext_array_getByKey");
456
457 if (nargs < 2) return js_mkerr(js,"%n args are required.", 2);
458 int Index = jse_getInt(js,args[0]);
459 const char* Key = js_str(js,args[1]);
460 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
461
462 if (data.type != JSE_ARRAY_TYPE_ARRAY){
463 return js_mkerr(js,"This element is not an array.");
464 }
465 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
466
467 JSE_ARRAY_VALUE val = data.value;
468 JSE_ARRAY* arr = val.array_value;
469
470 //qemu_note("[NOTE] Test dump! Size:%d | Capacity:%d", arr->size, arr->capacity);
471
472 JSE_ARRAY_DATA a_data = jse_array_getByKey(arr, Key);
473 if (a_data.type == JSE_ARRAY_TYPE_NULL) return js_mkundef();
474 if (a_data.type == JSE_ARRAY_TYPE_ARRAY) return js_mkstr(js,"(Array)",strlen("(Array)"));
475 JSE_ARRAY_VALUE a_val = a_data.value;
476 if (a_data.type == JSE_ARRAY_TYPE_STRING) return js_mkstr(js,a_val.str_value,strlen("a_val.str_value"));
477 if (a_data.type == JSE_ARRAY_TYPE_INT) {
478 char* buf = calloc(1, 64);
479 itoa(a_val.int_value, buf);
480 jsval_t ret = js_mkstr(js, buf, strlen(buf));
481 free(buf);
482 return ret;
483 }
484 return js_mkundef();
485 }
486
487
488 jsval_t jse_ext_array_getByIndex(struct js *js, jsval_t *args, int nargs) {
489 //qemu_note("[NOTE] jse_ext_array_getByIndex");
490 if (nargs < 2) return js_mkerr(js,"%n args are required.", 2);
491 int Index = jse_getInt(js,args[0]);
492 int Key = jse_getInt(js,args[1]);
493 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
494
495 if (data.type != JSE_ARRAY_TYPE_ARRAY){
496 qemu_err("It's no ARRAY!");
497 return js_mkfalse();
498 }
499 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
500
501 JSE_ARRAY_VALUE val = data.value;
502 JSE_ARRAY* arr = val.array_value;
503
504 //qemu_note("[NOTE] Test dump! Size:%d | Capacity:%d", arr->size, arr->capacity);
505
506 JSE_ARRAY_DATA a_data = jse_array_getByIndex(arr, Key);
507 if (a_data.type == JSE_ARRAY_TYPE_NULL) return js_mkundef();
508 if (a_data.type == JSE_ARRAY_TYPE_ARRAY) return js_mkstr(js,"(Array)",strlen("(Array)"));
509 JSE_ARRAY_VALUE a_val = a_data.value;
510 if (a_data.type == JSE_ARRAY_TYPE_STRING) return js_mkstr(js,a_val.str_value,strlen("a_val.str_value"));
511 if (a_data.type == JSE_ARRAY_TYPE_INT) {
512 char* buf = calloc(1, 64);
513 itoa(a_val.int_value, buf);
514 jsval_t ret = js_mkstr(js, buf, strlen(buf));
515 free(buf);
516 return ret;
517 }
518 return js_mkundef();
519 }
520
521 jsval_t jse_ext_array_length(struct js *js, jsval_t *args, int nargs) {
522 //qemu_note("[NOTE] jse_ext_array_getByIndex");
523 if (nargs < 1) return js_mkerr(js,"%n args are required.", 1);
524 int Index = jse_getInt(js,args[0]);
525 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
526
527 if (data.type != JSE_ARRAY_TYPE_ARRAY){
528 qemu_err("It's no ARRAY!");
529 return js_mkfalse();
530 }
531 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
532
533 JSE_ARRAY_VALUE val = data.value;
534 JSE_ARRAY* arr = val.array_value;
535
536 //qemu_note("[NOTE] Test dump! Size:%d | Capacity:%d", arr->size, arr->capacity);
537
538 return js_mknum(arr->size);
539 }
540
541 jsval_t jse_ext_array_dump(struct js *js, jsval_t *args, int nargs) {
542 //qemu_note("[NOTE] jse_ext_array_getByIndex");
543 if (nargs < 1) return js_mkerr(js,"%n args are required.", 1);
544 int Index = jse_getInt(js,args[0]);
545 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
546
547 if (data.type != JSE_ARRAY_TYPE_ARRAY){
548 qemu_err("It's no ARRAY!");
549 return js_mkfalse();
550 }
551 //qemu_note("Array => Index: %d | Type: %d",Index, data.type);
552
553 JSE_ARRAY_VALUE val = data.value;
554 JSE_ARRAY* arr = val.array_value;
555
556 jse_array_link_dump(arr,0);
557
558 return js_mkundef();
559 }
560
561 jsval_t jse_ext_array_diff(struct js *js, jsval_t *args, int nargs) {
562 //qemu_note("[NOTE] jse_ext_array_getByIndex");
563 if (nargs < 2) return js_mkerr(js,"%n args are required.", 2);
564 int Index = jse_getInt(js,args[0]);
565 int Index2 = jse_getInt(js,args[1]);
566 JSE_ARRAY_DATA data = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index);
567
568 if (data.type != JSE_ARRAY_TYPE_ARRAY){
569 qemu_err("#1 It's no ARRAY!");
570 return js_mkfalse();
571 }
572
573 JSE_ARRAY_DATA data2 = jse_array_getByIndex(&JSE_GLOBAL_ARRAY, Index2);
574
575 if (data2.type != JSE_ARRAY_TYPE_ARRAY){
576 qemu_err("#2 It's no ARRAY!");
577 return js_mkfalse();
578 }
579
580 JSE_ARRAY_VALUE val1 = data.value;
581 JSE_ARRAY* array = val1.array_value;
582
583 JSE_ARRAY_VALUE val2 = data2.value;
584 JSE_ARRAY* other_array = val2.array_value;
585
587
588 // Создаем буфер
589 JSE_ARRAY* diff_array = (JSE_ARRAY*)calloc(1, sizeof(JSE_ARRAY));
590 diff_array->pairs = (JSE_ARRAY_DATA*)calloc(1, JSE_EXT_ARRAY_INITIAL_SIZE * sizeof(JSE_ARRAY_DATA));
591 diff_array->size = 0;
592 diff_array->capacity = JSE_EXT_ARRAY_INITIAL_SIZE;
593
594 qemu_log("Created array: %x ",diff_array);
595 jse_array_push(&JSE_GLOBAL_ARRAY, "__GLOBAL__", (JSE_ARRAY_VALUE){.array_value = diff_array}, JSE_ARRAY_TYPE_ARRAY);
596
597 for (int i = 0; i < array->size; i++) {
598 int found_in_other = 0;
599 for (int j = 0; j < other_array->size; j++) {
600 if (array->pairs[i].type == other_array->pairs[j].type) {
601 if (array->pairs[i].type == JSE_ARRAY_TYPE_INT) {
602 if (array->pairs[i].value.int_value == other_array->pairs[j].value.int_value) {
603 found_in_other = 1;
604 break;
605 }
606 } else if (array->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
607 if (strcmp(array->pairs[i].value.str_value, other_array->pairs[j].value.str_value) == 0) {
608 found_in_other = 1;
609 break;
610 }
611 } else if (array->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
612 // Рекурсивное сравнение для массивов
613 JSE_ARRAY nested_diff = jse_array_diff(array->pairs[i].value.array_value, other_array->pairs[j].value.array_value);
614 if (nested_diff.size > 0) {
615 found_in_other = 1;
616 jse_array_push(&diff_array, array->pairs[i].key, array->pairs[i].value, array->pairs[i].type);
617 }
618 }
619 }
620 }
621 if (!found_in_other) {
622 jse_array_push(diff_array, array->pairs[i].key, array->pairs[i].value, array->pairs[i].type);
623 }
624 }
625
627
628 return js_mknum(JSE_GLOBAL_ARRAY.size - 1);
629 }
630
636 void jse_array_config(struct js* js){
637 qemu_note("[JSE] [EXT] [Array] Registration of functions");
638 JSE_GLOBAL_ARRAY = jse_array_create();
639 js_set(js, js_glob(js), "array", js_mkfun(jse_ext_array));
640 js_set(js, js_glob(js), "array_push", js_mkfun(jse_ext_array_push));
641 js_set(js, js_glob(js), "array_getByKey", js_mkfun(jse_ext_array_getByKey));
642 js_set(js, js_glob(js), "array_getByIndex", js_mkfun(jse_ext_array_getByIndex));
643 js_set(js, js_glob(js), "array_length", js_mkfun(jse_ext_array_length));
644 js_set(js, js_glob(js), "array_dump", js_mkfun(jse_ext_array_dump));
645 js_set(js, js_glob(js), "array_diff", js_mkfun(jse_ext_array_diff));
646 js_set(js, js_glob(js), "array_editByID", js_mkfun(jse_ext_array_editByID));
647
649 }
650
651 void jse_array_destroZ(JSE_ARRAY* array, int space){
652 char* sp = (char*)calloc(1, sizeof(char) * (space + 1));
653 memset(sp,' ', space);
654 sp[space] = 0;
655
656 qemu_note("%s|--- Size:%d | Capacity:%d", sp, array->size, array->capacity);
657 for (int i = 0; i < array->size; i++){
658 qemu_note("%s|--- Delete data %d / %d",sp, i + 1, array->size);
659 if (array->pairs[i].type != JSE_ARRAY_TYPE_STRING &&
660 array->pairs[i].type != JSE_ARRAY_TYPE_INT &&
661 array->pairs[i].type != JSE_ARRAY_TYPE_ARRAY){
662 qemu_log(" %s |--- Skipping data (Fantom type: %x)", sp, array->pairs[i].type);
663 continue;
664 }
665 free(array->pairs[i].key);
666 qemu_ok(" %s |--- Delete key",sp);
667 if (array->pairs[i].type == JSE_ARRAY_TYPE_STRING) {
668 free(array->pairs[i].value.str_value);
669 qemu_ok(" %s |--- Delete string",sp);
670 } else if (array->pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
671 // Дополнительная логика для освобождения памяти массива
672 // Игнорируется, тк данный участок будет позже отчищен автоматический
673 qemu_note("%s |--- [IGN] GOTO %x",sp , array->pairs[i].value.array_value);
674 //jse_array_destroZ(array->pairs[i].value.array_value, sp + 8);
675 }
676 }
677 qemu_ok(" %s|--- Delete pars",sp);
678 free(array->pairs);
679 qemu_ok(" %s|--- Delete array",sp);
680 free(array);
681 free(sp);
682 }
683
684 void jse_array_destroy(struct js* js){
685 //qemu_err("[JSE] [EXT] [Array] [Destroy] DISABLED!!!!!");
686 //return;
687 qemu_note("[JSE] [EXT] [Array] Destroy");
688
689 qemu_note(" |--- Size:%d | Capacity:%d", JSE_GLOBAL_ARRAY.size, JSE_GLOBAL_ARRAY.capacity);
690 for (int i = 0; i < JSE_GLOBAL_ARRAY.size; i++){
691 qemu_note(" |--- Delete data %d / %d", i + 1, JSE_GLOBAL_ARRAY.size);
692 free(JSE_GLOBAL_ARRAY.pairs[i].key);
693 qemu_ok(" |--- Delete key");
694 if (JSE_GLOBAL_ARRAY.pairs[i].type == JSE_ARRAY_TYPE_STRING) {
695 free(JSE_GLOBAL_ARRAY.pairs[i].value.str_value);
696 qemu_ok(" |--- Delete string");
697 } else if (JSE_GLOBAL_ARRAY.pairs[i].type == JSE_ARRAY_TYPE_ARRAY) {
698 // Дополнительная логика для освобождения памяти массива
699 qemu_note(" |--- GOTO %x", JSE_GLOBAL_ARRAY.pairs[i].value.array_value);
700 jse_array_destroZ(JSE_GLOBAL_ARRAY.pairs[i].value.array_value, 8);
701 }
702 }
703 qemu_ok(" |--- Delete global pars");
704 free(JSE_GLOBAL_ARRAY.pairs);
705 qemu_ok(" |--- Delete global array");
706 //free(JSE_GLOBAL_ARRAY);
707 }
strlen
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
strcmp
int strcmp(const char *s1, const char *s2)
Сравнение строк
Definition: string.c:253
memset
void * memset(void *ptr, char value, size_t num)
Заполнение массива указанными символами
Definition: string.c:203
itoa
size_t itoa(int32_t n, char *buffer)
Конвертируем число в символы
Definition: string.c:623
JSE_ARRAY_DATA
Данные массива
Definition: jse_array.h:31
JSE_ARRAY_DATA::value
JSE_ARRAY_VALUE value
! Ключ
Definition: jse_array.h:33
JSE_ARRAY_DATA::type
JSE_ARRAY_TYPE type
! Значение
Definition: jse_array.h:34
JSE_ARRAY
Структура массива
Definition: jse_array.h:40
JSE_ARRAY::size
int size
! Данные (ключ,значение)
Definition: jse_array.h:42
JSE_ARRAY::capacity
int capacity
! Кол-во элементов
Definition: jse_array.h:43
array
Definition: minesweeper.c:29
js
Definition: elk.h:48
JSE_ARRAY_VALUE
Значение ключа
Definition: jse_array.h:22
JSE_ARRAY_VALUE::array_value
struct JSE_ARRAY * array_value
! Строка (при JSE_ARRAY_TYPE_STRING)
Definition: jse_array.h:25
JSE_ARRAY_VALUE::str_value
char * str_value
! Числовое значение (при JSE_ARRAY_TYPE_INT)
Definition: jse_array.h:24