js_browser.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /* Browser bindings for MuJS: the DOM layer (interned node wrappers,
  2. * attributes, query selectors, classList/dataset/style, innerHTML, timers)
  3. * plus document/window/location/navigator globals. */
  4. #include <setjmp.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "ucmd_lib.h"
  10. #include "browser_html.h"
  11. #include "js_browser.h"
  12. #include "mujs.h"
  13. /* Instructions per script/handler. MuJS consumes the budget globally, so
  14. * it is reset before every entry; a runaway loop becomes a catchable
  15. * "script ran too long" exception instead of a frozen browser. */
  16. #define JS_RUN_LIMIT 8000000
  17. #define JS_TIMERS 16
  18. struct js_timer {
  19. int used;
  20. int repeat;
  21. unsigned int deadline;
  22. unsigned int interval;
  23. char ref[32];
  24. };
  25. static struct js_timer timers[JS_TIMERS];
  26. static js_State *J;
  27. static struct br_doc *D;
  28. static struct js_browser_host host;
  29. static const char *current_url;
  30. static jmp_buf panic_env;
  31. static int panic_armed;
  32. static void report_cb(js_State *state, const char *message)
  33. {
  34. (void)state;
  35. if (host.report)
  36. host.report(message);
  37. }
  38. static void panic_cb(js_State *state)
  39. {
  40. (void)state;
  41. if (panic_armed)
  42. longjmp(panic_env, 1);
  43. if (host.report)
  44. host.report("uncaught JavaScript exception");
  45. }
  46. static unsigned int node_of(void *p)
  47. {
  48. return (unsigned int)(uintptr_t)p - 1;
  49. }
  50. static int node_has(js_State *state, void *p, const char *name);
  51. static int node_put(js_State *state, void *p, const char *name);
  52. static int node_delete(js_State *state, void *p, const char *name);
  53. static void push_element_url(js_State *state, unsigned int node,
  54. const char *name);
  55. /* One wrapper per DOM node: the same element must have the same object
  56. * identity on every access, or library caches (jQuery expando data, Sizzle)
  57. * silently split into disconnected copies. */
  58. static void push_node(js_State *state, unsigned int handle)
  59. {
  60. char key[24];
  61. if (!handle) {
  62. js_pushnull(state);
  63. return;
  64. }
  65. snprintf(key, sizeof(key), "jsonnode_%u", handle);
  66. js_getregistry(state, key);
  67. if (!js_isundefined(state, -1))
  68. return;
  69. js_pop(state, 1);
  70. /* js_newuserdatax() pops a prototype from the top. */
  71. js_pushnull(state);
  72. js_newuserdatax(state, "node", (void *)(uintptr_t)handle,
  73. node_has, node_put, node_delete, 0);
  74. js_copy(state, -1);
  75. js_setregistry(state, key);
  76. }
  77. static unsigned int arg_node(js_State *state, int idx)
  78. {
  79. if (!js_isuserdata(state, idx, "node"))
  80. js_typeerror(state, "not a DOM node");
  81. return (unsigned int)(uintptr_t)js_touserdata(state, idx, "node");
  82. }
  83. static void def_func_data(js_State *state, const char *name,
  84. js_CFunction fn, int length, unsigned int handle)
  85. {
  86. js_newcfunctionx(state, fn, name, length,
  87. (void *)(uintptr_t)handle, 0);
  88. js_setproperty(state, -2, name);
  89. }
  90. static void def_func(js_State *state, const char *name,
  91. js_CFunction fn, int length)
  92. {
  93. js_newcfunction(state, fn, name, length);
  94. js_setproperty(state, -2, name);
  95. }
  96. static void def_global_func(js_State *state, const char *name,
  97. js_CFunction fn, int length)
  98. {
  99. js_newcfunction(state, fn, name, length);
  100. js_defglobal(state, name, JS_DONTENUM);
  101. }
  102. static void push_func_data(js_State *state, const char *name,
  103. js_CFunction fn, int length, unsigned int handle)
  104. {
  105. js_newcfunctionx(state, fn, name, length,
  106. (void *)(uintptr_t)handle, 0);
  107. }
  108. static const char *node_attr(unsigned int node, const char *name)
  109. {
  110. return br_find_attr(D, node, name);
  111. }
  112. static void push_attr(js_State *state, unsigned int node, const char *name,
  113. int null_if_missing)
  114. {
  115. const char *s = node_attr(node, name);
  116. if (s)
  117. js_pushstring(state, s);
  118. else if (null_if_missing)
  119. js_pushnull(state);
  120. else
  121. js_pushstring(state, "");
  122. }
  123. static unsigned int node_name(unsigned int node, char *buf, unsigned int cap)
  124. {
  125. struct br_node *n;
  126. const char *s;
  127. unsigned int i = 0;
  128. if (!D || node >= D->nnode || cap == 0)
  129. return 0;
  130. n = &D->nodes[node];
  131. if (n->type == BR_NODE_TEXT)
  132. s = "#text";
  133. else if (n->type == BR_NODE_COMMENT)
  134. s = "#comment";
  135. else if (n->type == BR_NODE_FRAGMENT)
  136. s = "#document-fragment";
  137. else if (n->tag == BR_TAG_UNKNOWN && n->name_off)
  138. s = D->attrs + n->name_off;
  139. else
  140. s = br_tag_name(n->tag);
  141. while (s && *s && i + 1 < cap) {
  142. char c = *s++;
  143. if (c >= 'a' && c <= 'z')
  144. c = (char)(c - 'a' + 'A');
  145. buf[i++] = c;
  146. }
  147. buf[i] = '\0';
  148. return i;
  149. }
  150. static unsigned int node_type_value(unsigned int node)
  151. {
  152. struct br_node *n;
  153. if (!D || node >= D->nnode)
  154. return 0;
  155. n = &D->nodes[node];
  156. if (n->type == BR_NODE_TEXT)
  157. return 3;
  158. if (n->type == BR_NODE_COMMENT)
  159. return 8;
  160. if (n->type == BR_NODE_FRAGMENT)
  161. return 11;
  162. return 1;
  163. }
  164. static void push_child_list(js_State *state, unsigned int node, int elements)
  165. {
  166. struct br_node *n;
  167. unsigned int c, i = 0;
  168. js_newarray(state);
  169. if (!D || node >= D->nnode)
  170. return;
  171. n = &D->nodes[node];
  172. for (c = n->first; c; c = D->nodes[c].next) {
  173. if (elements && D->nodes[c].type != BR_NODE_ELEM)
  174. continue;
  175. push_node(state, c + 1);
  176. js_setindex(state, -2, (int)i++);
  177. }
  178. }
  179. static unsigned int node_prev_sibling(unsigned int node)
  180. {
  181. unsigned int p, c, prev = 0;
  182. if (!D || node >= D->nnode)
  183. return 0;
  184. p = D->nodes[node].parent;
  185. for (c = D->nodes[p].first; c && c != node; c = D->nodes[c].next)
  186. prev = c;
  187. return prev;
  188. }
  189. /* ---- inline style and dataset objects ---- */
  190. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  191. {
  192. unsigned int i;
  193. for (i = 0; i < n; i++) {
  194. char ca = a[i], cb = b[i];
  195. if (ca >= 'A' && ca <= 'Z')
  196. ca = (char)(ca - 'A' + 'a');
  197. if (cb >= 'A' && cb <= 'Z')
  198. cb = (char)(cb - 'A' + 'a');
  199. if (ca != cb)
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. /* camelCase JS property -> CSS property ("marginTop" -> "margin-top"). */
  205. static void css_name(const char *name, char *out, unsigned int cap)
  206. {
  207. unsigned int n = 0, i;
  208. if (!strcmp(name, "cssFloat")) {
  209. if (cap > 6)
  210. memcpy(out, "float", 6);
  211. else if (cap)
  212. out[0] = '\0';
  213. return;
  214. }
  215. for (i = 0; name[i] && n + 2 < cap; i++) {
  216. char c = name[i];
  217. if (c >= 'A' && c <= 'Z') {
  218. if (n == 0 || out[n - 1] != '-')
  219. out[n++] = '-';
  220. out[n++] = (char)(c - 'A' + 'a');
  221. } else {
  222. out[n++] = c;
  223. }
  224. }
  225. if (cap)
  226. out[n] = '\0';
  227. }
  228. /* camelCase dataset property -> attribute name ("fooBar" -> "data-foo-bar"). */
  229. static void data_name(const char *name, char *out, unsigned int cap)
  230. {
  231. unsigned int n = 0, i;
  232. for (i = 0; i < 5 && n + 1 < cap; i++)
  233. out[n++] = "data-"[i];
  234. for (i = 0; name[i] && n + 2 < cap; i++) {
  235. char c = name[i];
  236. if (c >= 'A' && c <= 'Z') {
  237. out[n++] = '-';
  238. c = (char)(c - 'A' + 'a');
  239. }
  240. out[n++] = c;
  241. }
  242. if (cap)
  243. out[n] = '\0';
  244. }
  245. static int style_lookup(const char *style, const char *prop, char *out,
  246. unsigned int cap)
  247. {
  248. unsigned int i = 0, nl = (unsigned int)strlen(prop);
  249. while (style && style[i]) {
  250. unsigned int ns, ne, vs, ve, a, b, k;
  251. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  252. i++;
  253. if (!style[i])
  254. break;
  255. ns = i;
  256. while (style[i] && style[i] != ':' && style[i] != ';')
  257. i++;
  258. ne = i;
  259. if (style[i] != ':') {
  260. if (style[i] == ';')
  261. i++;
  262. continue;
  263. }
  264. i++;
  265. while (style[i] == ' ' || style[i] == '\t')
  266. i++;
  267. vs = i;
  268. while (style[i] && style[i] != ';')
  269. i++;
  270. ve = i;
  271. a = ns;
  272. b = ne;
  273. while (a < b && (style[a] == ' ' || style[a] == '\t'))
  274. a++;
  275. while (b > a && (style[b - 1] == ' ' || style[b - 1] == '\t'))
  276. b--;
  277. while (ve > vs && (style[ve - 1] == ' ' || style[ve - 1] == '\t'))
  278. ve--;
  279. if (b - a == nl && ci_eq_n(style + a, prop, nl)) {
  280. for (k = 0; k < ve - vs && k + 1 < cap; k++)
  281. out[k] = style[vs + k];
  282. out[k < cap ? k : cap - 1] = '\0';
  283. return 1;
  284. }
  285. }
  286. return 0;
  287. }
  288. static unsigned int style_count(const char *style)
  289. {
  290. unsigned int i = 0, n = 0;
  291. while (style && style[i]) {
  292. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  293. i++;
  294. if (!style[i])
  295. break;
  296. while (style[i] && style[i] != ';')
  297. i++;
  298. n++;
  299. }
  300. return n;
  301. }
  302. static unsigned int style_span(const char *style, unsigned int i)
  303. {
  304. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  305. i++;
  306. return i;
  307. }
  308. static void style_update(unsigned int node, const char *prop,
  309. const char *value, int remove)
  310. {
  311. const char *old = br_find_attr(D, node, "style");
  312. char arr[512];
  313. unsigned int n = 0, i = 0, nl = (unsigned int)strlen(prop);
  314. int replaced = 0;
  315. if (remove && !old)
  316. return;
  317. while (old && old[i] && n + 2 < sizeof(arr)) {
  318. unsigned int start = style_span(old, i);
  319. unsigned int ns, ne, ve, a, b, k;
  320. i = start;
  321. if (!old[i])
  322. break;
  323. ns = i;
  324. while (old[i] && old[i] != ':' && old[i] != ';')
  325. i++;
  326. ne = i;
  327. if (old[i] == ':') {
  328. i++;
  329. while (old[i] == ' ' || old[i] == '\t')
  330. i++;
  331. while (old[i] && old[i] != ';')
  332. i++;
  333. }
  334. ve = i;
  335. a = ns;
  336. b = ne;
  337. while (a < b && (old[a] == ' ' || old[a] == '\t'))
  338. a++;
  339. while (b > a && (old[b - 1] == ' ' || old[b - 1] == '\t'))
  340. b--;
  341. if (b - a == nl && ci_eq_n(old + a, prop, nl)) {
  342. replaced = 1;
  343. if (remove || !value || !value[0])
  344. continue;
  345. }
  346. for (k = ns; k < ve && n + 1 < sizeof(arr); k++)
  347. arr[n++] = old[k];
  348. if (n + 1 < sizeof(arr))
  349. arr[n++] = ';';
  350. }
  351. if (replaced && (remove || !value || !value[0])) {
  352. arr[n] = '\0';
  353. br_set_attr(D, node, "style", arr, n);
  354. if (host.changed)
  355. host.changed();
  356. return;
  357. }
  358. if (value && value[0] && n + nl + strlen(value) + 3 < sizeof(arr)) {
  359. for (i = 0; i < nl; i++)
  360. arr[n++] = prop[i];
  361. arr[n++] = ':';
  362. for (i = 0; value[i]; i++)
  363. arr[n++] = value[i];
  364. arr[n++] = ';';
  365. }
  366. arr[n] = '\0';
  367. br_set_attr(D, node, "style", arr, n);
  368. if (host.changed)
  369. host.changed();
  370. }
  371. static void style_getproperty(js_State *state)
  372. {
  373. unsigned int node = node_of(js_currentfunctiondata(state));
  374. char val[256];
  375. if (style_lookup(br_find_attr(D, node, "style"),
  376. js_tostring(state, 1), val, sizeof(val)))
  377. js_pushstring(state, val);
  378. else
  379. js_pushstring(state, "");
  380. }
  381. static void style_setproperty(js_State *state)
  382. {
  383. unsigned int node = node_of(js_currentfunctiondata(state));
  384. style_update(node, js_tostring(state, 1),
  385. js_tostring(state, 2), 0);
  386. }
  387. static void style_removeproperty(js_State *state)
  388. {
  389. unsigned int node = node_of(js_currentfunctiondata(state));
  390. const char *prop = js_tostring(state, 1);
  391. char old[256];
  392. int had = style_lookup(br_find_attr(D, node, "style"), prop,
  393. old, sizeof(old));
  394. style_update(node, prop, "", 1);
  395. if (had)
  396. js_pushstring(state, old);
  397. else
  398. js_pushstring(state, "");
  399. }
  400. static int style_has(js_State *state, void *p, const char *name)
  401. {
  402. unsigned int node = node_of(p);
  403. const char *style;
  404. char prop[64], val[256];
  405. if (!D || node >= D->nnode)
  406. return 0;
  407. style = br_find_attr(D, node, "style");
  408. if (!strcmp(name, "cssText")) {
  409. js_pushstring(state, style ? style : "");
  410. return 1;
  411. }
  412. if (!strcmp(name, "length")) {
  413. js_pushnumber(state, style_count(style));
  414. return 1;
  415. }
  416. if (!strcmp(name, "getPropertyValue")) {
  417. push_func_data(state, name, style_getproperty, 1, node + 1);
  418. return 1;
  419. }
  420. if (!strcmp(name, "setProperty")) {
  421. push_func_data(state, name, style_setproperty, 2, node + 1);
  422. return 1;
  423. }
  424. if (!strcmp(name, "removeProperty")) {
  425. push_func_data(state, name, style_removeproperty, 1, node + 1);
  426. return 1;
  427. }
  428. css_name(name, prop, sizeof(prop));
  429. if (style_lookup(style, prop, val, sizeof(val)))
  430. js_pushstring(state, val);
  431. else
  432. js_pushstring(state, "");
  433. return 1;
  434. }
  435. static int style_put(js_State *state, void *p, const char *name)
  436. {
  437. unsigned int node = node_of(p);
  438. const char *value;
  439. char prop[64];
  440. if (!D || node >= D->nnode)
  441. return 0;
  442. value = js_tostring(state, -1);
  443. if (!strcmp(name, "cssText")) {
  444. br_set_attr(D, node, "style", value, (unsigned int)strlen(value));
  445. if (host.changed)
  446. host.changed();
  447. return 1;
  448. }
  449. css_name(name, prop, sizeof(prop));
  450. style_update(node, prop, value, 0);
  451. return 1;
  452. }
  453. static void push_style_object(js_State *state, unsigned int node)
  454. {
  455. char key[32];
  456. snprintf(key, sizeof(key), "sty_%u", node + 1);
  457. js_getregistry(state, key);
  458. if (!js_isundefined(state, -1))
  459. return;
  460. js_pop(state, 1);
  461. js_pushnull(state);
  462. js_newuserdatax(state, "style", (void *)(uintptr_t)(node + 1),
  463. style_has, style_put, 0, 0);
  464. js_copy(state, -1);
  465. js_setregistry(state, key);
  466. }
  467. static int dataset_has(js_State *state, void *p, const char *name)
  468. {
  469. unsigned int node = node_of(p);
  470. char attr[64];
  471. const char *value;
  472. if (!D || node >= D->nnode)
  473. return 0;
  474. data_name(name, attr, sizeof(attr));
  475. value = br_find_attr(D, node, attr);
  476. if (value)
  477. js_pushstring(state, value);
  478. else
  479. js_pushundefined(state);
  480. return 1;
  481. }
  482. static int dataset_put(js_State *state, void *p, const char *name)
  483. {
  484. unsigned int node = node_of(p);
  485. char attr[64];
  486. const char *value = js_tostring(state, -1);
  487. if (!D || node >= D->nnode)
  488. return 0;
  489. data_name(name, attr, sizeof(attr));
  490. br_set_attr(D, node, attr, value, (unsigned int)strlen(value));
  491. if (host.changed)
  492. host.changed();
  493. return 1;
  494. }
  495. static int dataset_delete(js_State *state, void *p, const char *name)
  496. {
  497. unsigned int node = node_of(p);
  498. char attr[64];
  499. (void)state;
  500. if (!D || node >= D->nnode)
  501. return 0;
  502. data_name(name, attr, sizeof(attr));
  503. br_remove_attr(D, node, attr);
  504. if (host.changed)
  505. host.changed();
  506. return 1;
  507. }
  508. static void push_dataset(js_State *state, unsigned int node)
  509. {
  510. char key[32];
  511. snprintf(key, sizeof(key), "ds_%u", node + 1);
  512. js_getregistry(state, key);
  513. if (!js_isundefined(state, -1))
  514. return;
  515. js_pop(state, 1);
  516. js_pushnull(state);
  517. js_newuserdatax(state, "dataset", (void *)(uintptr_t)(node + 1),
  518. dataset_has, dataset_put, dataset_delete, 0);
  519. js_copy(state, -1);
  520. js_setregistry(state, key);
  521. }
  522. static void node_class_set(unsigned int node, const char *classes)
  523. {
  524. br_set_attr(D, node, "class", classes,
  525. (unsigned int)strlen(classes));
  526. if (host.changed)
  527. host.changed();
  528. }
  529. static int class_has(const char *classes, const char *name)
  530. {
  531. unsigned int i = 0;
  532. if (!classes)
  533. return 0;
  534. while (classes[i]) {
  535. unsigned int start;
  536. while (classes[i] == ' ' || classes[i] == '\t')
  537. i++;
  538. start = i;
  539. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  540. i++;
  541. if (i - start == strlen(name) &&
  542. !strncmp(classes + start, name, i - start))
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. static void class_add(unsigned int node, const char *name, int add)
  548. {
  549. const char *old = node_attr(node, "class");
  550. char buf[256];
  551. unsigned int n = 0, i = 0;
  552. if (add) {
  553. if (class_has(old, name))
  554. return;
  555. if (old && old[0]) {
  556. while (old[i] && n + 1 < sizeof(buf))
  557. buf[n++] = old[i++];
  558. if (n + 1 < sizeof(buf))
  559. buf[n++] = ' ';
  560. }
  561. i = 0;
  562. while (name[i] && n + 1 < sizeof(buf))
  563. buf[n++] = name[i++];
  564. } else {
  565. if (!old)
  566. return;
  567. while (old[i]) {
  568. unsigned int start;
  569. while (old[i] == ' ' || old[i] == '\t')
  570. i++;
  571. start = i;
  572. while (old[i] && old[i] != ' ' && old[i] != '\t')
  573. i++;
  574. if (i - start == strlen(name) &&
  575. !strncmp(old + start, name, i - start))
  576. continue;
  577. if (n && n + 1 < sizeof(buf))
  578. buf[n++] = ' ';
  579. {
  580. unsigned int k;
  581. for (k = start; k < i && n + 1 < sizeof(buf);
  582. k++)
  583. buf[n++] = old[k];
  584. }
  585. }
  586. }
  587. buf[n] = '\0';
  588. node_class_set(node, buf);
  589. }
  590. static void node_classlist_add(js_State *state)
  591. {
  592. class_add(node_of(js_currentfunctiondata(state)),
  593. js_tostring(state, 1), 1);
  594. }
  595. static void node_classlist_remove(js_State *state)
  596. {
  597. class_add(node_of(js_currentfunctiondata(state)),
  598. js_tostring(state, 1), 0);
  599. }
  600. static void node_classlist_toggle(js_State *state)
  601. {
  602. unsigned int node = node_of(js_currentfunctiondata(state));
  603. const char *name = js_tostring(state, 1);
  604. int has = class_has(node_attr(node, "class"), name);
  605. int want = !has;
  606. if (js_isdefined(state, 2))
  607. want = js_toboolean(state, 2);
  608. if (want != has)
  609. class_add(node, name, want);
  610. js_pushboolean(state, want);
  611. }
  612. static void node_classlist_contains(js_State *state)
  613. {
  614. unsigned int node = node_of(js_currentfunctiondata(state));
  615. js_pushboolean(state, class_has(node_attr(node, "class"),
  616. js_tostring(state, 1)));
  617. }
  618. static unsigned int class_count(const char *classes)
  619. {
  620. unsigned int i = 0, n = 0;
  621. if (!classes)
  622. return 0;
  623. while (classes[i]) {
  624. while (classes[i] == ' ' || classes[i] == '\t')
  625. i++;
  626. if (!classes[i])
  627. break;
  628. n++;
  629. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  630. i++;
  631. }
  632. return n;
  633. }
  634. static void push_attributes(js_State *state, unsigned int node)
  635. {
  636. struct br_node *n;
  637. const char *p, *end;
  638. unsigned int count = 0;
  639. js_newobject(state);
  640. if (!D || node >= D->nnode)
  641. return;
  642. n = &D->nodes[node];
  643. p = D->attrs + n->extra_off;
  644. end = p + n->extra_len;
  645. while (p + 1 < end) {
  646. const char *an = p;
  647. const char *av;
  648. p += strlen(p) + 1;
  649. if (p >= end)
  650. break;
  651. av = p;
  652. p += strlen(p) + 1;
  653. js_newobject(state);
  654. js_pushstring(state, an);
  655. js_setproperty(state, -2, "name");
  656. js_pushstring(state, av);
  657. js_setproperty(state, -2, "value");
  658. js_pushboolean(state, 1);
  659. js_setproperty(state, -2, "specified");
  660. /* Array-like: attributes[i] and attributes[name] both work. */
  661. js_copy(state, -1);
  662. js_setproperty(state, -3, an);
  663. js_setindex(state, -2, (int)count);
  664. count++;
  665. }
  666. js_pushnumber(state, count);
  667. js_setproperty(state, -2, "length");
  668. }
  669. static void push_classlist(js_State *state, unsigned int node)
  670. {
  671. js_newobject(state);
  672. def_func_data(state, "add", node_classlist_add, 1, node + 1);
  673. def_func_data(state, "remove", node_classlist_remove, 1, node + 1);
  674. def_func_data(state, "toggle", node_classlist_toggle, 1, node + 1);
  675. def_func_data(state, "contains", node_classlist_contains, 1, node + 1);
  676. js_pushnumber(state, class_count(node_attr(node, "class")));
  677. js_setproperty(state, -2, "length");
  678. }
  679. static void node_getattr(js_State *state)
  680. {
  681. unsigned int node = node_of(js_currentfunctiondata(state));
  682. push_attr(state, node, js_tostring(state, 1), 1);
  683. }
  684. static void node_setattr(js_State *state)
  685. {
  686. unsigned int node = node_of(js_currentfunctiondata(state));
  687. const char *name = js_tostring(state, 1);
  688. const char *value = js_tostring(state, 2);
  689. char lower[64];
  690. unsigned int i;
  691. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  692. char c = name[i];
  693. if (c >= 'A' && c <= 'Z')
  694. c = (char)(c - 'A' + 'a');
  695. lower[i] = c;
  696. }
  697. lower[i] = '\0';
  698. br_set_attr(D, node, lower, value, (unsigned int)strlen(value));
  699. if (host.changed)
  700. host.changed();
  701. }
  702. static void node_removeattr(js_State *state)
  703. {
  704. unsigned int node = node_of(js_currentfunctiondata(state));
  705. const char *name = js_tostring(state, 1);
  706. char lower[64];
  707. unsigned int i;
  708. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  709. char c = name[i];
  710. if (c >= 'A' && c <= 'Z')
  711. c = (char)(c - 'A' + 'a');
  712. lower[i] = c;
  713. }
  714. lower[i] = '\0';
  715. br_remove_attr(D, node, lower);
  716. if (host.changed)
  717. host.changed();
  718. }
  719. static void node_hasattr(js_State *state)
  720. {
  721. unsigned int node = node_of(js_currentfunctiondata(state));
  722. js_pushboolean(state, node_attr(node, js_tostring(state, 1)) != 0);
  723. }
  724. static void node_appendchild(js_State *state)
  725. {
  726. unsigned int node = node_of(js_currentfunctiondata(state));
  727. unsigned int child = arg_node(state, 1);
  728. if (br_append_child(D, node, child) != 0) {
  729. js_pushnull(state);
  730. return;
  731. }
  732. if (host.changed)
  733. host.changed();
  734. push_node(state, child);
  735. }
  736. static void node_insertbefore(js_State *state)
  737. {
  738. unsigned int node = node_of(js_currentfunctiondata(state));
  739. unsigned int child = arg_node(state, 1);
  740. unsigned int before = 0;
  741. if (!js_isnull(state, 2) && !js_isundefined(state, 2))
  742. before = arg_node(state, 2);
  743. if (br_insert_before(D, node, child, before) != 0) {
  744. js_pushnull(state);
  745. return;
  746. }
  747. if (host.changed)
  748. host.changed();
  749. push_node(state, child);
  750. }
  751. static void node_removechild(js_State *state)
  752. {
  753. unsigned int node = node_of(js_currentfunctiondata(state));
  754. unsigned int child = arg_node(state, 1);
  755. if (br_remove_child(D, node, child) != 0) {
  756. js_pushnull(state);
  757. return;
  758. }
  759. if (host.changed)
  760. host.changed();
  761. push_node(state, child);
  762. }
  763. static void node_replacechild(js_State *state)
  764. {
  765. unsigned int node = node_of(js_currentfunctiondata(state));
  766. unsigned int child = arg_node(state, 1);
  767. unsigned int old = arg_node(state, 2);
  768. br_insert_before(D, node, child, old);
  769. br_remove_child(D, node, old);
  770. if (host.changed)
  771. host.changed();
  772. push_node(state, old);
  773. }
  774. static void node_clone(js_State *state)
  775. {
  776. unsigned int node = node_of(js_currentfunctiondata(state));
  777. unsigned int h = br_clone_node(D, node + 1,
  778. js_toboolean(state, 1));
  779. push_node(state, h);
  780. }
  781. static void node_contains(js_State *state)
  782. {
  783. unsigned int node = node_of(js_currentfunctiondata(state));
  784. unsigned int other = arg_node(state, 1);
  785. unsigned int p = other;
  786. while (1) {
  787. if (p == node) {
  788. js_pushboolean(state, 1);
  789. return;
  790. }
  791. if (p == 0)
  792. break;
  793. p = D->nodes[p].parent;
  794. }
  795. js_pushboolean(state, 0);
  796. }
  797. static void node_haschildren(js_State *state)
  798. {
  799. unsigned int node = node_of(js_currentfunctiondata(state));
  800. js_pushboolean(state, node < D->nnode && D->nodes[node].first != 0);
  801. }
  802. static void node_remove(js_State *state)
  803. {
  804. unsigned int node = node_of(js_currentfunctiondata(state));
  805. unsigned int parent = node < D->nnode ? D->nodes[node].parent : 0;
  806. (void)state;
  807. if (parent)
  808. br_remove_child(D, parent, node + 1);
  809. if (host.changed)
  810. host.changed();
  811. }
  812. static void node_matches(js_State *state)
  813. {
  814. unsigned int node = node_of(js_currentfunctiondata(state));
  815. js_pushboolean(state, br_matches(D, node + 1,
  816. js_tostring(state, 1)));
  817. }
  818. static void node_getelementsbytagname(js_State *state)
  819. {
  820. unsigned int node = node_of(js_currentfunctiondata(state));
  821. const char *name = js_tostring(state, 1);
  822. unsigned int out[256];
  823. unsigned int n = br_query_all(D, node + 1, name, out, 256);
  824. js_newarray(state);
  825. for (unsigned int i = 0; i < n; i++) {
  826. push_node(state, out[i]);
  827. js_setindex(state, -2, (int)i);
  828. }
  829. }
  830. static void node_getelementsbyclassname(js_State *state)
  831. {
  832. unsigned int node = node_of(js_currentfunctiondata(state));
  833. const char *name = js_tostring(state, 1);
  834. char sel[256];
  835. unsigned int n = 0, i = 0;
  836. unsigned int out[256], count;
  837. while (name[i] && n + 2 < sizeof(sel)) {
  838. while (name[i] == ' ')
  839. i++;
  840. if (!name[i])
  841. break;
  842. sel[n++] = '.';
  843. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  844. sel[n++] = name[i++];
  845. }
  846. sel[n] = '\0';
  847. count = br_query_all(D, node + 1, sel, out, 256);
  848. js_newarray(state);
  849. for (i = 0; i < count; i++) {
  850. push_node(state, out[i]);
  851. js_setindex(state, -2, (int)i);
  852. }
  853. }
  854. static void node_queryselector(js_State *state)
  855. {
  856. unsigned int node = node_of(js_currentfunctiondata(state));
  857. unsigned int out[1];
  858. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  859. out, 1);
  860. if (n)
  861. push_node(state, out[0]);
  862. else
  863. js_pushnull(state);
  864. }
  865. static void node_queryselectorall(js_State *state)
  866. {
  867. unsigned int node = node_of(js_currentfunctiondata(state));
  868. unsigned int out[256];
  869. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  870. out, 256);
  871. js_newarray(state);
  872. for (unsigned int i = 0; i < n; i++) {
  873. push_node(state, out[i]);
  874. js_setindex(state, -2, (int)i);
  875. }
  876. }
  877. static void node_getrect(js_State *state)
  878. {
  879. js_newobject(state);
  880. js_pushnumber(state, 0); js_setproperty(state, -2, "top");
  881. js_pushnumber(state, 0); js_setproperty(state, -2, "left");
  882. js_pushnumber(state, 0); js_setproperty(state, -2, "right");
  883. js_pushnumber(state, 0); js_setproperty(state, -2, "bottom");
  884. js_pushnumber(state, 0); js_setproperty(state, -2, "width");
  885. js_pushnumber(state, 0); js_setproperty(state, -2, "height");
  886. }
  887. static void node_noop(js_State *state)
  888. {
  889. (void)state;
  890. }
  891. static void node_push_text(js_State *state, unsigned int node)
  892. {
  893. unsigned int cap = D->text_used + 1;
  894. char *buf;
  895. unsigned int len;
  896. if (cap < 32)
  897. cap = 32;
  898. buf = (char *)malloc(cap);
  899. if (!buf) {
  900. js_pushstring(state, "");
  901. return;
  902. }
  903. len = br_text_content(D, node, buf, cap);
  904. js_pushlstring(state, buf, (int)len);
  905. free(buf);
  906. }
  907. /* Userdata property hooks: every element wrapper carries node + 1. */
  908. static int node_has(js_State *state, void *p, const char *name)
  909. {
  910. unsigned int node = node_of(p);
  911. struct br_node *n;
  912. char buf[64];
  913. if (!D || node >= D->nnode)
  914. return 0;
  915. n = &D->nodes[node];
  916. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  917. node_push_text(state, node);
  918. return 1;
  919. }
  920. if (!strcmp(name, "nodeValue")) {
  921. if (n->type == BR_NODE_TEXT || n->type == BR_NODE_COMMENT)
  922. js_pushlstring(state, D->text + n->text_off,
  923. (int)n->text_len);
  924. else
  925. js_pushnull(state);
  926. return 1;
  927. }
  928. if (!strcmp(name, "nodeType")) {
  929. js_pushnumber(state, node_type_value(node));
  930. return 1;
  931. }
  932. if (!strcmp(name, "nodeName") || !strcmp(name, "tagName")) {
  933. node_name(node, buf, sizeof(buf));
  934. js_pushstring(state, buf);
  935. return 1;
  936. }
  937. if (!strcmp(name, "parentNode") || !strcmp(name, "parentElement")) {
  938. unsigned int p = n->parent;
  939. push_node(state, p ? p + 1 : 0);
  940. return 1;
  941. }
  942. if (!strcmp(name, "childNodes")) {
  943. push_child_list(state, node, 0);
  944. return 1;
  945. }
  946. if (!strcmp(name, "children")) {
  947. push_child_list(state, node, 1);
  948. return 1;
  949. }
  950. if (!strcmp(name, "firstChild")) {
  951. push_node(state, n->first ? n->first + 1 : 0);
  952. return 1;
  953. }
  954. if (!strcmp(name, "lastChild")) {
  955. push_node(state, n->last ? n->last + 1 : 0);
  956. return 1;
  957. }
  958. if (!strcmp(name, "firstElementChild") ||
  959. !strcmp(name, "lastElementChild")) {
  960. unsigned int c = n->first;
  961. unsigned int found = 0;
  962. if (!strcmp(name, "lastElementChild")) {
  963. for (c = n->first; c; c = D->nodes[c].next)
  964. if (D->nodes[c].type == BR_NODE_ELEM)
  965. found = c;
  966. } else {
  967. while (c && D->nodes[c].type != BR_NODE_ELEM)
  968. c = D->nodes[c].next;
  969. found = c;
  970. }
  971. push_node(state, found ? found + 1 : 0);
  972. return 1;
  973. }
  974. if (!strcmp(name, "nextSibling") || !strcmp(name, "nextElementSibling")) {
  975. unsigned int c = n->next;
  976. if (!strcmp(name, "nextElementSibling"))
  977. while (c && D->nodes[c].type != BR_NODE_ELEM)
  978. c = D->nodes[c].next;
  979. push_node(state, c ? c + 1 : 0);
  980. return 1;
  981. }
  982. if (!strcmp(name, "previousSibling") ||
  983. !strcmp(name, "previousElementSibling")) {
  984. unsigned int c = node_prev_sibling(node);
  985. if (!strcmp(name, "previousElementSibling"))
  986. while (c && D->nodes[c].type != BR_NODE_ELEM)
  987. c = node_prev_sibling(c);
  988. push_node(state, c ? c + 1 : 0);
  989. return 1;
  990. }
  991. if (!strcmp(name, "childElementCount")) {
  992. unsigned int c, count = 0;
  993. for (c = n->first; c; c = D->nodes[c].next)
  994. if (D->nodes[c].type == BR_NODE_ELEM)
  995. count++;
  996. js_pushnumber(state, count);
  997. return 1;
  998. }
  999. if (n->tag == BR_TAG_A || n->tag == BR_TAG_AREA) {
  1000. if (!strcmp(name, "href") || !strcmp(name, "protocol") ||
  1001. !strcmp(name, "host") || !strcmp(name, "hostname") ||
  1002. !strcmp(name, "port") || !strcmp(name, "pathname") ||
  1003. !strcmp(name, "search") || !strcmp(name, "hash") ||
  1004. !strcmp(name, "origin")) {
  1005. push_element_url(state, node, name);
  1006. return 1;
  1007. }
  1008. }
  1009. if (!strcmp(name, "id")) { push_attr(state, node, "id", 0); return 1; }
  1010. if (!strcmp(name, "className")) {
  1011. push_attr(state, node, "class", 0);
  1012. return 1;
  1013. }
  1014. if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
  1015. if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
  1016. if (!strcmp(name, "value")) { push_attr(state, node, "value", 0); return 1; }
  1017. if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
  1018. if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
  1019. if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
  1020. if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
  1021. if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
  1022. !strcmp(name, "selected")) {
  1023. js_pushboolean(state, node_attr(node, name) != 0);
  1024. return 1;
  1025. }
  1026. if (!strcmp(name, "style")) {
  1027. push_style_object(state, node);
  1028. return 1;
  1029. }
  1030. if (!strcmp(name, "dataset") || !strcmp(name, "domStringMap")) {
  1031. push_dataset(state, node);
  1032. return 1;
  1033. }
  1034. if (!strcmp(name, "classList")) {
  1035. push_classlist(state, node);
  1036. return 1;
  1037. }
  1038. if (!strcmp(name, "attributes")) {
  1039. push_attributes(state, node);
  1040. return 1;
  1041. }
  1042. if (!strcmp(name, "innerHTML") || !strcmp(name, "outerHTML")) {
  1043. char key[32];
  1044. snprintf(key, sizeof(key), "html_%u", node + 1);
  1045. js_getregistry(state, key);
  1046. if (js_isundefined(state, -1)) {
  1047. js_pop(state, 1);
  1048. js_pushstring(state, "");
  1049. }
  1050. return 1;
  1051. }
  1052. if (!strcmp(name, "ownerDocument")) {
  1053. js_getglobal(state, "document");
  1054. return 1;
  1055. }
  1056. if (!strcmp(name, "getAttribute"))
  1057. { push_func_data(state, "getAttribute", node_getattr, 1, node + 1); return 1; }
  1058. if (!strcmp(name, "setAttribute"))
  1059. { push_func_data(state, "setAttribute", node_setattr, 2, node + 1); return 1; }
  1060. if (!strcmp(name, "removeAttribute"))
  1061. { push_func_data(state, "removeAttribute", node_removeattr, 1, node + 1); return 1; }
  1062. if (!strcmp(name, "hasAttribute"))
  1063. { push_func_data(state, "hasAttribute", node_hasattr, 1, node + 1); return 1; }
  1064. if (!strcmp(name, "appendChild"))
  1065. { push_func_data(state, "appendChild", node_appendchild, 1, node + 1); return 1; }
  1066. if (!strcmp(name, "insertBefore"))
  1067. { push_func_data(state, "insertBefore", node_insertbefore, 2, node + 1); return 1; }
  1068. if (!strcmp(name, "removeChild"))
  1069. { push_func_data(state, "removeChild", node_removechild, 1, node + 1); return 1; }
  1070. if (!strcmp(name, "replaceChild"))
  1071. { push_func_data(state, "replaceChild", node_replacechild, 2, node + 1); return 1; }
  1072. if (!strcmp(name, "cloneNode"))
  1073. { push_func_data(state, "cloneNode", node_clone, 1, node + 1); return 1; }
  1074. if (!strcmp(name, "contains"))
  1075. { push_func_data(state, "contains", node_contains, 1, node + 1); return 1; }
  1076. if (!strcmp(name, "hasChildNodes"))
  1077. { push_func_data(state, "hasChildNodes", node_haschildren, 0, node + 1); return 1; }
  1078. if (!strcmp(name, "remove"))
  1079. { push_func_data(state, "remove", node_remove, 0, node + 1); return 1; }
  1080. if (!strcmp(name, "matches") || !strcmp(name, "webkitMatchesSelector") ||
  1081. !strcmp(name, "mozMatchesSelector") ||
  1082. !strcmp(name, "msMatchesSelector"))
  1083. { push_func_data(state, name, node_matches, 1, node + 1); return 1; }
  1084. if (!strcmp(name, "getElementsByTagName"))
  1085. { push_func_data(state, "getElementsByTagName", node_getelementsbytagname, 1, node + 1); return 1; }
  1086. if (!strcmp(name, "getElementsByClassName"))
  1087. { push_func_data(state, "getElementsByClassName", node_getelementsbyclassname, 1, node + 1); return 1; }
  1088. if (!strcmp(name, "querySelector"))
  1089. { push_func_data(state, "querySelector", node_queryselector, 1, node + 1); return 1; }
  1090. if (!strcmp(name, "querySelectorAll"))
  1091. { push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
  1092. if (!strcmp(name, "addEventListener"))
  1093. { push_func_data(state, "addEventListener", node_noop, 2, node + 1); return 1; }
  1094. if (!strcmp(name, "removeEventListener"))
  1095. { push_func_data(state, "removeEventListener", node_noop, 2, node + 1); return 1; }
  1096. if (!strcmp(name, "dispatchEvent"))
  1097. { push_func_data(state, "dispatchEvent", node_noop, 1, node + 1); return 1; }
  1098. if (!strcmp(name, "getBoundingClientRect")) {
  1099. node_getrect(state);
  1100. return 1;
  1101. }
  1102. if (!strcmp(name, "getClientRects")) {
  1103. js_newarray(state);
  1104. return 1;
  1105. }
  1106. if (!strcmp(name, "offsetWidth") || !strcmp(name, "offsetHeight") ||
  1107. !strcmp(name, "clientWidth") || !strcmp(name, "clientHeight") ||
  1108. !strcmp(name, "scrollWidth") || !strcmp(name, "scrollHeight") ||
  1109. !strcmp(name, "scrollTop") || !strcmp(name, "scrollLeft")) {
  1110. js_pushnumber(state, 0);
  1111. return 1;
  1112. }
  1113. if (!strcmp(name, "click") || !strcmp(name, "focus") ||
  1114. !strcmp(name, "blur")) {
  1115. push_func_data(state, name, node_noop, 0, node + 1);
  1116. return 1;
  1117. }
  1118. return 0;
  1119. }
  1120. static void node_set_string(unsigned int node, const char *name,
  1121. const char *value)
  1122. {
  1123. br_set_attr(D, node, name, value, (unsigned int)strlen(value));
  1124. if (host.changed)
  1125. host.changed();
  1126. }
  1127. static int node_put(js_State *state, void *p, const char *name)
  1128. {
  1129. unsigned int node = node_of(p);
  1130. if (!D || node >= D->nnode)
  1131. return 0;
  1132. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  1133. const char *text = js_tostring(state, -1);
  1134. unsigned int len = (unsigned int)strlen(text);
  1135. if (br_set_text_content(D, node, text, len) != 0) {
  1136. if (host.status)
  1137. host.status("Page text arena is full", 1);
  1138. } else if (host.changed) {
  1139. host.changed();
  1140. }
  1141. return 1;
  1142. }
  1143. if (!strcmp(name, "nodeValue")) {
  1144. const char *text = js_tostring(state, -1);
  1145. if (br_set_text_node(D, node, text,
  1146. (unsigned int)strlen(text)) == 0 &&
  1147. host.changed)
  1148. host.changed();
  1149. return 1;
  1150. }
  1151. if (!strcmp(name, "id") || !strcmp(name, "className") ||
  1152. !strcmp(name, "href") || !strcmp(name, "src") ||
  1153. !strcmp(name, "value") || !strcmp(name, "type") ||
  1154. !strcmp(name, "name") || !strcmp(name, "alt") ||
  1155. !strcmp(name, "title")) {
  1156. const char *value = js_tostring(state, -1);
  1157. if (!strcmp(name, "className"))
  1158. node_set_string(node, "class", value);
  1159. else
  1160. node_set_string(node, name, value);
  1161. return 1;
  1162. }
  1163. if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
  1164. !strcmp(name, "selected")) {
  1165. if (js_toboolean(state, -1))
  1166. br_set_attr(D, node, name, "", 0);
  1167. else
  1168. br_remove_attr(D, node, name);
  1169. if (host.changed)
  1170. host.changed();
  1171. return 1;
  1172. }
  1173. if (!strcmp(name, "innerHTML")) {
  1174. const char *html = js_tostring(state, -1);
  1175. char key[32];
  1176. br_set_inner_html(D, node, html, (unsigned int)strlen(html));
  1177. snprintf(key, sizeof(key), "html_%u", node + 1);
  1178. js_pushstring(state, html);
  1179. js_setregistry(state, key);
  1180. if (host.changed)
  1181. host.changed();
  1182. return 1;
  1183. }
  1184. if (!strcmp(name, "outerHTML")) {
  1185. char key[32];
  1186. snprintf(key, sizeof(key), "html_%u", node + 1);
  1187. js_pushstring(state, js_tostring(state, -1));
  1188. js_setregistry(state, key);
  1189. return 1;
  1190. }
  1191. if (!strcmp(name, "style")) {
  1192. if (js_isstring(state, -1))
  1193. node_set_string(node, "style",
  1194. js_tostring(state, -1));
  1195. return 1;
  1196. }
  1197. return 0;
  1198. }
  1199. static int node_delete(js_State *state, void *p, const char *name)
  1200. {
  1201. (void)state;
  1202. (void)p;
  1203. (void)name;
  1204. return 0;
  1205. }
  1206. static void doc_getelementbyid(js_State *state)
  1207. {
  1208. const char *id = js_tostring(state, 1);
  1209. push_node(state, br_find_id(D, id));
  1210. }
  1211. static void console_log(js_State *state)
  1212. {
  1213. int i;
  1214. int n = js_gettop(state);
  1215. for (i = 1; i < n; i++) {
  1216. if (i > 1)
  1217. u_putc(' ');
  1218. u_puts(js_tostring(state, i));
  1219. }
  1220. u_putc('\n');
  1221. }
  1222. static void js_alert(js_State *state)
  1223. {
  1224. char msg[160];
  1225. unsigned int used = 0;
  1226. int i;
  1227. int n = js_gettop(state);
  1228. for (i = 1; i < n; i++) {
  1229. const char *s = js_tostring(state, i);
  1230. unsigned int k;
  1231. if (i > 1 && used + 1 < sizeof(msg))
  1232. msg[used++] = ' ';
  1233. for (k = 0; s[k] && used + 1 < sizeof(msg); k++)
  1234. msg[used++] = s[k];
  1235. }
  1236. msg[used] = '\0';
  1237. if (host.status)
  1238. host.status(msg, 0);
  1239. }
  1240. static void push_query_array(js_State *state, unsigned int root,
  1241. const char *selector)
  1242. {
  1243. unsigned int out[256];
  1244. unsigned int n = br_query_all(D, root, selector, out, 256);
  1245. unsigned int i;
  1246. js_newarray(state);
  1247. for (i = 0; i < n; i++) {
  1248. push_node(state, out[i]);
  1249. js_setindex(state, -2, (int)i);
  1250. }
  1251. }
  1252. static void doc_create_element(js_State *state)
  1253. {
  1254. const char *name = js_tostring(state, 1);
  1255. char lower[32];
  1256. unsigned int i;
  1257. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  1258. char c = name[i];
  1259. if (c >= 'A' && c <= 'Z')
  1260. c = (char)(c - 'A' + 'a');
  1261. lower[i] = c;
  1262. }
  1263. lower[i] = '\0';
  1264. push_node(state, br_create_element(D, lower, i));
  1265. }
  1266. static void doc_create_text(js_State *state)
  1267. {
  1268. const char *text = js_tostring(state, 1);
  1269. push_node(state, br_create_text(D, text, (unsigned int)strlen(text)));
  1270. }
  1271. static void doc_create_comment(js_State *state)
  1272. {
  1273. const char *text = js_tostring(state, 1);
  1274. push_node(state, br_create_comment(D, text, (unsigned int)strlen(text)));
  1275. }
  1276. static void doc_create_fragment(js_State *state)
  1277. {
  1278. push_node(state, br_create_fragment(D));
  1279. }
  1280. static void doc_create_attribute(js_State *state)
  1281. {
  1282. const char *name = js_tostring(state, 1);
  1283. js_newobject(state);
  1284. js_pushstring(state, name);
  1285. js_setproperty(state, -2, "name");
  1286. js_pushstring(state, "");
  1287. js_setproperty(state, -2, "value");
  1288. js_pushboolean(state, 1);
  1289. js_setproperty(state, -2, "specified");
  1290. }
  1291. static void doc_getelementsbytagname(js_State *state)
  1292. {
  1293. push_query_array(state, 0, js_tostring(state, 1));
  1294. }
  1295. static void doc_getelementsbyclassname(js_State *state)
  1296. {
  1297. const char *name = js_tostring(state, 1);
  1298. char sel[256];
  1299. unsigned int n = 0, i = 0;
  1300. while (name[i] && n + 2 < sizeof(sel)) {
  1301. while (name[i] == ' ')
  1302. i++;
  1303. if (!name[i])
  1304. break;
  1305. sel[n++] = '.';
  1306. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  1307. sel[n++] = name[i++];
  1308. }
  1309. sel[n] = '\0';
  1310. push_query_array(state, 0, sel);
  1311. }
  1312. static void doc_queryselector(js_State *state)
  1313. {
  1314. unsigned int out[1];
  1315. unsigned int n = br_query_all(D, 0, js_tostring(state, 1), out, 1);
  1316. if (n)
  1317. push_node(state, out[0]);
  1318. else
  1319. js_pushnull(state);
  1320. }
  1321. static void doc_queryselectorall(js_State *state)
  1322. {
  1323. push_query_array(state, 0, js_tostring(state, 1));
  1324. }
  1325. static void doc_noop(js_State *state)
  1326. {
  1327. (void)state;
  1328. }
  1329. static void location_get(js_State *state)
  1330. {
  1331. js_pushstring(state, current_url ? current_url : "");
  1332. }
  1333. static void location_set(js_State *state)
  1334. {
  1335. const char *url = js_tostring(state, 1);
  1336. if (host.navigate)
  1337. host.navigate(url);
  1338. }
  1339. static void win_noop(js_State *state);
  1340. struct loc_parts {
  1341. const char *protocol, *host, *hostname, *port, *path, *search, *hash;
  1342. const char *origin;
  1343. unsigned int protocol_len, host_len, hostname_len, port_len;
  1344. unsigned int path_len, search_len, hash_len, origin_len;
  1345. };
  1346. static void loc_split(const char *u, struct loc_parts *p)
  1347. {
  1348. unsigned int n = (unsigned int)strlen(u), i;
  1349. const char *rest;
  1350. memset(p, 0, sizeof(*p));
  1351. p->path = u;
  1352. p->path_len = n;
  1353. p->origin = "null";
  1354. p->origin_len = 4;
  1355. for (i = 0; i < n; i++)
  1356. if (u[i] == ':')
  1357. break;
  1358. if (i + 2 < n && u[i] == ':' && u[i + 1] == '/' && u[i + 2] == '/') {
  1359. const char *hosts = u + i + 3, *hoste = hosts;
  1360. while (*hoste && *hoste != '/' && *hoste != '?' && *hoste != '#')
  1361. hoste++;
  1362. p->protocol = u;
  1363. p->protocol_len = i + 1;
  1364. p->host = hosts;
  1365. p->host_len = (unsigned int)(hoste - hosts);
  1366. p->hostname = hosts;
  1367. p->hostname_len = p->host_len;
  1368. for (i = 0; i < p->hostname_len; i++)
  1369. if (hosts[i] == ':') {
  1370. p->port = hosts + i + 1;
  1371. p->port_len = p->hostname_len - i - 1;
  1372. p->hostname_len = i;
  1373. break;
  1374. }
  1375. p->origin = u;
  1376. p->origin_len = (unsigned int)(hoste - u);
  1377. rest = hoste;
  1378. } else {
  1379. p->protocol = "file:";
  1380. p->protocol_len = 5;
  1381. rest = u;
  1382. }
  1383. {
  1384. const char *q = rest;
  1385. while (*q && *q != '?' && *q != '#')
  1386. q++;
  1387. p->path = rest;
  1388. p->path_len = (unsigned int)(q - rest);
  1389. if (*q == '?') {
  1390. const char *s = q;
  1391. while (*q && *q != '#')
  1392. q++;
  1393. p->search = s;
  1394. p->search_len = (unsigned int)(q - s);
  1395. }
  1396. if (*q == '#') {
  1397. p->hash = q;
  1398. p->hash_len = (unsigned int)strlen(q);
  1399. }
  1400. }
  1401. }
  1402. static void push_location(js_State *state)
  1403. {
  1404. struct loc_parts p;
  1405. loc_split(current_url ? current_url : "", &p);
  1406. js_newobject(state);
  1407. js_newcfunction(state, location_get, "get href", 0);
  1408. js_newcfunction(state, location_set, "set href", 1);
  1409. js_defaccessor(state, -3, "href", 0);
  1410. #define LOC_FIELD(field) \
  1411. do { \
  1412. js_pushlstring(state, p.field ? p.field : "", \
  1413. p.field ? (int)p.field##_len : 0); \
  1414. js_setproperty(state, -2, #field); \
  1415. } while (0)
  1416. LOC_FIELD(protocol);
  1417. LOC_FIELD(host);
  1418. LOC_FIELD(hostname);
  1419. LOC_FIELD(port);
  1420. js_pushlstring(state, p.path ? p.path : "",
  1421. p.path ? (int)p.path_len : 0);
  1422. js_setproperty(state, -2, "pathname");
  1423. LOC_FIELD(search);
  1424. LOC_FIELD(hash);
  1425. LOC_FIELD(origin);
  1426. #undef LOC_FIELD
  1427. js_newcfunction(state, win_noop, "reload", 0);
  1428. js_setproperty(state, -2, "reload");
  1429. js_newcfunction(state, win_noop, "assign", 1);
  1430. js_setproperty(state, -2, "assign");
  1431. js_newcfunction(state, win_noop, "replace", 1);
  1432. js_setproperty(state, -2, "replace");
  1433. js_newcfunction(state, win_noop, "toString", 0);
  1434. js_setproperty(state, -2, "toString");
  1435. }
  1436. /* ---- URL resolution for <a>/<area> URL properties ---- */
  1437. static int url_has_scheme(const char *s)
  1438. {
  1439. unsigned int i = 1;
  1440. if (!((s[0] >= 'a' && s[0] <= 'z') ||
  1441. (s[0] >= 'A' && s[0] <= 'Z')))
  1442. return 0;
  1443. while (s[i]) {
  1444. char c = s[i];
  1445. if (c == ':')
  1446. return 1;
  1447. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1448. (c >= '0' && c <= '9') || c == '+' || c == '-' ||
  1449. c == '.'))
  1450. return 0;
  1451. i++;
  1452. }
  1453. return 0;
  1454. }
  1455. static void url_put(char *out, unsigned int cap, unsigned int *n,
  1456. const char *s, unsigned int len)
  1457. {
  1458. unsigned int i;
  1459. for (i = 0; i < len && *n + 1 < cap; i++)
  1460. out[(*n)++] = s[i];
  1461. }
  1462. /* Collapse //, /./ and /../ inside the path part of an absolute URL. */
  1463. static void url_normalize(char *url)
  1464. {
  1465. char tmp[512];
  1466. char *path;
  1467. unsigned int n = 0, i = 0;
  1468. path = strstr(url, "://");
  1469. if (path) {
  1470. path += 3;
  1471. while (*path && *path != '/')
  1472. path++;
  1473. } else {
  1474. path = url;
  1475. }
  1476. if (path[0] != '/')
  1477. return;
  1478. while (path[i]) {
  1479. unsigned int start;
  1480. while (path[i] == '/')
  1481. i++;
  1482. start = i;
  1483. while (path[i] && path[i] != '/')
  1484. i++;
  1485. if (i == start)
  1486. continue;
  1487. if (i - start == 1 && path[start] == '.')
  1488. continue;
  1489. if (i - start == 2 && path[start] == '.' &&
  1490. path[start + 1] == '.') {
  1491. while (n > 0 && tmp[n - 1] != '/')
  1492. n--;
  1493. if (n > 0)
  1494. n--;
  1495. continue;
  1496. }
  1497. if (n + 1 < sizeof(tmp))
  1498. tmp[n++] = '/';
  1499. while (start < i && n + 1 < sizeof(tmp))
  1500. tmp[n++] = path[start++];
  1501. }
  1502. if (n == 0)
  1503. tmp[n++] = '/';
  1504. tmp[n] = '\0';
  1505. memcpy(path, tmp, n + 1);
  1506. }
  1507. static void url_resolve(const char *base, const char *rel,
  1508. char *out, unsigned int cap)
  1509. {
  1510. struct loc_parts bp;
  1511. unsigned int n = 0, i;
  1512. if (cap == 0)
  1513. return;
  1514. if (!rel[0]) {
  1515. url_put(out, cap, &n, base, (unsigned int)strlen(base));
  1516. out[n] = '\0';
  1517. return;
  1518. }
  1519. if (url_has_scheme(rel)) {
  1520. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1521. out[n] = '\0';
  1522. url_normalize(out);
  1523. return;
  1524. }
  1525. loc_split(base, &bp);
  1526. if (rel[0] == '/' && rel[1] == '/') {
  1527. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1528. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1529. } else if (rel[0] == '/') {
  1530. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1531. url_put(out, cap, &n, "//", 2);
  1532. url_put(out, cap, &n, bp.host, bp.host_len);
  1533. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1534. } else if (rel[0] == '#' || rel[0] == '?') {
  1535. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1536. if (bp.host_len || (bp.protocol_len == 5 &&
  1537. bp.protocol[0] == 'f')) {
  1538. url_put(out, cap, &n, "//", 2);
  1539. url_put(out, cap, &n, bp.host, bp.host_len);
  1540. }
  1541. url_put(out, cap, &n, bp.path, bp.path_len);
  1542. if (rel[0] == '#')
  1543. url_put(out, cap, &n, bp.search, bp.search_len);
  1544. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1545. } else {
  1546. const char *dir_end = bp.path;
  1547. for (i = 0; i < bp.path_len; i++)
  1548. if (bp.path[i] == '/')
  1549. dir_end = bp.path + i + 1;
  1550. if (dir_end == bp.path)
  1551. dir_end = bp.path + bp.path_len;
  1552. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1553. if (bp.host_len || (bp.protocol_len == 5 &&
  1554. bp.protocol[0] == 'f')) {
  1555. url_put(out, cap, &n, "//", 2);
  1556. url_put(out, cap, &n, bp.host, bp.host_len);
  1557. }
  1558. url_put(out, cap, &n, bp.path,
  1559. (unsigned int)(dir_end - bp.path));
  1560. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1561. }
  1562. out[n] = '\0';
  1563. url_normalize(out);
  1564. }
  1565. /* <a> URL properties are resolved against the page URL, like the DOM. */
  1566. static void push_element_url(js_State *state, unsigned int node,
  1567. const char *name)
  1568. {
  1569. char abs_url[512];
  1570. const char *href = br_find_attr(D, node, "href");
  1571. struct loc_parts p;
  1572. if (!href || !href[0]) {
  1573. js_pushstring(state, "");
  1574. return;
  1575. }
  1576. url_resolve(current_url ? current_url : "/", href,
  1577. abs_url, sizeof(abs_url));
  1578. loc_split(abs_url, &p);
  1579. if (!strcmp(name, "href"))
  1580. js_pushstring(state, abs_url);
  1581. else if (!strcmp(name, "protocol"))
  1582. js_pushlstring(state, p.protocol, (int)p.protocol_len);
  1583. else if (!strcmp(name, "origin"))
  1584. js_pushlstring(state, p.origin ? p.origin : "",
  1585. (int)p.origin_len);
  1586. else if (!strcmp(name, "host"))
  1587. js_pushlstring(state, p.host ? p.host : "", (int)p.host_len);
  1588. else if (!strcmp(name, "hostname"))
  1589. js_pushlstring(state, p.hostname ? p.hostname : "",
  1590. (int)p.hostname_len);
  1591. else if (!strcmp(name, "port"))
  1592. js_pushlstring(state, p.port ? p.port : "", (int)p.port_len);
  1593. else if (!strcmp(name, "pathname"))
  1594. js_pushlstring(state, p.path ? p.path : "", (int)p.path_len);
  1595. else if (!strcmp(name, "search"))
  1596. js_pushlstring(state, p.search ? p.search : "",
  1597. (int)p.search_len);
  1598. else
  1599. js_pushlstring(state, p.hash ? p.hash : "", (int)p.hash_len);
  1600. }
  1601. static void win_noop(js_State *state)
  1602. {
  1603. (void)state;
  1604. }
  1605. static void timer_add(js_State *state, int repeat)
  1606. {
  1607. int i;
  1608. unsigned int ms;
  1609. if (!js_iscallable(state, 1)) {
  1610. js_pushnumber(state, 0);
  1611. return;
  1612. }
  1613. ms = js_isdefined(state, 2) ?
  1614. (unsigned int)js_tointeger(state, 2) : 0;
  1615. if (repeat && ms == 0)
  1616. ms = 1;
  1617. for (i = 0; i < JS_TIMERS; i++)
  1618. if (!timers[i].used)
  1619. break;
  1620. if (i == JS_TIMERS) {
  1621. js_pushnumber(state, 0);
  1622. return;
  1623. }
  1624. js_copy(state, 1);
  1625. snprintf(timers[i].ref, sizeof(timers[i].ref), "%s",
  1626. js_ref(state));
  1627. timers[i].used = 1;
  1628. timers[i].repeat = repeat;
  1629. timers[i].interval = ms;
  1630. timers[i].deadline = u_ticks_ms() + ms;
  1631. js_pushnumber(state, i + 1);
  1632. }
  1633. static void win_settimeout(js_State *state)
  1634. {
  1635. timer_add(state, 0);
  1636. }
  1637. static void win_setinterval(js_State *state)
  1638. {
  1639. timer_add(state, 1);
  1640. }
  1641. static void timer_clear(js_State *state)
  1642. {
  1643. int id = js_tointeger(state, 1);
  1644. if (id >= 1 && id <= JS_TIMERS && timers[id - 1].used) {
  1645. js_unref(state, timers[id - 1].ref);
  1646. timers[id - 1].used = 0;
  1647. }
  1648. }
  1649. static void win_requestanimationframe(js_State *state)
  1650. {
  1651. timer_add(state, 0);
  1652. }
  1653. static void win_performance_now(js_State *state)
  1654. {
  1655. js_pushnumber(state, (double)u_ticks_ms());
  1656. }
  1657. static void win_getproperty(js_State *state)
  1658. {
  1659. (void)state;
  1660. js_pushstring(state, "");
  1661. }
  1662. static void win_getcomputedstyle(js_State *state)
  1663. {
  1664. (void)state;
  1665. js_newobject(state);
  1666. def_func(state, "getPropertyValue", win_getproperty, 1);
  1667. js_pushstring(state, "");
  1668. js_setproperty(state, -2, "cssText");
  1669. }
  1670. static void win_matchmedia(js_State *state)
  1671. {
  1672. const char *query = js_tostring(state, 1);
  1673. js_newobject(state);
  1674. js_pushboolean(state, 0);
  1675. js_setproperty(state, -2, "matches");
  1676. js_pushstring(state, query);
  1677. js_setproperty(state, -2, "media");
  1678. def_func(state, "addListener", win_noop, 1);
  1679. def_func(state, "removeListener", win_noop, 1);
  1680. def_func(state, "addEventListener", win_noop, 2);
  1681. def_func(state, "removeEventListener", win_noop, 2);
  1682. }
  1683. static void install_globals(js_State *state)
  1684. {
  1685. unsigned int h;
  1686. js_newobject(state);
  1687. def_func(state, "getElementById", doc_getelementbyid, 1);
  1688. def_func(state, "createElement", doc_create_element, 1);
  1689. def_func(state, "createTextNode", doc_create_text, 1);
  1690. def_func(state, "createComment", doc_create_comment, 1);
  1691. def_func(state, "createDocumentFragment", doc_create_fragment, 0);
  1692. def_func(state, "createAttribute", doc_create_attribute, 1);
  1693. def_func(state, "getElementsByTagName", doc_getelementsbytagname, 1);
  1694. def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
  1695. def_func(state, "querySelector", doc_queryselector, 1);
  1696. def_func(state, "querySelectorAll", doc_queryselectorall, 1);
  1697. def_func(state, "addEventListener", doc_noop, 2);
  1698. def_func(state, "removeEventListener", doc_noop, 2);
  1699. h = br_find_tag(D, BR_TAG_HTML);
  1700. if (!h && D->nnode > 1)
  1701. h = D->nodes[0].first ? D->nodes[0].first + 1 : 0;
  1702. push_node(state, h);
  1703. js_setproperty(state, -2, "documentElement");
  1704. h = br_find_tag(D, BR_TAG_BODY);
  1705. push_node(state, h);
  1706. js_setproperty(state, -2, "body");
  1707. h = br_find_tag(D, BR_TAG_HEAD);
  1708. push_node(state, h);
  1709. js_setproperty(state, -2, "head");
  1710. js_pushnumber(state, 9);
  1711. js_setproperty(state, -2, "nodeType");
  1712. js_pushstring(state, "#document");
  1713. js_setproperty(state, -2, "nodeName");
  1714. js_pushnull(state);
  1715. js_setproperty(state, -2, "ownerDocument");
  1716. js_pushstring(state, "CSS1Compat");
  1717. js_setproperty(state, -2, "compatMode");
  1718. js_pushstring(state, D->title);
  1719. js_setproperty(state, -2, "title");
  1720. js_pushstring(state, "complete");
  1721. js_setproperty(state, -2, "readyState");
  1722. js_pushstring(state, "");
  1723. js_setproperty(state, -2, "cookie");
  1724. js_pushstring(state, current_url ? current_url : "");
  1725. js_setproperty(state, -2, "URL");
  1726. js_defglobal(state, "document", JS_DONTENUM);
  1727. js_newobject(state);
  1728. js_newcfunction(state, console_log, "log", 0);
  1729. js_setproperty(state, -2, "log");
  1730. js_defglobal(state, "console", JS_DONTENUM);
  1731. js_newcfunction(state, js_alert, "alert", 1);
  1732. js_defglobal(state, "alert", JS_DONTENUM);
  1733. push_location(state);
  1734. js_defglobal(state, "location", JS_DONTENUM);
  1735. js_newobject(state);
  1736. js_pushstring(state, "Mozilla/5.0 (X11; Linux i686) NebOS/0.2");
  1737. js_setproperty(state, -2, "userAgent");
  1738. js_pushstring(state, "NebOS");
  1739. js_setproperty(state, -2, "platform");
  1740. js_pushstring(state, "en-US");
  1741. js_setproperty(state, -2, "language");
  1742. js_defglobal(state, "navigator", JS_DONTENUM);
  1743. js_newobject(state);
  1744. js_pushnumber(state, 1);
  1745. js_setproperty(state, -2, "length");
  1746. def_func(state, "pushState", win_noop, 3);
  1747. def_func(state, "replaceState", win_noop, 3);
  1748. def_func(state, "back", win_noop, 0);
  1749. def_func(state, "forward", win_noop, 0);
  1750. def_func(state, "go", win_noop, 1);
  1751. js_defglobal(state, "history", JS_DONTENUM);
  1752. def_global_func(state, "matchMedia", win_matchmedia, 1);
  1753. def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
  1754. def_global_func(state, "addEventListener", win_noop, 2);
  1755. def_global_func(state, "removeEventListener", win_noop, 2);
  1756. def_global_func(state, "scrollTo", win_noop, 2);
  1757. def_global_func(state, "scrollBy", win_noop, 2);
  1758. def_global_func(state, "setTimeout", win_settimeout, 1);
  1759. def_global_func(state, "setInterval", win_setinterval, 1);
  1760. def_global_func(state, "clearTimeout", timer_clear, 1);
  1761. def_global_func(state, "clearInterval", timer_clear, 1);
  1762. def_global_func(state, "requestAnimationFrame", win_requestanimationframe, 1);
  1763. def_global_func(state, "cancelAnimationFrame", timer_clear, 1);
  1764. js_newobject(state);
  1765. def_func(state, "now", win_performance_now, 0);
  1766. js_newobject(state);
  1767. js_setproperty(state, -2, "timing");
  1768. js_defglobal(state, "performance", JS_DONTENUM);
  1769. js_pushnumber(state, host.width ? (int)host.width : 1024);
  1770. js_defglobal(state, "innerWidth", JS_DONTENUM);
  1771. js_pushnumber(state, host.height ? (int)host.height : 768);
  1772. js_defglobal(state, "innerHeight", JS_DONTENUM);
  1773. js_pushnumber(state, 0);
  1774. js_defglobal(state, "pageXOffset", JS_DONTENUM);
  1775. js_pushnumber(state, 0);
  1776. js_defglobal(state, "pageYOffset", JS_DONTENUM);
  1777. js_pushnumber(state, 0);
  1778. js_defglobal(state, "scrollX", JS_DONTENUM);
  1779. js_pushnumber(state, 0);
  1780. js_defglobal(state, "scrollY", JS_DONTENUM);
  1781. js_pushnumber(state, 1);
  1782. js_defglobal(state, "devicePixelRatio", JS_DONTENUM);
  1783. js_pushglobal(state);
  1784. js_defglobal(state, "window", JS_DONTENUM);
  1785. js_pushglobal(state);
  1786. js_defglobal(state, "globalThis", JS_DONTENUM);
  1787. js_getglobal(state, "document");
  1788. js_getglobal(state, "location");
  1789. js_setproperty(state, -2, "location");
  1790. js_pop(state, 1);
  1791. js_getglobal(state, "document");
  1792. js_pushglobal(state);
  1793. js_setproperty(state, -2, "defaultView");
  1794. js_pop(state, 1);
  1795. }
  1796. int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
  1797. {
  1798. if (!doc || !h)
  1799. return -1;
  1800. D = doc;
  1801. host = *h;
  1802. current_url = h->page_url;
  1803. panic_armed = 0;
  1804. memset(timers, 0, sizeof(timers));
  1805. /* Classic scripts are sloppy by default in browsers; strict mode is
  1806. * opt-in via a "use strict" directive. */
  1807. J = js_newstate(0, 0, 0);
  1808. if (!J)
  1809. return -1;
  1810. js_setreport(J, report_cb);
  1811. js_atpanic(J, panic_cb);
  1812. install_globals(J);
  1813. return 0;
  1814. }
  1815. void js_browser_stop(void)
  1816. {
  1817. if (J)
  1818. js_freestate(J);
  1819. J = 0;
  1820. D = 0;
  1821. current_url = 0;
  1822. panic_armed = 0;
  1823. memset(timers, 0, sizeof(timers));
  1824. }
  1825. int js_browser_active(void)
  1826. {
  1827. return J != 0;
  1828. }
  1829. static void report_top(js_State *state)
  1830. {
  1831. const char *msg;
  1832. js_getproperty(state, -1, "stack");
  1833. if (js_isstring(state, -1)) {
  1834. msg = js_tostring(state, -1);
  1835. if (host.report)
  1836. host.report(msg);
  1837. js_pop(state, 1);
  1838. } else {
  1839. js_pop(state, 1);
  1840. msg = js_trystring(state, -1, "JavaScript error");
  1841. if (host.report)
  1842. host.report(msg);
  1843. }
  1844. js_pop(state, 1);
  1845. }
  1846. void js_browser_tick(void)
  1847. {
  1848. unsigned int now;
  1849. int i, ran = 0;
  1850. if (!J)
  1851. return;
  1852. now = u_ticks_ms();
  1853. for (i = 0; i < JS_TIMERS; i++) {
  1854. if (!timers[i].used ||
  1855. (int)(now - timers[i].deadline) < 0)
  1856. continue;
  1857. js_setlimit(J, JS_RUN_LIMIT, 0);
  1858. js_getregistry(J, timers[i].ref);
  1859. if (!js_iscallable(J, -1)) {
  1860. js_pop(J, 1);
  1861. js_unref(J, timers[i].ref);
  1862. timers[i].used = 0;
  1863. continue;
  1864. }
  1865. js_pushundefined(J);
  1866. if (js_pcall(J, 0))
  1867. report_top(J);
  1868. else
  1869. js_pop(J, 1);
  1870. if (timers[i].used) {
  1871. if (timers[i].repeat)
  1872. timers[i].deadline = now + timers[i].interval;
  1873. else {
  1874. js_unref(J, timers[i].ref);
  1875. timers[i].used = 0;
  1876. }
  1877. }
  1878. if (++ran >= 4)
  1879. break;
  1880. }
  1881. }
  1882. int js_browser_run(const char *source, const char *url)
  1883. {
  1884. int r;
  1885. if (!J || !D)
  1886. return -1;
  1887. js_setlimit(J, JS_RUN_LIMIT, 0);
  1888. panic_armed = 1;
  1889. if (setjmp(panic_env)) {
  1890. panic_armed = 0;
  1891. if (host.report)
  1892. host.report("uncaught JavaScript exception");
  1893. /* The try/exception stack is unusable after a panic. Leak the
  1894. * state; the page heap reset on navigation reclaims it. */
  1895. J = 0;
  1896. D = 0;
  1897. return -1;
  1898. }
  1899. if (js_ploadstring(J, url ? url : "[inline]", source)) {
  1900. report_top(J);
  1901. panic_armed = 0;
  1902. return -1;
  1903. }
  1904. js_pushundefined(J);
  1905. r = js_pcall(J, 0);
  1906. if (r) {
  1907. report_top(J);
  1908. panic_armed = 0;
  1909. return -1;
  1910. }
  1911. js_pop(J, 1);
  1912. panic_armed = 0;
  1913. return 0;
  1914. }