pocketpy_c.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_none(struct pkpy_vm_wrapper* w) {
  183. ERRHANDLER_OPEN
  184. w->c_data->push(w->vm->None);
  185. return true;
  186. ERRHANDLER_CLOSE
  187. }
  188. bool pkpy_set_global(struct pkpy_vm_wrapper* w, const char* name) {
  189. ERRHANDLER_OPEN
  190. w->vm->_main->attr().set(name, w->c_data->top());
  191. w->c_data->pop();
  192. return true;
  193. ERRHANDLER_CLOSE
  194. }
  195. //get global will also get bulitins
  196. bool pkpy_get_global(struct pkpy_vm_wrapper* w, const char* name) {
  197. ERRHANDLER_OPEN
  198. PyObject* o = w->vm->_main->attr().try_get(name);
  199. if (o == nullptr) {
  200. o = w->vm->builtins->attr().try_get(name);
  201. if (o == nullptr)
  202. throw Exception("AttributeError", "could not find requested global");
  203. }
  204. w->c_data->push(o);
  205. return true;
  206. ERRHANDLER_CLOSE
  207. }
  208. bool pkpy_call(struct pkpy_vm_wrapper* w, int argc) {
  209. ERRHANDLER_OPEN
  210. int callable_index = w->c_data->size() - argc - 1;
  211. PyObject* callable = w->c_data->begin()[callable_index];
  212. w->vm->s_data.push(callable);
  213. w->vm->s_data.push(PY_NULL);
  214. for (int i = 0; i < argc; i++)
  215. w->vm->s_data.push(w->c_data->begin()[callable_index + i + 1]);
  216. PyObject* o = w->vm->vectorcall(argc);
  217. w->c_data->shrink(argc + 1);
  218. unpack_return(w, o);
  219. return true;
  220. ERRHANDLER_CLOSE
  221. }
  222. bool pkpy_call_method(struct pkpy_vm_wrapper* w, const char* name, int argc) {
  223. ERRHANDLER_OPEN
  224. int self_index = w->c_data->size() - argc - 1;
  225. PyObject* self = w->c_data->begin()[self_index];
  226. PyObject* callable = w->vm->get_unbound_method(self, name, &self);
  227. w->vm->s_data.push(callable);
  228. w->vm->s_data.push(self);
  229. for (int i = 0; i < argc; i++)
  230. w->vm->s_data.push(w->c_data->begin()[self_index + i + 1]);
  231. PyObject* o = w->vm->vectorcall(argc);
  232. w->c_data->shrink(argc + 1);
  233. unpack_return(w, o);
  234. return true;
  235. ERRHANDLER_CLOSE
  236. }
  237. static int lua_to_cstack_index(int index, int size) {
  238. if (index < 0)
  239. index = size + index;
  240. return index;
  241. }
  242. bool pkpy_to_int(struct pkpy_vm_wrapper* w, int index, int* ret) {
  243. ERRHANDLER_OPEN
  244. index = lua_to_cstack_index(index, w->c_data->size());
  245. PyObject* o = w->c_data->begin()[index];
  246. if (ret != nullptr)
  247. *ret = py_cast<int>(w->vm, o);
  248. return true;
  249. ERRHANDLER_CLOSE
  250. }
  251. bool pkpy_to_float(struct pkpy_vm_wrapper* w, int index, double* ret) {
  252. ERRHANDLER_OPEN
  253. index = lua_to_cstack_index(index, w->c_data->size());
  254. PyObject* o = w->c_data->begin()[index];
  255. if (ret != nullptr)
  256. *ret = py_cast<double>(w->vm, o);
  257. return true;
  258. ERRHANDLER_CLOSE
  259. }
  260. bool pkpy_to_bool(struct pkpy_vm_wrapper* w, int index, bool* ret) {
  261. ERRHANDLER_OPEN
  262. index = lua_to_cstack_index(index, w->c_data->size());
  263. PyObject* o = w->c_data->begin()[index];
  264. if (ret != nullptr)
  265. *ret = py_cast<bool>(w->vm, o);
  266. return true;
  267. ERRHANDLER_CLOSE
  268. }
  269. bool pkpy_to_string(struct pkpy_vm_wrapper* w, int index, char** ret) {
  270. ERRHANDLER_OPEN
  271. index = lua_to_cstack_index(index, w->c_data->size());
  272. PyObject* o = w->c_data->begin()[index];
  273. if (ret != nullptr) {
  274. Str& s = py_cast<Str&>(w->vm, o);
  275. *ret = manage_string(w, s.c_str_dup());
  276. }
  277. return true;
  278. ERRHANDLER_CLOSE
  279. }
  280. bool pkpy_is_int(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_int);
  284. }
  285. bool pkpy_is_float(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_float);
  289. }
  290. bool pkpy_is_bool(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 is_type(o, w->vm->tp_bool);
  294. }
  295. bool pkpy_is_string(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_str);
  299. }
  300. bool pkpy_is_none(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 o == w->vm->None;
  304. }
  305. bool pkpy_check_stack(struct pkpy_vm_wrapper* w, int free) {
  306. return free + w->c_data->size() <= PKPY_STACK_SIZE;
  307. }
  308. int pkpy_stack_size(struct pkpy_vm_wrapper* w) {
  309. return w->c_data->size();
  310. }
  311. bool pkpy_pop(struct pkpy_vm_wrapper* w, int n) {
  312. w->c_data->shrink(n);
  313. return true;
  314. }