-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm_ext.c
96 lines (77 loc) · 2.69 KB
/
vm_ext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "shared/shared.h"
#include "vm.h"
#include "vm_ext.h"
static void QC_ModInt(qcvm_t *vm)
{
const int a = qcvm_argv_int32(vm, 0);
const int b = qcvm_argv_int32(vm, 1);
qcvm_return_int32(vm, a % b);
}
static void QC_func_get(qcvm_t *vm)
{
const char *s = qcvm_argv_string(vm, 0);
qcvm_func_t func = qcvm_find_function_id(vm, s);
qcvm_return_func(vm, func);
}
static void QC_handle_free(qcvm_t *vm)
{
const int32_t id = qcvm_argv_int32(vm, 0);
qcvm_handle_free(vm, qcvm_fetch_handle(vm, id));
}
typedef struct
{
qcvm_t *vm;
qcvm_pointer_t elements;
qcvm_pointer_t ctx;
qcvm_function_t *func;
} qsort_context_t;
#if defined(_WIN32)
static int QC_qsort_callback(void *ctx, const void *a, const void *b)
#else
static int QC_qsort_callback(const void *a, const void *b, void *ctx)
#endif
{
qsort_context_t *context = (qsort_context_t *)ctx;
qcvm_pointer_t a_ptr = qcvm_make_pointer(context->vm, context->elements.raw.type, a);
qcvm_pointer_t b_ptr = qcvm_make_pointer(context->vm, context->elements.raw.type, b);
qcvm_set_global_typed_value(qcvm_pointer_t, context->vm, GLOBAL_PARM0, a_ptr);
qcvm_set_global_typed_value(qcvm_pointer_t, context->vm, GLOBAL_PARM1, b_ptr);
qcvm_set_global_typed_value(qcvm_pointer_t, context->vm, GLOBAL_PARM2, context->ctx);
qcvm_execute(context->vm, context->func);
return *qcvm_get_global_typed(int32_t, context->vm, GLOBAL_RETURN);
}
#if !defined(__STDC_LIB_EXT1__) && !defined(_WIN32)
static int (*qsort_s_callback)(const void *, const void *, void *);
static void *qsort_s_ctx;
static int qsort_s_wrapper(const void *a, const void *b)
{
return qsort_s_callback(a, b, qsort_s_ctx);
}
static void qsort_s(const void *base, size_t count, size_t size, int (*callback)(const void *, const void *, void *), void *context)
{
qsort_s_callback = callback;
qsort_s_ctx = context;
qsort((void *)base, count, size, qsort_s_wrapper);
}
#endif
static void QC_qsort(qcvm_t *vm)
{
const qcvm_pointer_t elements = qcvm_argv_pointer(vm, 0);
const int32_t num = qcvm_argv_int32(vm, 1);
const int32_t size_of_element = qcvm_argv_int32(vm, 2);
void *address;
if (elements.raw.type == QCVM_POINTER_HANDLE || !qcvm_resolve_pointer(vm, elements, false, num * size_of_element, &address))
qcvm_error(vm, "bad pointer");
qcvm_function_t *comparator_func = qcvm_get_function(vm, qcvm_argv_int32(vm, 3));
qsort_context_t context = { vm, elements, { 0 }, comparator_func };
if (vm->state.argc > 4)
context.ctx = qcvm_argv_pointer(vm, 4);
qsort_s(address, num, size_of_element, QC_qsort_callback, &context);
}
void qcvm_init_ext_builtins(qcvm_t *vm)
{
qcvm_register_builtin(ModInt);
qcvm_register_builtin(func_get);
qcvm_register_builtin(handle_free);
qcvm_register_builtin(qsort);
}