jsrun.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  1. #include "jsi.h"
  2. #include "utf.h"
  3. #include <assert.h>
  4. static void jsR_run(js_State *J, js_Function *F);
  5. /* Push values on stack */
  6. #define STACK (J->stack)
  7. #define TOP (J->top)
  8. #define BOT (J->bot)
  9. static void js_trystackoverflow(js_State *J)
  10. {
  11. STACK[TOP].t.type = JS_TLITSTR;
  12. STACK[TOP].u.litstr = "exception stack overflow";
  13. ++TOP;
  14. js_throw(J);
  15. }
  16. static void js_stackoverflow(js_State *J)
  17. {
  18. STACK[TOP].t.type = JS_TLITSTR;
  19. STACK[TOP].u.litstr = "stack overflow";
  20. ++TOP;
  21. js_throw(J);
  22. }
  23. static void js_outofmemory(js_State *J)
  24. {
  25. STACK[TOP].t.type = JS_TLITSTR;
  26. STACK[TOP].u.litstr = "out of memory";
  27. ++TOP;
  28. js_throw(J);
  29. }
  30. static void js_runlimit(js_State *J)
  31. {
  32. STACK[TOP].t.type = JS_TLITSTR;
  33. STACK[TOP].u.litstr = "script ran too long";
  34. ++TOP;
  35. js_throw(J);
  36. }
  37. void js_setlimit(js_State *J, int runlimit, int memlimit)
  38. {
  39. J->runlimit = runlimit;
  40. J->memlimit = memlimit;
  41. }
  42. void *js_malloc(js_State *J, int size)
  43. {
  44. void *ptr;
  45. if (J->memlimit > 0) {
  46. if (size >= J->memlimit)
  47. js_outofmemory(J);
  48. J->memlimit -= size;
  49. }
  50. ptr = J->alloc(J->actx, NULL, size);
  51. if (!ptr)
  52. js_outofmemory(J);
  53. return ptr;
  54. }
  55. void *js_realloc(js_State *J, void *ptr, int size)
  56. {
  57. if (J->memlimit > 0) {
  58. // TODO: track released memory
  59. if (size >= J->memlimit)
  60. js_outofmemory(J);
  61. J->memlimit -= size;
  62. }
  63. ptr = J->alloc(J->actx, ptr, size);
  64. if (!ptr)
  65. js_outofmemory(J);
  66. return ptr;
  67. }
  68. char *js_strdup(js_State *J, const char *s)
  69. {
  70. int n = strlen(s) + 1;
  71. char *p = js_malloc(J, n);
  72. memcpy(p, s, n);
  73. return p;
  74. }
  75. void js_free(js_State *J, void *ptr)
  76. {
  77. // TODO: track released memory (J->memlimit)
  78. J->alloc(J->actx, ptr, 0);
  79. }
  80. js_String *jsV_newmemstring(js_State *J, const char *s, int n)
  81. {
  82. js_String *v = js_malloc(J, soffsetof(js_String, p) + n + 1);
  83. memcpy(v->p, s, n);
  84. v->p[n] = 0;
  85. v->gcmark = 0;
  86. v->gcnext = J->gcstr;
  87. J->gcstr = v;
  88. ++J->gccounter;
  89. return v;
  90. }
  91. #define CHECKSTACK(n) if (TOP + n >= JS_STACKSIZE) js_stackoverflow(J)
  92. void js_pushvalue(js_State *J, js_Value v)
  93. {
  94. CHECKSTACK(1);
  95. STACK[TOP] = v;
  96. ++TOP;
  97. }
  98. void js_pushundefined(js_State *J)
  99. {
  100. CHECKSTACK(1);
  101. STACK[TOP].t.type = JS_TUNDEFINED;
  102. ++TOP;
  103. }
  104. void js_pushnull(js_State *J)
  105. {
  106. CHECKSTACK(1);
  107. STACK[TOP].t.type = JS_TNULL;
  108. ++TOP;
  109. }
  110. void js_pushboolean(js_State *J, int v)
  111. {
  112. CHECKSTACK(1);
  113. STACK[TOP].t.type = JS_TBOOLEAN;
  114. STACK[TOP].u.boolean = !!v;
  115. ++TOP;
  116. }
  117. void js_pushnumber(js_State *J, double v)
  118. {
  119. CHECKSTACK(1);
  120. STACK[TOP].t.type = JS_TNUMBER;
  121. STACK[TOP].u.number = v;
  122. ++TOP;
  123. }
  124. void js_pushstring(js_State *J, const char *v)
  125. {
  126. size_t n = strlen(v);
  127. if (n > JS_STRLIMIT)
  128. js_rangeerror(J, "invalid string length");
  129. CHECKSTACK(1);
  130. if (n <= soffsetof(js_Value, t.type)) {
  131. char *s = STACK[TOP].u.shrstr;
  132. while (n--) *s++ = *v++;
  133. *s = 0;
  134. STACK[TOP].t.type = JS_TSHRSTR;
  135. } else {
  136. STACK[TOP].t.type = JS_TMEMSTR;
  137. STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
  138. }
  139. ++TOP;
  140. }
  141. void js_pushlstring(js_State *J, const char *v, int n)
  142. {
  143. if (n > JS_STRLIMIT)
  144. js_rangeerror(J, "invalid string length");
  145. CHECKSTACK(1);
  146. if (n <= soffsetof(js_Value, t.type)) {
  147. char *s = STACK[TOP].u.shrstr;
  148. while (n--) *s++ = *v++;
  149. *s = 0;
  150. STACK[TOP].t.type = JS_TSHRSTR;
  151. } else {
  152. STACK[TOP].t.type = JS_TMEMSTR;
  153. STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
  154. }
  155. ++TOP;
  156. }
  157. void js_pushliteral(js_State *J, const char *v)
  158. {
  159. CHECKSTACK(1);
  160. STACK[TOP].t.type = JS_TLITSTR;
  161. STACK[TOP].u.litstr = v;
  162. ++TOP;
  163. }
  164. void js_pushobject(js_State *J, js_Object *v)
  165. {
  166. CHECKSTACK(1);
  167. STACK[TOP].t.type = JS_TOBJECT;
  168. STACK[TOP].u.object = v;
  169. ++TOP;
  170. }
  171. void js_pushglobal(js_State *J)
  172. {
  173. js_pushobject(J, J->G);
  174. }
  175. void js_currentfunction(js_State *J)
  176. {
  177. CHECKSTACK(1);
  178. if (BOT > 0)
  179. STACK[TOP] = STACK[BOT-1];
  180. else
  181. STACK[TOP].t.type = JS_TUNDEFINED;
  182. ++TOP;
  183. }
  184. void *js_currentfunctiondata(js_State *J)
  185. {
  186. if (BOT > 0)
  187. return STACK[BOT-1].u.object->u.c.data;
  188. return NULL;
  189. }
  190. /* Read values from stack */
  191. static js_Value *stackidx(js_State *J, int idx)
  192. {
  193. static js_Value undefined = { { {0}, JS_TUNDEFINED } };
  194. idx = idx < 0 ? TOP + idx : BOT + idx;
  195. if (idx < 0 || idx >= TOP)
  196. return &undefined;
  197. return STACK + idx;
  198. }
  199. js_Value *js_tovalue(js_State *J, int idx)
  200. {
  201. return stackidx(J, idx);
  202. }
  203. int js_isdefined(js_State *J, int idx) { return stackidx(J, idx)->t.type != JS_TUNDEFINED; }
  204. int js_isundefined(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TUNDEFINED; }
  205. int js_isnull(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TNULL; }
  206. int js_isboolean(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TBOOLEAN; }
  207. int js_isnumber(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TNUMBER; }
  208. int js_isstring(js_State *J, int idx) { enum js_Type t = stackidx(J, idx)->t.type; return t == JS_TSHRSTR || t == JS_TLITSTR || t == JS_TMEMSTR; }
  209. int js_isprimitive(js_State *J, int idx) { return stackidx(J, idx)->t.type != JS_TOBJECT; }
  210. int js_isobject(js_State *J, int idx) { return stackidx(J, idx)->t.type == JS_TOBJECT; }
  211. int js_iscoercible(js_State *J, int idx) { js_Value *v = stackidx(J, idx); return v->t.type != JS_TUNDEFINED && v->t.type != JS_TNULL; }
  212. int js_iscallable(js_State *J, int idx)
  213. {
  214. js_Value *v = stackidx(J, idx);
  215. if (v->t.type == JS_TOBJECT)
  216. return v->u.object->type == JS_CFUNCTION ||
  217. v->u.object->type == JS_CSCRIPT ||
  218. v->u.object->type == JS_CCFUNCTION;
  219. return 0;
  220. }
  221. int js_isarray(js_State *J, int idx)
  222. {
  223. js_Value *v = stackidx(J, idx);
  224. return v->t.type == JS_TOBJECT && v->u.object->type == JS_CARRAY;
  225. }
  226. int js_isregexp(js_State *J, int idx)
  227. {
  228. js_Value *v = stackidx(J, idx);
  229. return v->t.type == JS_TOBJECT && v->u.object->type == JS_CREGEXP;
  230. }
  231. int js_isuserdata(js_State *J, int idx, const char *tag)
  232. {
  233. js_Value *v = stackidx(J, idx);
  234. if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
  235. return !strcmp(tag, v->u.object->u.user.tag);
  236. return 0;
  237. }
  238. int js_iserror(js_State *J, int idx)
  239. {
  240. js_Value *v = stackidx(J, idx);
  241. return v->t.type == JS_TOBJECT && v->u.object->type == JS_CERROR;
  242. }
  243. const char *js_typeof(js_State *J, int idx)
  244. {
  245. js_Value *v = stackidx(J, idx);
  246. switch (v->t.type) {
  247. default:
  248. case JS_TSHRSTR: return "string";
  249. case JS_TUNDEFINED: return "undefined";
  250. case JS_TNULL: return "object";
  251. case JS_TBOOLEAN: return "boolean";
  252. case JS_TNUMBER: return "number";
  253. case JS_TLITSTR: return "string";
  254. case JS_TMEMSTR: return "string";
  255. case JS_TOBJECT:
  256. if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
  257. return "function";
  258. return "object";
  259. }
  260. }
  261. int js_type(js_State *J, int idx)
  262. {
  263. js_Value *v = stackidx(J, idx);
  264. switch (v->t.type) {
  265. default:
  266. case JS_TSHRSTR: return JS_ISSTRING;
  267. case JS_TUNDEFINED: return JS_ISUNDEFINED;
  268. case JS_TNULL: return JS_ISNULL;
  269. case JS_TBOOLEAN: return JS_ISBOOLEAN;
  270. case JS_TNUMBER: return JS_ISNUMBER;
  271. case JS_TLITSTR: return JS_ISSTRING;
  272. case JS_TMEMSTR: return JS_ISSTRING;
  273. case JS_TOBJECT:
  274. if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
  275. return JS_ISFUNCTION;
  276. return JS_ISOBJECT;
  277. }
  278. }
  279. int js_toboolean(js_State *J, int idx)
  280. {
  281. return jsV_toboolean(J, stackidx(J, idx));
  282. }
  283. double js_tonumber(js_State *J, int idx)
  284. {
  285. return jsV_tonumber(J, stackidx(J, idx));
  286. }
  287. int js_tointeger(js_State *J, int idx)
  288. {
  289. return jsV_numbertointeger(jsV_tonumber(J, stackidx(J, idx)));
  290. }
  291. int js_toint32(js_State *J, int idx)
  292. {
  293. return jsV_numbertoint32(jsV_tonumber(J, stackidx(J, idx)));
  294. }
  295. unsigned int js_touint32(js_State *J, int idx)
  296. {
  297. return jsV_numbertouint32(jsV_tonumber(J, stackidx(J, idx)));
  298. }
  299. short js_toint16(js_State *J, int idx)
  300. {
  301. return jsV_numbertoint16(jsV_tonumber(J, stackidx(J, idx)));
  302. }
  303. unsigned short js_touint16(js_State *J, int idx)
  304. {
  305. return jsV_numbertouint16(jsV_tonumber(J, stackidx(J, idx)));
  306. }
  307. const char *js_tostring(js_State *J, int idx)
  308. {
  309. return jsV_tostring(J, stackidx(J, idx));
  310. }
  311. js_Object *js_toobject(js_State *J, int idx)
  312. {
  313. return jsV_toobject(J, stackidx(J, idx));
  314. }
  315. void js_toprimitive(js_State *J, int idx, int hint)
  316. {
  317. jsV_toprimitive(J, stackidx(J, idx), hint);
  318. }
  319. js_Regexp *js_toregexp(js_State *J, int idx)
  320. {
  321. js_Value *v = stackidx(J, idx);
  322. if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CREGEXP)
  323. return &v->u.object->u.r;
  324. js_typeerror(J, "not a regexp");
  325. }
  326. void *js_touserdata(js_State *J, int idx, const char *tag)
  327. {
  328. js_Value *v = stackidx(J, idx);
  329. if (v->t.type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
  330. if (!strcmp(tag, v->u.object->u.user.tag))
  331. return v->u.object->u.user.data;
  332. js_typeerror(J, "not a %s", tag);
  333. }
  334. static js_Object *jsR_tofunction(js_State *J, int idx)
  335. {
  336. js_Value *v = stackidx(J, idx);
  337. if (v->t.type == JS_TUNDEFINED || v->t.type == JS_TNULL)
  338. return NULL;
  339. if (v->t.type == JS_TOBJECT)
  340. if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
  341. return v->u.object;
  342. js_typeerror(J, "not a function");
  343. }
  344. /* Stack manipulation */
  345. int js_gettop(js_State *J)
  346. {
  347. return TOP - BOT;
  348. }
  349. void js_pop(js_State *J, int n)
  350. {
  351. TOP -= n;
  352. if (TOP < BOT) {
  353. TOP = BOT;
  354. js_error(J, "stack underflow!");
  355. }
  356. }
  357. void js_remove(js_State *J, int idx)
  358. {
  359. idx = idx < 0 ? TOP + idx : BOT + idx;
  360. if (idx < BOT || idx >= TOP)
  361. js_error(J, "stack error!");
  362. for (;idx < TOP - 1; ++idx)
  363. STACK[idx] = STACK[idx+1];
  364. --TOP;
  365. }
  366. void js_insert(js_State *J, int idx)
  367. {
  368. js_error(J, "not implemented yet");
  369. }
  370. void js_replace(js_State* J, int idx)
  371. {
  372. idx = idx < 0 ? TOP + idx : BOT + idx;
  373. if (idx < BOT || idx >= TOP)
  374. js_error(J, "stack error!");
  375. STACK[idx] = STACK[--TOP];
  376. }
  377. void js_copy(js_State *J, int idx)
  378. {
  379. CHECKSTACK(1);
  380. STACK[TOP] = *stackidx(J, idx);
  381. ++TOP;
  382. }
  383. void js_dup(js_State *J)
  384. {
  385. CHECKSTACK(1);
  386. STACK[TOP] = STACK[TOP-1];
  387. ++TOP;
  388. }
  389. void js_dup2(js_State *J)
  390. {
  391. CHECKSTACK(2);
  392. STACK[TOP] = STACK[TOP-2];
  393. STACK[TOP+1] = STACK[TOP-1];
  394. TOP += 2;
  395. }
  396. void js_rot2(js_State *J)
  397. {
  398. /* A B -> B A */
  399. js_Value tmp = STACK[TOP-1]; /* A B (B) */
  400. STACK[TOP-1] = STACK[TOP-2]; /* A A */
  401. STACK[TOP-2] = tmp; /* B A */
  402. }
  403. void js_rot3(js_State *J)
  404. {
  405. /* A B C -> C A B */
  406. js_Value tmp = STACK[TOP-1]; /* A B C (C) */
  407. STACK[TOP-1] = STACK[TOP-2]; /* A B B */
  408. STACK[TOP-2] = STACK[TOP-3]; /* A A B */
  409. STACK[TOP-3] = tmp; /* C A B */
  410. }
  411. void js_rot4(js_State *J)
  412. {
  413. /* A B C D -> D A B C */
  414. js_Value tmp = STACK[TOP-1]; /* A B C D (D) */
  415. STACK[TOP-1] = STACK[TOP-2]; /* A B C C */
  416. STACK[TOP-2] = STACK[TOP-3]; /* A B B C */
  417. STACK[TOP-3] = STACK[TOP-4]; /* A A B C */
  418. STACK[TOP-4] = tmp; /* D A B C */
  419. }
  420. void js_rot2pop1(js_State *J)
  421. {
  422. /* A B -> B */
  423. STACK[TOP-2] = STACK[TOP-1];
  424. --TOP;
  425. }
  426. void js_rot3pop2(js_State *J)
  427. {
  428. /* A B C -> C */
  429. STACK[TOP-3] = STACK[TOP-1];
  430. TOP -= 2;
  431. }
  432. void js_rot(js_State *J, int n)
  433. {
  434. int i;
  435. js_Value tmp = STACK[TOP-1];
  436. for (i = 1; i < n; ++i)
  437. STACK[TOP-i] = STACK[TOP-i-1];
  438. STACK[TOP-i] = tmp;
  439. }
  440. /* Property access that takes care of attributes and getters/setters */
  441. int js_isarrayindex(js_State *J, const char *p, int *idx)
  442. {
  443. int n = 0;
  444. /* check for empty string */
  445. if (p[0] == 0)
  446. return 0;
  447. /* check for '0' and integers with leading zero */
  448. if (p[0] == '0')
  449. return (p[1] == 0) ? *idx = 0, 1 : 0;
  450. while (*p) {
  451. int c = *p++;
  452. if (c >= '0' && c <= '9') {
  453. if (n >= INT_MAX / 10)
  454. return 0;
  455. n = n * 10 + (c - '0');
  456. } else {
  457. return 0;
  458. }
  459. }
  460. return *idx = n, 1;
  461. }
  462. static void js_pushrune(js_State *J, Rune rune)
  463. {
  464. char buf[UTFmax + 1];
  465. if (rune >= 0) {
  466. buf[runetochar(buf, &rune)] = 0;
  467. js_pushstring(J, buf);
  468. } else {
  469. js_pushundefined(J);
  470. }
  471. }
  472. void jsR_unflattenarray(js_State *J, js_Object *obj) {
  473. if (obj->type == JS_CARRAY && obj->u.a.simple) {
  474. js_Property *ref;
  475. volatile int i;
  476. int k;
  477. char name[32];
  478. if (js_try(J)) {
  479. /* delete any array entries that were migrated to the property tree */
  480. for (k = 0; k < i; ++k)
  481. jsV_delproperty(J, obj, js_itoa(name, i));
  482. js_throw(J);
  483. }
  484. for (i = 0; i < obj->u.a.flat_length; ++i) {
  485. js_itoa(name, i);
  486. ref = jsV_setproperty(J, obj, name);
  487. ref->value = obj->u.a.array[i];
  488. }
  489. js_free(J, obj->u.a.array);
  490. obj->u.a.simple = 0;
  491. obj->u.a.flat_length = 0;
  492. obj->u.a.flat_capacity = 0;
  493. obj->u.a.array = NULL;
  494. js_endtry(J);
  495. }
  496. }
  497. static int jsR_hasproperty(js_State *J, js_Object *obj, const char *name)
  498. {
  499. js_Property *ref;
  500. int k;
  501. if (obj->type == JS_CARRAY) {
  502. if (!strcmp(name, "length")) {
  503. js_pushnumber(J, obj->u.a.length);
  504. return 1;
  505. }
  506. if (obj->u.a.simple) {
  507. if (js_isarrayindex(J, name, &k)) {
  508. if (k >= 0 && k < obj->u.a.flat_length) {
  509. js_pushvalue(J, obj->u.a.array[k]);
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. }
  515. }
  516. else if (obj->type == JS_CSTRING) {
  517. if (!strcmp(name, "length")) {
  518. js_pushnumber(J, obj->u.s.length);
  519. return 1;
  520. }
  521. if (js_isarrayindex(J, name, &k)) {
  522. if (k >= 0 && k < obj->u.s.length) {
  523. js_pushrune(J, js_runeat(J, obj->u.s.string, k));
  524. return 1;
  525. }
  526. }
  527. }
  528. else if (obj->type == JS_CREGEXP) {
  529. if (!strcmp(name, "source")) {
  530. js_pushstring(J, obj->u.r.source);
  531. return 1;
  532. }
  533. if (!strcmp(name, "global")) {
  534. js_pushboolean(J, obj->u.r.flags & JS_REGEXP_G);
  535. return 1;
  536. }
  537. if (!strcmp(name, "ignoreCase")) {
  538. js_pushboolean(J, obj->u.r.flags & JS_REGEXP_I);
  539. return 1;
  540. }
  541. if (!strcmp(name, "multiline")) {
  542. js_pushboolean(J, obj->u.r.flags & JS_REGEXP_M);
  543. return 1;
  544. }
  545. if (!strcmp(name, "lastIndex")) {
  546. js_pushnumber(J, obj->u.r.last);
  547. return 1;
  548. }
  549. }
  550. else if (obj->type == JS_CUSERDATA) {
  551. if (obj->u.user.has && obj->u.user.has(J, obj->u.user.data, name))
  552. return 1;
  553. }
  554. ref = jsV_getproperty(J, obj, name);
  555. if (ref) {
  556. if (ref->getter) {
  557. js_pushobject(J, ref->getter);
  558. js_pushobject(J, obj);
  559. js_call(J, 0);
  560. } else {
  561. js_pushvalue(J, ref->value);
  562. }
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. static void jsR_getproperty(js_State *J, js_Object *obj, const char *name)
  568. {
  569. if (!jsR_hasproperty(J, obj, name))
  570. js_pushundefined(J);
  571. }
  572. static int jsR_hasindex(js_State *J, js_Object *obj, int k)
  573. {
  574. char buf[32];
  575. if (obj->type == JS_CARRAY && obj->u.a.simple) {
  576. if (k >= 0 && k < obj->u.a.flat_length) {
  577. js_pushvalue(J, obj->u.a.array[k]);
  578. return 1;
  579. }
  580. return 0;
  581. }
  582. return jsR_hasproperty(J, obj, js_itoa(buf, k));
  583. }
  584. static void jsR_getindex(js_State *J, js_Object *obj, int k)
  585. {
  586. if (!jsR_hasindex(J, obj, k))
  587. js_pushundefined(J);
  588. }
  589. static void jsR_setarrayindex(js_State *J, js_Object *obj, int k, js_Value *value)
  590. {
  591. int newlen = k + 1;
  592. assert(obj->type == JS_CARRAY);
  593. assert(obj->u.a.simple);
  594. assert(k >= 0);
  595. if (newlen > JS_ARRAYLIMIT)
  596. js_rangeerror(J, "array too large");
  597. if (newlen > obj->u.a.flat_length) {
  598. assert(newlen == obj->u.a.flat_length + 1);
  599. if (newlen > obj->u.a.flat_capacity) {
  600. int newcap = obj->u.a.flat_capacity;
  601. if (newcap == 0)
  602. newcap = 8;
  603. while (newcap < newlen)
  604. newcap <<= 1;
  605. obj->u.a.array = js_realloc(J, obj->u.a.array, newcap * sizeof(js_Value));
  606. obj->u.a.flat_capacity = newcap;
  607. }
  608. obj->u.a.flat_length = newlen;
  609. }
  610. if (newlen > obj->u.a.length)
  611. obj->u.a.length = newlen;
  612. obj->u.a.array[k] = *value;
  613. }
  614. static void jsR_setproperty(js_State *J, js_Object *obj, const char *name, int transient)
  615. {
  616. js_Value *value = stackidx(J, -1);
  617. js_Property *ref;
  618. int k;
  619. int own;
  620. if (obj->type == JS_CARRAY) {
  621. if (!strcmp(name, "length")) {
  622. double rawlen = jsV_tonumber(J, value);
  623. int newlen = jsV_numbertointeger(rawlen);
  624. if (newlen != rawlen || newlen < 0)
  625. js_rangeerror(J, "invalid array length");
  626. if (newlen > JS_ARRAYLIMIT)
  627. js_rangeerror(J, "array too large");
  628. if (obj->u.a.simple) {
  629. obj->u.a.length = newlen;
  630. if (newlen <= obj->u.a.flat_length)
  631. obj->u.a.flat_length = newlen;
  632. } else {
  633. jsV_resizearray(J, obj, newlen);
  634. }
  635. return;
  636. }
  637. if (js_isarrayindex(J, name, &k)) {
  638. if (obj->u.a.simple) {
  639. if (k >= 0 && k <= obj->u.a.flat_length) {
  640. jsR_setarrayindex(J, obj, k, value);
  641. } else {
  642. jsR_unflattenarray(J, obj);
  643. if (obj->u.a.length < k + 1)
  644. obj->u.a.length = k + 1;
  645. }
  646. } else {
  647. if (obj->u.a.length < k + 1)
  648. obj->u.a.length = k + 1;
  649. }
  650. }
  651. }
  652. else if (obj->type == JS_CSTRING) {
  653. if (!strcmp(name, "length"))
  654. goto readonly;
  655. if (js_isarrayindex(J, name, &k))
  656. if (k >= 0 && k < obj->u.s.length)
  657. goto readonly;
  658. }
  659. else if (obj->type == JS_CREGEXP) {
  660. if (!strcmp(name, "source")) goto readonly;
  661. if (!strcmp(name, "global")) goto readonly;
  662. if (!strcmp(name, "ignoreCase")) goto readonly;
  663. if (!strcmp(name, "multiline")) goto readonly;
  664. if (!strcmp(name, "lastIndex")) {
  665. obj->u.r.last = jsV_tointeger(J, value);
  666. return;
  667. }
  668. }
  669. else if (obj->type == JS_CUSERDATA) {
  670. if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
  671. return;
  672. }
  673. /* First try to find a setter in prototype chain */
  674. ref = jsV_getpropertyx(J, obj, name, &own);
  675. if (ref) {
  676. if (ref->setter) {
  677. js_pushobject(J, ref->setter);
  678. js_pushobject(J, obj);
  679. js_pushvalue(J, *value);
  680. js_call(J, 1);
  681. js_pop(J, 1);
  682. return;
  683. } else {
  684. if (J->strict)
  685. if (ref->getter)
  686. js_typeerror(J, "setting property '%s' that only has a getter", name);
  687. if (ref->atts & JS_READONLY)
  688. goto readonly;
  689. }
  690. }
  691. /* Property not found on this object, so create one */
  692. if (!ref || !own) {
  693. if (transient) {
  694. if (J->strict)
  695. js_typeerror(J, "cannot create property '%s' on transient object", name);
  696. return;
  697. }
  698. ref = jsV_setproperty(J, obj, name);
  699. }
  700. if (ref) {
  701. if (!(ref->atts & JS_READONLY))
  702. ref->value = *value;
  703. else
  704. goto readonly;
  705. }
  706. return;
  707. readonly:
  708. if (J->strict)
  709. js_typeerror(J, "'%s' is read-only", name);
  710. }
  711. static void jsR_setindex(js_State *J, js_Object *obj, int k, int transient)
  712. {
  713. char buf[32];
  714. if (obj->type == JS_CARRAY && obj->u.a.simple && k >= 0 && k <= obj->u.a.flat_length) {
  715. jsR_setarrayindex(J, obj, k, stackidx(J, -1));
  716. } else {
  717. jsR_setproperty(J, obj, js_itoa(buf, k), transient);
  718. }
  719. }
  720. static void jsR_defproperty(js_State *J, js_Object *obj, const char *name,
  721. int atts, js_Value *value, js_Object *getter, js_Object *setter,
  722. int throw)
  723. {
  724. js_Property *ref;
  725. int k;
  726. if (obj->type == JS_CARRAY) {
  727. if (!strcmp(name, "length"))
  728. goto readonly;
  729. if (obj->u.a.simple)
  730. jsR_unflattenarray(J, obj);
  731. }
  732. else if (obj->type == JS_CSTRING) {
  733. if (!strcmp(name, "length"))
  734. goto readonly;
  735. if (js_isarrayindex(J, name, &k))
  736. if (k >= 0 && k < obj->u.s.length)
  737. goto readonly;
  738. }
  739. else if (obj->type == JS_CREGEXP) {
  740. if (!strcmp(name, "source")) goto readonly;
  741. if (!strcmp(name, "global")) goto readonly;
  742. if (!strcmp(name, "ignoreCase")) goto readonly;
  743. if (!strcmp(name, "multiline")) goto readonly;
  744. if (!strcmp(name, "lastIndex")) goto readonly;
  745. }
  746. else if (obj->type == JS_CUSERDATA) {
  747. if (obj->u.user.put && obj->u.user.put(J, obj->u.user.data, name))
  748. return;
  749. }
  750. ref = jsV_setproperty(J, obj, name);
  751. if (ref) {
  752. if (value) {
  753. if (!(ref->atts & JS_READONLY))
  754. ref->value = *value;
  755. else if (J->strict)
  756. js_typeerror(J, "'%s' is read-only", name);
  757. }
  758. if (getter) {
  759. if (!(ref->atts & JS_DONTCONF))
  760. ref->getter = getter;
  761. else if (J->strict)
  762. js_typeerror(J, "'%s' is non-configurable", name);
  763. }
  764. if (setter) {
  765. if (!(ref->atts & JS_DONTCONF))
  766. ref->setter = setter;
  767. else if (J->strict)
  768. js_typeerror(J, "'%s' is non-configurable", name);
  769. }
  770. ref->atts |= atts;
  771. }
  772. return;
  773. readonly:
  774. if (J->strict || throw)
  775. js_typeerror(J, "'%s' is read-only or non-configurable", name);
  776. }
  777. static int jsR_delproperty(js_State *J, js_Object *obj, const char *name)
  778. {
  779. js_Property *ref;
  780. int k;
  781. if (obj->type == JS_CARRAY) {
  782. if (!strcmp(name, "length"))
  783. goto dontconf;
  784. if (obj->u.a.simple)
  785. jsR_unflattenarray(J, obj);
  786. }
  787. else if (obj->type == JS_CSTRING) {
  788. if (!strcmp(name, "length"))
  789. goto dontconf;
  790. if (js_isarrayindex(J, name, &k))
  791. if (k >= 0 && k < obj->u.s.length)
  792. goto dontconf;
  793. }
  794. else if (obj->type == JS_CREGEXP) {
  795. if (!strcmp(name, "source")) goto dontconf;
  796. if (!strcmp(name, "global")) goto dontconf;
  797. if (!strcmp(name, "ignoreCase")) goto dontconf;
  798. if (!strcmp(name, "multiline")) goto dontconf;
  799. if (!strcmp(name, "lastIndex")) goto dontconf;
  800. }
  801. else if (obj->type == JS_CUSERDATA) {
  802. if (obj->u.user.delete && obj->u.user.delete(J, obj->u.user.data, name))
  803. return 1;
  804. }
  805. ref = jsV_getownproperty(J, obj, name);
  806. if (ref) {
  807. if (ref->atts & JS_DONTCONF)
  808. goto dontconf;
  809. jsV_delproperty(J, obj, name);
  810. }
  811. return 1;
  812. dontconf:
  813. if (J->strict)
  814. js_typeerror(J, "'%s' is non-configurable", name);
  815. return 0;
  816. }
  817. static void jsR_delindex(js_State *J, js_Object *obj, int k)
  818. {
  819. char buf[32];
  820. /* Allow deleting last element of a simple array without unflattening */
  821. if (obj->type == JS_CARRAY && obj->u.a.simple && k == obj->u.a.flat_length - 1)
  822. obj->u.a.flat_length = k;
  823. else
  824. jsR_delproperty(J, obj, js_itoa(buf, k));
  825. }
  826. /* Registry, global and object property accessors */
  827. const char *js_ref(js_State *J)
  828. {
  829. js_Value *v = stackidx(J, -1);
  830. const char *s;
  831. char buf[32];
  832. switch (v->t.type) {
  833. case JS_TUNDEFINED: s = "_Undefined"; break;
  834. case JS_TNULL: s = "_Null"; break;
  835. case JS_TBOOLEAN:
  836. s = v->u.boolean ? "_True" : "_False";
  837. break;
  838. case JS_TOBJECT:
  839. sprintf(buf, "%p", (void*)v->u.object);
  840. s = js_intern(J, buf);
  841. break;
  842. default:
  843. sprintf(buf, "%d", J->nextref++);
  844. s = js_intern(J, buf);
  845. break;
  846. }
  847. js_setregistry(J, s);
  848. return s;
  849. }
  850. void js_unref(js_State *J, const char *ref)
  851. {
  852. js_delregistry(J, ref);
  853. }
  854. void js_getregistry(js_State *J, const char *name)
  855. {
  856. jsR_getproperty(J, J->R, name);
  857. }
  858. void js_setregistry(js_State *J, const char *name)
  859. {
  860. jsR_setproperty(J, J->R, name, 0);
  861. js_pop(J, 1);
  862. }
  863. void js_delregistry(js_State *J, const char *name)
  864. {
  865. jsR_delproperty(J, J->R, name);
  866. }
  867. void js_getglobal(js_State *J, const char *name)
  868. {
  869. jsR_getproperty(J, J->G, name);
  870. }
  871. void js_setglobal(js_State *J, const char *name)
  872. {
  873. jsR_setproperty(J, J->G, name, 0);
  874. js_pop(J, 1);
  875. }
  876. void js_defglobal(js_State *J, const char *name, int atts)
  877. {
  878. jsR_defproperty(J, J->G, name, atts, stackidx(J, -1), NULL, NULL, 0);
  879. js_pop(J, 1);
  880. }
  881. void js_delglobal(js_State *J, const char *name)
  882. {
  883. jsR_delproperty(J, J->G, name);
  884. }
  885. void js_getproperty(js_State *J, int idx, const char *name)
  886. {
  887. jsR_getproperty(J, js_toobject(J, idx), name);
  888. }
  889. void js_setproperty(js_State *J, int idx, const char *name)
  890. {
  891. jsR_setproperty(J, js_toobject(J, idx), name, !js_isobject(J, idx));
  892. js_pop(J, 1);
  893. }
  894. void js_defproperty(js_State *J, int idx, const char *name, int atts)
  895. {
  896. jsR_defproperty(J, js_toobject(J, idx), name, atts, stackidx(J, -1), NULL, NULL, 1);
  897. js_pop(J, 1);
  898. }
  899. void js_delproperty(js_State *J, int idx, const char *name)
  900. {
  901. jsR_delproperty(J, js_toobject(J, idx), name);
  902. }
  903. void js_defaccessor(js_State *J, int idx, const char *name, int atts)
  904. {
  905. jsR_defproperty(J, js_toobject(J, idx), name, atts, NULL, jsR_tofunction(J, -2), jsR_tofunction(J, -1), 1);
  906. js_pop(J, 2);
  907. }
  908. int js_hasproperty(js_State *J, int idx, const char *name)
  909. {
  910. return jsR_hasproperty(J, js_toobject(J, idx), name);
  911. }
  912. void js_getindex(js_State *J, int idx, int i)
  913. {
  914. jsR_getindex(J, js_toobject(J, idx), i);
  915. }
  916. int js_hasindex(js_State *J, int idx, int i)
  917. {
  918. return jsR_hasindex(J, js_toobject(J, idx), i);
  919. }
  920. void js_setindex(js_State *J, int idx, int i)
  921. {
  922. jsR_setindex(J, js_toobject(J, idx), i, !js_isobject(J, idx));
  923. js_pop(J, 1);
  924. }
  925. void js_delindex(js_State *J, int idx, int i)
  926. {
  927. jsR_delindex(J, js_toobject(J, idx), i);
  928. }
  929. /* Iterator */
  930. void js_pushiterator(js_State *J, int idx, int own)
  931. {
  932. js_pushobject(J, jsV_newiterator(J, js_toobject(J, idx), own));
  933. }
  934. const char *js_nextiterator(js_State *J, int idx)
  935. {
  936. return jsV_nextiterator(J, js_toobject(J, idx));
  937. }
  938. /* Environment records */
  939. js_Environment *jsR_newenvironment(js_State *J, js_Object *vars, js_Environment *outer)
  940. {
  941. js_Environment *E = js_malloc(J, sizeof *E);
  942. E->gcmark = 0;
  943. E->gcnext = J->gcenv;
  944. J->gcenv = E;
  945. ++J->gccounter;
  946. E->outer = outer;
  947. E->variables = vars;
  948. return E;
  949. }
  950. static void js_initvar(js_State *J, const char *name, int idx)
  951. {
  952. jsR_defproperty(J, J->E->variables, name, JS_DONTENUM | JS_DONTCONF, stackidx(J, idx), NULL, NULL, 0);
  953. }
  954. static int js_hasvar(js_State *J, const char *name)
  955. {
  956. js_Environment *E = J->E;
  957. do {
  958. js_Property *ref = jsV_getproperty(J, E->variables, name);
  959. if (ref) {
  960. if (ref->getter) {
  961. js_pushobject(J, ref->getter);
  962. js_pushobject(J, E->variables);
  963. js_call(J, 0);
  964. } else {
  965. js_pushvalue(J, ref->value);
  966. }
  967. return 1;
  968. }
  969. E = E->outer;
  970. } while (E);
  971. return 0;
  972. }
  973. static void js_setvar(js_State *J, const char *name)
  974. {
  975. js_Environment *E = J->E;
  976. do {
  977. js_Property *ref = jsV_getproperty(J, E->variables, name);
  978. if (ref) {
  979. if (ref->setter) {
  980. js_pushobject(J, ref->setter);
  981. js_pushobject(J, E->variables);
  982. js_copy(J, -3);
  983. js_call(J, 1);
  984. js_pop(J, 1);
  985. return;
  986. }
  987. if (!(ref->atts & JS_READONLY))
  988. ref->value = *stackidx(J, -1);
  989. else if (J->strict)
  990. js_typeerror(J, "'%s' is read-only", name);
  991. return;
  992. }
  993. E = E->outer;
  994. } while (E);
  995. if (J->strict)
  996. js_referenceerror(J, "assignment to undeclared variable '%s'", name);
  997. jsR_setproperty(J, J->G, name, 0);
  998. }
  999. static int js_delvar(js_State *J, const char *name)
  1000. {
  1001. js_Environment *E = J->E;
  1002. do {
  1003. js_Property *ref = jsV_getownproperty(J, E->variables, name);
  1004. if (ref) {
  1005. if (ref->atts & JS_DONTCONF) {
  1006. if (J->strict)
  1007. js_typeerror(J, "'%s' is non-configurable", name);
  1008. return 0;
  1009. }
  1010. jsV_delproperty(J, E->variables, name);
  1011. return 1;
  1012. }
  1013. E = E->outer;
  1014. } while (E);
  1015. return jsR_delproperty(J, J->G, name);
  1016. }
  1017. /* Function calls */
  1018. static void jsR_savescope(js_State *J, js_Environment *newE)
  1019. {
  1020. if (J->envtop + 1 >= JS_ENVLIMIT)
  1021. js_stackoverflow(J);
  1022. J->envstack[J->envtop++] = J->E;
  1023. J->E = newE;
  1024. }
  1025. static void jsR_restorescope(js_State *J)
  1026. {
  1027. J->E = J->envstack[--J->envtop];
  1028. }
  1029. static void jsR_calllwfunction(js_State *J, int n, js_Function *F, js_Environment *scope)
  1030. {
  1031. js_Value v;
  1032. int i;
  1033. jsR_savescope(J, scope);
  1034. if (n > F->numparams) {
  1035. js_pop(J, n - F->numparams);
  1036. n = F->numparams;
  1037. }
  1038. for (i = n; i < F->varlen; ++i)
  1039. js_pushundefined(J);
  1040. jsR_run(J, F);
  1041. v = *stackidx(J, -1);
  1042. TOP = --BOT; /* clear stack */
  1043. js_pushvalue(J, v);
  1044. jsR_restorescope(J);
  1045. }
  1046. static void jsR_callfunction(js_State *J, int n, js_Function *F, js_Environment *scope)
  1047. {
  1048. js_Value v;
  1049. int i;
  1050. scope = jsR_newenvironment(J, jsV_newobject(J, JS_COBJECT, NULL), scope);
  1051. jsR_savescope(J, scope);
  1052. if (F->arguments) {
  1053. js_newarguments(J);
  1054. if (!J->strict) {
  1055. js_currentfunction(J);
  1056. js_defproperty(J, -2, "callee", JS_DONTENUM);
  1057. }
  1058. js_pushnumber(J, n);
  1059. js_defproperty(J, -2, "length", JS_DONTENUM);
  1060. for (i = 0; i < n; ++i) {
  1061. js_copy(J, i + 1);
  1062. js_setindex(J, -2, i);
  1063. }
  1064. js_initvar(J, "arguments", -1);
  1065. js_pop(J, 1);
  1066. }
  1067. for (i = 0; i < n && i < F->numparams; ++i)
  1068. js_initvar(J, F->vartab[i], i + 1);
  1069. js_pop(J, n);
  1070. for (; i < F->varlen; ++i) {
  1071. js_pushundefined(J);
  1072. js_initvar(J, F->vartab[i], -1);
  1073. js_pop(J, 1);
  1074. }
  1075. jsR_run(J, F);
  1076. v = *stackidx(J, -1);
  1077. TOP = --BOT; /* clear stack */
  1078. js_pushvalue(J, v);
  1079. jsR_restorescope(J);
  1080. }
  1081. static void jsR_callscript(js_State *J, int n, js_Function *F, js_Environment *scope)
  1082. {
  1083. js_Value v;
  1084. int i;
  1085. if (scope)
  1086. jsR_savescope(J, scope);
  1087. /* scripts take no arguments */
  1088. js_pop(J, n);
  1089. for (i = 0; i < F->varlen; ++i) {
  1090. /* Bug 701886: don't redefine existing vars in eval/scripts */
  1091. if (!js_hasvar(J, F->vartab[i])) {
  1092. js_pushundefined(J);
  1093. js_initvar(J, F->vartab[i], -1);
  1094. js_pop(J, 1);
  1095. }
  1096. }
  1097. jsR_run(J, F);
  1098. v = *stackidx(J, -1);
  1099. TOP = --BOT; /* clear stack */
  1100. js_pushvalue(J, v);
  1101. if (scope)
  1102. jsR_restorescope(J);
  1103. }
  1104. static void jsR_callcfunction(js_State *J, int n, int min, js_CFunction F)
  1105. {
  1106. int save_top;
  1107. int i;
  1108. js_Value v;
  1109. for (i = n; i < min; ++i)
  1110. js_pushundefined(J);
  1111. save_top = TOP;
  1112. F(J);
  1113. if (TOP > save_top) {
  1114. v = *stackidx(J, -1);
  1115. TOP = --BOT; /* clear stack */
  1116. js_pushvalue(J, v);
  1117. } else {
  1118. TOP = --BOT; /* clear stack */
  1119. js_pushundefined(J);
  1120. }
  1121. }
  1122. static void jsR_pushtrace(js_State *J, const char *name, const char *file, int line)
  1123. {
  1124. if (J->tracetop + 1 == JS_ENVLIMIT)
  1125. js_error(J, "call stack overflow");
  1126. ++J->tracetop;
  1127. J->trace[J->tracetop].stack = J->bot;
  1128. J->trace[J->tracetop].name = name;
  1129. J->trace[J->tracetop].file = file;
  1130. J->trace[J->tracetop].line = line;
  1131. }
  1132. void js_call(js_State *J, int n)
  1133. {
  1134. js_Object *obj;
  1135. int savebot;
  1136. if (n < 0)
  1137. js_rangeerror(J, "number of arguments cannot be negative");
  1138. if (!js_iscallable(J, -n-2))
  1139. js_typeerror(J, "%s is not callable", js_typeof(J, -n-2));
  1140. obj = js_toobject(J, -n-2);
  1141. savebot = BOT;
  1142. BOT = TOP - n - 1;
  1143. if (obj->type == JS_CFUNCTION) {
  1144. jsR_pushtrace(J, obj->u.f.function->name, obj->u.f.function->filename, obj->u.f.function->line);
  1145. if (obj->u.f.function->lightweight)
  1146. jsR_calllwfunction(J, n, obj->u.f.function, obj->u.f.scope);
  1147. else
  1148. jsR_callfunction(J, n, obj->u.f.function, obj->u.f.scope);
  1149. --J->tracetop;
  1150. } else if (obj->type == JS_CSCRIPT) {
  1151. jsR_pushtrace(J, obj->u.f.function->name, obj->u.f.function->filename, obj->u.f.function->line);
  1152. jsR_callscript(J, n, obj->u.f.function, obj->u.f.scope);
  1153. --J->tracetop;
  1154. } else if (obj->type == JS_CCFUNCTION) {
  1155. jsR_pushtrace(J, obj->u.c.name, "native", 0);
  1156. jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.function);
  1157. --J->tracetop;
  1158. }
  1159. BOT = savebot;
  1160. }
  1161. void js_construct(js_State *J, int n)
  1162. {
  1163. js_Object *obj;
  1164. js_Object *prototype;
  1165. js_Object *newobj;
  1166. if (!js_iscallable(J, -n-1))
  1167. js_typeerror(J, "%s is not callable", js_typeof(J, -n-1));
  1168. obj = js_toobject(J, -n-1);
  1169. /* built-in constructors create their own objects, give them a 'null' this */
  1170. if (obj->type == JS_CCFUNCTION && obj->u.c.constructor) {
  1171. int savebot = BOT;
  1172. js_pushnull(J);
  1173. if (n > 0)
  1174. js_rot(J, n + 1);
  1175. BOT = TOP - n - 1;
  1176. jsR_pushtrace(J, obj->u.c.name, "native", 0);
  1177. jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.constructor);
  1178. --J->tracetop;
  1179. BOT = savebot;
  1180. return;
  1181. }
  1182. /* extract the function object's prototype property */
  1183. js_getproperty(J, -n - 1, "prototype");
  1184. if (js_isobject(J, -1))
  1185. prototype = js_toobject(J, -1);
  1186. else
  1187. prototype = J->Object_prototype;
  1188. js_pop(J, 1);
  1189. /* create a new object with above prototype, and shift it into the 'this' slot */
  1190. newobj = jsV_newobject(J, JS_COBJECT, prototype);
  1191. js_pushobject(J, newobj);
  1192. if (n > 0)
  1193. js_rot(J, n + 1);
  1194. /* and save a copy to return */
  1195. js_pushobject(J, newobj);
  1196. js_rot(J, n + 3);
  1197. /* call the function */
  1198. js_call(J, n);
  1199. /* if result is not an object, return the original object we created */
  1200. if (!js_isobject(J, -1)) {
  1201. js_pop(J, 1);
  1202. } else {
  1203. js_rot2pop1(J);
  1204. }
  1205. }
  1206. void js_eval(js_State *J)
  1207. {
  1208. if (!js_isstring(J, -1))
  1209. return;
  1210. js_loadeval(J, "(eval)", js_tostring(J, -1));
  1211. js_rot2pop1(J);
  1212. js_copy(J, 0); /* copy 'this' */
  1213. js_call(J, 0);
  1214. }
  1215. int js_pconstruct(js_State *J, int n)
  1216. {
  1217. int savetop = TOP - n - 2;
  1218. if (js_try(J)) {
  1219. /* clean up the stack to only hold the error object */
  1220. STACK[savetop] = STACK[TOP-1];
  1221. TOP = savetop + 1;
  1222. return 1;
  1223. }
  1224. js_construct(J, n);
  1225. js_endtry(J);
  1226. return 0;
  1227. }
  1228. int js_pcall(js_State *J, int n)
  1229. {
  1230. int savetop = TOP - n - 2;
  1231. if (js_try(J)) {
  1232. /* clean up the stack to only hold the error object */
  1233. STACK[savetop] = STACK[TOP-1];
  1234. TOP = savetop + 1;
  1235. return 1;
  1236. }
  1237. js_call(J, n);
  1238. js_endtry(J);
  1239. return 0;
  1240. }
  1241. /* Exceptions */
  1242. void *js_savetrypc(js_State *J, js_Instruction *pc)
  1243. {
  1244. if (J->trytop == JS_TRYLIMIT)
  1245. js_trystackoverflow(J);
  1246. J->trybuf[J->trytop].E = J->E;
  1247. J->trybuf[J->trytop].envtop = J->envtop;
  1248. J->trybuf[J->trytop].tracetop = J->tracetop;
  1249. J->trybuf[J->trytop].top = J->top;
  1250. J->trybuf[J->trytop].bot = J->bot;
  1251. J->trybuf[J->trytop].strict = J->strict;
  1252. J->trybuf[J->trytop].pc = pc;
  1253. return J->trybuf[J->trytop++].buf;
  1254. }
  1255. void *js_savetry(js_State *J)
  1256. {
  1257. if (J->trytop == JS_TRYLIMIT)
  1258. js_trystackoverflow(J);
  1259. J->trybuf[J->trytop].E = J->E;
  1260. J->trybuf[J->trytop].envtop = J->envtop;
  1261. J->trybuf[J->trytop].tracetop = J->tracetop;
  1262. J->trybuf[J->trytop].top = J->top;
  1263. J->trybuf[J->trytop].bot = J->bot;
  1264. J->trybuf[J->trytop].strict = J->strict;
  1265. J->trybuf[J->trytop].pc = NULL;
  1266. return J->trybuf[J->trytop++].buf;
  1267. }
  1268. void js_endtry(js_State *J)
  1269. {
  1270. if (J->trytop == 0)
  1271. js_error(J, "endtry: exception stack underflow");
  1272. --J->trytop;
  1273. }
  1274. void js_throw(js_State *J)
  1275. {
  1276. if (J->trytop > 0) {
  1277. js_Value v = *stackidx(J, -1);
  1278. --J->trytop;
  1279. J->E = J->trybuf[J->trytop].E;
  1280. J->envtop = J->trybuf[J->trytop].envtop;
  1281. J->tracetop = J->trybuf[J->trytop].tracetop;
  1282. J->top = J->trybuf[J->trytop].top;
  1283. J->bot = J->trybuf[J->trytop].bot;
  1284. J->strict = J->trybuf[J->trytop].strict;
  1285. js_pushvalue(J, v);
  1286. longjmp(J->trybuf[J->trytop].buf, 1);
  1287. }
  1288. if (J->panic)
  1289. J->panic(J);
  1290. abort();
  1291. }
  1292. /* Main interpreter loop */
  1293. static void js_dumpvalue(js_State *J, js_Value v)
  1294. {
  1295. switch (v.t.type) {
  1296. case JS_TUNDEFINED: printf("undefined"); break;
  1297. case JS_TNULL: printf("null"); break;
  1298. case JS_TBOOLEAN: printf(v.u.boolean ? "true" : "false"); break;
  1299. case JS_TNUMBER: printf("%.9g", v.u.number); break;
  1300. case JS_TSHRSTR: printf("'%s'", v.u.shrstr); break;
  1301. case JS_TLITSTR: printf("'%s'", v.u.litstr); break;
  1302. case JS_TMEMSTR: printf("'%s'", v.u.memstr->p); break;
  1303. case JS_TOBJECT:
  1304. if (v.u.object == J->G) {
  1305. printf("[Global]");
  1306. break;
  1307. }
  1308. switch (v.u.object->type) {
  1309. case JS_COBJECT: printf("[Object %p]", (void*)v.u.object); break;
  1310. case JS_CARRAY: printf("[Array %p]", (void*)v.u.object); break;
  1311. case JS_CFUNCTION:
  1312. printf("[Function %p, %s, %s:%d]",
  1313. (void*)v.u.object,
  1314. v.u.object->u.f.function->name,
  1315. v.u.object->u.f.function->filename,
  1316. v.u.object->u.f.function->line);
  1317. break;
  1318. case JS_CSCRIPT: printf("[Script %s]", v.u.object->u.f.function->filename); break;
  1319. case JS_CCFUNCTION: printf("[CFunction %s]", v.u.object->u.c.name); break;
  1320. case JS_CBOOLEAN: printf("[Boolean %d]", v.u.object->u.boolean); break;
  1321. case JS_CNUMBER: printf("[Number %g]", v.u.object->u.number); break;
  1322. case JS_CSTRING: printf("[String'%s']", v.u.object->u.s.string); break;
  1323. case JS_CERROR: printf("[Error]"); break;
  1324. case JS_CARGUMENTS: printf("[Arguments %p]", (void*)v.u.object); break;
  1325. case JS_CITERATOR: printf("[Iterator %p]", (void*)v.u.object); break;
  1326. case JS_CUSERDATA:
  1327. printf("[Userdata %s %p]", v.u.object->u.user.tag, v.u.object->u.user.data);
  1328. break;
  1329. default: printf("[Object %p]", (void*)v.u.object); break;
  1330. }
  1331. break;
  1332. }
  1333. }
  1334. static void js_stacktrace(js_State *J)
  1335. {
  1336. int n;
  1337. printf("stack trace:\n");
  1338. for (n = J->tracetop; n >= 0; --n) {
  1339. const char *name = J->trace[n].name;
  1340. const char *file = J->trace[n].file;
  1341. int line = J->trace[n].line;
  1342. if (line > 0) {
  1343. if (name[0])
  1344. printf("\tat %s (%s:%d)\n", name, file, line);
  1345. else
  1346. printf("\tat %s:%d\n", file, line);
  1347. } else
  1348. printf("\tat %s (%s)\n", name, file);
  1349. }
  1350. }
  1351. static void js_dumpstack(js_State *J)
  1352. {
  1353. int i;
  1354. printf("stack {\n");
  1355. for (i = 0; i < TOP; ++i) {
  1356. putchar(i == BOT ? '>' : ' ');
  1357. printf("%4d: ", i);
  1358. js_dumpvalue(J, STACK[i]);
  1359. putchar('\n');
  1360. }
  1361. printf("}\n");
  1362. }
  1363. void js_trap(js_State *J, int pc)
  1364. {
  1365. js_dumpstack(J);
  1366. js_stacktrace(J);
  1367. }
  1368. static int jsR_isindex(js_State *J, int idx, int *k)
  1369. {
  1370. js_Value *v = stackidx(J, idx);
  1371. if (v->t.type == JS_TNUMBER) {
  1372. if (v->u.number >= 0 && v->u.number <= INT_MAX) {
  1373. *k = v->u.number;
  1374. return *k == v->u.number;
  1375. }
  1376. }
  1377. return 0;
  1378. }
  1379. static void jsR_run(js_State *J, js_Function *F)
  1380. {
  1381. js_Function **FT = F->funtab;
  1382. const char **VT = F->vartab ? F->vartab - 1 : NULL;
  1383. int lightweight = F->lightweight;
  1384. js_Instruction *pcstart = F->code;
  1385. js_Instruction *pc = F->code;
  1386. enum js_OpCode opcode;
  1387. int offset;
  1388. int savestrict;
  1389. const char *str;
  1390. js_Object *obj;
  1391. double x, y;
  1392. unsigned int ux, uy;
  1393. int ix, iy, okay;
  1394. int b;
  1395. int transient;
  1396. savestrict = J->strict;
  1397. J->strict = F->strict;
  1398. #define READSTRING() \
  1399. memcpy(&str, pc, sizeof(str)); \
  1400. pc += sizeof(str) / sizeof(*pc)
  1401. while (1) {
  1402. if (J->runlimit > 0) {
  1403. if (J->runlimit == 1)
  1404. js_runlimit(J);
  1405. --J->runlimit;
  1406. }
  1407. if (J->gccounter > J->gcthresh)
  1408. js_gc(J, 0);
  1409. J->trace[J->tracetop].line = *pc++;
  1410. opcode = *pc++;
  1411. switch (opcode) {
  1412. case OP_POP: js_pop(J, 1); break;
  1413. case OP_DUP: js_dup(J); break;
  1414. case OP_DUP2: js_dup2(J); break;
  1415. case OP_ROT2: js_rot2(J); break;
  1416. case OP_ROT3: js_rot3(J); break;
  1417. case OP_ROT4: js_rot4(J); break;
  1418. case OP_INTEGER:
  1419. js_pushnumber(J, *pc++ - 32768);
  1420. break;
  1421. case OP_NUMBER:
  1422. memcpy(&x, pc, sizeof(x));
  1423. pc += sizeof(x) / sizeof(*pc);
  1424. js_pushnumber(J, x);
  1425. break;
  1426. case OP_STRING:
  1427. READSTRING();
  1428. js_pushliteral(J, str);
  1429. break;
  1430. case OP_CLOSURE: js_newfunction(J, FT[*pc++], J->E); break;
  1431. case OP_NEWOBJECT: js_newobject(J); break;
  1432. case OP_NEWARRAY: js_newarray(J); break;
  1433. case OP_NEWREGEXP:
  1434. READSTRING();
  1435. js_newregexp(J, str, *pc++);
  1436. break;
  1437. case OP_UNDEF: js_pushundefined(J); break;
  1438. case OP_NULL: js_pushnull(J); break;
  1439. case OP_TRUE: js_pushboolean(J, 1); break;
  1440. case OP_FALSE: js_pushboolean(J, 0); break;
  1441. case OP_THIS:
  1442. if (J->strict) {
  1443. js_copy(J, 0);
  1444. } else {
  1445. if (js_iscoercible(J, 0))
  1446. js_copy(J, 0);
  1447. else
  1448. js_pushglobal(J);
  1449. }
  1450. break;
  1451. case OP_CURRENT:
  1452. js_currentfunction(J);
  1453. break;
  1454. case OP_GETLOCAL:
  1455. if (lightweight) {
  1456. CHECKSTACK(1);
  1457. STACK[TOP++] = STACK[BOT + *pc++];
  1458. } else {
  1459. str = VT[*pc++];
  1460. if (!js_hasvar(J, str))
  1461. js_referenceerror(J, "'%s' is not defined", str);
  1462. }
  1463. break;
  1464. case OP_SETLOCAL:
  1465. if (lightweight) {
  1466. STACK[BOT + *pc++] = STACK[TOP-1];
  1467. } else {
  1468. js_setvar(J, VT[*pc++]);
  1469. }
  1470. break;
  1471. case OP_DELLOCAL:
  1472. if (lightweight) {
  1473. ++pc;
  1474. js_pushboolean(J, 0);
  1475. } else {
  1476. b = js_delvar(J, VT[*pc++]);
  1477. js_pushboolean(J, b);
  1478. }
  1479. break;
  1480. case OP_GETVAR:
  1481. READSTRING();
  1482. if (!js_hasvar(J, str))
  1483. js_referenceerror(J, "'%s' is not defined", str);
  1484. break;
  1485. case OP_HASVAR:
  1486. READSTRING();
  1487. if (!js_hasvar(J, str))
  1488. js_pushundefined(J);
  1489. break;
  1490. case OP_SETVAR:
  1491. READSTRING();
  1492. js_setvar(J, str);
  1493. break;
  1494. case OP_DELVAR:
  1495. READSTRING();
  1496. b = js_delvar(J, str);
  1497. js_pushboolean(J, b);
  1498. break;
  1499. case OP_IN:
  1500. str = js_tostring(J, -2);
  1501. if (!js_isobject(J, -1))
  1502. js_typeerror(J, "operand to 'in' is not an object");
  1503. b = js_hasproperty(J, -1, str);
  1504. js_pop(J, 2 + b);
  1505. js_pushboolean(J, b);
  1506. break;
  1507. case OP_SKIPARRAY:
  1508. js_setlength(J, -1, js_getlength(J, -1) + 1);
  1509. break;
  1510. case OP_INITARRAY:
  1511. js_setindex(J, -2, js_getlength(J, -2));
  1512. break;
  1513. case OP_INITPROP:
  1514. obj = js_toobject(J, -3);
  1515. str = js_tostring(J, -2);
  1516. jsR_setproperty(J, obj, str, 0);
  1517. js_pop(J, 2);
  1518. break;
  1519. case OP_INITGETTER:
  1520. obj = js_toobject(J, -3);
  1521. str = js_tostring(J, -2);
  1522. jsR_defproperty(J, obj, str, 0, NULL, jsR_tofunction(J, -1), NULL, 0);
  1523. js_pop(J, 2);
  1524. break;
  1525. case OP_INITSETTER:
  1526. obj = js_toobject(J, -3);
  1527. str = js_tostring(J, -2);
  1528. jsR_defproperty(J, obj, str, 0, NULL, NULL, jsR_tofunction(J, -1), 0);
  1529. js_pop(J, 2);
  1530. break;
  1531. case OP_GETPROP:
  1532. if (jsR_isindex(J, -1, &ix)) {
  1533. obj = js_toobject(J, -2);
  1534. jsR_getindex(J, obj, ix);
  1535. } else {
  1536. str = js_tostring(J, -1);
  1537. obj = js_toobject(J, -2);
  1538. jsR_getproperty(J, obj, str);
  1539. }
  1540. js_rot3pop2(J);
  1541. break;
  1542. case OP_GETPROP_S:
  1543. READSTRING();
  1544. obj = js_toobject(J, -1);
  1545. jsR_getproperty(J, obj, str);
  1546. js_rot2pop1(J);
  1547. break;
  1548. case OP_SETPROP:
  1549. if (jsR_isindex(J, -2, &ix)) {
  1550. obj = js_toobject(J, -3);
  1551. transient = !js_isobject(J, -3);
  1552. jsR_setindex(J, obj, ix, transient);
  1553. } else {
  1554. str = js_tostring(J, -2);
  1555. obj = js_toobject(J, -3);
  1556. transient = !js_isobject(J, -3);
  1557. jsR_setproperty(J, obj, str, transient);
  1558. }
  1559. js_rot3pop2(J);
  1560. break;
  1561. case OP_SETPROP_S:
  1562. READSTRING();
  1563. obj = js_toobject(J, -2);
  1564. transient = !js_isobject(J, -2);
  1565. jsR_setproperty(J, obj, str, transient);
  1566. js_rot2pop1(J);
  1567. break;
  1568. case OP_DELPROP:
  1569. str = js_tostring(J, -1);
  1570. obj = js_toobject(J, -2);
  1571. b = jsR_delproperty(J, obj, str);
  1572. js_pop(J, 2);
  1573. js_pushboolean(J, b);
  1574. break;
  1575. case OP_DELPROP_S:
  1576. READSTRING();
  1577. obj = js_toobject(J, -1);
  1578. b = jsR_delproperty(J, obj, str);
  1579. js_pop(J, 1);
  1580. js_pushboolean(J, b);
  1581. break;
  1582. case OP_ITERATOR:
  1583. if (js_iscoercible(J, -1)) {
  1584. obj = jsV_newiterator(J, js_toobject(J, -1), 0);
  1585. js_pop(J, 1);
  1586. js_pushobject(J, obj);
  1587. }
  1588. break;
  1589. case OP_NEXTITER:
  1590. if (js_isobject(J, -1)) {
  1591. obj = js_toobject(J, -1);
  1592. str = jsV_nextiterator(J, obj);
  1593. if (str) {
  1594. js_pushstring(J, str);
  1595. js_pushboolean(J, 1);
  1596. } else {
  1597. js_pop(J, 1);
  1598. js_pushboolean(J, 0);
  1599. }
  1600. } else {
  1601. js_pop(J, 1);
  1602. js_pushboolean(J, 0);
  1603. }
  1604. break;
  1605. /* Function calls */
  1606. case OP_EVAL:
  1607. js_eval(J);
  1608. break;
  1609. case OP_CALL:
  1610. js_call(J, *pc++);
  1611. break;
  1612. case OP_NEW:
  1613. js_construct(J, *pc++);
  1614. break;
  1615. /* Unary operators */
  1616. case OP_TYPEOF:
  1617. str = js_typeof(J, -1);
  1618. js_pop(J, 1);
  1619. js_pushliteral(J, str);
  1620. break;
  1621. case OP_POS:
  1622. x = js_tonumber(J, -1);
  1623. js_pop(J, 1);
  1624. js_pushnumber(J, x);
  1625. break;
  1626. case OP_NEG:
  1627. x = js_tonumber(J, -1);
  1628. js_pop(J, 1);
  1629. js_pushnumber(J, -x);
  1630. break;
  1631. case OP_BITNOT:
  1632. ix = js_toint32(J, -1);
  1633. js_pop(J, 1);
  1634. js_pushnumber(J, ~ix);
  1635. break;
  1636. case OP_LOGNOT:
  1637. b = js_toboolean(J, -1);
  1638. js_pop(J, 1);
  1639. js_pushboolean(J, !b);
  1640. break;
  1641. case OP_INC:
  1642. x = js_tonumber(J, -1);
  1643. js_pop(J, 1);
  1644. js_pushnumber(J, x + 1);
  1645. break;
  1646. case OP_DEC:
  1647. x = js_tonumber(J, -1);
  1648. js_pop(J, 1);
  1649. js_pushnumber(J, x - 1);
  1650. break;
  1651. case OP_POSTINC:
  1652. x = js_tonumber(J, -1);
  1653. js_pop(J, 1);
  1654. js_pushnumber(J, x + 1);
  1655. js_pushnumber(J, x);
  1656. break;
  1657. case OP_POSTDEC:
  1658. x = js_tonumber(J, -1);
  1659. js_pop(J, 1);
  1660. js_pushnumber(J, x - 1);
  1661. js_pushnumber(J, x);
  1662. break;
  1663. /* Multiplicative operators */
  1664. case OP_MUL:
  1665. x = js_tonumber(J, -2);
  1666. y = js_tonumber(J, -1);
  1667. js_pop(J, 2);
  1668. js_pushnumber(J, x * y);
  1669. break;
  1670. case OP_DIV:
  1671. x = js_tonumber(J, -2);
  1672. y = js_tonumber(J, -1);
  1673. js_pop(J, 2);
  1674. js_pushnumber(J, x / y);
  1675. break;
  1676. case OP_MOD:
  1677. x = js_tonumber(J, -2);
  1678. y = js_tonumber(J, -1);
  1679. js_pop(J, 2);
  1680. js_pushnumber(J, fmod(x, y));
  1681. break;
  1682. /* Additive operators */
  1683. case OP_ADD:
  1684. js_concat(J);
  1685. break;
  1686. case OP_SUB:
  1687. x = js_tonumber(J, -2);
  1688. y = js_tonumber(J, -1);
  1689. js_pop(J, 2);
  1690. js_pushnumber(J, x - y);
  1691. break;
  1692. /* Shift operators */
  1693. case OP_SHL:
  1694. ix = js_toint32(J, -2);
  1695. uy = js_touint32(J, -1);
  1696. js_pop(J, 2);
  1697. js_pushnumber(J, ix << (uy & 0x1F));
  1698. break;
  1699. case OP_SHR:
  1700. ix = js_toint32(J, -2);
  1701. uy = js_touint32(J, -1);
  1702. js_pop(J, 2);
  1703. js_pushnumber(J, ix >> (uy & 0x1F));
  1704. break;
  1705. case OP_USHR:
  1706. ux = js_touint32(J, -2);
  1707. uy = js_touint32(J, -1);
  1708. js_pop(J, 2);
  1709. js_pushnumber(J, ux >> (uy & 0x1F));
  1710. break;
  1711. /* Relational operators */
  1712. case OP_LT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b < 0); break;
  1713. case OP_GT: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b > 0); break;
  1714. case OP_LE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b <= 0); break;
  1715. case OP_GE: b = js_compare(J, &okay); js_pop(J, 2); js_pushboolean(J, okay && b >= 0); break;
  1716. case OP_INSTANCEOF:
  1717. b = js_instanceof(J);
  1718. js_pop(J, 2);
  1719. js_pushboolean(J, b);
  1720. break;
  1721. /* Equality */
  1722. case OP_EQ: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, b); break;
  1723. case OP_NE: b = js_equal(J); js_pop(J, 2); js_pushboolean(J, !b); break;
  1724. case OP_STRICTEQ: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, b); break;
  1725. case OP_STRICTNE: b = js_strictequal(J); js_pop(J, 2); js_pushboolean(J, !b); break;
  1726. case OP_JCASE:
  1727. offset = *pc++;
  1728. b = js_strictequal(J);
  1729. if (b) {
  1730. js_pop(J, 2);
  1731. pc = pcstart + offset;
  1732. } else {
  1733. js_pop(J, 1);
  1734. }
  1735. break;
  1736. /* Binary bitwise operators */
  1737. case OP_BITAND:
  1738. ix = js_toint32(J, -2);
  1739. iy = js_toint32(J, -1);
  1740. js_pop(J, 2);
  1741. js_pushnumber(J, ix & iy);
  1742. break;
  1743. case OP_BITXOR:
  1744. ix = js_toint32(J, -2);
  1745. iy = js_toint32(J, -1);
  1746. js_pop(J, 2);
  1747. js_pushnumber(J, ix ^ iy);
  1748. break;
  1749. case OP_BITOR:
  1750. ix = js_toint32(J, -2);
  1751. iy = js_toint32(J, -1);
  1752. js_pop(J, 2);
  1753. js_pushnumber(J, ix | iy);
  1754. break;
  1755. /* Try and Catch */
  1756. case OP_THROW:
  1757. js_throw(J);
  1758. case OP_TRY:
  1759. offset = *pc++;
  1760. if (js_trypc(J, pc)) {
  1761. pc = J->trybuf[J->trytop].pc;
  1762. } else {
  1763. pc = pcstart + offset;
  1764. }
  1765. break;
  1766. case OP_ENDTRY:
  1767. js_endtry(J);
  1768. break;
  1769. case OP_CATCH:
  1770. READSTRING();
  1771. obj = jsV_newobject(J, JS_COBJECT, NULL);
  1772. js_pushobject(J, obj);
  1773. js_rot2(J);
  1774. js_setproperty(J, -2, str);
  1775. J->E = jsR_newenvironment(J, obj, J->E);
  1776. js_pop(J, 1);
  1777. break;
  1778. case OP_ENDCATCH:
  1779. J->E = J->E->outer;
  1780. break;
  1781. /* With */
  1782. case OP_WITH:
  1783. obj = js_toobject(J, -1);
  1784. J->E = jsR_newenvironment(J, obj, J->E);
  1785. js_pop(J, 1);
  1786. break;
  1787. case OP_ENDWITH:
  1788. J->E = J->E->outer;
  1789. break;
  1790. /* Branching */
  1791. case OP_DEBUGGER:
  1792. js_trap(J, (int)(pc - pcstart) - 1);
  1793. break;
  1794. case OP_JUMP:
  1795. pc = pcstart + *pc;
  1796. break;
  1797. case OP_JTRUE:
  1798. offset = *pc++;
  1799. b = js_toboolean(J, -1);
  1800. js_pop(J, 1);
  1801. if (b)
  1802. pc = pcstart + offset;
  1803. break;
  1804. case OP_JFALSE:
  1805. offset = *pc++;
  1806. b = js_toboolean(J, -1);
  1807. js_pop(J, 1);
  1808. if (!b)
  1809. pc = pcstart + offset;
  1810. break;
  1811. case OP_RETURN:
  1812. J->strict = savestrict;
  1813. return;
  1814. }
  1815. }
  1816. }