| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041 |
- /* Browser bindings for MuJS: the DOM layer (interned node wrappers,
- * attributes, query selectors, classList/dataset/style, innerHTML, timers)
- * plus document/window/location/navigator globals. */
- #include <setjmp.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "ucmd_lib.h"
- #include "browser_html.h"
- #include "js_browser.h"
- #include "mujs.h"
- /* Instructions per script/handler. MuJS consumes the budget globally, so
- * it is reset before every entry; a runaway loop becomes a catchable
- * "script ran too long" exception instead of a frozen browser. */
- #define JS_RUN_LIMIT 8000000
- #define JS_TIMERS 16
- struct js_timer {
- int used;
- int repeat;
- unsigned int deadline;
- unsigned int interval;
- char ref[32];
- };
- static struct js_timer timers[JS_TIMERS];
- static js_State *J;
- static struct br_doc *D;
- static struct js_browser_host host;
- static const char *current_url;
- static jmp_buf panic_env;
- static int panic_armed;
- static void report_top(js_State *state);
- static void report_cb(js_State *state, const char *message)
- {
- (void)state;
- if (host.report)
- host.report(message);
- }
- static void panic_cb(js_State *state)
- {
- if (panic_armed)
- longjmp(panic_env, 1);
- if (js_isobject(state, -1))
- report_top(state);
- else if (host.report)
- host.report(js_trystring(state, -1,
- "uncaught JavaScript exception"));
- if (host.report)
- host.report("uncaught JavaScript exception");
- }
- static unsigned int node_of(void *p)
- {
- return (unsigned int)(uintptr_t)p - 1;
- }
- static int node_has(js_State *state, void *p, const char *name);
- static int node_put(js_State *state, void *p, const char *name);
- static int node_delete(js_State *state, void *p, const char *name);
- static void push_element_url(js_State *state, unsigned int node,
- const char *name);
- /* One wrapper per DOM node: the same element must have the same object
- * identity on every access, or library caches (jQuery expando data, Sizzle)
- * silently split into disconnected copies. */
- static void push_node(js_State *state, unsigned int handle)
- {
- char key[24];
- if (!handle) {
- js_pushnull(state);
- return;
- }
- snprintf(key, sizeof(key), "jsonnode_%u", handle);
- js_getregistry(state, key);
- if (!js_isundefined(state, -1))
- return;
- js_pop(state, 1);
- /* js_newuserdatax() pops a prototype from the top. */
- js_pushnull(state);
- js_newuserdatax(state, "node", (void *)(uintptr_t)handle,
- node_has, node_put, node_delete, 0);
- js_copy(state, -1);
- js_setregistry(state, key);
- }
- static unsigned int arg_node(js_State *state, int idx)
- {
- if (!js_isuserdata(state, idx, "node"))
- js_typeerror(state, "not a DOM node");
- return (unsigned int)(uintptr_t)js_touserdata(state, idx, "node");
- }
- static void def_func_data(js_State *state, const char *name,
- js_CFunction fn, int length, unsigned int handle)
- {
- js_newcfunctionx(state, fn, name, length,
- (void *)(uintptr_t)handle, 0);
- js_setproperty(state, -2, name);
- }
- static void def_func(js_State *state, const char *name,
- js_CFunction fn, int length)
- {
- js_newcfunction(state, fn, name, length);
- js_setproperty(state, -2, name);
- }
- static void def_global_func(js_State *state, const char *name,
- js_CFunction fn, int length)
- {
- js_newcfunction(state, fn, name, length);
- js_defglobal(state, name, JS_DONTENUM);
- }
- static void push_func_data(js_State *state, const char *name,
- js_CFunction fn, int length, unsigned int handle)
- {
- js_newcfunctionx(state, fn, name, length,
- (void *)(uintptr_t)handle, 0);
- }
- static const char *node_attr(unsigned int node, const char *name)
- {
- return br_find_attr(D, node, name);
- }
- static void push_attr(js_State *state, unsigned int node, const char *name,
- int null_if_missing)
- {
- const char *s = node_attr(node, name);
- if (s)
- js_pushstring(state, s);
- else if (null_if_missing)
- js_pushnull(state);
- else
- js_pushstring(state, "");
- }
- static unsigned int node_name(unsigned int node, char *buf, unsigned int cap)
- {
- struct br_node *n;
- const char *s;
- unsigned int i = 0;
- if (!D || node >= D->nnode || cap == 0)
- return 0;
- n = &D->nodes[node];
- if (n->type == BR_NODE_TEXT)
- s = "#text";
- else if (n->type == BR_NODE_COMMENT)
- s = "#comment";
- else if (n->type == BR_NODE_FRAGMENT)
- s = "#document-fragment";
- else if (n->tag == BR_TAG_UNKNOWN && n->name_off)
- s = D->attrs + n->name_off;
- else
- s = br_tag_name(n->tag);
- while (s && *s && i + 1 < cap) {
- char c = *s++;
- if (c >= 'a' && c <= 'z')
- c = (char)(c - 'a' + 'A');
- buf[i++] = c;
- }
- buf[i] = '\0';
- return i;
- }
- /* ---- DOM events ------------------------------------------------------- *
- *
- * Listeners live in a fixed table, one entry per (target, type, callback)
- * registration, and callbacks are retained through the MuJS registry. The
- * dispatcher walks the parent chain for bubbling and runs one listener at a
- * time so a handler can safely mutate the tree. `preventDefault` is tracked
- * per dispatch in a small event-state table and mirrored onto the event
- * object, so a submit handler can cancel the browser's default action. */
- #define JS_LISTENERS 64
- #define JS_EVENT_SLOTS 8
- #define JS_EVENT_TEMPLATES 16
- #define JS_TARGET_WINDOW 0xFFFFFFF0u
- #define JS_TARGET_DOCUMENT 0xFFFFFFF1u
- struct js_listener {
- unsigned char used;
- unsigned char capture;
- unsigned int target;
- char type[24];
- char ref[32];
- };
- struct js_event_state {
- int used;
- int prevented;
- int stopped;
- int immediate;
- char objref[32];
- };
- struct js_template {
- int used;
- char ref[32];
- };
- static struct js_listener listeners[JS_LISTENERS];
- static struct js_event_state events[JS_EVENT_SLOTS];
- static struct js_template event_templates[JS_EVENT_TEMPLATES];
- static void push_event_target(js_State *state, unsigned int target)
- {
- if (target == JS_TARGET_WINDOW)
- js_pushglobal(state);
- else if (target == JS_TARGET_DOCUMENT)
- js_getglobal(state, "document");
- else
- push_node(state, target + 1);
- }
- static void obj_set(js_State *state, int idx, const char *name)
- {
- /* The value was just pushed, so a negative object index moved down. */
- if (idx < 0)
- idx--;
- js_setproperty(state, idx, name);
- }
- static void obj_set_string(js_State *state, int idx, const char *name,
- const char *value)
- {
- js_pushstring(state, value ? value : "");
- obj_set(state, idx, name);
- }
- static void obj_set_bool(js_State *state, int idx, const char *name, int value)
- {
- js_pushboolean(state, value != 0);
- obj_set(state, idx, name);
- }
- static int event_slot(js_State *state)
- {
- int slot = (int)(uintptr_t)js_currentfunctiondata(state);
- if (slot < 0 || slot >= JS_EVENT_SLOTS)
- return -1;
- return slot;
- }
- static void event_preventdefault(js_State *state)
- {
- int slot = event_slot(state);
- if (slot >= 0) {
- struct js_event_state *ev = &events[slot];
- ev->prevented = 1;
- if (ev->objref[0]) {
- js_getregistry(state, ev->objref);
- if (js_isobject(state, -1)) {
- js_pushboolean(state, 1);
- js_setproperty(state, -2, "defaultPrevented");
- }
- js_pop(state, 1);
- }
- }
- js_pushundefined(state);
- }
- static void event_stoppropagation(js_State *state)
- {
- int slot = event_slot(state);
- if (slot >= 0)
- events[slot].stopped = 1;
- js_pushundefined(state);
- }
- static void event_stopimmediate(js_State *state)
- {
- int slot = event_slot(state);
- if (slot >= 0) {
- events[slot].stopped = 1;
- events[slot].immediate = 1;
- }
- js_pushundefined(state);
- }
- /* Event-object field helpers. Every dispatch owns a registry key for its
- * event object so preventDefault can update the object without access to
- * `this` (this MuJS has no this accessor). */
- static void ev_set_string(js_State *state, int slot, const char *name,
- const char *value)
- {
- js_getregistry(state, events[slot].objref);
- js_pushstring(state, value ? value : "");
- js_setproperty(state, -2, name);
- js_pop(state, 1);
- }
- static void ev_set_bool(js_State *state, int slot, const char *name, int value)
- {
- js_getregistry(state, events[slot].objref);
- js_pushboolean(state, value != 0);
- js_setproperty(state, -2, name);
- js_pop(state, 1);
- }
- static void ev_set_number(js_State *state, int slot, const char *name,
- double value)
- {
- js_getregistry(state, events[slot].objref);
- js_pushnumber(state, value);
- js_setproperty(state, -2, name);
- js_pop(state, 1);
- }
- static void ev_set_target(js_State *state, int slot, const char *name,
- unsigned int target)
- {
- js_getregistry(state, events[slot].objref);
- push_event_target(state, target);
- js_setproperty(state, -2, name);
- js_pop(state, 1);
- }
- static int ev_get_bool(js_State *state, int slot, const char *name, int def)
- {
- int rv = def;
- js_getregistry(state, events[slot].objref);
- js_getproperty(state, -1, name);
- if (js_isboolean(state, -1))
- rv = js_toboolean(state, -1);
- js_pop(state, 2);
- return rv;
- }
- static void ev_get_string(js_State *state, int slot, const char *name,
- char *out, int cap, const char *def)
- {
- const char *s = def ? def : "";
- int n = 0;
- js_getregistry(state, events[slot].objref);
- js_getproperty(state, -1, name);
- if (js_isstring(state, -1))
- s = js_tostring(state, -1);
- while (s[n] && n + 1 < cap) {
- out[n] = s[n];
- n++;
- }
- out[n] = '\0';
- js_pop(state, 2);
- }
- static void event_bind_methods(js_State *state, int slot)
- {
- js_getregistry(state, events[slot].objref);
- def_func_data(state, "preventDefault", event_preventdefault, 0,
- (unsigned int)slot);
- def_func_data(state, "stopPropagation", event_stoppropagation, 0,
- (unsigned int)slot);
- def_func_data(state, "stopImmediatePropagation", event_stopimmediate, 0,
- (unsigned int)slot);
- js_pop(state, 1);
- }
- /* The event object is on top of the stack; move it into the registry and
- * fill in the standard fields. */
- static int event_prepare(js_State *state, unsigned int target)
- {
- int slot;
- for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
- if (!events[slot].used)
- break;
- if (slot == JS_EVENT_SLOTS)
- return -1;
- events[slot].used = 1;
- events[slot].prevented = 0;
- events[slot].stopped = 0;
- events[slot].immediate = 0;
- snprintf(events[slot].objref, sizeof(events[slot].objref),
- "jseventobj_%d", slot);
- js_setregistry(state, events[slot].objref);
- ev_set_bool(state, slot, "defaultPrevented", 0);
- ev_set_bool(state, slot, "isTrusted", 0);
- ev_set_number(state, slot, "timeStamp", (double)u_ticks_ms());
- ev_set_target(state, slot, "target", target);
- ev_set_target(state, slot, "currentTarget", target);
- ev_set_number(state, slot, "eventPhase", 2);
- event_bind_methods(state, slot);
- return slot;
- }
- static int listener_call(js_State *state, const struct js_listener *l,
- int slot, unsigned int current_target)
- {
- js_getregistry(state, l->ref);
- if (!js_iscallable(state, -1)) {
- js_pop(state, 1);
- return 0;
- }
- push_event_target(state, current_target);
- js_getregistry(state, events[slot].objref);
- if (js_pcall(state, 1)) {
- report_top(state);
- return 0;
- }
- js_pop(state, 1);
- return 1;
- }
- static void listener_phase(js_State *state, unsigned int target,
- const char *type, int phase, int slot)
- {
- int i;
- for (i = 0; i < JS_LISTENERS; i++) {
- struct js_listener *l = &listeners[i];
- if (!l->used || l->target != target ||
- strcmp(l->type, type) != 0)
- continue;
- if (phase == 1 && !l->capture)
- continue;
- if (phase == 2 && l->capture)
- continue;
- ev_set_target(state, slot, "currentTarget", target);
- listener_call(state, l, slot, target);
- if (events[slot].immediate)
- return;
- }
- }
- /* supplied_idx: 0 creates a fresh object, otherwise it is the argument
- * index of an Event supplied by JavaScript. Leaves a boolean result. */
- static void inline_attr_decode(const char *s, char *d, unsigned int cap)
- {
- unsigned int i = 0, n = 0;
- while (s && s[i] && n + 1 < cap) {
- if (s[i] == '&') {
- if (!strncmp(s + i, """, 6)) {
- d[n++] = '"'; i += 6; continue;
- }
- if (!strncmp(s + i, "'", 5)) {
- d[n++] = '\''; i += 5; continue;
- }
- if (!strncmp(s + i, "&", 5)) {
- d[n++] = '&'; i += 5; continue;
- }
- if (!strncmp(s + i, "<", 4)) {
- d[n++] = '<'; i += 4; continue;
- }
- if (!strncmp(s + i, ">", 4)) {
- d[n++] = '>'; i += 4; continue;
- }
- }
- d[n++] = s[i++];
- }
- d[n] = '\0';
- }
- /* Inline on<event> attributes run at the target and while bubbling, after
- * the registered listeners for that node. The handler gets the event and
- * the element as `this`; a false return prevents the default action. */
- static void run_inline_handler(js_State *state, unsigned int node,
- const char *type, int slot, int *prevented)
- {
- char name[40], body[512], src[640];
- const char *attr;
- int truthy;
- if (!D || node >= D->nnode || !events[slot].objref[0])
- return;
- snprintf(name, sizeof(name), "on%s", type);
- attr = br_find_attr(D, node, name);
- if (!attr || !attr[0])
- return;
- inline_attr_decode(attr, body, sizeof(body));
- snprintf(src, sizeof(src), "(function(event){ %s\n})", body);
- if (js_try(state)) {
- js_pop(state, 1);
- js_endtry(state);
- return;
- }
- js_loadstring(state, "[inline]", src);
- /* The script evaluates the function expression; run it first, then
- * call the resulting function with the element as `this`. */
- js_pushundefined(state);
- if (js_pcall(state, 0) || !js_iscallable(state, -1)) {
- js_pop(state, 1);
- js_endtry(state);
- return;
- }
- push_node(state, node + 1);
- js_getregistry(state, events[slot].objref);
- if (js_pcall(state, 1)) {
- js_pop(state, 1);
- js_endtry(state);
- return;
- }
- truthy = js_toboolean(state, -1);
- js_pop(state, 1);
- js_endtry(state);
- if (!truthy && prevented)
- *prevented = 1;
- }
- static int dispatch_object(js_State *state, unsigned int target,
- int supplied_idx, const char *fallback,
- int def_bubbles, int def_cancelable)
- {
- unsigned int path[64];
- char type[32];
- int np = 0, slot, i, prevented;
- struct br_node *nd;
- if (supplied_idx)
- js_copy(state, supplied_idx);
- else
- js_newobject(state);
- slot = event_prepare(state, target);
- if (slot < 0) {
- js_pushboolean(state, 1);
- return 1;
- }
- ev_get_string(state, slot, "type", type, sizeof(type), fallback);
- if (!type[0]) {
- js_unref(state, events[slot].objref);
- events[slot].objref[0] = '\0';
- events[slot].used = 0;
- js_pushboolean(state, 1);
- return 1;
- }
- ev_set_string(state, slot, "type", type);
- ev_set_bool(state, slot, "bubbles",
- ev_get_bool(state, slot, "bubbles", def_bubbles));
- ev_set_bool(state, slot, "cancelable",
- ev_get_bool(state, slot, "cancelable", def_cancelable));
- path[np++] = target;
- if (target != JS_TARGET_WINDOW && target != JS_TARGET_DOCUMENT &&
- D && target < D->nnode) {
- nd = &D->nodes[target];
- while (nd->parent && nd->parent < D->nnode &&
- np < (int)(sizeof(path) / sizeof(path[0]))) {
- path[np++] = nd->parent;
- nd = &D->nodes[nd->parent];
- }
- }
- if (target != JS_TARGET_WINDOW)
- path[np++] = JS_TARGET_DOCUMENT;
- if (np < (int)(sizeof(path) / sizeof(path[0])))
- path[np++] = JS_TARGET_WINDOW;
- /* Capture from the outermost ancestor down to the parent. */
- for (i = np - 1; i > 0; i--) {
- if (events[slot].stopped)
- break;
- listener_phase(state, path[i], type, 1, slot);
- }
- /* Target phase: capture and bubble listeners in registration order. */
- if (!events[slot].stopped) {
- listener_phase(state, path[0], type, 0, slot);
- if (!events[slot].stopped)
- run_inline_handler(state, path[0], type, slot,
- &events[slot].prevented);
- }
- /* Bubble only for events that ask for it; stopPropagation stops the
- * remaining ancestors. */
- if (ev_get_bool(state, slot, "bubbles", def_bubbles)) {
- for (i = 1; i < np && !events[slot].stopped; i++) {
- listener_phase(state, path[i], type, 2, slot);
- if (!events[slot].stopped &&
- path[i] != JS_TARGET_WINDOW &&
- path[i] != JS_TARGET_DOCUMENT)
- run_inline_handler(state, path[i], type, slot,
- &events[slot].prevented);
- }
- }
- prevented = events[slot].prevented;
- js_unref(state, events[slot].objref);
- events[slot].objref[0] = '\0';
- events[slot].used = 0;
- js_pushboolean(state, prevented ? 0 : 1);
- return prevented ? 0 : 1;
- }
- static void add_listener(js_State *state, unsigned int target, int remove)
- {
- const char *type;
- int i;
- if (!js_iscallable(state, 2))
- return;
- type = js_tostring(state, 1);
- if (!type[0])
- return;
- if (!remove) {
- for (i = 0; i < JS_LISTENERS; i++)
- if (!listeners[i].used)
- break;
- if (i == JS_LISTENERS)
- return;
- listeners[i].used = 1;
- listeners[i].capture = js_isboolean(state, 3) &&
- js_toboolean(state, 3);
- listeners[i].target = target;
- snprintf(listeners[i].type, sizeof(listeners[i].type), "%s",
- type);
- js_copy(state, 2);
- snprintf(listeners[i].ref, sizeof(listeners[i].ref), "%s",
- js_ref(state));
- return;
- }
- for (i = 0; i < JS_LISTENERS; i++) {
- struct js_listener *l = &listeners[i];
- int eq;
- if (!l->used || l->target != target ||
- strcmp(l->type, type) != 0)
- continue;
- js_getregistry(state, l->ref);
- js_copy(state, 2);
- eq = js_equal(state);
- js_pop(state, 2);
- if (!eq)
- continue;
- js_unref(state, l->ref);
- l->used = 0;
- return;
- }
- }
- static void node_addlistener(js_State *state)
- {
- unsigned int node = (unsigned int)(uintptr_t)
- js_currentfunctiondata(state) - 1;
- add_listener(state, node, 0);
- }
- static void node_removelistener(js_State *state)
- {
- unsigned int node = (unsigned int)(uintptr_t)
- js_currentfunctiondata(state) - 1;
- add_listener(state, node, 1);
- }
- static void node_dispatchevent(js_State *state)
- {
- unsigned int node = (unsigned int)(uintptr_t)
- js_currentfunctiondata(state) - 1;
- if (!D || !node) {
- js_pushboolean(state, 1);
- return;
- }
- if (js_isobject(state, 1)) {
- dispatch_object(state, node, 1, "event", 1, 0);
- } else {
- char type[32];
- const char *s = js_tostring(state, 1);
- unsigned int i = 0;
- while (s[i] && i + 1 < sizeof(type)) {
- type[i] = s[i];
- i++;
- }
- type[i] = '\0';
- dispatch_object(state, node, 0, type, 1, 0);
- }
- }
- static void win_addlistener(js_State *state)
- {
- add_listener(state, JS_TARGET_WINDOW, 0);
- }
- static void win_removelistener(js_State *state)
- {
- add_listener(state, JS_TARGET_WINDOW, 1);
- }
- static void doc_addlistener(js_State *state)
- {
- add_listener(state, JS_TARGET_DOCUMENT, 0);
- }
- static void doc_removelistener(js_State *state)
- {
- add_listener(state, JS_TARGET_DOCUMENT, 1);
- }
- static void event_init(js_State *state)
- {
- int slot = (int)(uintptr_t)js_currentfunctiondata(state);
- const char *type;
- if (slot < 0 || slot >= JS_EVENT_TEMPLATES ||
- !event_templates[slot].used)
- return;
- type = js_tostring(state, 1);
- js_getregistry(state, event_templates[slot].ref);
- if (!js_isobject(state, -1)) {
- js_pop(state, 1);
- return;
- }
- obj_set_string(state, -1, "type", type);
- obj_set_bool(state, -1, "bubbles", js_toboolean(state, 2));
- obj_set_bool(state, -1, "cancelable", js_toboolean(state, 3));
- js_pop(state, 1);
- }
- static void event_build_object(js_State *state, const char *type,
- int bubbles, int cancelable, int with_init)
- {
- js_newobject(state);
- js_pushstring(state, type ? type : "");
- js_setproperty(state, -2, "type");
- js_pushboolean(state, bubbles != 0);
- js_setproperty(state, -2, "bubbles");
- js_pushboolean(state, cancelable != 0);
- js_setproperty(state, -2, "cancelable");
- js_pushboolean(state, 0);
- js_setproperty(state, -2, "defaultPrevented");
- if (with_init) {
- int i;
- for (i = 0; i < JS_EVENT_TEMPLATES; i++)
- if (!event_templates[i].used)
- break;
- if (i < JS_EVENT_TEMPLATES) {
- event_templates[i].used = 1;
- snprintf(event_templates[i].ref,
- sizeof(event_templates[i].ref),
- "jseventtmpl_%d", i);
- js_copy(state, -1);
- js_setregistry(state, event_templates[i].ref);
- def_func_data(state, "initEvent", event_init, 3,
- (unsigned int)i);
- }
- }
- }
- static void event_call(js_State *state)
- {
- const char *type = js_isdefined(state, 1) ? js_tostring(state, 1) : "";
- int bubbles = 0, cancelable = 0;
- if (js_isobject(state, 2)) {
- js_getproperty(state, 2, "bubbles");
- bubbles = js_toboolean(state, -1);
- js_pop(state, 1);
- js_getproperty(state, 2, "cancelable");
- cancelable = js_toboolean(state, -1);
- js_pop(state, 1);
- }
- event_build_object(state, type, bubbles, cancelable, 1);
- }
- static void doc_createevent(js_State *state)
- {
- const char *type = js_tostring(state, 1);
- event_build_object(state, type, 0, 0, 1);
- }
- static void custom_event_call(js_State *state)
- {
- event_call(state);
- if (js_isobject(state, 2))
- js_getproperty(state, 2, "detail");
- else
- js_pushnull(state);
- js_setproperty(state, -2, "detail");
- }
- static void mouse_event_call(js_State *state)
- {
- static const struct {
- const char *name;
- int is_number;
- } props[] = {
- { "clientX", 1 }, { "clientY", 1 }, { "screenX", 1 },
- { "screenY", 1 }, { "button", 1 }, { "buttons", 1 },
- { "ctrlKey", 0 }, { "shiftKey", 0 }, { "altKey", 0 },
- { "metaKey", 0 }
- };
- unsigned int i;
- event_call(state);
- if (!js_isobject(state, 2))
- return;
- for (i = 0; i < sizeof(props) / sizeof(props[0]); i++) {
- js_getproperty(state, 2, props[i].name);
- if (props[i].is_number) {
- double v = js_isnumber(state, -1) ?
- js_tonumber(state, -1) : 0;
- js_pop(state, 1);
- js_pushnumber(state, v);
- } else {
- int v = js_toboolean(state, -1);
- js_pop(state, 1);
- js_pushboolean(state, v);
- }
- js_setproperty(state, -2, props[i].name);
- }
- }
- int js_browser_event(unsigned int node, const char *type, int bubbles,
- int cancelable)
- {
- int slot, rv;
- if (!J || !D)
- return 1;
- for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
- if (!events[slot].used)
- break;
- if (slot == JS_EVENT_SLOTS)
- return 1;
- js_setlimit(J, JS_RUN_LIMIT, 0);
- rv = dispatch_object(J, node, 0, type, bubbles, cancelable);
- js_pop(J, 1);
- return rv;
- }
- static unsigned int node_type_value(unsigned int node)
- {
- struct br_node *n;
- if (!D || node >= D->nnode)
- return 0;
- n = &D->nodes[node];
- if (n->type == BR_NODE_TEXT)
- return 3;
- if (n->type == BR_NODE_COMMENT)
- return 8;
- if (n->type == BR_NODE_FRAGMENT)
- return 11;
- return 1;
- }
- static void push_child_list(js_State *state, unsigned int node, int elements)
- {
- struct br_node *n;
- unsigned int c, i = 0;
- js_newarray(state);
- if (!D || node >= D->nnode)
- return;
- n = &D->nodes[node];
- for (c = n->first; c; c = D->nodes[c].next) {
- if (elements && D->nodes[c].type != BR_NODE_ELEM)
- continue;
- push_node(state, c + 1);
- js_setindex(state, -2, (int)i++);
- }
- }
- static unsigned int node_prev_sibling(unsigned int node)
- {
- unsigned int p, c, prev = 0;
- if (!D || node >= D->nnode)
- return 0;
- p = D->nodes[node].parent;
- for (c = D->nodes[p].first; c && c != node; c = D->nodes[c].next)
- prev = c;
- return prev;
- }
- /* ---- inline style and dataset objects ---- */
- static int ci_eq_n(const char *a, const char *b, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++) {
- char ca = a[i], cb = b[i];
- if (ca >= 'A' && ca <= 'Z')
- ca = (char)(ca - 'A' + 'a');
- if (cb >= 'A' && cb <= 'Z')
- cb = (char)(cb - 'A' + 'a');
- if (ca != cb)
- return 0;
- }
- return 1;
- }
- /* camelCase JS property -> CSS property ("marginTop" -> "margin-top"). */
- static void css_name(const char *name, char *out, unsigned int cap)
- {
- unsigned int n = 0, i;
- if (!strcmp(name, "cssFloat")) {
- if (cap > 6)
- memcpy(out, "float", 6);
- else if (cap)
- out[0] = '\0';
- return;
- }
- for (i = 0; name[i] && n + 2 < cap; i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z') {
- if (n == 0 || out[n - 1] != '-')
- out[n++] = '-';
- out[n++] = (char)(c - 'A' + 'a');
- } else {
- out[n++] = c;
- }
- }
- if (cap)
- out[n] = '\0';
- }
- /* camelCase dataset property -> attribute name ("fooBar" -> "data-foo-bar"). */
- static void data_name(const char *name, char *out, unsigned int cap)
- {
- unsigned int n = 0, i;
- for (i = 0; i < 5 && n + 1 < cap; i++)
- out[n++] = "data-"[i];
- for (i = 0; name[i] && n + 2 < cap; i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z') {
- out[n++] = '-';
- c = (char)(c - 'A' + 'a');
- }
- out[n++] = c;
- }
- if (cap)
- out[n] = '\0';
- }
- static int style_lookup(const char *style, const char *prop, char *out,
- unsigned int cap)
- {
- unsigned int i = 0, nl = (unsigned int)strlen(prop);
- while (style && style[i]) {
- unsigned int ns, ne, vs, ve, a, b, k;
- while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
- i++;
- if (!style[i])
- break;
- ns = i;
- while (style[i] && style[i] != ':' && style[i] != ';')
- i++;
- ne = i;
- if (style[i] != ':') {
- if (style[i] == ';')
- i++;
- continue;
- }
- i++;
- while (style[i] == ' ' || style[i] == '\t')
- i++;
- vs = i;
- while (style[i] && style[i] != ';')
- i++;
- ve = i;
- a = ns;
- b = ne;
- while (a < b && (style[a] == ' ' || style[a] == '\t'))
- a++;
- while (b > a && (style[b - 1] == ' ' || style[b - 1] == '\t'))
- b--;
- while (ve > vs && (style[ve - 1] == ' ' || style[ve - 1] == '\t'))
- ve--;
- if (b - a == nl && ci_eq_n(style + a, prop, nl)) {
- for (k = 0; k < ve - vs && k + 1 < cap; k++)
- out[k] = style[vs + k];
- out[k < cap ? k : cap - 1] = '\0';
- return 1;
- }
- }
- return 0;
- }
- static unsigned int style_count(const char *style)
- {
- unsigned int i = 0, n = 0;
- while (style && style[i]) {
- while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
- i++;
- if (!style[i])
- break;
- while (style[i] && style[i] != ';')
- i++;
- n++;
- }
- return n;
- }
- static unsigned int style_span(const char *style, unsigned int i)
- {
- while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
- i++;
- return i;
- }
- static void style_update(unsigned int node, const char *prop,
- const char *value, int remove)
- {
- const char *old = br_find_attr(D, node, "style");
- char arr[512];
- unsigned int n = 0, i = 0, nl = (unsigned int)strlen(prop);
- int replaced = 0;
- if (remove && !old)
- return;
- while (old && old[i] && n + 2 < sizeof(arr)) {
- unsigned int start = style_span(old, i);
- unsigned int ns, ne, ve, a, b, k;
- i = start;
- if (!old[i])
- break;
- ns = i;
- while (old[i] && old[i] != ':' && old[i] != ';')
- i++;
- ne = i;
- if (old[i] == ':') {
- i++;
- while (old[i] == ' ' || old[i] == '\t')
- i++;
- while (old[i] && old[i] != ';')
- i++;
- }
- ve = i;
- a = ns;
- b = ne;
- while (a < b && (old[a] == ' ' || old[a] == '\t'))
- a++;
- while (b > a && (old[b - 1] == ' ' || old[b - 1] == '\t'))
- b--;
- if (b - a == nl && ci_eq_n(old + a, prop, nl)) {
- replaced = 1;
- if (remove || !value || !value[0])
- continue;
- }
- for (k = ns; k < ve && n + 1 < sizeof(arr); k++)
- arr[n++] = old[k];
- if (n + 1 < sizeof(arr))
- arr[n++] = ';';
- }
- if (replaced && (remove || !value || !value[0])) {
- arr[n] = '\0';
- br_set_attr(D, node, "style", arr, n);
- if (host.changed)
- host.changed();
- return;
- }
- if (value && value[0] && n + nl + strlen(value) + 3 < sizeof(arr)) {
- for (i = 0; i < nl; i++)
- arr[n++] = prop[i];
- arr[n++] = ':';
- for (i = 0; value[i]; i++)
- arr[n++] = value[i];
- arr[n++] = ';';
- }
- arr[n] = '\0';
- br_set_attr(D, node, "style", arr, n);
- if (host.changed)
- host.changed();
- }
- static void style_getproperty(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- char val[256];
- if (style_lookup(br_find_attr(D, node, "style"),
- js_tostring(state, 1), val, sizeof(val)))
- js_pushstring(state, val);
- else
- js_pushstring(state, "");
- }
- static void style_setproperty(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- style_update(node, js_tostring(state, 1),
- js_tostring(state, 2), 0);
- }
- static void style_removeproperty(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *prop = js_tostring(state, 1);
- char old[256];
- int had = style_lookup(br_find_attr(D, node, "style"), prop,
- old, sizeof(old));
- style_update(node, prop, "", 1);
- if (had)
- js_pushstring(state, old);
- else
- js_pushstring(state, "");
- }
- static int style_has(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- const char *style;
- char prop[64], val[256];
- if (!D || node >= D->nnode)
- return 0;
- style = br_find_attr(D, node, "style");
- if (!strcmp(name, "cssText")) {
- js_pushstring(state, style ? style : "");
- return 1;
- }
- if (!strcmp(name, "length")) {
- js_pushnumber(state, style_count(style));
- return 1;
- }
- if (!strcmp(name, "getPropertyValue")) {
- push_func_data(state, name, style_getproperty, 1, node + 1);
- return 1;
- }
- if (!strcmp(name, "setProperty")) {
- push_func_data(state, name, style_setproperty, 2, node + 1);
- return 1;
- }
- if (!strcmp(name, "removeProperty")) {
- push_func_data(state, name, style_removeproperty, 1, node + 1);
- return 1;
- }
- css_name(name, prop, sizeof(prop));
- if (style_lookup(style, prop, val, sizeof(val)))
- js_pushstring(state, val);
- else
- js_pushstring(state, "");
- return 1;
- }
- static int style_put(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- const char *value;
- char prop[64];
- if (!D || node >= D->nnode)
- return 0;
- value = js_tostring(state, -1);
- if (!strcmp(name, "cssText")) {
- br_set_attr(D, node, "style", value, (unsigned int)strlen(value));
- if (host.changed)
- host.changed();
- return 1;
- }
- css_name(name, prop, sizeof(prop));
- style_update(node, prop, value, 0);
- return 1;
- }
- static void push_style_object(js_State *state, unsigned int node)
- {
- char key[32];
- snprintf(key, sizeof(key), "sty_%u", node + 1);
- js_getregistry(state, key);
- if (!js_isundefined(state, -1))
- return;
- js_pop(state, 1);
- js_pushnull(state);
- js_newuserdatax(state, "style", (void *)(uintptr_t)(node + 1),
- style_has, style_put, 0, 0);
- js_copy(state, -1);
- js_setregistry(state, key);
- }
- static int dataset_has(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- char attr[64];
- const char *value;
- if (!D || node >= D->nnode)
- return 0;
- data_name(name, attr, sizeof(attr));
- value = br_find_attr(D, node, attr);
- if (value)
- js_pushstring(state, value);
- else
- js_pushundefined(state);
- return 1;
- }
- static int dataset_put(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- char attr[64];
- const char *value = js_tostring(state, -1);
- if (!D || node >= D->nnode)
- return 0;
- data_name(name, attr, sizeof(attr));
- br_set_attr(D, node, attr, value, (unsigned int)strlen(value));
- if (host.changed)
- host.changed();
- return 1;
- }
- static int dataset_delete(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- char attr[64];
- (void)state;
- if (!D || node >= D->nnode)
- return 0;
- data_name(name, attr, sizeof(attr));
- br_remove_attr(D, node, attr);
- if (host.changed)
- host.changed();
- return 1;
- }
- static void push_dataset(js_State *state, unsigned int node)
- {
- char key[32];
- snprintf(key, sizeof(key), "ds_%u", node + 1);
- js_getregistry(state, key);
- if (!js_isundefined(state, -1))
- return;
- js_pop(state, 1);
- js_pushnull(state);
- js_newuserdatax(state, "dataset", (void *)(uintptr_t)(node + 1),
- dataset_has, dataset_put, dataset_delete, 0);
- js_copy(state, -1);
- js_setregistry(state, key);
- }
- static void node_class_set(unsigned int node, const char *classes)
- {
- br_set_attr(D, node, "class", classes,
- (unsigned int)strlen(classes));
- if (host.changed)
- host.changed();
- }
- static int class_has(const char *classes, const char *name)
- {
- unsigned int i = 0;
- if (!classes)
- return 0;
- while (classes[i]) {
- unsigned int start;
- while (classes[i] == ' ' || classes[i] == '\t')
- i++;
- start = i;
- while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
- i++;
- if (i - start == strlen(name) &&
- !strncmp(classes + start, name, i - start))
- return 1;
- }
- return 0;
- }
- static void class_add(unsigned int node, const char *name, int add)
- {
- const char *old = node_attr(node, "class");
- char buf[256];
- unsigned int n = 0, i = 0;
- if (add) {
- if (class_has(old, name))
- return;
- if (old && old[0]) {
- while (old[i] && n + 1 < sizeof(buf))
- buf[n++] = old[i++];
- if (n + 1 < sizeof(buf))
- buf[n++] = ' ';
- }
- i = 0;
- while (name[i] && n + 1 < sizeof(buf))
- buf[n++] = name[i++];
- } else {
- if (!old)
- return;
- while (old[i]) {
- unsigned int start;
- while (old[i] == ' ' || old[i] == '\t')
- i++;
- start = i;
- while (old[i] && old[i] != ' ' && old[i] != '\t')
- i++;
- if (i - start == strlen(name) &&
- !strncmp(old + start, name, i - start))
- continue;
- if (n && n + 1 < sizeof(buf))
- buf[n++] = ' ';
- {
- unsigned int k;
- for (k = start; k < i && n + 1 < sizeof(buf);
- k++)
- buf[n++] = old[k];
- }
- }
- }
- buf[n] = '\0';
- node_class_set(node, buf);
- }
- static void node_classlist_add(js_State *state)
- {
- class_add(node_of(js_currentfunctiondata(state)),
- js_tostring(state, 1), 1);
- }
- static void node_classlist_remove(js_State *state)
- {
- class_add(node_of(js_currentfunctiondata(state)),
- js_tostring(state, 1), 0);
- }
- static void node_classlist_toggle(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *name = js_tostring(state, 1);
- int has = class_has(node_attr(node, "class"), name);
- int want = !has;
- if (js_isdefined(state, 2))
- want = js_toboolean(state, 2);
- if (want != has)
- class_add(node, name, want);
- js_pushboolean(state, want);
- }
- static void node_classlist_contains(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- js_pushboolean(state, class_has(node_attr(node, "class"),
- js_tostring(state, 1)));
- }
- static unsigned int class_count(const char *classes)
- {
- unsigned int i = 0, n = 0;
- if (!classes)
- return 0;
- while (classes[i]) {
- while (classes[i] == ' ' || classes[i] == '\t')
- i++;
- if (!classes[i])
- break;
- n++;
- while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
- i++;
- }
- return n;
- }
- static void push_attributes(js_State *state, unsigned int node)
- {
- struct br_node *n;
- const char *p, *end;
- unsigned int count = 0;
- js_newobject(state);
- if (!D || node >= D->nnode)
- return;
- n = &D->nodes[node];
- p = D->attrs + n->extra_off;
- end = p + n->extra_len;
- while (p + 1 < end) {
- const char *an = p;
- const char *av;
- p += strlen(p) + 1;
- if (p >= end)
- break;
- av = p;
- p += strlen(p) + 1;
- js_newobject(state);
- js_pushstring(state, an);
- js_setproperty(state, -2, "name");
- js_pushstring(state, av);
- js_setproperty(state, -2, "value");
- js_pushboolean(state, 1);
- js_setproperty(state, -2, "specified");
- /* Array-like: attributes[i] and attributes[name] both work. */
- js_copy(state, -1);
- js_setproperty(state, -3, an);
- js_setindex(state, -2, (int)count);
- count++;
- }
- js_pushnumber(state, count);
- js_setproperty(state, -2, "length");
- }
- static void push_classlist(js_State *state, unsigned int node)
- {
- js_newobject(state);
- def_func_data(state, "add", node_classlist_add, 1, node + 1);
- def_func_data(state, "remove", node_classlist_remove, 1, node + 1);
- def_func_data(state, "toggle", node_classlist_toggle, 1, node + 1);
- def_func_data(state, "contains", node_classlist_contains, 1, node + 1);
- js_pushnumber(state, class_count(node_attr(node, "class")));
- js_setproperty(state, -2, "length");
- }
- static void node_getattr(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- push_attr(state, node, js_tostring(state, 1), 1);
- }
- static void node_setattr(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *name = js_tostring(state, 1);
- const char *value = js_tostring(state, 2);
- char lower[64];
- unsigned int i;
- for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- lower[i] = c;
- }
- lower[i] = '\0';
- br_set_attr(D, node, lower, value, (unsigned int)strlen(value));
- if (host.changed)
- host.changed();
- }
- static void node_removeattr(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *name = js_tostring(state, 1);
- char lower[64];
- unsigned int i;
- for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- lower[i] = c;
- }
- lower[i] = '\0';
- br_remove_attr(D, node, lower);
- if (host.changed)
- host.changed();
- }
- static void node_hasattr(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- js_pushboolean(state, node_attr(node, js_tostring(state, 1)) != 0);
- }
- static void node_appendchild(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int child = arg_node(state, 1);
- if (br_append_child(D, node, child) != 0) {
- js_pushnull(state);
- return;
- }
- if (host.changed)
- host.changed();
- push_node(state, child);
- }
- static void node_insertbefore(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int child = arg_node(state, 1);
- unsigned int before = 0;
- if (!js_isnull(state, 2) && !js_isundefined(state, 2))
- before = arg_node(state, 2);
- if (br_insert_before(D, node, child, before) != 0) {
- js_pushnull(state);
- return;
- }
- if (host.changed)
- host.changed();
- push_node(state, child);
- }
- static void node_removechild(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int child = arg_node(state, 1);
- if (br_remove_child(D, node, child) != 0) {
- js_pushnull(state);
- return;
- }
- if (host.changed)
- host.changed();
- push_node(state, child);
- }
- static void node_replacechild(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int child = arg_node(state, 1);
- unsigned int old = arg_node(state, 2);
- br_insert_before(D, node, child, old);
- br_remove_child(D, node, old);
- if (host.changed)
- host.changed();
- push_node(state, old);
- }
- static void node_clone(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int h = br_clone_node(D, node + 1,
- js_toboolean(state, 1));
- push_node(state, h);
- }
- static void node_contains(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int other = arg_node(state, 1);
- unsigned int p = other;
- while (1) {
- if (p == node) {
- js_pushboolean(state, 1);
- return;
- }
- if (p == 0)
- break;
- p = D->nodes[p].parent;
- }
- js_pushboolean(state, 0);
- }
- static void node_haschildren(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- js_pushboolean(state, node < D->nnode && D->nodes[node].first != 0);
- }
- static void node_remove(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int parent = node < D->nnode ? D->nodes[node].parent : 0;
- (void)state;
- if (parent)
- br_remove_child(D, parent, node + 1);
- if (host.changed)
- host.changed();
- }
- static void node_matches(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- js_pushboolean(state, br_matches(D, node + 1,
- js_tostring(state, 1)));
- }
- static void node_click(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- if (host.click)
- host.click(node);
- js_pushundefined(state);
- }
- static void node_getelementsbytagname(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *name = js_tostring(state, 1);
- unsigned int out[256];
- unsigned int n = br_query_all(D, node + 1, name, out, 256);
- js_newarray(state);
- for (unsigned int i = 0; i < n; i++) {
- push_node(state, out[i]);
- js_setindex(state, -2, (int)i);
- }
- }
- static void node_getelementsbyclassname(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *name = js_tostring(state, 1);
- char sel[256];
- unsigned int n = 0, i = 0;
- unsigned int out[256], count;
- while (name[i] && n + 2 < sizeof(sel)) {
- while (name[i] == ' ')
- i++;
- if (!name[i])
- break;
- sel[n++] = '.';
- while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
- sel[n++] = name[i++];
- }
- sel[n] = '\0';
- count = br_query_all(D, node + 1, sel, out, 256);
- js_newarray(state);
- for (i = 0; i < count; i++) {
- push_node(state, out[i]);
- js_setindex(state, -2, (int)i);
- }
- }
- static void node_queryselector(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int out[1];
- unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
- out, 1);
- if (n)
- push_node(state, out[0]);
- else
- js_pushnull(state);
- }
- static void node_queryselectorall(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- unsigned int out[256];
- unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
- out, 256);
- js_newarray(state);
- for (unsigned int i = 0; i < n; i++) {
- push_node(state, out[i]);
- js_setindex(state, -2, (int)i);
- }
- }
- static void node_getrect(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- int x = 0, y = 0, w = 0, h = 0;
- if (host.rect)
- host.rect(node, &x, &y, &w, &h);
- js_newobject(state);
- js_pushnumber(state, y); js_setproperty(state, -2, "top");
- js_pushnumber(state, x); js_setproperty(state, -2, "left");
- js_pushnumber(state, x + w); js_setproperty(state, -2, "right");
- js_pushnumber(state, y + h); js_setproperty(state, -2, "bottom");
- js_pushnumber(state, w); js_setproperty(state, -2, "width");
- js_pushnumber(state, h); js_setproperty(state, -2, "height");
- }
- static void node_noop(js_State *state)
- {
- (void)state;
- }
- static void node_push_text(js_State *state, unsigned int node)
- {
- unsigned int cap = D->text_used + 1;
- char *buf;
- unsigned int len;
- if (cap < 32)
- cap = 32;
- buf = (char *)malloc(cap);
- if (!buf) {
- js_pushstring(state, "");
- return;
- }
- len = br_text_content(D, node, buf, cap);
- js_pushlstring(state, buf, (int)len);
- free(buf);
- }
- static unsigned int node_form_ancestor(unsigned int node)
- {
- while (node && node < D->nnode) {
- if (D->nodes[node].type == BR_NODE_ELEM &&
- D->nodes[node].tag == BR_TAG_FORM)
- return node;
- node = D->nodes[node].parent;
- }
- return 0;
- }
- static void push_form_elements(js_State *state, unsigned int form)
- {
- unsigned int out[128];
- int n = 0, i;
- if (host.form_elements)
- n = host.form_elements(form, out, (int)(sizeof(out) /
- sizeof(out[0])));
- js_newarray(state);
- for (i = 0; i < n; i++) {
- const char *name;
- push_node(state, out[i] + 1);
- js_setindex(state, -2, i);
- name = br_find_attr(D, out[i], "name");
- if (name && name[0]) {
- push_node(state, out[i] + 1);
- js_setproperty(state, -2, name);
- }
- }
- }
- static void form_submit_js(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- if (host.form_submit)
- host.form_submit(node);
- }
- static void form_reset_js(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- if (host.form_reset)
- host.form_reset(node);
- }
- /* Userdata property hooks: every element wrapper carries node + 1. */
- static int node_has(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- struct br_node *n;
- char buf[64];
- if (!D || node >= D->nnode)
- return 0;
- n = &D->nodes[node];
- if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM) {
- if (!strcmp(name, "elements")) {
- push_form_elements(state, node);
- return 1;
- }
- if (!strcmp(name, "length")) {
- unsigned int out[128];
- int count = 0;
- if (host.form_elements)
- count = host.form_elements(node, out,
- (int)(sizeof(out) / sizeof(out[0])));
- js_pushnumber(state, count);
- return 1;
- }
- if (!strcmp(name, "submit")) {
- push_func_data(state, "submit", form_submit_js, 0,
- node + 1);
- return 1;
- }
- if (!strcmp(name, "reset")) {
- push_func_data(state, "reset", form_reset_js, 0,
- node + 1);
- return 1;
- }
- }
- if (!strcmp(name, "form")) {
- unsigned int f;
- if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM)
- f = 0;
- else
- f = node_form_ancestor(node);
- if (f)
- push_node(state, f + 1);
- else
- js_pushnull(state);
- return 1;
- }
- if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
- node_push_text(state, node);
- return 1;
- }
- if (!strcmp(name, "nodeValue")) {
- if (n->type == BR_NODE_TEXT || n->type == BR_NODE_COMMENT)
- js_pushlstring(state, D->text + n->text_off,
- (int)n->text_len);
- else
- js_pushnull(state);
- return 1;
- }
- if (!strcmp(name, "nodeType")) {
- js_pushnumber(state, node_type_value(node));
- return 1;
- }
- if (!strcmp(name, "nodeName") || !strcmp(name, "tagName")) {
- node_name(node, buf, sizeof(buf));
- js_pushstring(state, buf);
- return 1;
- }
- if (!strcmp(name, "parentNode") || !strcmp(name, "parentElement")) {
- unsigned int p = n->parent;
- push_node(state, p ? p + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "childNodes")) {
- push_child_list(state, node, 0);
- return 1;
- }
- if (!strcmp(name, "children")) {
- push_child_list(state, node, 1);
- return 1;
- }
- if (!strcmp(name, "firstChild")) {
- push_node(state, n->first ? n->first + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "lastChild")) {
- push_node(state, n->last ? n->last + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "firstElementChild") ||
- !strcmp(name, "lastElementChild")) {
- unsigned int c = n->first;
- unsigned int found = 0;
- if (!strcmp(name, "lastElementChild")) {
- for (c = n->first; c; c = D->nodes[c].next)
- if (D->nodes[c].type == BR_NODE_ELEM)
- found = c;
- } else {
- while (c && D->nodes[c].type != BR_NODE_ELEM)
- c = D->nodes[c].next;
- found = c;
- }
- push_node(state, found ? found + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "nextSibling") || !strcmp(name, "nextElementSibling")) {
- unsigned int c = n->next;
- if (!strcmp(name, "nextElementSibling"))
- while (c && D->nodes[c].type != BR_NODE_ELEM)
- c = D->nodes[c].next;
- push_node(state, c ? c + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "previousSibling") ||
- !strcmp(name, "previousElementSibling")) {
- unsigned int c = node_prev_sibling(node);
- if (!strcmp(name, "previousElementSibling"))
- while (c && D->nodes[c].type != BR_NODE_ELEM)
- c = node_prev_sibling(c);
- push_node(state, c ? c + 1 : 0);
- return 1;
- }
- if (!strcmp(name, "childElementCount")) {
- unsigned int c, count = 0;
- for (c = n->first; c; c = D->nodes[c].next)
- if (D->nodes[c].type == BR_NODE_ELEM)
- count++;
- js_pushnumber(state, count);
- return 1;
- }
- if (n->tag == BR_TAG_A || n->tag == BR_TAG_AREA) {
- if (!strcmp(name, "href") || !strcmp(name, "protocol") ||
- !strcmp(name, "host") || !strcmp(name, "hostname") ||
- !strcmp(name, "port") || !strcmp(name, "pathname") ||
- !strcmp(name, "search") || !strcmp(name, "hash") ||
- !strcmp(name, "origin")) {
- push_element_url(state, node, name);
- return 1;
- }
- }
- if (!strcmp(name, "id")) { push_attr(state, node, "id", 0); return 1; }
- if (!strcmp(name, "className")) {
- push_attr(state, node, "class", 0);
- return 1;
- }
- if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
- if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
- if (!strcmp(name, "value")) {
- char buf[128];
- if (host.form_value && host.form_value(node, buf, sizeof(buf)))
- js_pushstring(state, buf);
- else
- push_attr(state, node, "value", 0);
- return 1;
- }
- if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
- if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
- if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
- if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
- if (!strcmp(name, "checked")) {
- int c = host.form_checked ? host.form_checked(node) : -1;
- js_pushboolean(state, c >= 0 ? c : node_attr(node, name) != 0);
- return 1;
- }
- if (!strcmp(name, "selectedIndex")) {
- int c = host.form_selected ? host.form_selected(node) : -1;
- js_pushnumber(state, c >= 0 ? c : 0);
- return 1;
- }
- if (!strcmp(name, "selected")) {
- int c = host.form_selected ? host.form_selected(node) : -1;
- if (c >= 0)
- js_pushboolean(state, c != 0);
- else
- js_pushboolean(state, node_attr(node, name) != 0);
- return 1;
- }
- if (!strcmp(name, "disabled")) {
- js_pushboolean(state, node_attr(node, name) != 0);
- return 1;
- }
- if (!strcmp(name, "style")) {
- push_style_object(state, node);
- return 1;
- }
- if (!strcmp(name, "dataset") || !strcmp(name, "domStringMap")) {
- push_dataset(state, node);
- return 1;
- }
- if (!strcmp(name, "classList")) {
- push_classlist(state, node);
- return 1;
- }
- if (!strcmp(name, "attributes")) {
- push_attributes(state, node);
- return 1;
- }
- if (!strcmp(name, "innerHTML") || !strcmp(name, "outerHTML")) {
- char key[32];
- snprintf(key, sizeof(key), "html_%u", node + 1);
- js_getregistry(state, key);
- if (js_isundefined(state, -1)) {
- js_pop(state, 1);
- js_pushstring(state, "");
- }
- return 1;
- }
- if (!strcmp(name, "ownerDocument")) {
- js_getglobal(state, "document");
- return 1;
- }
- if (!strcmp(name, "getAttribute"))
- { push_func_data(state, "getAttribute", node_getattr, 1, node + 1); return 1; }
- if (!strcmp(name, "setAttribute"))
- { push_func_data(state, "setAttribute", node_setattr, 2, node + 1); return 1; }
- if (!strcmp(name, "removeAttribute"))
- { push_func_data(state, "removeAttribute", node_removeattr, 1, node + 1); return 1; }
- if (!strcmp(name, "hasAttribute"))
- { push_func_data(state, "hasAttribute", node_hasattr, 1, node + 1); return 1; }
- if (!strcmp(name, "appendChild"))
- { push_func_data(state, "appendChild", node_appendchild, 1, node + 1); return 1; }
- if (!strcmp(name, "insertBefore"))
- { push_func_data(state, "insertBefore", node_insertbefore, 2, node + 1); return 1; }
- if (!strcmp(name, "removeChild"))
- { push_func_data(state, "removeChild", node_removechild, 1, node + 1); return 1; }
- if (!strcmp(name, "replaceChild"))
- { push_func_data(state, "replaceChild", node_replacechild, 2, node + 1); return 1; }
- if (!strcmp(name, "cloneNode"))
- { push_func_data(state, "cloneNode", node_clone, 1, node + 1); return 1; }
- if (!strcmp(name, "contains"))
- { push_func_data(state, "contains", node_contains, 1, node + 1); return 1; }
- if (!strcmp(name, "hasChildNodes"))
- { push_func_data(state, "hasChildNodes", node_haschildren, 0, node + 1); return 1; }
- if (!strcmp(name, "remove"))
- { push_func_data(state, "remove", node_remove, 0, node + 1); return 1; }
- if (!strcmp(name, "matches") || !strcmp(name, "webkitMatchesSelector") ||
- !strcmp(name, "mozMatchesSelector") ||
- !strcmp(name, "msMatchesSelector"))
- { push_func_data(state, name, node_matches, 1, node + 1); return 1; }
- if (!strcmp(name, "getElementsByTagName"))
- { push_func_data(state, "getElementsByTagName", node_getelementsbytagname, 1, node + 1); return 1; }
- if (!strcmp(name, "getElementsByClassName"))
- { push_func_data(state, "getElementsByClassName", node_getelementsbyclassname, 1, node + 1); return 1; }
- if (!strcmp(name, "querySelector"))
- { push_func_data(state, "querySelector", node_queryselector, 1, node + 1); return 1; }
- if (!strcmp(name, "querySelectorAll"))
- { push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
- if (!strcmp(name, "addEventListener"))
- { push_func_data(state, "addEventListener", node_addlistener, 2, node + 1); return 1; }
- if (!strcmp(name, "removeEventListener"))
- { push_func_data(state, "removeEventListener", node_removelistener, 2, node + 1); return 1; }
- if (!strcmp(name, "dispatchEvent"))
- { push_func_data(state, "dispatchEvent", node_dispatchevent, 1, node + 1); return 1; }
- if (!strcmp(name, "getBoundingClientRect"))
- { push_func_data(state, "getBoundingClientRect", node_getrect, 0, node + 1); return 1; }
- if (!strcmp(name, "getClientRects")) {
- js_newarray(state);
- return 1;
- }
- if (!strcmp(name, "offsetWidth") || !strcmp(name, "offsetHeight") ||
- !strcmp(name, "clientWidth") || !strcmp(name, "clientHeight") ||
- !strcmp(name, "scrollWidth") || !strcmp(name, "scrollHeight")) {
- int x = 0, y = 0, w = 0, h = 0;
- if (host.rect)
- host.rect(node, &x, &y, &w, &h);
- js_pushnumber(state, (!strcmp(name, "offsetHeight") ||
- !strcmp(name, "clientHeight") ||
- !strcmp(name, "scrollHeight")) ? h : w);
- return 1;
- }
- if (!strcmp(name, "scrollTop") || !strcmp(name, "scrollLeft")) {
- js_pushnumber(state, 0);
- return 1;
- }
- if (!strcmp(name, "click")) {
- push_func_data(state, "click", node_click, 0, node + 1);
- return 1;
- }
- if (!strcmp(name, "focus") || !strcmp(name, "blur")) {
- push_func_data(state, name, node_noop, 0, node + 1);
- return 1;
- }
- return 0;
- }
- static void node_set_string(unsigned int node, const char *name,
- const char *value)
- {
- br_set_attr(D, node, name, value, (unsigned int)strlen(value));
- if (host.changed)
- host.changed();
- }
- static int node_put(js_State *state, void *p, const char *name)
- {
- unsigned int node = node_of(p);
- if (!D || node >= D->nnode)
- return 0;
- if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
- const char *text = js_tostring(state, -1);
- unsigned int len = (unsigned int)strlen(text);
- if (br_set_text_content(D, node, text, len) != 0) {
- if (host.status)
- host.status("Page text arena is full", 1);
- } else if (host.changed) {
- host.changed();
- }
- return 1;
- }
- if (!strcmp(name, "nodeValue")) {
- const char *text = js_tostring(state, -1);
- if (br_set_text_node(D, node, text,
- (unsigned int)strlen(text)) == 0 &&
- host.changed)
- host.changed();
- return 1;
- }
- if (!strcmp(name, "id") || !strcmp(name, "className") ||
- !strcmp(name, "href") || !strcmp(name, "src") ||
- !strcmp(name, "value") || !strcmp(name, "type") ||
- !strcmp(name, "name") || !strcmp(name, "alt") ||
- !strcmp(name, "title")) {
- const char *value = js_tostring(state, -1);
- if (!strcmp(name, "value") && host.form_set_value &&
- host.form_set_value(node, value))
- return 1;
- if (!strcmp(name, "className"))
- node_set_string(node, "class", value);
- else
- node_set_string(node, name, value);
- return 1;
- }
- if (!strcmp(name, "selectedIndex")) {
- if (host.form_set_selected)
- host.form_set_selected(node, js_tointeger(state, -1));
- return 1;
- }
- if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
- !strcmp(name, "selected")) {
- int b = js_toboolean(state, -1);
- if (!strcmp(name, "checked") && host.form_set_checked &&
- host.form_set_checked(node, b))
- return 1;
- if (!strcmp(name, "selected") && host.form_set_selected &&
- host.form_set_selected(node, b ? 1 : 0))
- return 1;
- if (b)
- br_set_attr(D, node, name, "", 0);
- else
- br_remove_attr(D, node, name);
- if (host.changed)
- host.changed();
- return 1;
- }
- if (!strcmp(name, "innerHTML")) {
- const char *html = js_tostring(state, -1);
- char key[32];
- br_set_inner_html(D, node, html, (unsigned int)strlen(html));
- snprintf(key, sizeof(key), "html_%u", node + 1);
- js_pushstring(state, html);
- js_setregistry(state, key);
- if (host.changed)
- host.changed();
- return 1;
- }
- if (!strcmp(name, "outerHTML")) {
- char key[32];
- snprintf(key, sizeof(key), "html_%u", node + 1);
- js_pushstring(state, js_tostring(state, -1));
- js_setregistry(state, key);
- return 1;
- }
- if (!strcmp(name, "style")) {
- if (js_isstring(state, -1))
- node_set_string(node, "style",
- js_tostring(state, -1));
- return 1;
- }
- return 0;
- }
- static int node_delete(js_State *state, void *p, const char *name)
- {
- (void)state;
- (void)p;
- (void)name;
- return 0;
- }
- static void doc_getelementbyid(js_State *state)
- {
- const char *id = js_tostring(state, 1);
- push_node(state, br_find_id(D, id));
- }
- static void console_log(js_State *state)
- {
- int i;
- int n = js_gettop(state);
- for (i = 1; i < n; i++) {
- if (i > 1)
- u_putc(' ');
- u_puts(js_tostring(state, i));
- }
- u_putc('\n');
- }
- static void js_alert(js_State *state)
- {
- char msg[160];
- unsigned int used = 0;
- int i;
- int n = js_gettop(state);
- for (i = 1; i < n; i++) {
- const char *s = js_tostring(state, i);
- unsigned int k;
- if (i > 1 && used + 1 < sizeof(msg))
- msg[used++] = ' ';
- for (k = 0; s[k] && used + 1 < sizeof(msg); k++)
- msg[used++] = s[k];
- }
- msg[used] = '\0';
- if (host.status)
- host.status(msg, 0);
- }
- static void push_query_array(js_State *state, unsigned int root,
- const char *selector)
- {
- unsigned int out[256];
- unsigned int n = br_query_all(D, root, selector, out, 256);
- unsigned int i;
- js_newarray(state);
- for (i = 0; i < n; i++) {
- push_node(state, out[i]);
- js_setindex(state, -2, (int)i);
- }
- }
- static void doc_create_element(js_State *state)
- {
- const char *name = js_tostring(state, 1);
- char lower[32];
- unsigned int i;
- for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
- char c = name[i];
- if (c >= 'A' && c <= 'Z')
- c = (char)(c - 'A' + 'a');
- lower[i] = c;
- }
- lower[i] = '\0';
- push_node(state, br_create_element(D, lower, i));
- }
- static void doc_create_text(js_State *state)
- {
- const char *text = js_tostring(state, 1);
- push_node(state, br_create_text(D, text, (unsigned int)strlen(text)));
- }
- static void doc_create_comment(js_State *state)
- {
- const char *text = js_tostring(state, 1);
- push_node(state, br_create_comment(D, text, (unsigned int)strlen(text)));
- }
- static void doc_create_fragment(js_State *state)
- {
- push_node(state, br_create_fragment(D));
- }
- static void doc_create_attribute(js_State *state)
- {
- const char *name = js_tostring(state, 1);
- js_newobject(state);
- js_pushstring(state, name);
- js_setproperty(state, -2, "name");
- js_pushstring(state, "");
- js_setproperty(state, -2, "value");
- js_pushboolean(state, 1);
- js_setproperty(state, -2, "specified");
- }
- static void doc_getelementsbytagname(js_State *state)
- {
- push_query_array(state, 0, js_tostring(state, 1));
- }
- static void doc_getelementsbyclassname(js_State *state)
- {
- const char *name = js_tostring(state, 1);
- char sel[256];
- unsigned int n = 0, i = 0;
- while (name[i] && n + 2 < sizeof(sel)) {
- while (name[i] == ' ')
- i++;
- if (!name[i])
- break;
- sel[n++] = '.';
- while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
- sel[n++] = name[i++];
- }
- sel[n] = '\0';
- push_query_array(state, 0, sel);
- }
- static void doc_queryselector(js_State *state)
- {
- unsigned int out[1];
- unsigned int n = br_query_all(D, 0, js_tostring(state, 1), out, 1);
- if (n)
- push_node(state, out[0]);
- else
- js_pushnull(state);
- }
- static void doc_queryselectorall(js_State *state)
- {
- push_query_array(state, 0, js_tostring(state, 1));
- }
- static void location_get(js_State *state)
- {
- js_pushstring(state, current_url ? current_url : "");
- }
- static void location_set(js_State *state)
- {
- const char *url = js_tostring(state, 1);
- if (host.navigate)
- host.navigate(url);
- }
- static void win_noop(js_State *state);
- struct loc_parts {
- const char *protocol, *host, *hostname, *port, *path, *search, *hash;
- const char *origin;
- unsigned int protocol_len, host_len, hostname_len, port_len;
- unsigned int path_len, search_len, hash_len, origin_len;
- };
- static void loc_split(const char *u, struct loc_parts *p)
- {
- unsigned int n = (unsigned int)strlen(u), i;
- const char *rest;
- memset(p, 0, sizeof(*p));
- p->path = u;
- p->path_len = n;
- p->origin = "null";
- p->origin_len = 4;
- for (i = 0; i < n; i++)
- if (u[i] == ':')
- break;
- if (i + 2 < n && u[i] == ':' && u[i + 1] == '/' && u[i + 2] == '/') {
- const char *hosts = u + i + 3, *hoste = hosts;
- while (*hoste && *hoste != '/' && *hoste != '?' && *hoste != '#')
- hoste++;
- p->protocol = u;
- p->protocol_len = i + 1;
- p->host = hosts;
- p->host_len = (unsigned int)(hoste - hosts);
- p->hostname = hosts;
- p->hostname_len = p->host_len;
- for (i = 0; i < p->hostname_len; i++)
- if (hosts[i] == ':') {
- p->port = hosts + i + 1;
- p->port_len = p->hostname_len - i - 1;
- p->hostname_len = i;
- break;
- }
- p->origin = u;
- p->origin_len = (unsigned int)(hoste - u);
- rest = hoste;
- } else {
- p->protocol = "file:";
- p->protocol_len = 5;
- rest = u;
- }
- {
- const char *q = rest;
- while (*q && *q != '?' && *q != '#')
- q++;
- p->path = rest;
- p->path_len = (unsigned int)(q - rest);
- if (*q == '?') {
- const char *s = q;
- while (*q && *q != '#')
- q++;
- p->search = s;
- p->search_len = (unsigned int)(q - s);
- }
- if (*q == '#') {
- p->hash = q;
- p->hash_len = (unsigned int)strlen(q);
- }
- }
- }
- static void push_location(js_State *state)
- {
- struct loc_parts p;
- loc_split(current_url ? current_url : "", &p);
- js_newobject(state);
- js_newcfunction(state, location_get, "get href", 0);
- js_newcfunction(state, location_set, "set href", 1);
- js_defaccessor(state, -3, "href", 0);
- #define LOC_FIELD(field) \
- do { \
- js_pushlstring(state, p.field ? p.field : "", \
- p.field ? (int)p.field##_len : 0); \
- js_setproperty(state, -2, #field); \
- } while (0)
- LOC_FIELD(protocol);
- LOC_FIELD(host);
- LOC_FIELD(hostname);
- LOC_FIELD(port);
- js_pushlstring(state, p.path ? p.path : "",
- p.path ? (int)p.path_len : 0);
- js_setproperty(state, -2, "pathname");
- LOC_FIELD(search);
- LOC_FIELD(hash);
- LOC_FIELD(origin);
- #undef LOC_FIELD
- js_newcfunction(state, win_noop, "reload", 0);
- js_setproperty(state, -2, "reload");
- js_newcfunction(state, win_noop, "assign", 1);
- js_setproperty(state, -2, "assign");
- js_newcfunction(state, win_noop, "replace", 1);
- js_setproperty(state, -2, "replace");
- js_newcfunction(state, win_noop, "toString", 0);
- js_setproperty(state, -2, "toString");
- }
- /* ---- URL resolution for <a>/<area> URL properties ---- */
- static int url_has_scheme(const char *s)
- {
- unsigned int i = 1;
- if (!((s[0] >= 'a' && s[0] <= 'z') ||
- (s[0] >= 'A' && s[0] <= 'Z')))
- return 0;
- while (s[i]) {
- char c = s[i];
- if (c == ':')
- return 1;
- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '+' || c == '-' ||
- c == '.'))
- return 0;
- i++;
- }
- return 0;
- }
- static void url_put(char *out, unsigned int cap, unsigned int *n,
- const char *s, unsigned int len)
- {
- unsigned int i;
- for (i = 0; i < len && *n + 1 < cap; i++)
- out[(*n)++] = s[i];
- }
- /* Collapse //, /./ and /../ inside the path part of an absolute URL. */
- static void url_normalize(char *url)
- {
- char tmp[512];
- char *path;
- unsigned int n = 0, i = 0;
- path = strstr(url, "://");
- if (path) {
- path += 3;
- while (*path && *path != '/')
- path++;
- } else {
- path = url;
- }
- if (path[0] != '/')
- return;
- while (path[i]) {
- unsigned int start;
- while (path[i] == '/')
- i++;
- start = i;
- while (path[i] && path[i] != '/')
- i++;
- if (i == start)
- continue;
- if (i - start == 1 && path[start] == '.')
- continue;
- if (i - start == 2 && path[start] == '.' &&
- path[start + 1] == '.') {
- while (n > 0 && tmp[n - 1] != '/')
- n--;
- if (n > 0)
- n--;
- continue;
- }
- if (n + 1 < sizeof(tmp))
- tmp[n++] = '/';
- while (start < i && n + 1 < sizeof(tmp))
- tmp[n++] = path[start++];
- }
- if (n == 0)
- tmp[n++] = '/';
- tmp[n] = '\0';
- memcpy(path, tmp, n + 1);
- }
- static void url_resolve(const char *base, const char *rel,
- char *out, unsigned int cap)
- {
- struct loc_parts bp;
- unsigned int n = 0, i;
- if (cap == 0)
- return;
- if (!rel[0]) {
- url_put(out, cap, &n, base, (unsigned int)strlen(base));
- out[n] = '\0';
- return;
- }
- if (url_has_scheme(rel)) {
- url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
- out[n] = '\0';
- url_normalize(out);
- return;
- }
- loc_split(base, &bp);
- if (rel[0] == '/' && rel[1] == '/') {
- url_put(out, cap, &n, bp.protocol, bp.protocol_len);
- url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
- } else if (rel[0] == '/') {
- url_put(out, cap, &n, bp.protocol, bp.protocol_len);
- url_put(out, cap, &n, "//", 2);
- url_put(out, cap, &n, bp.host, bp.host_len);
- url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
- } else if (rel[0] == '#' || rel[0] == '?') {
- url_put(out, cap, &n, bp.protocol, bp.protocol_len);
- if (bp.host_len || (bp.protocol_len == 5 &&
- bp.protocol[0] == 'f')) {
- url_put(out, cap, &n, "//", 2);
- url_put(out, cap, &n, bp.host, bp.host_len);
- }
- url_put(out, cap, &n, bp.path, bp.path_len);
- if (rel[0] == '#')
- url_put(out, cap, &n, bp.search, bp.search_len);
- url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
- } else {
- const char *dir_end = bp.path;
- for (i = 0; i < bp.path_len; i++)
- if (bp.path[i] == '/')
- dir_end = bp.path + i + 1;
- if (dir_end == bp.path)
- dir_end = bp.path + bp.path_len;
- url_put(out, cap, &n, bp.protocol, bp.protocol_len);
- if (bp.host_len || (bp.protocol_len == 5 &&
- bp.protocol[0] == 'f')) {
- url_put(out, cap, &n, "//", 2);
- url_put(out, cap, &n, bp.host, bp.host_len);
- }
- url_put(out, cap, &n, bp.path,
- (unsigned int)(dir_end - bp.path));
- url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
- }
- out[n] = '\0';
- url_normalize(out);
- }
- /* <a> URL properties are resolved against the page URL, like the DOM. */
- static void push_element_url(js_State *state, unsigned int node,
- const char *name)
- {
- char abs_url[512];
- const char *href = br_find_attr(D, node, "href");
- struct loc_parts p;
- if (!href || !href[0]) {
- js_pushstring(state, "");
- return;
- }
- url_resolve(current_url ? current_url : "/", href,
- abs_url, sizeof(abs_url));
- loc_split(abs_url, &p);
- if (!strcmp(name, "href"))
- js_pushstring(state, abs_url);
- else if (!strcmp(name, "protocol"))
- js_pushlstring(state, p.protocol, (int)p.protocol_len);
- else if (!strcmp(name, "origin"))
- js_pushlstring(state, p.origin ? p.origin : "",
- (int)p.origin_len);
- else if (!strcmp(name, "host"))
- js_pushlstring(state, p.host ? p.host : "", (int)p.host_len);
- else if (!strcmp(name, "hostname"))
- js_pushlstring(state, p.hostname ? p.hostname : "",
- (int)p.hostname_len);
- else if (!strcmp(name, "port"))
- js_pushlstring(state, p.port ? p.port : "", (int)p.port_len);
- else if (!strcmp(name, "pathname"))
- js_pushlstring(state, p.path ? p.path : "", (int)p.path_len);
- else if (!strcmp(name, "search"))
- js_pushlstring(state, p.search ? p.search : "",
- (int)p.search_len);
- else
- js_pushlstring(state, p.hash ? p.hash : "", (int)p.hash_len);
- }
- static void win_noop(js_State *state)
- {
- (void)state;
- }
- static void timer_add(js_State *state, int repeat)
- {
- int i;
- unsigned int ms;
- if (!js_iscallable(state, 1)) {
- js_pushnumber(state, 0);
- return;
- }
- ms = js_isdefined(state, 2) ?
- (unsigned int)js_tointeger(state, 2) : 0;
- if (repeat && ms == 0)
- ms = 1;
- for (i = 0; i < JS_TIMERS; i++)
- if (!timers[i].used)
- break;
- if (i == JS_TIMERS) {
- js_pushnumber(state, 0);
- return;
- }
- js_copy(state, 1);
- snprintf(timers[i].ref, sizeof(timers[i].ref), "%s",
- js_ref(state));
- timers[i].used = 1;
- timers[i].repeat = repeat;
- timers[i].interval = ms;
- timers[i].deadline = u_ticks_ms() + ms;
- js_pushnumber(state, i + 1);
- }
- static void win_settimeout(js_State *state)
- {
- timer_add(state, 0);
- }
- static void win_setinterval(js_State *state)
- {
- timer_add(state, 1);
- }
- static void timer_clear(js_State *state)
- {
- int id = js_tointeger(state, 1);
- if (id >= 1 && id <= JS_TIMERS && timers[id - 1].used) {
- js_unref(state, timers[id - 1].ref);
- timers[id - 1].used = 0;
- }
- }
- static void win_requestanimationframe(js_State *state)
- {
- timer_add(state, 0);
- }
- static void win_performance_now(js_State *state)
- {
- js_pushnumber(state, (double)u_ticks_ms());
- }
- struct computed_prop {
- const char *js;
- const char *css;
- };
- static const struct computed_prop computed_props[] = {
- { "display", "display" }, { "position", "position" },
- { "width", "width" }, { "height", "height" },
- { "color", "color" }, { "backgroundColor", "background-color" },
- { "fill", "fill" }, { "stroke", "stroke" },
- { "fontSize", "font-size" }, { "fontWeight", "font-weight" },
- { "fontStyle", "font-style" }, { "fontFamily", "font-family" },
- { "textAlign", "text-align" }, { "float", "float" },
- { "cssFloat", "float" }, { "clear", "clear" },
- { "opacity", "opacity" }, { "visibility", "visibility" },
- { "overflow", "overflow" }, { "whiteSpace", "white-space" },
- { "textTransform", "text-transform" }, { "boxSizing", "box-sizing" },
- { "lineHeight", "line-height" }, { "zIndex", "z-index" },
- { "top", "top" }, { "left", "left" }, { "right", "right" },
- { "bottom", "bottom" },
- { "marginTop", "margin-top" }, { "marginRight", "margin-right" },
- { "marginBottom", "margin-bottom" }, { "marginLeft", "margin-left" },
- { "paddingTop", "padding-top" }, { "paddingRight", "padding-right" },
- { "paddingBottom", "padding-bottom" },
- { "paddingLeft", "padding-left" },
- { "borderTopWidth", "border-top-width" },
- { "borderRightWidth", "border-right-width" },
- { "borderBottomWidth", "border-bottom-width" },
- { "borderLeftWidth", "border-left-width" },
- { "borderTopColor", "border-top-color" },
- { "borderRightColor", "border-right-color" },
- { "borderBottomColor", "border-bottom-color" },
- { "borderLeftColor", "border-left-color" },
- { "backgroundImage", "background-image" },
- { "backgroundRepeat", "background-repeat" },
- { "backgroundPosition", "background-position" },
- { "backgroundSize", "background-size" },
- { "transform", "transform" },
- { 0, 0 }
- };
- static void computed_get(js_State *state)
- {
- unsigned int node = node_of(js_currentfunctiondata(state));
- const char *prop = js_tostring(state, 1);
- char buf[192];
- if (host.computed && host.computed(node, prop, buf, sizeof(buf)))
- js_pushstring(state, buf);
- else
- js_pushstring(state, "");
- }
- static void win_getcomputedstyle(js_State *state)
- {
- unsigned int node = 0;
- char buf[192];
- int i;
- if (js_isuserdata(state, 1, "node"))
- node = node_of(js_touserdata(state, 1, "node"));
- js_newobject(state);
- def_func_data(state, "getPropertyValue", computed_get, 1, node + 1);
- for (i = 0; computed_props[i].js; i++) {
- if (!host.computed ||
- !host.computed(node, computed_props[i].css, buf,
- sizeof(buf)))
- continue;
- js_pushstring(state, buf);
- js_setproperty(state, -2, computed_props[i].js);
- }
- js_pushstring(state, "");
- js_setproperty(state, -2, "cssText");
- }
- static void win_matchmedia(js_State *state)
- {
- const char *query = js_tostring(state, 1);
- js_newobject(state);
- js_pushboolean(state, 0);
- js_setproperty(state, -2, "matches");
- js_pushstring(state, query);
- js_setproperty(state, -2, "media");
- def_func(state, "addListener", win_noop, 1);
- def_func(state, "removeListener", win_noop, 1);
- def_func(state, "addEventListener", win_noop, 2);
- def_func(state, "removeEventListener", win_noop, 2);
- }
- static void install_globals(js_State *state)
- {
- unsigned int h;
- js_newobject(state);
- def_func(state, "getElementById", doc_getelementbyid, 1);
- def_func(state, "createElement", doc_create_element, 1);
- def_func(state, "createTextNode", doc_create_text, 1);
- def_func(state, "createComment", doc_create_comment, 1);
- def_func(state, "createDocumentFragment", doc_create_fragment, 0);
- def_func(state, "createAttribute", doc_create_attribute, 1);
- def_func(state, "getElementsByTagName", doc_getelementsbytagname, 1);
- def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
- def_func(state, "querySelector", doc_queryselector, 1);
- def_func(state, "querySelectorAll", doc_queryselectorall, 1);
- def_func(state, "addEventListener", doc_addlistener, 2);
- def_func(state, "removeEventListener", doc_removelistener, 2);
- def_func(state, "createEvent", doc_createevent, 1);
- h = br_find_tag(D, BR_TAG_HTML);
- if (!h && D->nnode > 1)
- h = D->nodes[0].first ? D->nodes[0].first + 1 : 0;
- push_node(state, h);
- js_setproperty(state, -2, "documentElement");
- h = br_find_tag(D, BR_TAG_BODY);
- push_node(state, h);
- js_setproperty(state, -2, "body");
- h = br_find_tag(D, BR_TAG_HEAD);
- push_node(state, h);
- js_setproperty(state, -2, "head");
- js_pushnumber(state, 9);
- js_setproperty(state, -2, "nodeType");
- js_pushstring(state, "#document");
- js_setproperty(state, -2, "nodeName");
- js_pushnull(state);
- js_setproperty(state, -2, "ownerDocument");
- js_pushstring(state, "CSS1Compat");
- js_setproperty(state, -2, "compatMode");
- js_pushstring(state, D->title);
- js_setproperty(state, -2, "title");
- js_pushstring(state, "complete");
- js_setproperty(state, -2, "readyState");
- js_pushstring(state, "");
- js_setproperty(state, -2, "cookie");
- js_pushstring(state, current_url ? current_url : "");
- js_setproperty(state, -2, "URL");
- js_defglobal(state, "document", JS_DONTENUM);
- js_newobject(state);
- js_newcfunction(state, console_log, "log", 0);
- js_setproperty(state, -2, "log");
- js_defglobal(state, "console", JS_DONTENUM);
- js_newcfunction(state, js_alert, "alert", 1);
- js_defglobal(state, "alert", JS_DONTENUM);
- js_newcfunction(state, event_call, "Event", 1);
- js_defglobal(state, "Event", JS_DONTENUM);
- js_newcfunction(state, custom_event_call, "CustomEvent", 1);
- js_defglobal(state, "CustomEvent", JS_DONTENUM);
- js_newcfunction(state, mouse_event_call, "MouseEvent", 1);
- js_defglobal(state, "MouseEvent", JS_DONTENUM);
- push_location(state);
- js_defglobal(state, "location", JS_DONTENUM);
- js_newobject(state);
- js_pushstring(state, "Mozilla/5.0 (X11; Linux i686) NebOS/0.2");
- js_setproperty(state, -2, "userAgent");
- js_pushstring(state, "NebOS");
- js_setproperty(state, -2, "platform");
- js_pushstring(state, "en-US");
- js_setproperty(state, -2, "language");
- js_defglobal(state, "navigator", JS_DONTENUM);
- js_newobject(state);
- js_pushnumber(state, 1);
- js_setproperty(state, -2, "length");
- def_func(state, "pushState", win_noop, 3);
- def_func(state, "replaceState", win_noop, 3);
- def_func(state, "back", win_noop, 0);
- def_func(state, "forward", win_noop, 0);
- def_func(state, "go", win_noop, 1);
- js_defglobal(state, "history", JS_DONTENUM);
- def_global_func(state, "matchMedia", win_matchmedia, 1);
- def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
- def_global_func(state, "addEventListener", win_addlistener, 2);
- def_global_func(state, "removeEventListener", win_removelistener, 2);
- def_global_func(state, "scrollTo", win_noop, 2);
- def_global_func(state, "scrollBy", win_noop, 2);
- def_global_func(state, "setTimeout", win_settimeout, 1);
- def_global_func(state, "setInterval", win_setinterval, 1);
- def_global_func(state, "clearTimeout", timer_clear, 1);
- def_global_func(state, "clearInterval", timer_clear, 1);
- def_global_func(state, "requestAnimationFrame", win_requestanimationframe, 1);
- def_global_func(state, "cancelAnimationFrame", timer_clear, 1);
- js_newobject(state);
- def_func(state, "now", win_performance_now, 0);
- js_newobject(state);
- js_setproperty(state, -2, "timing");
- js_defglobal(state, "performance", JS_DONTENUM);
- js_pushnumber(state, host.width ? (int)host.width : 1024);
- js_defglobal(state, "innerWidth", JS_DONTENUM);
- js_pushnumber(state, host.height ? (int)host.height : 768);
- js_defglobal(state, "innerHeight", JS_DONTENUM);
- js_pushnumber(state, 0);
- js_defglobal(state, "pageXOffset", JS_DONTENUM);
- js_pushnumber(state, 0);
- js_defglobal(state, "pageYOffset", JS_DONTENUM);
- js_pushnumber(state, 0);
- js_defglobal(state, "scrollX", JS_DONTENUM);
- js_pushnumber(state, 0);
- js_defglobal(state, "scrollY", JS_DONTENUM);
- js_pushnumber(state, 1);
- js_defglobal(state, "devicePixelRatio", JS_DONTENUM);
- js_pushglobal(state);
- js_defglobal(state, "window", JS_DONTENUM);
- js_pushglobal(state);
- js_defglobal(state, "globalThis", JS_DONTENUM);
- js_getglobal(state, "document");
- js_getglobal(state, "location");
- js_setproperty(state, -2, "location");
- js_pop(state, 1);
- js_getglobal(state, "document");
- js_pushglobal(state);
- js_setproperty(state, -2, "defaultView");
- js_pop(state, 1);
- }
- int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
- {
- if (!doc || !h)
- return -1;
- D = doc;
- host = *h;
- current_url = h->page_url;
- panic_armed = 0;
- memset(timers, 0, sizeof(timers));
- memset(listeners, 0, sizeof(listeners));
- memset(events, 0, sizeof(events));
- memset(event_templates, 0, sizeof(event_templates));
- /* Classic scripts are sloppy by default in browsers; strict mode is
- * opt-in via a "use strict" directive. */
- J = js_newstate(0, 0, 0);
- if (!J)
- return -1;
- js_setreport(J, report_cb);
- js_atpanic(J, panic_cb);
- install_globals(J);
- return 0;
- }
- void js_browser_stop(void)
- {
- if (J)
- js_freestate(J);
- J = 0;
- D = 0;
- current_url = 0;
- panic_armed = 0;
- memset(timers, 0, sizeof(timers));
- }
- int js_browser_active(void)
- {
- return J != 0;
- }
- static void report_top(js_State *state)
- {
- const char *msg;
- js_getproperty(state, -1, "stack");
- if (js_isstring(state, -1)) {
- msg = js_tostring(state, -1);
- if (host.report)
- host.report(msg);
- js_pop(state, 1);
- } else {
- js_pop(state, 1);
- msg = js_trystring(state, -1, "JavaScript error");
- if (host.report)
- host.report(msg);
- }
- js_pop(state, 1);
- }
- void js_browser_tick(void)
- {
- unsigned int now;
- int i, ran = 0;
- if (!J)
- return;
- now = u_ticks_ms();
- for (i = 0; i < JS_TIMERS; i++) {
- if (!timers[i].used ||
- (int)(now - timers[i].deadline) < 0)
- continue;
- js_setlimit(J, JS_RUN_LIMIT, 0);
- js_getregistry(J, timers[i].ref);
- if (!js_iscallable(J, -1)) {
- js_pop(J, 1);
- js_unref(J, timers[i].ref);
- timers[i].used = 0;
- continue;
- }
- js_pushundefined(J);
- if (js_pcall(J, 0))
- report_top(J);
- else
- js_pop(J, 1);
- if (timers[i].used) {
- if (timers[i].repeat)
- timers[i].deadline = now + timers[i].interval;
- else {
- js_unref(J, timers[i].ref);
- timers[i].used = 0;
- }
- }
- if (++ran >= 4)
- break;
- }
- }
- int js_browser_run(const char *source, const char *url)
- {
- int r;
- if (!J || !D)
- return -1;
- js_setlimit(J, JS_RUN_LIMIT, 0);
- panic_armed = 1;
- if (setjmp(panic_env)) {
- panic_armed = 0;
- if (host.report)
- host.report("uncaught JavaScript exception");
- /* The try/exception stack is unusable after a panic. Leak the
- * state; the page heap reset on navigation reclaims it. */
- J = 0;
- D = 0;
- return -1;
- }
- if (js_ploadstring(J, url ? url : "[inline]", source)) {
- report_top(J);
- panic_armed = 0;
- return -1;
- }
- js_pushundefined(J);
- r = js_pcall(J, 0);
- if (r) {
- report_top(J);
- panic_armed = 0;
- return -1;
- }
- js_pop(J, 1);
- panic_armed = 0;
- return 0;
- }
|