pocketpy_c.cpp 11 KB

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