Skip to content

Commit 9573413

Browse files
committed
Refactor UI import to std.ui and fix CI build failure (create_empty_module and shadowing warnings)
1 parent b32547d commit 9573413

File tree

8 files changed

+205
-197
lines changed

8 files changed

+205
-197
lines changed

src/runtime/vm_helpers.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
#include "../include/compiler.h"
1717

1818

19-
void closeUpvalues(VM *vm, Value *last) {
20-
while (vm->openUpvalues != NULL &&
21-
vm->openUpvalues->location >= last) {
22-
ObjUpvalue *upvalue = vm->openUpvalues;
19+
void closeUpvalues(VM *pVM, Value *last) {
20+
while (pVM->openUpvalues != NULL &&
21+
pVM->openUpvalues->location >= last) {
22+
ObjUpvalue *upvalue = pVM->openUpvalues;
2323
upvalue->closed = *upvalue->location;
2424
upvalue->location = &upvalue->closed;
25-
vm->openUpvalues = upvalue->next;
25+
pVM->openUpvalues = upvalue->next;
2626
}
2727
}
2828

29-
ObjUpvalue *captureUpvalue(Value *local, VM *vm) {
29+
ObjUpvalue *captureUpvalue(Value *local, VM *pVM) {
3030
ObjUpvalue *prevUpvalue = NULL;
31-
ObjUpvalue *upvalue = vm->openUpvalues;
31+
ObjUpvalue *upvalue = pVM->openUpvalues;
3232
while (upvalue != NULL && upvalue->location > local) {
3333
prevUpvalue = upvalue;
3434
upvalue = upvalue->next;
@@ -42,120 +42,120 @@ ObjUpvalue *captureUpvalue(Value *local, VM *vm) {
4242
createdUpvalue->next = upvalue;
4343

4444
if (prevUpvalue == NULL) {
45-
vm->openUpvalues = createdUpvalue;
45+
pVM->openUpvalues = createdUpvalue;
4646
} else {
4747
prevUpvalue->next = createdUpvalue;
4848
}
4949

5050
return createdUpvalue;
5151
}
5252

53-
void defineMethod(ObjString *name, VM *vm) {
54-
Value method = peek(vm, 0);
55-
ObjClass *klass = AS_CLASS(peek(vm, 1));
53+
void defineMethod(ObjString *name, VM *pVM) {
54+
Value method = peek(pVM, 0);
55+
ObjClass *klass = AS_CLASS(peek(pVM, 1));
5656
if (IS_CLOSURE(method)) {
5757
AS_CLOSURE(method)->function->ownerClass = klass;
5858
}
5959
tableSet(&klass->methods, name, method);
60-
pop(vm);
60+
pop(pVM);
6161
}
6262

63-
bool bindMethod(struct ObjClass *klass, ObjString *name, VM *vm) {
63+
bool bindMethod(struct ObjClass *klass, ObjString *name, VM *pVM) {
6464
Value method;
6565
if (!tableGet(&klass->methods, name, &method)) {
66-
runtimeError(vm, "Undefined property '%s'.", name->chars);
66+
runtimeError(pVM, "Undefined property '%s'.", name->chars);
6767
return false;
6868
}
6969

70-
ObjBoundMethod *bound = newBoundMethod(peek(vm, 0), AS_CLOSURE(method));
71-
pop(vm);
72-
push(vm, OBJ_VAL(bound));
70+
ObjBoundMethod *bound = newBoundMethod(peek(pVM, 0), AS_CLOSURE(method));
71+
pop(pVM);
72+
push(pVM, OBJ_VAL(bound));
7373
return true;
7474
}
7575

76-
bool call(ObjClosure *closure, int argCount, VM *vm) {
76+
bool call(ObjClosure *closure, int argCount, VM *pVM) {
7777
if (argCount != closure->function->arity) {
78-
runtimeError(vm, "Expected %d arguments but got %d.",
78+
runtimeError(pVM, "Expected %d arguments but got %d.",
7979
closure->function->arity, argCount);
8080
return false;
8181
}
8282

83-
if (vm->frameCount == FRAMES_MAX) {
84-
runtimeError(vm, "Stack overflow.");
83+
if (pVM->frameCount == FRAMES_MAX) {
84+
runtimeError(pVM, "Stack overflow.");
8585
return false;
8686
}
8787

88-
CallFrame *frame = &vm->frames[vm->frameCount++];
88+
CallFrame *frame = &pVM->frames[pVM->frameCount++];
8989
frame->closure = closure;
9090
frame->ip = closure->function->chunk.code;
91-
frame->slots = vm->stackTop - argCount - 1;
91+
frame->slots = pVM->stackTop - argCount - 1;
9292
return true;
9393
}
9494

95-
bool callValue(Value callee, int argCount, VM *vm) {
95+
bool callValue(Value callee, int argCount, VM *pVM) {
9696
if (IS_OBJ(callee)) {
9797
switch (OBJ_TYPE(callee)) {
9898
case OBJ_BOUND_METHOD: {
9999
ObjBoundMethod *bound = AS_BOUND_METHOD(callee);
100-
vm->stackTop[-argCount - 1] = bound->receiver;
101-
return call(bound->method, argCount, vm);
100+
pVM->stackTop[-argCount - 1] = bound->receiver;
101+
return call(bound->method, argCount, pVM);
102102
}
103103
case OBJ_CLASS: {
104104
ObjClass *klass = AS_CLASS(callee);
105-
vm->stackTop[-argCount - 1] = OBJ_VAL(newInstance(klass));
105+
pVM->stackTop[-argCount - 1] = OBJ_VAL(newInstance(klass));
106106
Value initializer;
107-
if (tableGet(&klass->methods, vm->initString, &initializer)) {
108-
return call(AS_CLOSURE(initializer), argCount, vm);
107+
if (tableGet(&klass->methods, pVM->initString, &initializer)) {
108+
return call(AS_CLOSURE(initializer), argCount, pVM);
109109
} else if (argCount != 0) {
110-
runtimeError(vm, "Expected 0 arguments but got %d.", argCount);
110+
runtimeError(pVM, "Expected 0 arguments but got %d.", argCount);
111111
return false;
112112
}
113113
return true;
114114
}
115115
case OBJ_CLOSURE:
116-
return call(AS_CLOSURE(callee), argCount, vm);
116+
return call(AS_CLOSURE(callee), argCount, pVM);
117117
case OBJ_NATIVE: {
118118
NativeFn native = AS_NATIVE(callee);
119-
Value result = native(argCount, vm->stackTop - argCount);
120-
vm->stackTop -= argCount + 1;
121-
push(vm, result);
119+
Value result = native(argCount, pVM->stackTop - argCount);
120+
pVM->stackTop -= argCount + 1;
121+
push(pVM, result);
122122
return true;
123123
}
124124
default:
125125
break; // Non-callable object type
126126
}
127127
}
128-
runtimeError(vm, "Can only call functions and classes.");
128+
runtimeError(pVM, "Can only call functions and classes.");
129129
return false;
130130
}
131131

132132
bool invokeFromClass(struct ObjClass *klass, ObjString *name,
133-
int argCount, VM *vm) {
133+
int argCount, VM *pVM) {
134134
Value method;
135135
if (!tableGet(&klass->methods, name, &method)) {
136-
runtimeError(vm, "Undefined property '%s'.", name->chars);
136+
runtimeError(pVM, "Undefined property '%s'.", name->chars);
137137
return false;
138138
}
139-
return call(AS_CLOSURE(method), argCount, vm);
139+
return call(AS_CLOSURE(method), argCount, pVM);
140140
}
141141

142-
bool invoke(ObjString *name, int argCount, VM *vm) {
143-
Value receiver = peek(vm, argCount);
142+
bool invoke(ObjString *name, int argCount, VM *pVM) {
143+
Value receiver = peek(pVM, argCount);
144144

145145
if (!IS_INSTANCE(receiver)) {
146-
runtimeError(vm, "Only instances have methods.");
146+
runtimeError(pVM, "Only instances have methods.");
147147
return false;
148148
}
149149

150150
struct ObjInstance *instance = AS_INSTANCE(receiver);
151151

152152
Value value;
153153
if (tableGet(&instance->fields, name, &value)) {
154-
vm->stackTop[-argCount - 1] = value;
155-
return callValue(value, argCount, vm);
154+
pVM->stackTop[-argCount - 1] = value;
155+
return callValue(value, argCount, pVM);
156156
}
157157

158-
return invokeFromClass(instance->klass, name, argCount, vm);
158+
return invokeFromClass(instance->klass, name, argCount, pVM);
159159
}
160160

161161
void appendToList(ObjList* list, Value value) {

src/stdlib/convert_native.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ static Value native_char_at(int argCount, Value* args) {
158158
// len(value) moved to stdlib_core.c
159159

160160
// Register all conversion functions with the VM
161-
void register_convert_natives(VM* vm) {
162-
defineNative(vm, "to_int", native_to_int);
163-
defineNative(vm, "to_float", native_to_float);
164-
defineNative(vm, "to_string", native_to_string);
165-
defineNative(vm, "to_bool", native_to_bool);
166-
defineNative(vm, "to_hex", native_to_hex);
167-
defineNative(vm, "to_bin", native_to_bin);
168-
defineNative(vm, "char_at", native_char_at);
169-
// defineNative(vm, "len", native_len); // Moved to stdlib_core.c
161+
void register_convert_natives(VM* pVM) {
162+
defineNative(pVM, "to_int", native_to_int);
163+
defineNative(pVM, "to_float", native_to_float);
164+
defineNative(pVM, "to_string", native_to_string);
165+
defineNative(pVM, "to_bool", native_to_bool);
166+
defineNative(pVM, "to_hex", native_to_hex);
167+
defineNative(pVM, "to_bin", native_to_bin);
168+
defineNative(pVM, "char_at", native_char_at);
169+
// defineNative(pVM, "len", native_len); // Moved to stdlib_core.c
170170
}
171171

src/stdlib/io_native.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ ObjModule* create_std_io_module() {
149149
}
150150

151151
// Register I/O functions as globals (for convenience)
152-
void register_io_globals(VM* vm) {
153-
defineNative(vm, "print", native_print_raw);
154-
defineNative(vm, "println", native_println);
155-
defineNative(vm, "input", native_input);
152+
void register_io_globals(VM* pVM) {
153+
defineNative(pVM, "print", native_print_raw);
154+
defineNative(pVM, "println", native_println);
155+
defineNative(pVM, "input", native_input);
156156
}

src/stdlib/math_native.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -384,28 +384,28 @@ ObjModule* create_std_math_module() {
384384
}
385385

386386
// Register math functions as globals (for benchmarks/ease of use)
387-
void register_math_globals(VM* vm) {
388-
defineNative(vm, "abs", native_abs);
389-
defineNative(vm, "ceil", native_ceil);
390-
defineNative(vm, "floor", native_floor);
391-
defineNative(vm, "round", native_round);
392-
defineNative(vm, "max", native_max);
393-
defineNative(vm, "min", native_min);
394-
defineNative(vm, "pow", native_pow);
395-
defineNative(vm, "sqrt", native_sqrt);
396-
defineNative(vm, "sin", native_sin);
397-
defineNative(vm, "cos", native_cos);
398-
defineNative(vm, "tan", native_tan);
399-
defineNative(vm, "asin", native_asin);
400-
defineNative(vm, "acos", native_acos);
401-
defineNative(vm, "atan", native_atan);
402-
defineNative(vm, "log", native_log);
403-
defineNative(vm, "exp", native_exp);
404-
defineNative(vm, "random", native_random);
405-
defineNative(vm, "randint", native_randint);
406-
defineNative(vm, "seed", native_seed);
407-
defineNative(vm, "sigmoid", native_sigmoid);
408-
defineNative(vm, "relu", native_relu);
409-
defineNative(vm, "tanh", native_tanh);
410-
defineNative(vm, "transpose", native_transpose);
387+
void register_math_globals(VM* pVM) {
388+
defineNative(pVM, "abs", native_abs);
389+
defineNative(pVM, "ceil", native_ceil);
390+
defineNative(pVM, "floor", native_floor);
391+
defineNative(pVM, "round", native_round);
392+
defineNative(pVM, "max", native_max);
393+
defineNative(pVM, "min", native_min);
394+
defineNative(pVM, "pow", native_pow);
395+
defineNative(pVM, "sqrt", native_sqrt);
396+
defineNative(pVM, "sin", native_sin);
397+
defineNative(pVM, "cos", native_cos);
398+
defineNative(pVM, "tan", native_tan);
399+
defineNative(pVM, "asin", native_asin);
400+
defineNative(pVM, "acos", native_acos);
401+
defineNative(pVM, "atan", native_atan);
402+
defineNative(pVM, "log", native_log);
403+
defineNative(pVM, "exp", native_exp);
404+
defineNative(pVM, "random", native_random);
405+
defineNative(pVM, "randint", native_randint);
406+
defineNative(pVM, "seed", native_seed);
407+
defineNative(pVM, "sigmoid", native_sigmoid);
408+
defineNative(pVM, "relu", native_relu);
409+
defineNative(pVM, "tanh", native_tanh);
410+
defineNative(pVM, "transpose", native_transpose);
411411
}

0 commit comments

Comments
 (0)