This commit is contained in:
BLUELOVETH 2023-07-18 12:17:26 +08:00
parent 9732828e85
commit 78ac9488ff
4 changed files with 26 additions and 11 deletions

View File

@ -413,11 +413,11 @@ int main(int argc, char** argv) {
PRINT_TITLE("test py_repr"); PRINT_TITLE("test py_repr");
check(pkpy_eval(vm, "['1', 2, (3, '4')]")); check(pkpy_eval(vm, "['1', 2, (3, '4')]"));
check(pkpy_py_repr(vm, -1)); check(pkpy_py_repr(vm));
check(pkpy_to_string(vm, -1, &r_string)); check(pkpy_to_string(vm, -1, &r_string));
for(int i = 0; i < r_string.size; i++) putchar(r_string.data[i]); for(int i = 0; i < r_string.size; i++) putchar(r_string.data[i]);
putchar('\n'); putchar('\n');
check(pkpy_pop(vm, 2)); check(pkpy_pop_top(vm));
check(pkpy_stack_size(vm) == 0); check(pkpy_stack_size(vm) == 0);
return 0; return 0;
} }

View File

@ -6,6 +6,10 @@ order: 8
### Basic manipulation ### Basic manipulation
+ `bool pkpy_dup(pkpy_vm*, int)`
Duplicate the value at the given index.
+ `bool pkpy_pop(pkpy_vm*, int)` + `bool pkpy_pop(pkpy_vm*, int)`
Pop `n` values from the stack. Pop `n` values from the stack.
@ -70,7 +74,7 @@ PK_EXPORT bool pkpy_is_none(pkpy_vm*, int i);
+ `pkpy_push_null(pkpy_vm*)` + `pkpy_push_null(pkpy_vm*)`
Push a `PY_NULL` onto the stack. Push a `PY_NULL` onto the stack. It is used for `pkpy_vectorcall`.
+ `pkpy_push_function(pkpy_vm*, const char* sig, pkpy_CFunction f)` + `pkpy_push_function(pkpy_vm*, const char* sig, pkpy_CFunction f)`
@ -140,10 +144,10 @@ PK_EXPORT bool pkpy_is_none(pkpy_vm*, int i);
``` ```
[obj] -> [obj.<name> self] [obj] -> [obj.<name> self]
``` ```
+ `bool pkpy_py_repr(pkpy_vm*, int i)` + `bool pkpy_py_repr(pkpy_vm*)`
Get the repr of the value at the given index and push it onto the stack. Get the repr of the value at the top of the stack.
``` ```
[..., value, ...] -> [..., value, ..., repr(value)] [value] -> [repr(value)]
``` ```

View File

@ -27,6 +27,7 @@ PK_EXPORT bool pkpy_exec(pkpy_vm*, const char* source);
PK_EXPORT bool pkpy_exec_2(pkpy_vm*, const char* source, const char* filename, int mode, const char* module); PK_EXPORT bool pkpy_exec_2(pkpy_vm*, const char* source, const char* filename, int mode, const char* module);
/* Stack Manipulation */ /* Stack Manipulation */
PK_EXPORT bool pkpy_dup(pkpy_vm*, int);
PK_EXPORT bool pkpy_pop(pkpy_vm*, int); PK_EXPORT bool pkpy_pop(pkpy_vm*, int);
PK_EXPORT bool pkpy_pop_top(pkpy_vm*); PK_EXPORT bool pkpy_pop_top(pkpy_vm*);
PK_EXPORT bool pkpy_dup_top(pkpy_vm*); PK_EXPORT bool pkpy_dup_top(pkpy_vm*);
@ -75,7 +76,7 @@ PK_EXPORT bool pkpy_setglobal(pkpy_vm*, pkpy_CName);
PK_EXPORT bool pkpy_eval(pkpy_vm*, const char* source); PK_EXPORT bool pkpy_eval(pkpy_vm*, const char* source);
PK_EXPORT bool pkpy_unpack_sequence(pkpy_vm*, int size); PK_EXPORT bool pkpy_unpack_sequence(pkpy_vm*, int size);
PK_EXPORT bool pkpy_get_unbound_method(pkpy_vm*, pkpy_CName); PK_EXPORT bool pkpy_get_unbound_method(pkpy_vm*, pkpy_CName);
PK_EXPORT bool pkpy_py_repr(pkpy_vm*, int i); PK_EXPORT bool pkpy_py_repr(pkpy_vm*);
/* Error Handling */ /* Error Handling */
PK_EXPORT bool pkpy_error(pkpy_vm*, const char* name, pkpy_CString); PK_EXPORT bool pkpy_error(pkpy_vm*, const char* name, pkpy_CString);

View File

@ -89,6 +89,16 @@ bool pkpy_exec_2(pkpy_vm* vm_handle, const char* source, const char* filename, i
return res != nullptr; return res != nullptr;
} }
bool pkpy_dup(pkpy_vm* vm_handle, int n){
VM* vm = (VM*) vm_handle;
PK_ASSERT_NO_ERROR()
PK_PROTECTED(
PyObject* item = stack_item(vm, n);
vm->s_data.push(item);
)
return true;
}
bool pkpy_pop(pkpy_vm* vm_handle, int n){ bool pkpy_pop(pkpy_vm* vm_handle, int n){
VM* vm = (VM*) vm_handle; VM* vm = (VM*) vm_handle;
PK_ASSERT_NO_ERROR() PK_ASSERT_NO_ERROR()
@ -461,15 +471,15 @@ bool pkpy_get_unbound_method(pkpy_vm* vm_handle, pkpy_CName name){
return true; return true;
} }
bool pkpy_py_repr(pkpy_vm* vm_handle, int i) { bool pkpy_py_repr(pkpy_vm* vm_handle) {
VM* vm = (VM*) vm_handle; VM* vm = (VM*) vm_handle;
PK_ASSERT_NO_ERROR() PK_ASSERT_NO_ERROR()
PK_ASSERT_N_EXTRA_ELEMENTS(1) PK_ASSERT_N_EXTRA_ELEMENTS(1)
PyObject* item = vm->s_data.top();
PK_PROTECTED( PK_PROTECTED(
PyObject* item = stack_item(vm, i); item = vm->py_repr(item);
PyObject* repr = vm->py_repr(item);
vm->s_data.push(repr);
) )
vm->s_data.top() = item;
return true; return true;
} }