adjusted things so that we minimize the number of changes necessary to

the main pocketpy library
This commit is contained in:
Kolten Pearson 2023-05-01 20:35:38 -06:00
parent 75de7b0b63
commit 669bf8b9b1
7 changed files with 190 additions and 207 deletions

View File

@ -3,6 +3,8 @@
using namespace pkpy; using namespace pkpy;
#define PKPY_STACK_SIZE 32
#define SAFEGUARD_OPEN try { \ #define SAFEGUARD_OPEN try { \
#define SAFEGUARD_CLOSE \ #define SAFEGUARD_CLOSE \
@ -20,27 +22,32 @@ using namespace pkpy;
#define ERRHANDLER_OPEN SAFEGUARD_OPEN \ #define ERRHANDLER_OPEN SAFEGUARD_OPEN \
try { \ try { \
if (vm->c_data.top() == nullptr) \ if (w->c_data->size() > 0 && w->c_data->top() == nullptr) \
return false; \ return false; \
#define ERRHANDLER_CLOSE \ #define ERRHANDLER_CLOSE \
} catch( Exception e ) { \ } catch( Exception e ) { \
vm->c_data.push(VAR(e)); \ w->c_data->push(py_var(w->vm, e)); \
vm->c_data.push(NULL); \ w->c_data->push(NULL); \
return false; \ return false; \
} \ } \
SAFEGUARD_CLOSE \ SAFEGUARD_CLOSE \
struct pkpy_vm_wrapper {
VM* vm;
ValueStackImpl<PKPY_STACK_SIZE>* 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(VM* vm, PyObject* ret) { static void unpack_return(struct pkpy_vm_wrapper* w, PyObject* ret) {
if (is_type(ret, vm->tp_tuple)) { if (is_type(ret, w->vm->tp_tuple)) {
Tuple& t = CAST(Tuple&, ret); Tuple& t = py_cast<Tuple&>(w->vm, ret);
for (int i = 0; i < t.size(); i++) for (int i = 0; i < t.size(); i++)
vm->c_data.push(t[i]); w->c_data->push(t[i]);
} else if (ret == vm->None) { } else if (ret == w->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
@ -49,66 +56,67 @@ static void unpack_return(VM* vm, PyObject* ret) {
//if this becomes a problem we can change it //if this becomes a problem we can change it
// //
//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 too //and after if you have to
} else } else
vm->c_data.push(ret); w->c_data->push(ret);
} }
bool pkpy_clear_error(pkpy_vm vm_handle, char** message) { bool pkpy_clear_error(struct pkpy_vm_wrapper* w, char** message) {
VM* vm = (VM*) vm_handle;
SAFEGUARD_OPEN SAFEGUARD_OPEN
if (vm->c_data.top() != nullptr) if (w->c_data->size() == 0 || w->c_data->top() != nullptr)
return false; return false;
vm->c_data.pop(); w->c_data->pop();
Exception& e = CAST(Exception&, vm->c_data.top()); Exception& e = py_cast<Exception&>(w->vm, w->c_data->top());
if (message != nullptr) if (message != nullptr)
*message = 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";
vm->c_data.clear(); w->c_data->clear();
vm->callstack.clear(); w->vm->callstack.clear();
vm->s_data.clear(); w->vm->s_data.clear();
return true; return true;
SAFEGUARD_CLOSE SAFEGUARD_CLOSE
} }
pkpy_vm pkpy_vm_create(bool use_stdio, bool enable_os) { struct pkpy_vm_wrapper* pkpy_vm_create(bool use_stdio, bool enable_os) {
VM* vm = new VM(use_stdio, enable_os); struct pkpy_vm_wrapper* w = (struct pkpy_vm_wrapper*) malloc(sizeof(*w));
w->vm = new VM(use_stdio, enable_os);
w->c_data = new ValueStackImpl<PKPY_STACK_SIZE>();
return (pkpy_vm) vm; return w;
} }
bool pkpy_vm_run(pkpy_vm vm_handle, const char* source) { bool pkpy_vm_run(struct pkpy_vm_wrapper* w, const char* source) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
CodeObject_ code = vm->compile(source, "<c-bound>", EXEC_MODE); CodeObject_ code = w->vm->compile(source, "<c-bound>", EXEC_MODE);
PyObject* result = vm->_exec(code, vm->_main); PyObject* result = w->vm->_exec(code, w->vm->_main);
unpack_return(vm, result); unpack_return(w, result);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
void pkpy_vm_destroy(pkpy_vm vm_handle) { void pkpy_vm_destroy(struct pkpy_vm_wrapper* w) {
VM* vm = (VM*) vm_handle; delete w->vm;
delete vm; delete w->c_data;
free(w);
} }
static void propagate_if_errored(VM* vm) { static void propagate_if_errored(struct pkpy_vm_wrapper* w) {
try { try {
if (vm->c_data.top() != nullptr) if (w->c_data->size() == 0 || w->c_data->top() != nullptr)
return; return;
vm->c_data.pop(); w->c_data->pop();
Exception& e = CAST(Exception&, vm->c_data.top()); Exception& e = py_cast<Exception&>(w->vm, w->c_data->top());
vm->c_data.pop(); w->c_data->pop();
throw e; throw e;
} catch(Exception& e) { } catch(Exception& e) {
@ -122,40 +130,41 @@ static void propagate_if_errored(VM* vm) {
PyObject* c_function_wrapper(VM* vm, ArgsView args) { PyObject* c_function_wrapper(VM* vm, ArgsView args) {
LuaStyleFuncC f = CAST(NativeFunc&, args[-2])._lua_f; LuaStyleFuncC f = py_cast<NativeFunc&>(vm, args[-2])._lua_f;
//setup c stack //setup c stack
int stored = vm->c_data.store();
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++)
vm->c_data.push(args[i]); w.c_data->push(args[i]);
int retc = f(vm); int retc = f(&w);
PyObject* ret = vm->None; PyObject* ret = w.vm->None;
propagate_if_errored(vm); propagate_if_errored(&w);
if (retc == 1) if (retc == 1)
ret = vm->c_data.top(); ret = w.c_data->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 = (vm->c_data.size() - retc) + i; int stack_index = (w.c_data->size() - retc) + i;
t[i] = vm->c_data.get(stack_index); t[i] = w.c_data->begin()[stack_index];
} }
ret = VAR(t); ret = py_var(w.vm, t);
} }
vm->c_data.clear();
vm->c_data.restore(stored);
return ret; return ret;
} }
bool pkpy_push_function(pkpy_vm vm_handle, pkpy_function f) { bool pkpy_push_function(struct pkpy_vm_wrapper* w, pkpy_function f) {
VM* vm = (VM*) 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
@ -163,69 +172,58 @@ bool pkpy_push_function(pkpy_vm vm_handle, 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;
vm->c_data.push(VAR(nf)); w->c_data->push(py_var(w->vm, nf));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_int(pkpy_vm vm_handle, int value) { bool pkpy_push_int(struct pkpy_vm_wrapper* w, int value) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->c_data.push(VAR(value)); w->c_data->push(py_var(w->vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_float(pkpy_vm vm_handle, double value) { bool pkpy_push_float(struct pkpy_vm_wrapper* w, double value) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->c_data.push(VAR(value)); w->c_data->push(py_var(w->vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_bool(pkpy_vm vm_handle, bool value) { bool pkpy_push_bool(struct pkpy_vm_wrapper* w, bool value) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->c_data.push(VAR(value)); w->c_data->push(py_var(w->vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_string(pkpy_vm vm_handle, const char* value) { bool pkpy_push_string(struct pkpy_vm_wrapper* w, const char* value) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->c_data.push(VAR(value)); w->c_data->push(py_var(w->vm, value));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_stringn(pkpy_vm vm_handle, const char* value, int length) { bool pkpy_push_stringn(struct pkpy_vm_wrapper* w, const char* value, int length) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
Str s = Str(value, length); Str s = Str(value, length);
vm->c_data.push(VAR(s)); w->c_data->push(py_var(w->vm, s));
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_push_none(pkpy_vm vm_handle) { bool pkpy_push_none(struct pkpy_vm_wrapper* w) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->c_data.push(vm->None); w->c_data->push(w->vm->None);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
@ -233,83 +231,77 @@ bool pkpy_push_none(pkpy_vm vm_handle) {
bool pkpy_set_global(pkpy_vm vm_handle, const char* name) { bool pkpy_set_global(struct pkpy_vm_wrapper* w, const char* name) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
vm->_main->attr().set(name, vm->c_data.top()); w->vm->_main->attr().set(name, w->c_data->top());
vm->c_data.pop(); w->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(pkpy_vm vm_handle, const char* name) { bool pkpy_get_global(struct pkpy_vm_wrapper* w, const char* name) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
PyObject* o = vm->_main->attr().try_get(name); PyObject* o = w->vm->_main->attr().try_get(name);
if (o == nullptr) { if (o == nullptr) {
o = vm->builtins->attr().try_get(name); o = w->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");
} }
vm->c_data.push(o); w->c_data->push(o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_call(pkpy_vm vm_handle, int argc) { bool pkpy_call(struct pkpy_vm_wrapper* w, int argc) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
int callable_index = vm->c_data.size() - argc - 1; int callable_index = w->c_data->size() - argc - 1;
PyObject* callable = vm->c_data.get(callable_index); PyObject* callable = w->c_data->begin()[callable_index];
vm->s_data.push(callable); w->vm->s_data.push(callable);
vm->s_data.push(PY_NULL); w->vm->s_data.push(PY_NULL);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
vm->s_data.push(vm->c_data.get(callable_index + i + 1)); w->vm->s_data.push(w->c_data->begin()[callable_index + i + 1]);
PyObject* o = vm->vectorcall(argc); PyObject* o = w->vm->vectorcall(argc);
vm->c_data.shrink(argc + 1); w->c_data->shrink(argc + 1);
unpack_return(vm, o); unpack_return(w, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_call_method(pkpy_vm vm_handle, const char* name, int argc) { bool pkpy_call_method(struct pkpy_vm_wrapper* w, const char* name, int argc) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
int self_index = vm->c_data.size() - argc - 1; int self_index = w->c_data->size() - argc - 1;
PyObject* self = vm->c_data.get(self_index); PyObject* self = w->c_data->begin()[self_index];
PyObject* callable = vm->get_unbound_method(self, name, &self); PyObject* callable = w->vm->get_unbound_method(self, name, &self);
vm->s_data.push(callable); w->vm->s_data.push(callable);
vm->s_data.push(self); w->vm->s_data.push(self);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
vm->s_data.push(vm->c_data.get(self_index + i + 1)); w->vm->s_data.push(w->c_data->begin()[self_index + i + 1]);
PyObject* o = vm->vectorcall(argc); PyObject* o = w->vm->vectorcall(argc);
vm->c_data.shrink(argc + 1); w->c_data->shrink(argc + 1);
unpack_return(vm, o); unpack_return(w, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
@ -323,57 +315,53 @@ static int lua_to_cstack_index(int index, int size) {
return index; return index;
} }
bool pkpy_to_int(pkpy_vm vm_handle, int index, int* ret) { bool pkpy_to_int(struct pkpy_vm_wrapper* w, int index, int* ret) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, vm->c_data.size()); index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = vm->c_data.get(index); PyObject* o = w->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<int>(vm, o); *ret = py_cast<int>(w->vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_float(pkpy_vm vm_handle, int index, double* ret) { bool pkpy_to_float(struct pkpy_vm_wrapper* w, int index, double* ret) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, vm->c_data.size()); index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = vm->c_data.get(index); PyObject* o = w->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<double>(vm, o); *ret = py_cast<double>(w->vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_bool(pkpy_vm vm_handle, int index, bool* ret) { bool pkpy_to_bool(struct pkpy_vm_wrapper* w, int index, bool* ret) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, vm->c_data.size()); index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = vm->c_data.get(index); PyObject* o = w->c_data->begin()[index];
if (ret != nullptr) if (ret != nullptr)
*ret = py_cast<bool>(vm, o); *ret = py_cast<bool>(w->vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_to_string(pkpy_vm vm_handle, int index, char** ret) { bool pkpy_to_string(struct pkpy_vm_wrapper* w, int index, char** ret) {
VM* vm = (VM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
index = lua_to_cstack_index(index, vm->c_data.size()); index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = vm->c_data.get(index); PyObject* o = w->c_data->begin()[index];
if (ret != nullptr) { if (ret != nullptr) {
Str& s = CAST(Str&, o); Str& s = py_cast<Str&>(w->vm, o);
*ret = s.c_str_dup(); *ret = s.c_str_dup();
} }
@ -381,54 +369,46 @@ bool pkpy_to_string(pkpy_vm vm_handle, int index, char** ret) {
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
bool pkpy_is_int(pkpy_vm vm_handle, int index) { bool pkpy_is_int(struct pkpy_vm_wrapper* w, int index) {
VM* vm = (VM*) vm_handle; index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
index = lua_to_cstack_index(index, vm->c_data.size()); return is_type(o, w->vm->tp_int);
PyObject* o = vm->c_data.get(index);
return is_type(o, vm->tp_int);
} }
bool pkpy_is_float(pkpy_vm vm_handle, int index) { bool pkpy_is_float(struct pkpy_vm_wrapper* w, int index) {
VM* vm = (VM*) vm_handle; index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
index = lua_to_cstack_index(index, vm->c_data.size()); return is_type(o, w->vm->tp_float);
PyObject* o = vm->c_data.get(index);
return is_type(o, vm->tp_float);
} }
bool pkpy_is_bool(pkpy_vm vm_handle, int index) { bool pkpy_is_bool(struct pkpy_vm_wrapper* w, int index) {
VM* vm = (VM*) vm_handle; index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
index = lua_to_cstack_index(index, vm->c_data.size()); return is_type(o, w->vm->tp_bool);
PyObject* o = vm->c_data.get(index);
return is_type(o, vm->tp_bool);
} }
bool pkpy_is_string(pkpy_vm vm_handle, int index) { bool pkpy_is_string(struct pkpy_vm_wrapper* w, int index) {
VM* vm = (VM*) vm_handle; index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
index = lua_to_cstack_index(index, vm->c_data.size()); return is_type(o, w->vm->tp_str);
PyObject* o = vm->c_data.get(index);
return is_type(o, vm->tp_str);
} }
bool pkpy_is_none(pkpy_vm vm_handle, int index) { bool pkpy_is_none(struct pkpy_vm_wrapper* w, int index) {
VM* vm = (VM*) vm_handle; index = lua_to_cstack_index(index, w->c_data->size());
PyObject* o = w->c_data->begin()[index];
index = lua_to_cstack_index(index, vm->c_data.size()); return o == w->vm->None;
PyObject* o = vm->c_data.get(index);
return o == vm->None;
} }
bool pkpy_check_stack(pkpy_vm vm_handle, int free) { bool pkpy_check_stack(struct pkpy_vm_wrapper* w, int free) {
VM* vm = (VM*) vm_handle; return free + w->c_data->size() <= PKPY_STACK_SIZE;
return free <= vm->c_data.remaining();
} }
int pkpy_stack_size(pkpy_vm vm_handle) { int pkpy_stack_size(struct pkpy_vm_wrapper* w) {
VM* vm = (VM*) vm_handle; return w->c_data->size();
return vm->c_data.size();
} }
bool pkpy_pop(struct pkpy_vm_wrapper* w, int n) {
w->c_data->shrink(n);
return true;
}

View File

@ -8,8 +8,7 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef struct pkpy_vm_handle* pkpy_vm; typedef struct pkpy_vm_wrapper pkpy_vm;
typedef struct pkpy_repl_hande* pkpy_repl;
//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,
@ -21,62 +20,65 @@ typedef struct pkpy_repl_hande* pkpy_repl;
//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)
//NOTE : you need to free the message that is passed back after you are done using it //NOTE : you need to free the message that is passed back after you are done using it
//or else pass in null as message, and it will just print the message to stderr //or else pass in null 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);
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);
void pkpy_vm_destroy(pkpy_vm); void pkpy_vm_destroy(pkpy_vm*);
typedef int (*pkpy_function)(pkpy_vm); typedef int (*pkpy_function)(pkpy_vm*);
bool pkpy_push_function(pkpy_vm, pkpy_function); bool pkpy_pop(pkpy_vm*, int n);
bool pkpy_push_int(pkpy_vm, int);
bool pkpy_push_float(pkpy_vm, double);
bool pkpy_push_bool(pkpy_vm, bool);
bool pkpy_push_string(pkpy_vm, const char*);
bool pkpy_push_stringn(pkpy_vm, const char*, int length);
bool pkpy_push_none(pkpy_vm);
bool pkpy_set_global(pkpy_vm, const char* name); bool pkpy_push_function(pkpy_vm*, pkpy_function);
bool pkpy_get_global(pkpy_vm, const char* name); bool pkpy_push_int(pkpy_vm*, int);
bool pkpy_push_float(pkpy_vm*, double);
bool pkpy_push_bool(pkpy_vm*, bool);
bool pkpy_push_string(pkpy_vm*, const char*);
bool pkpy_push_stringn(pkpy_vm*, const char*, int length);
bool pkpy_push_none(pkpy_vm*);
bool pkpy_set_global(pkpy_vm*, const char* name);
bool pkpy_get_global(pkpy_vm*, const char* name);
//first push callable you want to call //first push callable you want to call
//then push the arguments to send //then push the arguments to send
//argc is the number of arguments that was pushed (not counting the callable) //argc is the number of arguments that was pushed (not counting the callable)
bool pkpy_call(pkpy_vm, int argc); bool pkpy_call(pkpy_vm*, int argc);
//first push the object the method belongs to (self) //first push the object the method belongs to (self)
//then push the the argments //then push the the argments
//argc is the number of arguments that was pushed (not counting the callable or self) //argc is the number of arguments that was pushed (not counting the callable or self)
//name is the name of the method to call on the object //name is the name of the method to call on the object
bool pkpy_call_method(pkpy_vm, const char* name, int argc); bool pkpy_call_method(pkpy_vm*, const char* name, int argc);
//we will break with the lua api here //we will break with the lua api here
//lua uses 1 as the index to the first pushed element for all of these functions //lua uses 1 as the index to the first pushed element for all of these functions
//but we will start counting at zero to match python //but we will start counting at zero to match python
//we will allow negative numbers to count backwards from the top //we will allow negative numbers to count backwards from the top
bool pkpy_to_int(pkpy_vm, int index, int* ret); 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);
//you have to free ret after you are done using it //you have to free ret after you are done using it
bool pkpy_to_string(pkpy_vm, int index, char** ret); bool pkpy_to_string(pkpy_vm*, int index, char** ret);
//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
bool pkpy_is_int(pkpy_vm, int index); bool pkpy_is_int(pkpy_vm*, int index);
bool pkpy_is_float(pkpy_vm, int index); bool pkpy_is_float(pkpy_vm*, int index);
bool pkpy_is_bool(pkpy_vm, int index); bool pkpy_is_bool(pkpy_vm*, int index);
bool pkpy_is_string(pkpy_vm, int index); bool pkpy_is_string(pkpy_vm*, int index);
bool pkpy_is_none(pkpy_vm, int index); bool pkpy_is_none(pkpy_vm*, int index);
//will return true if at least free empty slots remain on the stack //will return true if at least free empty slots remain on the stack
bool pkpy_check_stack(pkpy_vm, int free); bool pkpy_check_stack(pkpy_vm*, int free);
//returns the number of elements on the stack //returns the number of elements on the stack
int pkpy_stack_size(pkpy_vm); int pkpy_stack_size(pkpy_vm*);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -4,7 +4,7 @@
//tests the c bindings for pocketpy //tests the c bindings for pocketpy
void check_impl(pkpy_vm vm, bool result, int lineno) { void check_impl(pkpy_vm* vm, bool result, int lineno) {
if (!result) { if (!result) {
printf("ERROR: failed where it should have succeed at line %i\n", lineno); printf("ERROR: failed where it should have succeed at line %i\n", lineno);
char* message; char* message;
@ -19,7 +19,7 @@ void check_impl(pkpy_vm vm, bool result, int lineno) {
} }
} }
void fail_impl(pkpy_vm vm, bool result, int lineno) { void fail_impl(pkpy_vm* vm, bool result, int lineno) {
if (result) { if (result) {
printf("ERROR: succeeded where it should have failed line %i\n", lineno); printf("ERROR: succeeded where it should have failed line %i\n", lineno);
exit(1); exit(1);
@ -33,7 +33,7 @@ void fail_impl(pkpy_vm vm, bool result, int lineno) {
} }
} }
void error_impl(pkpy_vm vm, bool result, int lineno) { void error_impl(pkpy_vm* vm, bool result, int lineno) {
if (result) { if (result) {
printf("ERROR: succeeded where it should have failed line %i\n", lineno); printf("ERROR: succeeded where it should have failed line %i\n", lineno);
exit(1); exit(1);
@ -54,28 +54,28 @@ void error_impl(pkpy_vm vm, bool result, int lineno) {
#define fail(r) fail_impl(vm, (r), __LINE__) #define fail(r) fail_impl(vm, (r), __LINE__)
#define error(r) error_impl(vm, (r), __LINE__) #define error(r) error_impl(vm, (r), __LINE__)
int test_binding(pkpy_vm vm) { int test_binding(pkpy_vm* vm) {
pkpy_push_int(vm, 12); pkpy_push_int(vm, 12);
return 1; return 1;
} }
int test_multiple_return(pkpy_vm vm) { int test_multiple_return(pkpy_vm* vm) {
pkpy_push_int(vm, 12); pkpy_push_int(vm, 12);
pkpy_push_int(vm, 13); pkpy_push_int(vm, 13);
return 2; return 2;
} }
int test_return_none(pkpy_vm vm) { int test_return_none(pkpy_vm* vm) {
return 0; return 0;
} }
int test_error_propagate(pkpy_vm vm) { int test_error_propagate(pkpy_vm* vm) {
pkpy_get_global(vm, "does not exist"); pkpy_get_global(vm, "does not exist");
return 1; return 1;
} }
pkpy_vm vm; pkpy_vm* vm;
void cleanup(void) { void cleanup(void) {
pkpy_vm_destroy(vm); pkpy_vm_destroy(vm);
@ -163,8 +163,8 @@ int main(int argc, char** argv) {
int stack_size = pkpy_stack_size(vm); int stack_size = pkpy_stack_size(vm);
printf("stack size %i\n", stack_size); printf("stack size %i\n", stack_size);
check(pkpy_check_stack(vm, 10)); check(pkpy_check_stack(vm, 10));
check(pkpy_check_stack(vm, 251)); check(pkpy_check_stack(vm, 27));
fail(pkpy_check_stack(vm, 252)); fail(pkpy_check_stack(vm, 28));
check(pkpy_is_int(vm, 0)); check(pkpy_is_int(vm, 0));
check(pkpy_is_float(vm, 1)); check(pkpy_is_float(vm, 1));
check(pkpy_is_bool(vm, 2)); check(pkpy_is_bool(vm, 2));
@ -258,5 +258,8 @@ int main(int argc, char** argv) {
check(pkpy_call(vm, 0)); check(pkpy_call(vm, 0));
check(pkpy_stack_size(vm) == 2); check(pkpy_stack_size(vm) == 2);
check(pkpy_pop(vm, 2));
check(pkpy_stack_size(vm) == 0);
return 0; return 0;
} }

View File

@ -18,7 +18,7 @@ echo "checking results (they should be identical)"
diff -q -s binding_test_scratch c_bindings/test_answers.txt diff -q -s binding_test_scratch c_bindings/test_answers.txt
echo "cleaning up" echo "cleaning up"
rm pocketpy_c.o #rm pocketpy_c.o
rm test.o rm test.o
rm binding_test_scratch rm binding_test_scratch

View File

@ -190,5 +190,4 @@ struct Frame {
} }
}; };
}; // namespace pkpy }; // namespace pkpy

View File

@ -12,7 +12,7 @@ struct Function;
class VM; class VM;
typedef PyObject* (*NativeFuncC)(VM*, ArgsView); typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
typedef int (*LuaStyleFuncC)(VM*); typedef int (*LuaStyleFuncC)(void*);
struct NativeFunc { struct NativeFunc {
NativeFuncC f; NativeFuncC f;
@ -370,4 +370,4 @@ struct Py_<DummyModule> final: PyObject {
}; };
} // namespace pkpy } // namespace pkpy

View File

@ -78,7 +78,6 @@ class VM {
public: public:
ManagedHeap heap; ManagedHeap heap;
ValueStack s_data; ValueStack s_data;
CVirtualStack c_data;
stack< Frame > callstack; stack< Frame > callstack;
std::vector<PyTypeInfo> _all_types; std::vector<PyTypeInfo> _all_types;
@ -1204,4 +1203,4 @@ inline Str obj_type_name(VM *vm, Type type){
#undef PY_VAR_INT #undef PY_VAR_INT
#undef PY_VAR_FLOAT #undef PY_VAR_FLOAT
} // namespace pkpy } // namespace pkpy