pocketpy_c.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. return true;
  81. ERRHANDLER_CLOSE
  82. }
  83. void pkpy_vm_destroy(struct pkpy_vm_wrapper* w) {
  84. delete w->vm;
  85. delete w->c_data;
  86. free(w);
  87. }
  88. static void propagate_if_errored(struct pkpy_vm_wrapper* w) {
  89. try {
  90. if (w->c_data->size() == 0 || w->c_data->top() != nullptr)
  91. return;
  92. w->c_data->pop();
  93. Exception& e = py_cast<Exception&>(w->vm, w->c_data->top());
  94. w->c_data->pop();
  95. throw e;
  96. } catch(Exception& e) {
  97. throw;
  98. } catch(...) {
  99. std::cerr << "ERROR: a non pocketpy exeception was thrown "
  100. << "this probably means pocketpy itself has a bug!\n";
  101. exit(2);
  102. }
  103. }
  104. PyObject* c_function_wrapper(VM* vm, ArgsView args) {
  105. LuaStyleFuncC f = py_cast<NativeFunc&>(vm, args[-2])._lua_f;
  106. //setup c stack
  107. struct pkpy_vm_wrapper w;
  108. ValueStackImpl c_stack = ValueStackImpl<PKPY_STACK_SIZE>();
  109. w.vm = vm;
  110. w.c_data = &c_stack;
  111. for (int i = 0; i < args.size(); i++)
  112. w.c_data->push(args[i]);
  113. int retc = f(&w);
  114. PyObject* ret = w.vm->None;
  115. propagate_if_errored(&w);
  116. if (retc == 1)
  117. ret = w.c_data->top();
  118. else if (retc > 1) {
  119. Tuple t = Tuple(retc);
  120. for (int i = 0; i < retc; i++) {
  121. int stack_index = (w.c_data->size() - retc) + i;
  122. t[i] = w.c_data->begin()[stack_index];
  123. }
  124. ret = py_var(w.vm, t);
  125. }
  126. return ret;
  127. }
  128. bool pkpy_push_function(struct pkpy_vm_wrapper* w, pkpy_function f) {
  129. ERRHANDLER_OPEN
  130. //TODO right now we just treat all c bound functions a varargs functions
  131. //do we want to change that?
  132. NativeFunc nf = NativeFunc(c_function_wrapper, -1, 0);
  133. nf._lua_f = (LuaStyleFuncC) f;
  134. w->c_data->push(py_var(w->vm, nf));
  135. return true;
  136. ERRHANDLER_CLOSE
  137. }
  138. bool pkpy_push_int(struct pkpy_vm_wrapper* w, int value) {
  139. ERRHANDLER_OPEN
  140. w->c_data->push(py_var(w->vm, value));
  141. return true;
  142. ERRHANDLER_CLOSE
  143. }
  144. bool pkpy_push_float(struct pkpy_vm_wrapper* w, double value) {
  145. ERRHANDLER_OPEN
  146. w->c_data->push(py_var(w->vm, value));
  147. return true;
  148. ERRHANDLER_CLOSE
  149. }
  150. bool pkpy_push_bool(struct pkpy_vm_wrapper* w, bool value) {
  151. ERRHANDLER_OPEN
  152. w->c_data->push(py_var(w->vm, value));
  153. return true;
  154. ERRHANDLER_CLOSE
  155. }
  156. bool pkpy_push_string(struct pkpy_vm_wrapper* w, const char* value) {
  157. ERRHANDLER_OPEN
  158. w->c_data->push(py_var(w->vm, value));
  159. return true;
  160. ERRHANDLER_CLOSE
  161. }
  162. bool pkpy_push_stringn(struct pkpy_vm_wrapper* w, const char* value, int length) {
  163. ERRHANDLER_OPEN
  164. Str s = Str(value, length);
  165. w->c_data->push(py_var(w->vm, s));
  166. return true;
  167. ERRHANDLER_CLOSE
  168. }
  169. bool pkpy_push_none(struct pkpy_vm_wrapper* w) {
  170. ERRHANDLER_OPEN
  171. w->c_data->push(w->vm->None);
  172. return true;
  173. ERRHANDLER_CLOSE
  174. }
  175. bool pkpy_set_global(struct pkpy_vm_wrapper* w, const char* name) {
  176. ERRHANDLER_OPEN
  177. w->vm->_main->attr().set(name, w->c_data->top());
  178. w->c_data->pop();
  179. return true;
  180. ERRHANDLER_CLOSE
  181. }
  182. //get global will also get bulitins
  183. bool pkpy_get_global(struct pkpy_vm_wrapper* w, const char* name) {
  184. ERRHANDLER_OPEN
  185. PyObject* o = w->vm->_main->attr().try_get(name);
  186. if (o == nullptr) {
  187. o = w->vm->builtins->attr().try_get(name);
  188. if (o == nullptr)
  189. throw Exception("AttributeError", "could not find requested global");
  190. }
  191. w->c_data->push(o);
  192. return true;
  193. ERRHANDLER_CLOSE
  194. }
  195. bool pkpy_call(struct pkpy_vm_wrapper* w, int argc) {
  196. ERRHANDLER_OPEN
  197. int callable_index = w->c_data->size() - argc - 1;
  198. PyObject* callable = w->c_data->begin()[callable_index];
  199. w->vm->s_data.push(callable);
  200. w->vm->s_data.push(PY_NULL);
  201. for (int i = 0; i < argc; i++)
  202. w->vm->s_data.push(w->c_data->begin()[callable_index + i + 1]);
  203. PyObject* o = w->vm->vectorcall(argc);
  204. w->c_data->shrink(argc + 1);
  205. unpack_return(w, o);
  206. return true;
  207. ERRHANDLER_CLOSE
  208. }
  209. bool pkpy_call_method(struct pkpy_vm_wrapper* w, const char* name, int argc) {
  210. ERRHANDLER_OPEN
  211. int self_index = w->c_data->size() - argc - 1;
  212. PyObject* self = w->c_data->begin()[self_index];
  213. PyObject* callable = w->vm->get_unbound_method(self, name, &self);
  214. w->vm->s_data.push(callable);
  215. w->vm->s_data.push(self);
  216. for (int i = 0; i < argc; i++)
  217. w->vm->s_data.push(w->c_data->begin()[self_index + i + 1]);
  218. PyObject* o = w->vm->vectorcall(argc);
  219. w->c_data->shrink(argc + 1);
  220. unpack_return(w, o);
  221. return true;
  222. ERRHANDLER_CLOSE
  223. }
  224. static int lua_to_cstack_index(int index, int size) {
  225. if (index < 0)
  226. index = size + index;
  227. return index;
  228. }
  229. bool pkpy_to_int(struct pkpy_vm_wrapper* w, int index, int* ret) {
  230. ERRHANDLER_OPEN
  231. index = lua_to_cstack_index(index, w->c_data->size());
  232. PyObject* o = w->c_data->begin()[index];
  233. if (ret != nullptr)
  234. *ret = py_cast<int>(w->vm, o);
  235. return true;
  236. ERRHANDLER_CLOSE
  237. }
  238. bool pkpy_to_float(struct pkpy_vm_wrapper* w, int index, double* ret) {
  239. ERRHANDLER_OPEN
  240. index = lua_to_cstack_index(index, w->c_data->size());
  241. PyObject* o = w->c_data->begin()[index];
  242. if (ret != nullptr)
  243. *ret = py_cast<double>(w->vm, o);
  244. return true;
  245. ERRHANDLER_CLOSE
  246. }
  247. bool pkpy_to_bool(struct pkpy_vm_wrapper* w, int index, bool* ret) {
  248. ERRHANDLER_OPEN
  249. index = lua_to_cstack_index(index, w->c_data->size());
  250. PyObject* o = w->c_data->begin()[index];
  251. if (ret != nullptr)
  252. *ret = py_cast<bool>(w->vm, o);
  253. return true;
  254. ERRHANDLER_CLOSE
  255. }
  256. bool pkpy_to_string(struct pkpy_vm_wrapper* w, int index, char** ret) {
  257. ERRHANDLER_OPEN
  258. index = lua_to_cstack_index(index, w->c_data->size());
  259. PyObject* o = w->c_data->begin()[index];
  260. if (ret != nullptr) {
  261. Str& s = py_cast<Str&>(w->vm, o);
  262. *ret = s.c_str_dup();
  263. }
  264. return true;
  265. ERRHANDLER_CLOSE
  266. }
  267. bool pkpy_is_int(struct pkpy_vm_wrapper* w, int index) {
  268. index = lua_to_cstack_index(index, w->c_data->size());
  269. PyObject* o = w->c_data->begin()[index];
  270. return is_type(o, w->vm->tp_int);
  271. }
  272. bool pkpy_is_float(struct pkpy_vm_wrapper* w, int index) {
  273. index = lua_to_cstack_index(index, w->c_data->size());
  274. PyObject* o = w->c_data->begin()[index];
  275. return is_type(o, w->vm->tp_float);
  276. }
  277. bool pkpy_is_bool(struct pkpy_vm_wrapper* w, int index) {
  278. index = lua_to_cstack_index(index, w->c_data->size());
  279. PyObject* o = w->c_data->begin()[index];
  280. return is_type(o, w->vm->tp_bool);
  281. }
  282. bool pkpy_is_string(struct pkpy_vm_wrapper* w, int index) {
  283. index = lua_to_cstack_index(index, w->c_data->size());
  284. PyObject* o = w->c_data->begin()[index];
  285. return is_type(o, w->vm->tp_str);
  286. }
  287. bool pkpy_is_none(struct pkpy_vm_wrapper* w, int index) {
  288. index = lua_to_cstack_index(index, w->c_data->size());
  289. PyObject* o = w->c_data->begin()[index];
  290. return o == w->vm->None;
  291. }
  292. bool pkpy_check_stack(struct pkpy_vm_wrapper* w, int free) {
  293. return free + w->c_data->size() <= PKPY_STACK_SIZE;
  294. }
  295. int pkpy_stack_size(struct pkpy_vm_wrapper* w) {
  296. return w->c_data->size();
  297. }
  298. bool pkpy_pop(struct pkpy_vm_wrapper* w, int n) {
  299. w->c_data->shrink(n);
  300. return true;
  301. }