pocketpy_c.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "pocketpy.h"
  2. #include "pocketpy_c.h"
  3. using namespace pkpy;
  4. #define PKPY_STACK_SIZE 32
  5. #define SAFEGUARD_OPEN try { \
  6. #define SAFEGUARD_CLOSE \
  7. } catch(std::exception& e) { \
  8. std::cerr << "ERROR: a std::exception " \
  9. << "this probably means pocketpy itself has a bug!\n" \
  10. << e.what() << "\n"; \
  11. exit(2); \
  12. } catch(...) { \
  13. std::cerr << "ERROR: a unknown exception was thrown " \
  14. << "this probably means pocketpy itself has a bug!\n"; \
  15. exit(2); \
  16. }
  17. #define ERRHANDLER_OPEN SAFEGUARD_OPEN \
  18. try { \
  19. if (w->c_data->size() > 0 && w->c_data->top() == nullptr) \
  20. return false; \
  21. #define ERRHANDLER_CLOSE \
  22. } catch( Exception e ) { \
  23. w->c_data->push(py_var(w->vm, e)); \
  24. w->c_data->push(NULL); \
  25. return false; \
  26. } \
  27. SAFEGUARD_CLOSE \
  28. struct pkpy_vm_wrapper {
  29. VM* vm;
  30. ValueStackImpl<PKPY_STACK_SIZE>* c_data;
  31. };
  32. //for now I will unpack a tuple automatically, we may not want to handle
  33. //it this way, not sure
  34. //it is more lua like, but maybe not python like
  35. static void unpack_return(struct pkpy_vm_wrapper* w, PyObject* ret) {
  36. if (is_type(ret, w->vm->tp_tuple)) {
  37. Tuple& t = py_cast<Tuple&>(w->vm, ret);
  38. for (int i = 0; i < t.size(); i++)
  39. w->c_data->push(t[i]);
  40. } else if (ret == w->vm->None) {
  41. //do nothing here
  42. //having to pop the stack after every call that returns none is annoying
  43. //lua does not do this
  44. //
  45. //so for now we will not push none on the stack when it is the sole thing returned
  46. //if this becomes a problem we can change it
  47. //
  48. //you can still check if it returned none by comparing stack size before
  49. //and after if you have to
  50. } else
  51. w->c_data->push(ret);
  52. }
  53. bool pkpy_clear_error(struct pkpy_vm_wrapper* w, char** message) {
  54. SAFEGUARD_OPEN
  55. if (w->c_data->size() == 0 || w->c_data->top() != nullptr)
  56. return false;
  57. w->c_data->pop();
  58. Exception& e = py_cast<Exception&>(w->vm, w->c_data->top());
  59. if (message != nullptr)
  60. *message = e.summary().c_str_dup();
  61. else
  62. std::cerr << "ERROR: " << e.summary() << "\n";
  63. w->c_data->clear();
  64. w->vm->callstack.clear();
  65. w->vm->s_data.clear();
  66. return true;
  67. SAFEGUARD_CLOSE
  68. }
  69. struct pkpy_vm_wrapper* pkpy_vm_create(bool use_stdio, bool enable_os) {
  70. struct pkpy_vm_wrapper* w = (struct pkpy_vm_wrapper*) malloc(sizeof(*w));
  71. w->vm = new VM(use_stdio, enable_os);
  72. w->c_data = new ValueStackImpl<PKPY_STACK_SIZE>();
  73. return w;
  74. }
  75. bool pkpy_vm_run(struct pkpy_vm_wrapper* w, const char* source) {
  76. ERRHANDLER_OPEN
  77. CodeObject_ code = w->vm->compile(source, "<c-bound>", EXEC_MODE);
  78. PyObject* result = w->vm->_exec(code, w->vm->_main);
  79. //unpack_return(w, result);
  80. //NOTE: it seems like w->vm->_exec should return whatever the last command it
  81. //ran returned but instead it seems to pretty much always return None
  82. //so I guess uncomment this line if that every changes
  83. return true;
  84. ERRHANDLER_CLOSE
  85. }
  86. void pkpy_vm_destroy(struct pkpy_vm_wrapper* w) {
  87. delete w->vm;
  88. delete w->c_data;
  89. free(w);
  90. }
  91. static void propagate_if_errored(struct pkpy_vm_wrapper* w) {
  92. try {
  93. if (w->c_data->size() == 0 || w->c_data->top() != nullptr)
  94. return;
  95. w->c_data->pop();
  96. Exception& e = py_cast<Exception&>(w->vm, w->c_data->top());
  97. w->c_data->pop();
  98. throw e;
  99. } catch(Exception& e) {
  100. throw;
  101. } catch(...) {
  102. std::cerr << "ERROR: a non pocketpy exeception was thrown "
  103. << "this probably means pocketpy itself has a bug!\n";
  104. exit(2);
  105. }
  106. }
  107. PyObject* c_function_wrapper(VM* vm, ArgsView args) {
  108. LuaStyleFuncC f = py_cast<NativeFunc&>(vm, args[-2])._lua_f;
  109. //setup c stack
  110. struct pkpy_vm_wrapper w;
  111. ValueStackImpl c_stack = ValueStackImpl<PKPY_STACK_SIZE>();
  112. w.vm = vm;
  113. w.c_data = &c_stack;
  114. for (int i = 0; i < args.size(); i++)
  115. w.c_data->push(args[i]);
  116. int retc = f(&w);
  117. PyObject* ret = w.vm->None;
  118. propagate_if_errored(&w);
  119. if (retc == 1)
  120. ret = w.c_data->top();
  121. else if (retc > 1) {
  122. Tuple t = Tuple(retc);
  123. for (int i = 0; i < retc; i++) {
  124. int stack_index = (w.c_data->size() - retc) + i;
  125. t[i] = w.c_data->begin()[stack_index];
  126. }
  127. ret = py_var(w.vm, t);
  128. }
  129. return ret;
  130. }
  131. bool pkpy_push_function(struct pkpy_vm_wrapper* w, pkpy_function f) {
  132. ERRHANDLER_OPEN
  133. //TODO right now we just treat all c bound functions a varargs functions
  134. //do we want to change that?
  135. NativeFunc nf = NativeFunc(c_function_wrapper, -1, 0);
  136. nf._lua_f = (LuaStyleFuncC) f;
  137. w->c_data->push(py_var(w->vm, nf));
  138. return true;
  139. ERRHANDLER_CLOSE
  140. }
  141. bool pkpy_push_int(struct pkpy_vm_wrapper* w, int value) {
  142. ERRHANDLER_OPEN
  143. w->c_data->push(py_var(w->vm, value));
  144. return true;
  145. ERRHANDLER_CLOSE
  146. }
  147. bool pkpy_push_float(struct pkpy_vm_wrapper* w, double value) {
  148. ERRHANDLER_OPEN
  149. w->c_data->push(py_var(w->vm, value));
  150. return true;
  151. ERRHANDLER_CLOSE
  152. }
  153. bool pkpy_push_bool(struct pkpy_vm_wrapper* w, bool value) {
  154. ERRHANDLER_OPEN
  155. w->c_data->push(py_var(w->vm, value));
  156. return true;
  157. ERRHANDLER_CLOSE
  158. }
  159. bool pkpy_push_string(struct pkpy_vm_wrapper* w, const char* value) {
  160. ERRHANDLER_OPEN
  161. w->c_data->push(py_var(w->vm, value));
  162. return true;
  163. ERRHANDLER_CLOSE
  164. }
  165. bool pkpy_push_stringn(struct pkpy_vm_wrapper* w, const char* value, int length) {
  166. ERRHANDLER_OPEN
  167. Str s = Str(value, length);
  168. w->c_data->push(py_var(w->vm, s));
  169. return true;
  170. ERRHANDLER_CLOSE
  171. }
  172. bool pkpy_push_none(struct pkpy_vm_wrapper* w) {
  173. ERRHANDLER_OPEN
  174. w->c_data->push(w->vm->None);
  175. return true;
  176. ERRHANDLER_CLOSE
  177. }
  178. bool pkpy_set_global(struct pkpy_vm_wrapper* w, const char* name) {
  179. ERRHANDLER_OPEN
  180. w->vm->_main->attr().set(name, w->c_data->top());
  181. w->c_data->pop();
  182. return true;
  183. ERRHANDLER_CLOSE
  184. }
  185. //get global will also get bulitins
  186. bool pkpy_get_global(struct pkpy_vm_wrapper* w, const char* name) {
  187. ERRHANDLER_OPEN
  188. PyObject* o = w->vm->_main->attr().try_get(name);
  189. if (o == nullptr) {
  190. o = w->vm->builtins->attr().try_get(name);
  191. if (o == nullptr)
  192. throw Exception("AttributeError", "could not find requested global");
  193. }
  194. w->c_data->push(o);
  195. return true;
  196. ERRHANDLER_CLOSE
  197. }
  198. bool pkpy_call(struct pkpy_vm_wrapper* w, int argc) {
  199. ERRHANDLER_OPEN
  200. int callable_index = w->c_data->size() - argc - 1;
  201. PyObject* callable = w->c_data->begin()[callable_index];
  202. w->vm->s_data.push(callable);
  203. w->vm->s_data.push(PY_NULL);
  204. for (int i = 0; i < argc; i++)
  205. w->vm->s_data.push(w->c_data->begin()[callable_index + i + 1]);
  206. PyObject* o = w->vm->vectorcall(argc);
  207. w->c_data->shrink(argc + 1);
  208. unpack_return(w, o);
  209. return true;
  210. ERRHANDLER_CLOSE
  211. }
  212. bool pkpy_call_method(struct pkpy_vm_wrapper* w, const char* name, int argc) {
  213. ERRHANDLER_OPEN
  214. int self_index = w->c_data->size() - argc - 1;
  215. PyObject* self = w->c_data->begin()[self_index];
  216. PyObject* callable = w->vm->get_unbound_method(self, name, &self);
  217. w->vm->s_data.push(callable);
  218. w->vm->s_data.push(self);
  219. for (int i = 0; i < argc; i++)
  220. w->vm->s_data.push(w->c_data->begin()[self_index + i + 1]);
  221. PyObject* o = w->vm->vectorcall(argc);
  222. w->c_data->shrink(argc + 1);
  223. unpack_return(w, o);
  224. return true;
  225. ERRHANDLER_CLOSE
  226. }
  227. static int lua_to_cstack_index(int index, int size) {
  228. if (index < 0)
  229. index = size + index;
  230. return index;
  231. }
  232. bool pkpy_to_int(struct pkpy_vm_wrapper* w, int index, int* ret) {
  233. ERRHANDLER_OPEN
  234. index = lua_to_cstack_index(index, w->c_data->size());
  235. PyObject* o = w->c_data->begin()[index];
  236. if (ret != nullptr)
  237. *ret = py_cast<int>(w->vm, o);
  238. return true;
  239. ERRHANDLER_CLOSE
  240. }
  241. bool pkpy_to_float(struct pkpy_vm_wrapper* w, int index, double* ret) {
  242. ERRHANDLER_OPEN
  243. index = lua_to_cstack_index(index, w->c_data->size());
  244. PyObject* o = w->c_data->begin()[index];
  245. if (ret != nullptr)
  246. *ret = py_cast<double>(w->vm, o);
  247. return true;
  248. ERRHANDLER_CLOSE
  249. }
  250. bool pkpy_to_bool(struct pkpy_vm_wrapper* w, int index, bool* ret) {
  251. ERRHANDLER_OPEN
  252. index = lua_to_cstack_index(index, w->c_data->size());
  253. PyObject* o = w->c_data->begin()[index];
  254. if (ret != nullptr)
  255. *ret = py_cast<bool>(w->vm, o);
  256. return true;
  257. ERRHANDLER_CLOSE
  258. }
  259. bool pkpy_to_string(struct pkpy_vm_wrapper* w, int index, char** ret) {
  260. ERRHANDLER_OPEN
  261. index = lua_to_cstack_index(index, w->c_data->size());
  262. PyObject* o = w->c_data->begin()[index];
  263. if (ret != nullptr) {
  264. Str& s = py_cast<Str&>(w->vm, o);
  265. *ret = s.c_str_dup();
  266. }
  267. return true;
  268. ERRHANDLER_CLOSE
  269. }
  270. bool pkpy_is_int(struct pkpy_vm_wrapper* w, int index) {
  271. index = lua_to_cstack_index(index, w->c_data->size());
  272. PyObject* o = w->c_data->begin()[index];
  273. return is_type(o, w->vm->tp_int);
  274. }
  275. bool pkpy_is_float(struct pkpy_vm_wrapper* w, int index) {
  276. index = lua_to_cstack_index(index, w->c_data->size());
  277. PyObject* o = w->c_data->begin()[index];
  278. return is_type(o, w->vm->tp_float);
  279. }
  280. bool pkpy_is_bool(struct pkpy_vm_wrapper* w, int index) {
  281. index = lua_to_cstack_index(index, w->c_data->size());
  282. PyObject* o = w->c_data->begin()[index];
  283. return is_type(o, w->vm->tp_bool);
  284. }
  285. bool pkpy_is_string(struct pkpy_vm_wrapper* w, int index) {
  286. index = lua_to_cstack_index(index, w->c_data->size());
  287. PyObject* o = w->c_data->begin()[index];
  288. return is_type(o, w->vm->tp_str);
  289. }
  290. bool pkpy_is_none(struct pkpy_vm_wrapper* w, int index) {
  291. index = lua_to_cstack_index(index, w->c_data->size());
  292. PyObject* o = w->c_data->begin()[index];
  293. return o == w->vm->None;
  294. }
  295. bool pkpy_check_stack(struct pkpy_vm_wrapper* w, int free) {
  296. return free + w->c_data->size() <= PKPY_STACK_SIZE;
  297. }
  298. int pkpy_stack_size(struct pkpy_vm_wrapper* w) {
  299. return w->c_data->size();
  300. }
  301. bool pkpy_pop(struct pkpy_vm_wrapper* w, int n) {
  302. w->c_data->shrink(n);
  303. return true;
  304. }