js_browser.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Browser bindings for MuJS: console, alert, document.getElementById,
  2. * textContent and location. Kept deliberately small; timers, events and
  3. * richer DOM mutation belong to the next JavaScript phase. */
  4. #include <setjmp.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "ucmd_lib.h"
  9. #include "browser_html.h"
  10. #include "js_browser.h"
  11. #include "mujs.h"
  12. /* Instructions per script/handler. MuJS consumes the budget globally, so
  13. * it is reset before every entry; a runaway loop becomes a catchable
  14. * "script ran too long" exception instead of a frozen browser. */
  15. #define JS_RUN_LIMIT 8000000
  16. static js_State *J;
  17. static struct br_doc *D;
  18. static struct js_browser_host host;
  19. static const char *current_url;
  20. static jmp_buf panic_env;
  21. static int panic_armed;
  22. static void report_cb(js_State *state, const char *message)
  23. {
  24. (void)state;
  25. if (host.report)
  26. host.report(message);
  27. }
  28. static void panic_cb(js_State *state)
  29. {
  30. (void)state;
  31. if (panic_armed)
  32. longjmp(panic_env, 1);
  33. if (host.report)
  34. host.report("uncaught JavaScript exception");
  35. }
  36. static unsigned int node_of(void *p)
  37. {
  38. return (unsigned int)(uintptr_t)p - 1;
  39. }
  40. static void node_push_text(js_State *state, unsigned int node)
  41. {
  42. unsigned int cap = D->text_used + 1;
  43. char *buf;
  44. unsigned int len;
  45. if (cap < 32)
  46. cap = 32;
  47. buf = (char *)malloc(cap);
  48. if (!buf) {
  49. js_pushstring(state, "");
  50. return;
  51. }
  52. len = br_text_content(D, node, buf, cap);
  53. js_pushlstring(state, buf, (int)len);
  54. free(buf);
  55. }
  56. /* Userdata property hooks: every element wrapper carries node + 1. */
  57. static int node_has(js_State *state, void *p, const char *name)
  58. {
  59. unsigned int node = node_of(p);
  60. struct br_node *n;
  61. if (!D || node >= D->nnode)
  62. return 0;
  63. n = &D->nodes[node];
  64. if (!strcmp(name, "textContent")) {
  65. node_push_text(state, node);
  66. return 1;
  67. }
  68. if (!strcmp(name, "id")) {
  69. js_pushstring(state, n->id_off ? D->attrs + n->id_off : "");
  70. return 1;
  71. }
  72. if (!strcmp(name, "tagName")) {
  73. js_pushstring(state, br_tag_name(n->tag));
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static int node_put(js_State *state, void *p, const char *name)
  79. {
  80. unsigned int node = node_of(p);
  81. if (!D || node >= D->nnode)
  82. return 0;
  83. if (!strcmp(name, "textContent")) {
  84. const char *text = js_tostring(state, -1);
  85. unsigned int len = (unsigned int)strlen(text);
  86. if (br_set_text_content(D, node, text, len) != 0) {
  87. if (host.status)
  88. host.status("Page text arena is full", 1);
  89. } else if (host.changed) {
  90. host.changed();
  91. }
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. static int node_delete(js_State *state, void *p, const char *name)
  97. {
  98. (void)state;
  99. (void)p;
  100. (void)name;
  101. return 0;
  102. }
  103. static void doc_getelementbyid(js_State *state)
  104. {
  105. const char *id = js_tostring(state, 1);
  106. unsigned int handle = br_find_id(D, id);
  107. if (!handle) {
  108. js_pushnull(state);
  109. return;
  110. }
  111. /* js_newuserdatax() pops the top value as the userdata prototype; push
  112. * a placeholder so it cannot consume the id argument. */
  113. js_pushnull(state);
  114. js_newuserdatax(state, "node", (void *)(uintptr_t)handle,
  115. node_has, node_put, node_delete, 0);
  116. }
  117. static void console_log(js_State *state)
  118. {
  119. int i;
  120. int n = js_gettop(state);
  121. for (i = 1; i < n; i++) {
  122. if (i > 1)
  123. u_putc(' ');
  124. u_puts(js_tostring(state, i));
  125. }
  126. u_putc('\n');
  127. }
  128. static void js_alert(js_State *state)
  129. {
  130. char msg[160];
  131. unsigned int used = 0;
  132. int i;
  133. int n = js_gettop(state);
  134. for (i = 1; i < n; i++) {
  135. const char *s = js_tostring(state, i);
  136. unsigned int k;
  137. if (i > 1 && used + 1 < sizeof(msg))
  138. msg[used++] = ' ';
  139. for (k = 0; s[k] && used + 1 < sizeof(msg); k++)
  140. msg[used++] = s[k];
  141. }
  142. msg[used] = '\0';
  143. if (host.status)
  144. host.status(msg, 0);
  145. }
  146. static void location_get(js_State *state)
  147. {
  148. js_pushstring(state, current_url ? current_url : "");
  149. }
  150. static void location_set(js_State *state)
  151. {
  152. const char *url = js_tostring(state, 1);
  153. if (host.navigate)
  154. host.navigate(url);
  155. }
  156. static void install_globals(js_State *state)
  157. {
  158. js_newobject(state);
  159. js_newcfunction(state, doc_getelementbyid, "getElementById", 1);
  160. js_setproperty(state, -2, "getElementById");
  161. js_defglobal(state, "document", JS_DONTENUM);
  162. js_newobject(state);
  163. js_newcfunction(state, console_log, "log", 0);
  164. js_setproperty(state, -2, "log");
  165. js_defglobal(state, "console", JS_DONTENUM);
  166. js_newcfunction(state, js_alert, "alert", 1);
  167. js_defglobal(state, "alert", JS_DONTENUM);
  168. js_newobject(state);
  169. js_newcfunction(state, location_get, "get href", 0);
  170. js_newcfunction(state, location_set, "set href", 1);
  171. js_defaccessor(state, -3, "href", 0);
  172. js_defglobal(state, "location", JS_DONTENUM);
  173. js_pushglobal(state);
  174. js_defglobal(state, "window", JS_DONTENUM);
  175. js_pushglobal(state);
  176. js_defglobal(state, "globalThis", JS_DONTENUM);
  177. }
  178. int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
  179. {
  180. if (!doc || !h)
  181. return -1;
  182. D = doc;
  183. host = *h;
  184. current_url = h->page_url;
  185. panic_armed = 0;
  186. /* Classic scripts are sloppy by default in browsers; strict mode is
  187. * opt-in via a "use strict" directive. */
  188. J = js_newstate(0, 0, 0);
  189. if (!J)
  190. return -1;
  191. js_setreport(J, report_cb);
  192. js_atpanic(J, panic_cb);
  193. install_globals(J);
  194. return 0;
  195. }
  196. void js_browser_stop(void)
  197. {
  198. if (J)
  199. js_freestate(J);
  200. J = 0;
  201. D = 0;
  202. current_url = 0;
  203. panic_armed = 0;
  204. }
  205. int js_browser_active(void)
  206. {
  207. return J != 0;
  208. }
  209. static void report_top(js_State *state)
  210. {
  211. const char *msg = js_trystring(state, -1, "JavaScript error");
  212. if (host.report)
  213. host.report(msg);
  214. js_pop(state, 1);
  215. }
  216. int js_browser_run(const char *source, const char *url)
  217. {
  218. int r;
  219. if (!J || !D)
  220. return -1;
  221. js_setlimit(J, JS_RUN_LIMIT, 0);
  222. panic_armed = 1;
  223. if (setjmp(panic_env)) {
  224. panic_armed = 0;
  225. if (host.report)
  226. host.report("uncaught JavaScript exception");
  227. /* The try/exception stack is unusable after a panic. Leak the
  228. * state; the page heap reset on navigation reclaims it. */
  229. J = 0;
  230. D = 0;
  231. return -1;
  232. }
  233. if (js_ploadstring(J, url ? url : "[inline]", source)) {
  234. report_top(J);
  235. panic_armed = 0;
  236. return -1;
  237. }
  238. js_pushundefined(J);
  239. r = js_pcall(J, 0);
  240. if (r) {
  241. report_top(J);
  242. panic_armed = 0;
  243. return -1;
  244. }
  245. js_pop(J, 1);
  246. panic_armed = 0;
  247. return 0;
  248. }