switch to using subclass for c virtual machine, strings methods return

strong references, except one new one which returns a weak refrence
This commit is contained in:
Kolten Pearson 2023-05-02 22:08:18 -06:00
parent d1f9aab008
commit 5852ba7435
4 changed files with 231 additions and 169 deletions

View File

@ -22,33 +22,42 @@ using namespace pkpy;
#define ERRHANDLER_OPEN SAFEGUARD_OPEN \ #define ERRHANDLER_OPEN SAFEGUARD_OPEN \
try { \ try { \
if (w->c_data->size() > 0 && w->c_data->top() == nullptr) \ if (vm->c_data->size() > 0 && vm->c_data->top() == nullptr) \
return false; \ return false; \
#define ERRHANDLER_CLOSE \ #define ERRHANDLER_CLOSE \
} catch( Exception e ) { \ } catch( Exception e ) { \
w->c_data->push(py_var(w->vm, e)); \ vm->c_data->push(py_var(vm, e)); \
w->c_data->push(NULL); \ vm->c_data->push(NULL); \
return false; \ return false; \
} \ } \
SAFEGUARD_CLOSE \ SAFEGUARD_CLOSE \
struct pkpy_vm_wrapper {
VM* vm;
class CVM : public VM {
public :
ValueStackImpl<PKPY_STACK_SIZE>* c_data; ValueStackImpl<PKPY_STACK_SIZE>* c_data;
char* string_ret; CVM(bool use_stdio = true, bool enable_os=true) : VM(use_stdio, enable_os) {
c_data = new ValueStackImpl<PKPY_STACK_SIZE>();
}
~CVM() {
delete c_data;
}
}; };
//for now I will unpack a tuple automatically, we may not want to handle //for now I will unpack a tuple automatically, we may not want to handle
//it this way, not sure //it this way, not sure
//it is more lua like, but maybe not python like //it is more lua like, but maybe not python like
static void unpack_return(struct pkpy_vm_wrapper* w, PyObject* ret) { static void unpack_return(CVM* vm, PyObject* ret) {
if (is_type(ret, w->vm->tp_tuple)) { if (is_type(ret, vm->tp_tuple)) {
Tuple& t = py_cast<Tuple&>(w->vm, ret); Tuple& t = py_cast<Tuple&>(vm, ret);
for (int i = 0; i < t.size(); i++) for (int i = 0; i < t.size(); i++)
w->c_data->push(t[i]); vm->c_data->push(t[i]);
} else if (ret == w->vm->None) { } else if (ret == vm->None) {
//do nothing here //do nothing here
//having to pop the stack after every call that returns none is annoying //having to pop the stack after every call that returns none is annoying
//lua does not do this //lua does not do this
@ -59,56 +68,48 @@ static void unpack_return(struct pkpy_vm_wrapper* w, PyObject* ret) {
//you can still check if it returned none by comparing stack size before //you can still check if it returned none by comparing stack size before
//and after if you have to //and after if you have to
} else } else
w->c_data->push(ret); vm->c_data->push(ret);
} }
static char* manage_string(struct pkpy_vm_wrapper* w, char* s) {
if (w->string_ret != NULL)
free(w->string_ret);
w->string_ret = s;
return w->string_ret;
}
bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) {
bool pkpy_clear_error(struct pkpy_vm_wrapper* w, char** message) { CVM* vm = (CVM*) vm_handle;
SAFEGUARD_OPEN SAFEGUARD_OPEN
if (w->c_data->size() == 0 || w->c_data->top() != nullptr) if (vm->c_data->size() == 0 || vm->c_data->top() != nullptr)
return false; return false;
w->c_data->pop(); vm->c_data->pop();
Exception& e = py_cast<Exception&>(w->vm, w->c_data->top()); Exception& e = py_cast<Exception&>(vm, vm->c_data->top());
if (message != nullptr) if (message != nullptr)
*message = manage_string(w, e.summary().c_str_dup()); *message = e.summary().c_str_dup();
else else
std::cerr << "ERROR: " << e.summary() << "\n"; std::cerr << "ERROR: " << e.summary() << "\n";
w->c_data->clear(); vm->c_data->clear();
w->vm->callstack.clear(); vm->callstack.clear();
w->vm->s_data.clear(); vm->s_data.clear();
return true; return true;
SAFEGUARD_CLOSE SAFEGUARD_CLOSE
} }
struct pkpy_vm_wrapper* pkpy_vm_create(bool use_stdio, bool enable_os) { pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
struct pkpy_vm_wrapper* w = (struct pkpy_vm_wrapper*) malloc(sizeof(*w)); CVM* vm = new CVM(use_stdio, enable_os);
w->vm = new VM(use_stdio, enable_os); vm->c_data = new ValueStackImpl<PKPY_STACK_SIZE>();
w->c_data = new ValueStackImpl<PKPY_STACK_SIZE>(); return (pkpy_vm*) vm;
w->string_ret = NULL;
return w;
} }
bool pkpy_vm_run(struct pkpy_vm_wrapper* w, const char* source) { bool pkpy_vm_run(pkpy_vm* vm_handle, const char* source) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
CodeObject_ code = w->vm->compile(source, "<c-bound>", EXEC_MODE); CodeObject_ code = vm->compile(source, "<c-bound>", EXEC_MODE);
PyObject* result = w->vm->_exec(code, w->vm->_main); PyObject* result = vm->_exec(code, vm->_main);
//unpack_return(w, result); //unpack_return(w, result);
//NOTE: it seems like w->vm->_exec should return whatever the last command it //NOTE: it seems like vm->_exec should return whatever the last command it
//ran returned but instead it seems to pretty much always return None //ran returned but instead it seems to pretty much always return None
//so I guess uncomment this line if that every changes //so I guess uncomment this line if that every changes
@ -116,22 +117,21 @@ bool pkpy_vm_run(struct pkpy_vm_wrapper* w, const char* source) {
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
void pkpy_vm_destroy(struct pkpy_vm_wrapper* w) { void pkpy_vm_destroy(pkpy_vm* vm_handle) {
delete w->vm; CVM* vm = (CVM*) vm_handle;
delete w->c_data; delete vm;
if (w->string_ret != NULL)
free(w->string_ret);
free(w);
} }
static void propagate_if_errored(struct pkpy_vm_wrapper* w) { static void propagate_if_errored(CVM* vm, ValueStackImpl<PKPY_STACK_SIZE>* stored_stack) {
try { try {
if (w->c_data->size() == 0 || w->c_data->top() != nullptr) if (vm->c_data->size() == 0 || vm->c_data->top() != nullptr)
return; return;
w->c_data->pop(); vm->c_data->pop();
Exception& e = py_cast<Exception&>(w->vm, w->c_data->top()); Exception& e = py_cast<Exception&>(vm, vm->c_data->top());
w->c_data->pop(); vm->c_data->pop();
vm->c_data = stored_stack;
throw e; throw e;
} catch(Exception& e) { } catch(Exception& e) {
@ -143,43 +143,44 @@ static void propagate_if_errored(struct pkpy_vm_wrapper* w) {
} }
} }
PyObject* c_function_wrapper(VM* vm, ArgsView args) { PyObject* c_function_wrapper(VM* vm, ArgsView args) {
LuaStyleFuncC f = py_cast<NativeFunc&>(vm, args[-2])._lua_f; LuaStyleFuncC f = py_cast<NativeFunc&>(vm, args[-2])._lua_f;
CVM* cvm = (CVM*) vm;
//setup c stack //setup c stack
ValueStackImpl<PKPY_STACK_SIZE> local_stack = ValueStackImpl<PKPY_STACK_SIZE>();
struct pkpy_vm_wrapper w;
ValueStackImpl c_stack = ValueStackImpl<PKPY_STACK_SIZE>();
w.vm = vm;
w.c_data = &c_stack;
for (int i = 0; i < args.size(); i++) for (int i = 0; i < args.size(); i++)
w.c_data->push(args[i]); local_stack.push(args[i]);
int retc = f(&w); ValueStackImpl<PKPY_STACK_SIZE>* stored_stack = cvm->c_data;
cvm->c_data = &local_stack;
PyObject* ret = w.vm->None; int retc = f(cvm);
propagate_if_errored(&w);
propagate_if_errored(cvm, stored_stack);
cvm->c_data = stored_stack;
PyObject* ret = cvm->None;
if (retc == 1) if (retc == 1)
ret = w.c_data->top(); ret = local_stack.top();
else if (retc > 1) { else if (retc > 1) {
Tuple t = Tuple(retc); Tuple t = Tuple(retc);
for (int i = 0; i < retc; i++) { for (int i = 0; i < retc; i++) {
int stack_index = (w.c_data->size() - retc) + i; int stack_index = (local_stack.size() - retc) + i;
t[i] = w.c_data->begin()[stack_index]; t[i] = local_stack.begin()[stack_index];
} }
ret = py_var(w.vm, t); ret = py_var(cvm, t);
} }
return ret; return ret;
} }
bool pkpy_push_function(struct pkpy_vm_wrapper* w, pkpy_function f) { bool pkpy_push_function(pkpy_vm* vm_handle, pkpy_function f) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
//TODO right now we just treat all c bound functions a varargs functions //TODO right now we just treat all c bound functions a varargs functions
@ -187,66 +188,73 @@ bool pkpy_push_function(struct pkpy_vm_wrapper* w, pkpy_function f) {
NativeFunc nf = NativeFunc(c_function_wrapper, -1, 0); NativeFunc nf = NativeFunc(c_function_wrapper, -1, 0);
nf._lua_f = (LuaStyleFuncC) f; nf._lua_f = (LuaStyleFuncC) f;
w->c_data->push(py_var(w->vm, nf)); vm->c_data->push(py_var(vm, nf));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_int(struct pkpy_vm_wrapper* w, int value) { bool pkpy_push_int(pkpy_vm* vm_handle, int value) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(py_var(w->vm, value)); vm->c_data->push(py_var(vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_float(struct pkpy_vm_wrapper* w, double value) { bool pkpy_push_float(pkpy_vm* vm_handle, double value) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(py_var(w->vm, value)); vm->c_data->push(py_var(vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_bool(struct pkpy_vm_wrapper* w, bool value) { bool pkpy_push_bool(pkpy_vm* vm_handle, bool value) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(py_var(w->vm, value)); vm->c_data->push(py_var(vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_string(struct pkpy_vm_wrapper* w, const char* value) { bool pkpy_push_string(pkpy_vm* vm_handle, const char* value) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(py_var(w->vm, value)); vm->c_data->push(py_var(vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_stringn(struct pkpy_vm_wrapper* w, const char* value, int length) { bool pkpy_push_stringn(pkpy_vm* vm_handle, const char* value, int length) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
Str s = Str(value, length); Str s = Str(value, length);
w->c_data->push(py_var(w->vm, s)); vm->c_data->push(py_var(vm, s));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_voidp(struct pkpy_vm_wrapper* w, void* value) { bool pkpy_push_voidp(pkpy_vm* vm_handle, void* value) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(py_var(w->vm, value)); vm->c_data->push(py_var(vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_none(struct pkpy_vm_wrapper* w) { bool pkpy_push_none(pkpy_vm* vm_handle) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->c_data->push(w->vm->None); vm->c_data->push(vm->None);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
@ -254,77 +262,81 @@ bool pkpy_push_none(struct pkpy_vm_wrapper* w) {
bool pkpy_set_global(struct pkpy_vm_wrapper* w, const char* name) { bool pkpy_set_global(pkpy_vm* vm_handle, const char* name) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
w->vm->_main->attr().set(name, w->c_data->top()); vm->_main->attr().set(name, vm->c_data->top());
w->c_data->pop(); vm->c_data->pop();
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
//get global will also get bulitins //get global will also get bulitins
bool pkpy_get_global(struct pkpy_vm_wrapper* w, const char* name) { bool pkpy_get_global(pkpy_vm* vm_handle, const char* name) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
PyObject* o = w->vm->_main->attr().try_get(name); PyObject* o = vm->_main->attr().try_get(name);
if (o == nullptr) { if (o == nullptr) {
o = w->vm->builtins->attr().try_get(name); o = vm->builtins->attr().try_get(name);
if (o == nullptr) if (o == nullptr)
throw Exception("AttributeError", "could not find requested global"); throw Exception("AttributeError", "could not find requested global");
} }
w->c_data->push(o); vm->c_data->push(o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_call(struct pkpy_vm_wrapper* w, int argc) { bool pkpy_call(pkpy_vm* vm_handle, int argc) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
int callable_index = w->c_data->size() - argc - 1; int callable_index = vm->c_data->size() - argc - 1;
PyObject* callable = w->c_data->begin()[callable_index]; PyObject* callable = vm->c_data->begin()[callable_index];
w->vm->s_data.push(callable); vm->s_data.push(callable);
w->vm->s_data.push(PY_NULL); vm->s_data.push(PY_NULL);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
w->vm->s_data.push(w->c_data->begin()[callable_index + i + 1]); vm->s_data.push(vm->c_data->begin()[callable_index + i + 1]);
PyObject* o = w->vm->vectorcall(argc); PyObject* o = vm->vectorcall(argc);
w->c_data->shrink(argc + 1); vm->c_data->shrink(argc + 1);
unpack_return(w, o); unpack_return(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_call_method(struct pkpy_vm_wrapper* w, const char* name, int argc) { bool pkpy_call_method(pkpy_vm* vm_handle, const char* name, int argc) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
int self_index = w->c_data->size() - argc - 1; int self_index = vm->c_data->size() - argc - 1;
PyObject* self = w->c_data->begin()[self_index]; PyObject* self = vm->c_data->begin()[self_index];
PyObject* callable = w->vm->get_unbound_method(self, name, &self); PyObject* callable = vm->get_unbound_method(self, name, &self);
w->vm->s_data.push(callable); vm->s_data.push(callable);
w->vm->s_data.push(self); vm->s_data.push(self);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
w->vm->s_data.push(w->c_data->begin()[self_index + i + 1]); vm->s_data.push(vm->c_data->begin()[self_index + i + 1]);
PyObject* o = w->vm->vectorcall(argc); PyObject* o = vm->vectorcall(argc);
w->c_data->shrink(argc + 1); vm->c_data->shrink(argc + 1);
unpack_return(w, o); unpack_return(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
@ -338,116 +350,146 @@ static int lua_to_cstack_index(int index, int size) {
return index; return index;
} }
bool pkpy_to_int(struct pkpy_vm_wrapper* w, int index, int* ret) { bool pkpy_to_int(pkpy_vm* vm_handle, int index, int* ret) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, w->c_data->size()); index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = w->c_data->begin()[index]; PyObject* o = vm->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<int>(w->vm, o); *ret = py_cast<int>(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_float(struct pkpy_vm_wrapper* w, int index, double* ret) { bool pkpy_to_float(pkpy_vm* vm_handle, int index, double* ret) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, w->c_data->size()); index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = w->c_data->begin()[index]; PyObject* o = vm->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<double>(w->vm, o); *ret = py_cast<double>(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_bool(struct pkpy_vm_wrapper* w, int index, bool* ret) { bool pkpy_to_bool(pkpy_vm* vm_handle, int index, bool* ret) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, w->c_data->size()); index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = w->c_data->begin()[index]; PyObject* o = vm->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<bool>(w->vm, o); *ret = py_cast<bool>(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_voidp(pkpy_vm_wrapper* w, int index, void** ret) { bool pkpy_to_voidp(pkpy_vm* vm_handle, int index, void** ret) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, w->c_data->size()); index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = w->c_data->begin()[index]; PyObject* o = vm->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<void*>(w->vm, o); *ret = py_cast<void*>(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_string(struct pkpy_vm_wrapper* w, int index, char** ret) { bool pkpy_to_string(pkpy_vm* vm_handle, int index, char** ret) {
CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, w->c_data->size()); index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = w->c_data->begin()[index]; PyObject* o = vm->c_data->begin()[index];
if (ret != nullptr) { if (ret != nullptr) {
Str& s = py_cast<Str&>(w->vm, o); Str& s = py_cast<Str&>(vm, o);
*ret = manage_string(w, s.c_str_dup()); *ret = s.c_str_dup();
} }
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_is_int(struct pkpy_vm_wrapper* w, int index) { bool pkpy_to_stringn(pkpy_vm* vm_handle, int index, const char** ret, int* size) {
index = lua_to_cstack_index(index, w->c_data->size()); CVM* vm = (CVM*) vm_handle;
PyObject* o = w->c_data->begin()[index]; ERRHANDLER_OPEN
return is_type(o, w->vm->tp_int); index = lua_to_cstack_index(index, vm->c_data->size());
}
bool pkpy_is_float(struct pkpy_vm_wrapper* w, int index) {
index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
return is_type(o, w->vm->tp_float); PyObject* o = vm->c_data->begin()[index];
} if (ret != nullptr) {
bool pkpy_is_bool(struct pkpy_vm_wrapper* w, int index) { std::string_view sv = py_cast<Str&>(vm, o).sv();
index = lua_to_cstack_index(index, w->c_data->size()); *ret = sv.data();
PyObject* o = w->c_data->begin()[index]; *size = sv.size();
}
return is_type(o, w->vm->tp_bool); return true;
} ERRHANDLER_CLOSE
bool pkpy_is_string(struct pkpy_vm_wrapper* w, int index) {
index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
return is_type(o, w->vm->tp_str);
}
bool pkpy_is_voidp(struct pkpy_vm_wrapper* w, int index) {
index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
return is_type(o, VoidP::_type(w->vm));
} }
bool pkpy_is_none(struct pkpy_vm_wrapper* w, int index) {
index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
return o == w->vm->None; bool pkpy_is_int(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return is_type(o, vm->tp_int);
}
bool pkpy_is_float(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return is_type(o, vm->tp_float);
}
bool pkpy_is_bool(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return is_type(o, vm->tp_bool);
}
bool pkpy_is_string(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return is_type(o, vm->tp_str);
}
bool pkpy_is_voidp(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return is_type(o, VoidP::_type(vm));
} }
bool pkpy_check_global(pkpy_vm_wrapper* w, const char* name) { bool pkpy_is_none(pkpy_vm* vm_handle, int index) {
CVM* vm = (CVM*) vm_handle;
index = lua_to_cstack_index(index, vm->c_data->size());
PyObject* o = vm->c_data->begin()[index];
return o == vm->None;
}
bool pkpy_check_global(pkpy_vm* vm_handle, const char* name) {
CVM* vm = (CVM*) vm_handle;
SAFEGUARD_OPEN SAFEGUARD_OPEN
PyObject* o = w->vm->_main->attr().try_get(name); PyObject* o = vm->_main->attr().try_get(name);
if (o == nullptr) { if (o == nullptr) {
o = w->vm->builtins->attr().try_get(name); o = vm->builtins->attr().try_get(name);
if (o == nullptr) if (o == nullptr)
return false; return false;
} }
@ -457,15 +499,18 @@ bool pkpy_check_global(pkpy_vm_wrapper* w, const char* name) {
} }
bool pkpy_check_stack(struct pkpy_vm_wrapper* w, int free) { bool pkpy_check_stack(pkpy_vm* vm_handle, int free) {
return free + w->c_data->size() <= PKPY_STACK_SIZE; CVM* vm = (CVM*) vm_handle;
return free + vm->c_data->size() <= PKPY_STACK_SIZE;
} }
int pkpy_stack_size(struct pkpy_vm_wrapper* w) { int pkpy_stack_size(pkpy_vm* vm_handle) {
return w->c_data->size(); CVM* vm = (CVM*) vm_handle;
return vm->c_data->size();
} }
bool pkpy_pop(struct pkpy_vm_wrapper* w, int n) { bool pkpy_pop(pkpy_vm* vm_handle, int n) {
w->c_data->shrink(n); CVM* vm = (CVM*) vm_handle;
vm->c_data->shrink(n);
return true; return true;
} }

View File

@ -8,7 +8,7 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef struct pkpy_vm_wrapper pkpy_vm; typedef struct pkpy_vm_handle pkpy_vm;
//we we take a lot of inspiration from the lua api for these bindings //we we take a lot of inspiration from the lua api for these bindings
//the key difference being most methods return a bool, //the key difference being most methods return a bool,
@ -20,7 +20,7 @@ typedef struct pkpy_vm_wrapper pkpy_vm;
//it will provide a string summary of the error in the message parameter (if it is not NULL) //it will provide a string summary of the error in the message parameter (if it is not NULL)
//if null is passed in as message, and it will just print the message to stderr //if null is passed in as message, and it will just print the message to stderr
bool pkpy_clear_error(pkpy_vm*, char** message); bool pkpy_clear_error(pkpy_vm*, char** message);
//the message pointer is only valid until the next api call, so copy it if you want it //NOTE you are responsible for freeing message
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os); pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os);
bool pkpy_vm_run(pkpy_vm*, const char* source); bool pkpy_vm_run(pkpy_vm*, const char* source);
@ -62,8 +62,16 @@ bool pkpy_to_int(pkpy_vm*, int index, int* ret);
bool pkpy_to_float(pkpy_vm*, int index, double* ret); bool pkpy_to_float(pkpy_vm*, int index, double* ret);
bool pkpy_to_bool(pkpy_vm*, int index, bool* ret); bool pkpy_to_bool(pkpy_vm*, int index, bool* ret);
bool pkpy_to_voidp(pkpy_vm*, int index, void** ret); bool pkpy_to_voidp(pkpy_vm*, int index, void** ret);
//this method provides a strong reference, you are responsible for freeing the
//string when you are done with it
bool pkpy_to_string(pkpy_vm*, int index, char** ret); bool pkpy_to_string(pkpy_vm*, int index, char** ret);
//the ret string pointer is only valid until the next api call, so copy it if you want it
//this method provides a weak reference, it is only valid until the
//next api call
//it is not null terminated
bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size);
//these do not follow the same error semantics as above, their return values //these do not follow the same error semantics as above, their return values
//just say whether the check succeeded or not, or else return the value asked for //just say whether the check succeeded or not, or else return the value asked for

View File

@ -14,6 +14,7 @@ void check_impl(pkpy_vm* vm, bool result, int lineno) {
} }
printf("%s\n", message); printf("%s\n", message);
free(message);
exit(1); exit(1);
} }
} }
@ -26,6 +27,7 @@ void fail_impl(pkpy_vm* vm, bool result, int lineno) {
char* message; char* message;
if (pkpy_clear_error(vm, &message)) { if (pkpy_clear_error(vm, &message)) {
printf("actually errored!\n"); printf("actually errored!\n");
free(message);
exit(1); exit(1);
} }
} }
@ -42,6 +44,7 @@ void error_impl(pkpy_vm* vm, bool result, int lineno) {
else { else {
printf("successfully errored with this message: \n"); printf("successfully errored with this message: \n");
printf("%s\n", message); printf("%s\n", message);
free(message);
} }
} }
} }
@ -142,6 +145,11 @@ int main(int argc, char** argv) {
check(pkpy_is_string(vm, -1)); check(pkpy_is_string(vm, -1));
check(pkpy_to_string(vm, -1, &r_string)); check(pkpy_to_string(vm, -1, &r_string));
printf("%s\n", r_string); printf("%s\n", r_string);
free(r_string);
const char* r_stringn;
int r_size;
check(pkpy_to_stringn(vm, -1, &r_stringn, &r_size));
printf("%.*s\n", r_size, r_stringn);
fail(pkpy_is_int(vm, -1)); fail(pkpy_is_int(vm, -1));
fail(pkpy_is_float(vm, -1)); fail(pkpy_is_float(vm, -1));
fail(pkpy_is_bool(vm, -1)); fail(pkpy_is_bool(vm, -1));

View File

@ -18,6 +18,7 @@ False
testing string methods testing string methods
hello! hello!
hello hello
hello
testing None methods testing None methods
None None