fix macrobind

This commit is contained in:
blueloveTH 2025-06-18 12:51:14 +08:00
parent 3688f69123
commit 03e368b755
3 changed files with 11 additions and 13 deletions

View File

@ -130,9 +130,9 @@ PK_API void py_watchdog_begin(py_i64 timeout);
PK_API void py_watchdog_end(); PK_API void py_watchdog_end();
/// Bind a compile-time function via "decl-based" style. /// Bind a compile-time function via "decl-based" style.
PK_API void py_compiletime_bind(const char* sig, py_CFunction f); PK_API void py_macrobind(const char* sig, py_CFunction f);
/// Find a compile-time function by name. /// Get a compile-time function by name.
PK_API py_ItemRef py_compiletime_getfunc(py_Name name); PK_API py_ItemRef py_macroget(py_Name name);
/// Get the current source location of the frame. /// Get the current source location of the frame.
PK_API const char* py_Frame_sourceloc(py_Frame* frame, int* lineno); PK_API const char* py_Frame_sourceloc(py_Frame* frame, int* lineno);

View File

@ -1935,8 +1935,7 @@ static Error* exprCall(Compiler* self) {
int line = prev()->line; int line = prev()->line;
if(callable->vt->is_name) { if(callable->vt->is_name) {
NameExpr* ne = (NameExpr*)callable; NameExpr* ne = (NameExpr*)callable;
if(ne->scope == NAME_GLOBAL) { py_ItemRef func = py_macroget(ne->name);
py_ItemRef func = py_compiletime_getfunc(ne->name);
if(func != NULL) { if(func != NULL) {
py_StackRef p0 = py_peek(0); py_StackRef p0 = py_peek(0);
err = exprCompileTimeCall(self, func, line); err = exprCompileTimeCall(self, func, line);
@ -1944,7 +1943,6 @@ static Error* exprCall(Compiler* self) {
return err; return err;
} }
} }
}
CallExpr* e = CallExpr__new(line, callable); CallExpr* e = CallExpr__new(line, callable);
Ctx__s_push(ctx(), (Expr*)e); // push onto the stack in advance Ctx__s_push(ctx(), (Expr*)e); // push onto the stack in advance

View File

@ -95,14 +95,14 @@ void py_bind(py_Ref obj, const char* sig, py_CFunction f) {
py_pop(); py_pop();
} }
void py_compiletime_bind(const char* sig, py_CFunction f) { void py_macrobind(const char* sig, py_CFunction f) {
py_Ref tmp = py_pushtmp(); py_Ref tmp = py_pushtmp();
py_Name name = py_newfunction(tmp, sig, f, NULL, 0); py_Name name = py_newfunction(tmp, sig, f, NULL, 0);
NameDict__set(&pk_current_vm->compile_time_funcs, name, tmp); NameDict__set(&pk_current_vm->compile_time_funcs, name, tmp);
py_pop(); py_pop();
} }
PK_API py_ItemRef py_compiletime_getfunc(py_Name name) { py_ItemRef py_macroget(py_Name name) {
NameDict* d = &pk_current_vm->compile_time_funcs; NameDict* d = &pk_current_vm->compile_time_funcs;
if(d->length == 0) return NULL; if(d->length == 0) return NULL;
return NameDict__try_get(d, name); return NameDict__try_get(d, name);