jscompile.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. #include "jsi.h"
  2. #define cexp jsC_cexp /* collision with math.h */
  3. #define JF js_State *J, js_Function *F
  4. JS_NORETURN void jsC_error(js_State *J, js_Ast *node, const char *fmt, ...) JS_PRINTFLIKE(3,4);
  5. static void cfunbody(JF, js_Ast *name, js_Ast *params, js_Ast *body, int is_fun_exp);
  6. static void cexp(JF, js_Ast *exp);
  7. static void cstmlist(JF, js_Ast *list);
  8. static void cstm(JF, js_Ast *stm);
  9. void jsC_error(js_State *J, js_Ast *node, const char *fmt, ...)
  10. {
  11. va_list ap;
  12. char buf[512];
  13. char msgbuf[256];
  14. va_start(ap, fmt);
  15. vsnprintf(msgbuf, 256, fmt, ap);
  16. va_end(ap);
  17. snprintf(buf, 256, "%s:%d: ", J->filename, node->line);
  18. strcat(buf, msgbuf);
  19. js_newsyntaxerror(J, buf);
  20. js_throw(J);
  21. }
  22. static const char *futurewords[] = {
  23. "class", "const", "enum", "export", "extends", "import", "super",
  24. };
  25. static const char *strictfuturewords[] = {
  26. "implements", "interface", "let", "package", "private", "protected",
  27. "public", "static", "yield",
  28. };
  29. static void checkfutureword(JF, js_Ast *exp)
  30. {
  31. if (jsY_findword(exp->string, futurewords, nelem(futurewords)) >= 0)
  32. jsC_error(J, exp, "'%s' is a future reserved word", exp->string);
  33. if (F->strict) {
  34. if (jsY_findword(exp->string, strictfuturewords, nelem(strictfuturewords)) >= 0)
  35. jsC_error(J, exp, "'%s' is a strict mode future reserved word", exp->string);
  36. }
  37. }
  38. static js_Function *newfun(js_State *J, int line, js_Ast *name, js_Ast *params, js_Ast *body, int script, int default_strict, int is_fun_exp)
  39. {
  40. js_Function *F = js_malloc(J, sizeof *F);
  41. memset(F, 0, sizeof *F);
  42. F->gcmark = 0;
  43. F->gcnext = J->gcfun;
  44. J->gcfun = F;
  45. ++J->gccounter;
  46. F->filename = js_intern(J, J->filename);
  47. F->line = line;
  48. F->script = script;
  49. F->strict = default_strict;
  50. F->name = name ? name->string : "";
  51. cfunbody(J, F, name, params, body, is_fun_exp);
  52. return F;
  53. }
  54. /* Emit opcodes, constants and jumps */
  55. static void emitraw(JF, int value)
  56. {
  57. if (value != (js_Instruction)value)
  58. js_syntaxerror(J, "integer overflow in instruction coding");
  59. if (F->codelen >= F->codecap) {
  60. F->codecap = F->codecap ? F->codecap * 2 : 64;
  61. F->code = js_realloc(J, F->code, F->codecap * sizeof *F->code);
  62. }
  63. F->code[F->codelen++] = value;
  64. }
  65. static void emit(JF, int value)
  66. {
  67. emitraw(J, F, F->lastline);
  68. emitraw(J, F, value);
  69. }
  70. static void emitarg(JF, int value)
  71. {
  72. emitraw(J, F, value);
  73. }
  74. static void emitline(JF, js_Ast *node)
  75. {
  76. F->lastline = node->line;
  77. }
  78. static int addfunction(JF, js_Function *value)
  79. {
  80. if (F->funlen >= F->funcap) {
  81. F->funcap = F->funcap ? F->funcap * 2 : 16;
  82. F->funtab = js_realloc(J, F->funtab, F->funcap * sizeof *F->funtab);
  83. }
  84. F->funtab[F->funlen] = value;
  85. return F->funlen++;
  86. }
  87. static int addlocal(JF, js_Ast *ident, int reuse)
  88. {
  89. const char *name = ident->string;
  90. if (F->strict) {
  91. if (!strcmp(name, "arguments"))
  92. jsC_error(J, ident, "redefining 'arguments' is not allowed in strict mode");
  93. if (!strcmp(name, "eval"))
  94. jsC_error(J, ident, "redefining 'eval' is not allowed in strict mode");
  95. } else {
  96. if (!strcmp(name, "eval"))
  97. js_evalerror(J, "%s:%d: invalid use of 'eval'", J->filename, ident->line);
  98. }
  99. if (reuse || F->strict) {
  100. int i;
  101. for (i = 0; i < F->varlen; ++i) {
  102. if (!strcmp(F->vartab[i], name)) {
  103. if (reuse)
  104. return i+1;
  105. if (F->strict)
  106. jsC_error(J, ident, "duplicate formal parameter '%s'", name);
  107. }
  108. }
  109. }
  110. if (F->varlen >= F->varcap) {
  111. F->varcap = F->varcap ? F->varcap * 2 : 16;
  112. F->vartab = js_realloc(J, F->vartab, F->varcap * sizeof *F->vartab);
  113. }
  114. F->vartab[F->varlen] = name;
  115. return ++F->varlen;
  116. }
  117. static int findlocal(JF, const char *name)
  118. {
  119. int i;
  120. for (i = F->varlen; i > 0; --i)
  121. if (!strcmp(F->vartab[i-1], name))
  122. return i;
  123. return -1;
  124. }
  125. static void emitfunction(JF, js_Function *fun)
  126. {
  127. F->lightweight = 0;
  128. emit(J, F, OP_CLOSURE);
  129. emitarg(J, F, addfunction(J, F, fun));
  130. }
  131. static void emitnumber(JF, double num)
  132. {
  133. if (num == 0) {
  134. emit(J, F, OP_INTEGER);
  135. emitarg(J, F, 32768);
  136. if (signbit(num))
  137. emit(J, F, OP_NEG);
  138. } else if (num >= SHRT_MIN && num <= SHRT_MAX && num == (int)num) {
  139. emit(J, F, OP_INTEGER);
  140. emitarg(J, F, num + 32768);
  141. } else {
  142. #define N (sizeof(num) / sizeof(js_Instruction))
  143. js_Instruction x[N];
  144. size_t i;
  145. emit(J, F, OP_NUMBER);
  146. memcpy(x, &num, sizeof(num));
  147. for (i = 0; i < N; ++i)
  148. emitarg(J, F, x[i]);
  149. #undef N
  150. }
  151. }
  152. static void emitstring(JF, int opcode, const char *str)
  153. {
  154. #define N (sizeof(str) / sizeof(js_Instruction))
  155. js_Instruction x[N];
  156. size_t i;
  157. emit(J, F, opcode);
  158. memcpy(x, &str, sizeof(str));
  159. for (i = 0; i < N; ++i)
  160. emitarg(J, F, x[i]);
  161. #undef N
  162. }
  163. static void emitlocal(JF, int oploc, int opvar, js_Ast *ident)
  164. {
  165. int is_arguments = !strcmp(ident->string, "arguments");
  166. int is_eval = !strcmp(ident->string, "eval");
  167. int i;
  168. if (is_arguments) {
  169. F->lightweight = 0;
  170. F->arguments = 1;
  171. }
  172. checkfutureword(J, F, ident);
  173. if (F->strict && oploc == OP_SETLOCAL) {
  174. if (is_arguments)
  175. jsC_error(J, ident, "'arguments' is read-only in strict mode");
  176. if (is_eval)
  177. jsC_error(J, ident, "'eval' is read-only in strict mode");
  178. }
  179. if (is_eval)
  180. js_evalerror(J, "%s:%d: invalid use of 'eval'", J->filename, ident->line);
  181. i = findlocal(J, F, ident->string);
  182. if (i < 0) {
  183. emitstring(J, F, opvar, ident->string);
  184. } else {
  185. emit(J, F, oploc);
  186. emitarg(J, F, i);
  187. }
  188. }
  189. static int here(JF)
  190. {
  191. return F->codelen;
  192. }
  193. static int emitjump(JF, int opcode)
  194. {
  195. int inst;
  196. emit(J, F, opcode);
  197. inst = F->codelen;
  198. emitarg(J, F, 0);
  199. return inst;
  200. }
  201. static void emitjumpto(JF, int opcode, int dest)
  202. {
  203. emit(J, F, opcode);
  204. if (dest != (js_Instruction)dest)
  205. js_syntaxerror(J, "jump address integer overflow");
  206. emitarg(J, F, dest);
  207. }
  208. static void labelto(JF, int inst, int addr)
  209. {
  210. if (addr != (js_Instruction)addr)
  211. js_syntaxerror(J, "jump address integer overflow");
  212. F->code[inst] = addr;
  213. }
  214. static void label(JF, int inst)
  215. {
  216. labelto(J, F, inst, F->codelen);
  217. }
  218. /* Expressions */
  219. static void ctypeof(JF, js_Ast *exp)
  220. {
  221. if (exp->a->type == EXP_IDENTIFIER) {
  222. emitline(J, F, exp->a);
  223. emitlocal(J, F, OP_GETLOCAL, OP_HASVAR, exp->a);
  224. } else {
  225. cexp(J, F, exp->a);
  226. }
  227. emitline(J, F, exp);
  228. emit(J, F, OP_TYPEOF);
  229. }
  230. static void cunary(JF, js_Ast *exp, int opcode)
  231. {
  232. cexp(J, F, exp->a);
  233. emitline(J, F, exp);
  234. emit(J, F, opcode);
  235. }
  236. static void cbinary(JF, js_Ast *exp, int opcode)
  237. {
  238. cexp(J, F, exp->a);
  239. cexp(J, F, exp->b);
  240. emitline(J, F, exp);
  241. emit(J, F, opcode);
  242. }
  243. static void carray(JF, js_Ast *list)
  244. {
  245. while (list) {
  246. emitline(J, F, list->a);
  247. if (list->a->type == EXP_ELISION) {
  248. emit(J, F, OP_SKIPARRAY);
  249. } else {
  250. cexp(J, F, list->a);
  251. emit(J, F, OP_INITARRAY);
  252. }
  253. list = list->b;
  254. }
  255. }
  256. static void checkdup(JF, js_Ast *list, js_Ast *end)
  257. {
  258. char nbuf[32], sbuf[32];
  259. const char *needle, *straw;
  260. if (end->a->type == EXP_NUMBER)
  261. needle = jsV_numbertostring(J, nbuf, end->a->number);
  262. else
  263. needle = end->a->string;
  264. while (list->a != end) {
  265. if (list->a->type == end->type) {
  266. js_Ast *prop = list->a->a;
  267. if (prop->type == EXP_NUMBER)
  268. straw = jsV_numbertostring(J, sbuf, prop->number);
  269. else
  270. straw = prop->string;
  271. if (!strcmp(needle, straw))
  272. jsC_error(J, list, "duplicate property '%s' in object literal", needle);
  273. }
  274. list = list->b;
  275. }
  276. }
  277. static void cobject(JF, js_Ast *list)
  278. {
  279. js_Ast *head = list;
  280. while (list) {
  281. js_Ast *kv = list->a;
  282. js_Ast *prop = kv->a;
  283. if (prop->type == AST_IDENTIFIER || prop->type == EXP_STRING) {
  284. emitline(J, F, prop);
  285. emitstring(J, F, OP_STRING, prop->string);
  286. } else if (prop->type == EXP_NUMBER) {
  287. emitline(J, F, prop);
  288. emitnumber(J, F, prop->number);
  289. } else {
  290. jsC_error(J, prop, "invalid property name in object initializer");
  291. }
  292. if (F->strict)
  293. checkdup(J, F, head, kv);
  294. switch (kv->type) {
  295. default: /* impossible */ break;
  296. case EXP_PROP_VAL:
  297. cexp(J, F, kv->b);
  298. emitline(J, F, kv);
  299. emit(J, F, OP_INITPROP);
  300. break;
  301. case EXP_PROP_GET:
  302. emitfunction(J, F, newfun(J, prop->line, NULL, NULL, kv->c, 0, F->strict, 1));
  303. emitline(J, F, kv);
  304. emit(J, F, OP_INITGETTER);
  305. break;
  306. case EXP_PROP_SET:
  307. emitfunction(J, F, newfun(J, prop->line, NULL, kv->b, kv->c, 0, F->strict, 1));
  308. emitline(J, F, kv);
  309. emit(J, F, OP_INITSETTER);
  310. break;
  311. }
  312. list = list->b;
  313. }
  314. }
  315. static int cargs(JF, js_Ast *list)
  316. {
  317. int n = 0;
  318. while (list) {
  319. cexp(J, F, list->a);
  320. list = list->b;
  321. ++n;
  322. }
  323. return n;
  324. }
  325. static void cassign(JF, js_Ast *exp)
  326. {
  327. js_Ast *lhs = exp->a;
  328. js_Ast *rhs = exp->b;
  329. switch (lhs->type) {
  330. case EXP_IDENTIFIER:
  331. cexp(J, F, rhs);
  332. emitline(J, F, exp);
  333. emitlocal(J, F, OP_SETLOCAL, OP_SETVAR, lhs);
  334. break;
  335. case EXP_INDEX:
  336. cexp(J, F, lhs->a);
  337. cexp(J, F, lhs->b);
  338. cexp(J, F, rhs);
  339. emitline(J, F, exp);
  340. emit(J, F, OP_SETPROP);
  341. break;
  342. case EXP_MEMBER:
  343. cexp(J, F, lhs->a);
  344. cexp(J, F, rhs);
  345. emitline(J, F, exp);
  346. emitstring(J, F, OP_SETPROP_S, lhs->b->string);
  347. break;
  348. default:
  349. jsC_error(J, lhs, "invalid l-value in assignment");
  350. }
  351. }
  352. static void cassignforin(JF, js_Ast *stm)
  353. {
  354. js_Ast *lhs = stm->a;
  355. if (stm->type == STM_FOR_IN_VAR) {
  356. if (lhs->b)
  357. jsC_error(J, lhs->b, "more than one loop variable in for-in statement");
  358. emitline(J, F, lhs->a);
  359. emitlocal(J, F, OP_SETLOCAL, OP_SETVAR, lhs->a->a); /* list(var-init(ident)) */
  360. emit(J, F, OP_POP);
  361. return;
  362. }
  363. switch (lhs->type) {
  364. case EXP_IDENTIFIER:
  365. emitline(J, F, lhs);
  366. emitlocal(J, F, OP_SETLOCAL, OP_SETVAR, lhs);
  367. emit(J, F, OP_POP);
  368. break;
  369. case EXP_INDEX:
  370. cexp(J, F, lhs->a);
  371. cexp(J, F, lhs->b);
  372. emitline(J, F, lhs);
  373. emit(J, F, OP_ROT3);
  374. emit(J, F, OP_SETPROP);
  375. emit(J, F, OP_POP);
  376. break;
  377. case EXP_MEMBER:
  378. cexp(J, F, lhs->a);
  379. emitline(J, F, lhs);
  380. emit(J, F, OP_ROT2);
  381. emitstring(J, F, OP_SETPROP_S, lhs->b->string);
  382. emit(J, F, OP_POP);
  383. break;
  384. default:
  385. jsC_error(J, lhs, "invalid l-value in for-in loop assignment");
  386. }
  387. }
  388. static void cassignop1(JF, js_Ast *lhs)
  389. {
  390. switch (lhs->type) {
  391. case EXP_IDENTIFIER:
  392. emitline(J, F, lhs);
  393. emitlocal(J, F, OP_GETLOCAL, OP_GETVAR, lhs);
  394. break;
  395. case EXP_INDEX:
  396. cexp(J, F, lhs->a);
  397. cexp(J, F, lhs->b);
  398. emitline(J, F, lhs);
  399. emit(J, F, OP_DUP2);
  400. emit(J, F, OP_GETPROP);
  401. break;
  402. case EXP_MEMBER:
  403. cexp(J, F, lhs->a);
  404. emitline(J, F, lhs);
  405. emit(J, F, OP_DUP);
  406. emitstring(J, F, OP_GETPROP_S, lhs->b->string);
  407. break;
  408. default:
  409. jsC_error(J, lhs, "invalid l-value in assignment");
  410. }
  411. }
  412. static void cassignop2(JF, js_Ast *lhs, int postfix)
  413. {
  414. switch (lhs->type) {
  415. case EXP_IDENTIFIER:
  416. emitline(J, F, lhs);
  417. if (postfix) emit(J, F, OP_ROT2);
  418. emitlocal(J, F, OP_SETLOCAL, OP_SETVAR, lhs);
  419. break;
  420. case EXP_INDEX:
  421. emitline(J, F, lhs);
  422. if (postfix) emit(J, F, OP_ROT4);
  423. emit(J, F, OP_SETPROP);
  424. break;
  425. case EXP_MEMBER:
  426. emitline(J, F, lhs);
  427. if (postfix) emit(J, F, OP_ROT3);
  428. emitstring(J, F, OP_SETPROP_S, lhs->b->string);
  429. break;
  430. default:
  431. jsC_error(J, lhs, "invalid l-value in assignment");
  432. }
  433. }
  434. static void cassignop(JF, js_Ast *exp, int opcode)
  435. {
  436. js_Ast *lhs = exp->a;
  437. js_Ast *rhs = exp->b;
  438. cassignop1(J, F, lhs);
  439. cexp(J, F, rhs);
  440. emitline(J, F, exp);
  441. emit(J, F, opcode);
  442. cassignop2(J, F, lhs, 0);
  443. }
  444. static void cdelete(JF, js_Ast *exp)
  445. {
  446. js_Ast *arg = exp->a;
  447. switch (arg->type) {
  448. case EXP_IDENTIFIER:
  449. if (F->strict)
  450. jsC_error(J, exp, "delete on an unqualified name is not allowed in strict mode");
  451. emitline(J, F, exp);
  452. emitlocal(J, F, OP_DELLOCAL, OP_DELVAR, arg);
  453. break;
  454. case EXP_INDEX:
  455. cexp(J, F, arg->a);
  456. cexp(J, F, arg->b);
  457. emitline(J, F, exp);
  458. emit(J, F, OP_DELPROP);
  459. break;
  460. case EXP_MEMBER:
  461. cexp(J, F, arg->a);
  462. emitline(J, F, exp);
  463. emitstring(J, F, OP_DELPROP_S, arg->b->string);
  464. break;
  465. default:
  466. jsC_error(J, exp, "invalid l-value in delete expression");
  467. }
  468. }
  469. static void ceval(JF, js_Ast *fun, js_Ast *args)
  470. {
  471. int n = cargs(J, F, args);
  472. F->lightweight = 0;
  473. F->arguments = 1;
  474. if (n == 0)
  475. emit(J, F, OP_UNDEF);
  476. else while (n-- > 1)
  477. emit(J, F, OP_POP);
  478. emit(J, F, OP_EVAL);
  479. }
  480. static void ccall(JF, js_Ast *fun, js_Ast *args)
  481. {
  482. int n;
  483. switch (fun->type) {
  484. case EXP_INDEX:
  485. cexp(J, F, fun->a);
  486. emit(J, F, OP_DUP);
  487. cexp(J, F, fun->b);
  488. emit(J, F, OP_GETPROP);
  489. emit(J, F, OP_ROT2);
  490. break;
  491. case EXP_MEMBER:
  492. cexp(J, F, fun->a);
  493. emit(J, F, OP_DUP);
  494. emitstring(J, F, OP_GETPROP_S, fun->b->string);
  495. emit(J, F, OP_ROT2);
  496. break;
  497. case EXP_IDENTIFIER:
  498. if (!strcmp(fun->string, "eval")) {
  499. ceval(J, F, fun, args);
  500. return;
  501. }
  502. /* fallthrough */
  503. default:
  504. cexp(J, F, fun);
  505. emit(J, F, OP_UNDEF);
  506. break;
  507. }
  508. n = cargs(J, F, args);
  509. emit(J, F, OP_CALL);
  510. emitarg(J, F, n);
  511. }
  512. static void cexp(JF, js_Ast *exp)
  513. {
  514. int then, end;
  515. int n;
  516. switch (exp->type) {
  517. case EXP_STRING:
  518. emitline(J, F, exp);
  519. emitstring(J, F, OP_STRING, exp->string);
  520. break;
  521. case EXP_NUMBER:
  522. emitline(J, F, exp);
  523. emitnumber(J, F, exp->number);
  524. break;
  525. case EXP_ELISION:
  526. break;
  527. case EXP_NULL:
  528. emitline(J, F, exp);
  529. emit(J, F, OP_NULL);
  530. break;
  531. case EXP_TRUE:
  532. emitline(J, F, exp);
  533. emit(J, F, OP_TRUE);
  534. break;
  535. case EXP_FALSE:
  536. emitline(J, F, exp);
  537. emit(J, F, OP_FALSE);
  538. break;
  539. case EXP_THIS:
  540. emitline(J, F, exp);
  541. emit(J, F, OP_THIS);
  542. break;
  543. case EXP_REGEXP:
  544. emitline(J, F, exp);
  545. emitstring(J, F, OP_NEWREGEXP, exp->string);
  546. emitarg(J, F, exp->number);
  547. break;
  548. case EXP_OBJECT:
  549. emitline(J, F, exp);
  550. emit(J, F, OP_NEWOBJECT);
  551. cobject(J, F, exp->a);
  552. break;
  553. case EXP_ARRAY:
  554. emitline(J, F, exp);
  555. emit(J, F, OP_NEWARRAY);
  556. carray(J, F, exp->a);
  557. break;
  558. case EXP_FUN:
  559. emitline(J, F, exp);
  560. emitfunction(J, F, newfun(J, exp->line, exp->a, exp->b, exp->c, 0, F->strict, 1));
  561. break;
  562. case EXP_IDENTIFIER:
  563. emitline(J, F, exp);
  564. emitlocal(J, F, OP_GETLOCAL, OP_GETVAR, exp);
  565. break;
  566. case EXP_INDEX:
  567. cexp(J, F, exp->a);
  568. cexp(J, F, exp->b);
  569. emitline(J, F, exp);
  570. emit(J, F, OP_GETPROP);
  571. break;
  572. case EXP_MEMBER:
  573. cexp(J, F, exp->a);
  574. emitline(J, F, exp);
  575. emitstring(J, F, OP_GETPROP_S, exp->b->string);
  576. break;
  577. case EXP_CALL:
  578. ccall(J, F, exp->a, exp->b);
  579. break;
  580. case EXP_NEW:
  581. cexp(J, F, exp->a);
  582. n = cargs(J, F, exp->b);
  583. emitline(J, F, exp);
  584. emit(J, F, OP_NEW);
  585. emitarg(J, F, n);
  586. break;
  587. case EXP_DELETE:
  588. cdelete(J, F, exp);
  589. break;
  590. case EXP_PREINC:
  591. cassignop1(J, F, exp->a);
  592. emitline(J, F, exp);
  593. emit(J, F, OP_INC);
  594. cassignop2(J, F, exp->a, 0);
  595. break;
  596. case EXP_PREDEC:
  597. cassignop1(J, F, exp->a);
  598. emitline(J, F, exp);
  599. emit(J, F, OP_DEC);
  600. cassignop2(J, F, exp->a, 0);
  601. break;
  602. case EXP_POSTINC:
  603. cassignop1(J, F, exp->a);
  604. emitline(J, F, exp);
  605. emit(J, F, OP_POSTINC);
  606. cassignop2(J, F, exp->a, 1);
  607. emit(J, F, OP_POP);
  608. break;
  609. case EXP_POSTDEC:
  610. cassignop1(J, F, exp->a);
  611. emitline(J, F, exp);
  612. emit(J, F, OP_POSTDEC);
  613. cassignop2(J, F, exp->a, 1);
  614. emit(J, F, OP_POP);
  615. break;
  616. case EXP_VOID:
  617. cexp(J, F, exp->a);
  618. emitline(J, F, exp);
  619. emit(J, F, OP_POP);
  620. emit(J, F, OP_UNDEF);
  621. break;
  622. case EXP_TYPEOF: ctypeof(J, F, exp); break;
  623. case EXP_POS: cunary(J, F, exp, OP_POS); break;
  624. case EXP_NEG: cunary(J, F, exp, OP_NEG); break;
  625. case EXP_BITNOT: cunary(J, F, exp, OP_BITNOT); break;
  626. case EXP_LOGNOT: cunary(J, F, exp, OP_LOGNOT); break;
  627. case EXP_BITOR: cbinary(J, F, exp, OP_BITOR); break;
  628. case EXP_BITXOR: cbinary(J, F, exp, OP_BITXOR); break;
  629. case EXP_BITAND: cbinary(J, F, exp, OP_BITAND); break;
  630. case EXP_EQ: cbinary(J, F, exp, OP_EQ); break;
  631. case EXP_NE: cbinary(J, F, exp, OP_NE); break;
  632. case EXP_STRICTEQ: cbinary(J, F, exp, OP_STRICTEQ); break;
  633. case EXP_STRICTNE: cbinary(J, F, exp, OP_STRICTNE); break;
  634. case EXP_LT: cbinary(J, F, exp, OP_LT); break;
  635. case EXP_GT: cbinary(J, F, exp, OP_GT); break;
  636. case EXP_LE: cbinary(J, F, exp, OP_LE); break;
  637. case EXP_GE: cbinary(J, F, exp, OP_GE); break;
  638. case EXP_INSTANCEOF: cbinary(J, F, exp, OP_INSTANCEOF); break;
  639. case EXP_IN: cbinary(J, F, exp, OP_IN); break;
  640. case EXP_SHL: cbinary(J, F, exp, OP_SHL); break;
  641. case EXP_SHR: cbinary(J, F, exp, OP_SHR); break;
  642. case EXP_USHR: cbinary(J, F, exp, OP_USHR); break;
  643. case EXP_ADD: cbinary(J, F, exp, OP_ADD); break;
  644. case EXP_SUB: cbinary(J, F, exp, OP_SUB); break;
  645. case EXP_MUL: cbinary(J, F, exp, OP_MUL); break;
  646. case EXP_DIV: cbinary(J, F, exp, OP_DIV); break;
  647. case EXP_MOD: cbinary(J, F, exp, OP_MOD); break;
  648. case EXP_ASS: cassign(J, F, exp); break;
  649. case EXP_ASS_MUL: cassignop(J, F, exp, OP_MUL); break;
  650. case EXP_ASS_DIV: cassignop(J, F, exp, OP_DIV); break;
  651. case EXP_ASS_MOD: cassignop(J, F, exp, OP_MOD); break;
  652. case EXP_ASS_ADD: cassignop(J, F, exp, OP_ADD); break;
  653. case EXP_ASS_SUB: cassignop(J, F, exp, OP_SUB); break;
  654. case EXP_ASS_SHL: cassignop(J, F, exp, OP_SHL); break;
  655. case EXP_ASS_SHR: cassignop(J, F, exp, OP_SHR); break;
  656. case EXP_ASS_USHR: cassignop(J, F, exp, OP_USHR); break;
  657. case EXP_ASS_BITAND: cassignop(J, F, exp, OP_BITAND); break;
  658. case EXP_ASS_BITXOR: cassignop(J, F, exp, OP_BITXOR); break;
  659. case EXP_ASS_BITOR: cassignop(J, F, exp, OP_BITOR); break;
  660. case EXP_COMMA:
  661. cexp(J, F, exp->a);
  662. emitline(J, F, exp);
  663. emit(J, F, OP_POP);
  664. cexp(J, F, exp->b);
  665. break;
  666. case EXP_LOGOR:
  667. cexp(J, F, exp->a);
  668. emitline(J, F, exp);
  669. emit(J, F, OP_DUP);
  670. end = emitjump(J, F, OP_JTRUE);
  671. emit(J, F, OP_POP);
  672. cexp(J, F, exp->b);
  673. label(J, F, end);
  674. break;
  675. case EXP_LOGAND:
  676. cexp(J, F, exp->a);
  677. emitline(J, F, exp);
  678. emit(J, F, OP_DUP);
  679. end = emitjump(J, F, OP_JFALSE);
  680. emit(J, F, OP_POP);
  681. cexp(J, F, exp->b);
  682. label(J, F, end);
  683. break;
  684. case EXP_COND:
  685. cexp(J, F, exp->a);
  686. emitline(J, F, exp);
  687. then = emitjump(J, F, OP_JTRUE);
  688. cexp(J, F, exp->c);
  689. end = emitjump(J, F, OP_JUMP);
  690. label(J, F, then);
  691. cexp(J, F, exp->b);
  692. label(J, F, end);
  693. break;
  694. default:
  695. jsC_error(J, exp, "unknown expression type");
  696. }
  697. }
  698. /* Patch break and continue statements */
  699. static void addjump(JF, enum js_AstType type, js_Ast *target, int inst)
  700. {
  701. js_JumpList *jump = js_malloc(J, sizeof *jump);
  702. jump->type = type;
  703. jump->inst = inst;
  704. jump->next = target->jumps;
  705. target->jumps = jump;
  706. }
  707. static void labeljumps(JF, js_Ast *stm, int baddr, int caddr)
  708. {
  709. js_JumpList *jump = stm->jumps;
  710. while (jump) {
  711. js_JumpList *next = jump->next;
  712. if (jump->type == STM_BREAK)
  713. labelto(J, F, jump->inst, baddr);
  714. if (jump->type == STM_CONTINUE)
  715. labelto(J, F, jump->inst, caddr);
  716. js_free(J, jump);
  717. stm->jumps = jump = next;
  718. }
  719. }
  720. static int isloop(enum js_AstType T)
  721. {
  722. return T == STM_DO || T == STM_WHILE ||
  723. T == STM_FOR || T == STM_FOR_VAR ||
  724. T == STM_FOR_IN || T == STM_FOR_IN_VAR;
  725. }
  726. static int isfun(enum js_AstType T)
  727. {
  728. return T == AST_FUNDEC || T == EXP_FUN || T == EXP_PROP_GET || T == EXP_PROP_SET;
  729. }
  730. static int matchlabel(js_Ast *node, const char *label)
  731. {
  732. while (node && node->type == STM_LABEL) {
  733. if (!strcmp(node->a->string, label))
  734. return 1;
  735. node = node->parent;
  736. }
  737. return 0;
  738. }
  739. static js_Ast *breaktarget(JF, js_Ast *node, const char *label)
  740. {
  741. while (node) {
  742. if (isfun(node->type))
  743. break;
  744. if (!label) {
  745. if (isloop(node->type) || node->type == STM_SWITCH)
  746. return node;
  747. } else {
  748. if (matchlabel(node->parent, label))
  749. return node;
  750. }
  751. node = node->parent;
  752. }
  753. return NULL;
  754. }
  755. static js_Ast *continuetarget(JF, js_Ast *node, const char *label)
  756. {
  757. while (node) {
  758. if (isfun(node->type))
  759. break;
  760. if (isloop(node->type)) {
  761. if (!label)
  762. return node;
  763. else if (matchlabel(node->parent, label))
  764. return node;
  765. }
  766. node = node->parent;
  767. }
  768. return NULL;
  769. }
  770. static js_Ast *returntarget(JF, js_Ast *node)
  771. {
  772. while (node) {
  773. if (isfun(node->type))
  774. return node;
  775. node = node->parent;
  776. }
  777. return NULL;
  778. }
  779. /* Emit code to rebalance stack and scopes during an abrupt exit */
  780. static void cexit(JF, enum js_AstType T, js_Ast *node, js_Ast *target)
  781. {
  782. js_Ast *prev;
  783. do {
  784. prev = node, node = node->parent;
  785. switch (node->type) {
  786. default:
  787. /* impossible */
  788. break;
  789. case STM_WITH:
  790. emitline(J, F, node);
  791. emit(J, F, OP_ENDWITH);
  792. break;
  793. case STM_FOR_IN:
  794. case STM_FOR_IN_VAR:
  795. emitline(J, F, node);
  796. /* pop the iterator if leaving the loop */
  797. if (F->script) {
  798. if (T == STM_RETURN || T == STM_BREAK || (T == STM_CONTINUE && target != node)) {
  799. /* pop the iterator, save the return or exp value */
  800. emit(J, F, OP_ROT2);
  801. emit(J, F, OP_POP);
  802. }
  803. if (T == STM_CONTINUE)
  804. emit(J, F, OP_ROT2); /* put the iterator back on top */
  805. } else {
  806. if (T == STM_RETURN) {
  807. /* pop the iterator, save the return value */
  808. emit(J, F, OP_ROT2);
  809. emit(J, F, OP_POP);
  810. }
  811. if (T == STM_BREAK || (T == STM_CONTINUE && target != node))
  812. emit(J, F, OP_POP); /* pop the iterator */
  813. }
  814. break;
  815. case STM_TRY:
  816. emitline(J, F, node);
  817. /* came from try block */
  818. if (prev == node->a) {
  819. emit(J, F, OP_ENDTRY);
  820. if (node->d) cstm(J, F, node->d); /* finally */
  821. }
  822. /* came from catch block */
  823. if (prev == node->c) {
  824. /* ... with finally */
  825. if (node->d) {
  826. emit(J, F, OP_ENDCATCH);
  827. emit(J, F, OP_ENDTRY);
  828. cstm(J, F, node->d); /* finally */
  829. } else {
  830. emit(J, F, OP_ENDCATCH);
  831. }
  832. }
  833. break;
  834. }
  835. } while (node != target);
  836. }
  837. /* Try/catch/finally */
  838. static void ctryfinally(JF, js_Ast *trystm, js_Ast *finallystm)
  839. {
  840. int L1;
  841. L1 = emitjump(J, F, OP_TRY);
  842. {
  843. /* if we get here, we have caught an exception in the try block */
  844. cstm(J, F, finallystm); /* inline finally block */
  845. emit(J, F, OP_THROW); /* rethrow exception */
  846. }
  847. label(J, F, L1);
  848. cstm(J, F, trystm);
  849. emit(J, F, OP_ENDTRY);
  850. cstm(J, F, finallystm);
  851. }
  852. static void ctrycatch(JF, js_Ast *trystm, js_Ast *catchvar, js_Ast *catchstm)
  853. {
  854. int L1, L2;
  855. L1 = emitjump(J, F, OP_TRY);
  856. {
  857. /* if we get here, we have caught an exception in the try block */
  858. checkfutureword(J, F, catchvar);
  859. if (F->strict) {
  860. if (!strcmp(catchvar->string, "arguments"))
  861. jsC_error(J, catchvar, "redefining 'arguments' is not allowed in strict mode");
  862. if (!strcmp(catchvar->string, "eval"))
  863. jsC_error(J, catchvar, "redefining 'eval' is not allowed in strict mode");
  864. }
  865. emitline(J, F, catchvar);
  866. emitstring(J, F, OP_CATCH, catchvar->string);
  867. cstm(J, F, catchstm);
  868. emit(J, F, OP_ENDCATCH);
  869. L2 = emitjump(J, F, OP_JUMP); /* skip past the try block */
  870. }
  871. label(J, F, L1);
  872. cstm(J, F, trystm);
  873. emit(J, F, OP_ENDTRY);
  874. label(J, F, L2);
  875. }
  876. static void ctrycatchfinally(JF, js_Ast *trystm, js_Ast *catchvar, js_Ast *catchstm, js_Ast *finallystm)
  877. {
  878. int L1, L2, L3;
  879. L1 = emitjump(J, F, OP_TRY);
  880. {
  881. /* if we get here, we have caught an exception in the try block */
  882. L2 = emitjump(J, F, OP_TRY);
  883. {
  884. /* if we get here, we have caught an exception in the catch block */
  885. cstm(J, F, finallystm); /* inline finally block */
  886. emit(J, F, OP_THROW); /* rethrow exception */
  887. }
  888. label(J, F, L2);
  889. if (F->strict) {
  890. checkfutureword(J, F, catchvar);
  891. if (!strcmp(catchvar->string, "arguments"))
  892. jsC_error(J, catchvar, "redefining 'arguments' is not allowed in strict mode");
  893. if (!strcmp(catchvar->string, "eval"))
  894. jsC_error(J, catchvar, "redefining 'eval' is not allowed in strict mode");
  895. }
  896. emitline(J, F, catchvar);
  897. emitstring(J, F, OP_CATCH, catchvar->string);
  898. cstm(J, F, catchstm);
  899. emit(J, F, OP_ENDCATCH);
  900. emit(J, F, OP_ENDTRY);
  901. L3 = emitjump(J, F, OP_JUMP); /* skip past the try block to the finally block */
  902. }
  903. label(J, F, L1);
  904. cstm(J, F, trystm);
  905. emit(J, F, OP_ENDTRY);
  906. label(J, F, L3);
  907. cstm(J, F, finallystm);
  908. }
  909. /* Switch */
  910. static void cswitch(JF, js_Ast *ref, js_Ast *head)
  911. {
  912. js_Ast *node, *clause, *def = NULL;
  913. int end;
  914. cexp(J, F, ref);
  915. /* emit an if-else chain of tests for the case clause expressions */
  916. for (node = head; node; node = node->b) {
  917. clause = node->a;
  918. if (clause->type == STM_DEFAULT) {
  919. if (def)
  920. jsC_error(J, clause, "more than one default label in switch");
  921. def = clause;
  922. } else {
  923. cexp(J, F, clause->a);
  924. emitline(J, F, clause);
  925. clause->casejump = emitjump(J, F, OP_JCASE);
  926. }
  927. }
  928. emit(J, F, OP_POP);
  929. if (def) {
  930. emitline(J, F, def);
  931. def->casejump = emitjump(J, F, OP_JUMP);
  932. end = 0;
  933. } else {
  934. end = emitjump(J, F, OP_JUMP);
  935. }
  936. /* emit the case clause bodies */
  937. for (node = head; node; node = node->b) {
  938. clause = node->a;
  939. label(J, F, clause->casejump);
  940. if (clause->type == STM_DEFAULT)
  941. cstmlist(J, F, clause->a);
  942. else
  943. cstmlist(J, F, clause->b);
  944. }
  945. if (end)
  946. label(J, F, end);
  947. }
  948. /* Statements */
  949. static void cvarinit(JF, js_Ast *list)
  950. {
  951. while (list) {
  952. js_Ast *var = list->a;
  953. if (var->b) {
  954. cexp(J, F, var->b);
  955. emitline(J, F, var);
  956. emitlocal(J, F, OP_SETLOCAL, OP_SETVAR, var->a);
  957. emit(J, F, OP_POP);
  958. }
  959. list = list->b;
  960. }
  961. }
  962. static void cstm(JF, js_Ast *stm)
  963. {
  964. js_Ast *target;
  965. int loop, cont, then, end;
  966. emitline(J, F, stm);
  967. switch (stm->type) {
  968. case AST_FUNDEC:
  969. break;
  970. case STM_BLOCK:
  971. cstmlist(J, F, stm->a);
  972. break;
  973. case STM_EMPTY:
  974. if (F->script) {
  975. emitline(J, F, stm);
  976. emit(J, F, OP_POP);
  977. emit(J, F, OP_UNDEF);
  978. }
  979. break;
  980. case STM_VAR:
  981. cvarinit(J, F, stm->a);
  982. break;
  983. case STM_IF:
  984. if (stm->c) {
  985. cexp(J, F, stm->a);
  986. emitline(J, F, stm);
  987. then = emitjump(J, F, OP_JTRUE);
  988. cstm(J, F, stm->c);
  989. emitline(J, F, stm);
  990. end = emitjump(J, F, OP_JUMP);
  991. label(J, F, then);
  992. cstm(J, F, stm->b);
  993. label(J, F, end);
  994. } else {
  995. cexp(J, F, stm->a);
  996. emitline(J, F, stm);
  997. end = emitjump(J, F, OP_JFALSE);
  998. cstm(J, F, stm->b);
  999. label(J, F, end);
  1000. }
  1001. break;
  1002. case STM_DO:
  1003. loop = here(J, F);
  1004. cstm(J, F, stm->a);
  1005. cont = here(J, F);
  1006. cexp(J, F, stm->b);
  1007. emitline(J, F, stm);
  1008. emitjumpto(J, F, OP_JTRUE, loop);
  1009. labeljumps(J, F, stm, here(J,F), cont);
  1010. break;
  1011. case STM_WHILE:
  1012. loop = here(J, F);
  1013. cexp(J, F, stm->a);
  1014. emitline(J, F, stm);
  1015. end = emitjump(J, F, OP_JFALSE);
  1016. cstm(J, F, stm->b);
  1017. emitline(J, F, stm);
  1018. emitjumpto(J, F, OP_JUMP, loop);
  1019. label(J, F, end);
  1020. labeljumps(J, F, stm, here(J,F), loop);
  1021. break;
  1022. case STM_FOR:
  1023. case STM_FOR_VAR:
  1024. if (stm->type == STM_FOR_VAR) {
  1025. cvarinit(J, F, stm->a);
  1026. } else {
  1027. if (stm->a) {
  1028. cexp(J, F, stm->a);
  1029. emit(J, F, OP_POP);
  1030. }
  1031. }
  1032. loop = here(J, F);
  1033. if (stm->b) {
  1034. cexp(J, F, stm->b);
  1035. emitline(J, F, stm);
  1036. end = emitjump(J, F, OP_JFALSE);
  1037. } else {
  1038. end = 0;
  1039. }
  1040. cstm(J, F, stm->d);
  1041. cont = here(J, F);
  1042. if (stm->c) {
  1043. cexp(J, F, stm->c);
  1044. emit(J, F, OP_POP);
  1045. }
  1046. emitline(J, F, stm);
  1047. emitjumpto(J, F, OP_JUMP, loop);
  1048. if (end)
  1049. label(J, F, end);
  1050. labeljumps(J, F, stm, here(J,F), cont);
  1051. break;
  1052. case STM_FOR_IN:
  1053. case STM_FOR_IN_VAR:
  1054. cexp(J, F, stm->b);
  1055. emitline(J, F, stm);
  1056. emit(J, F, OP_ITERATOR);
  1057. loop = here(J, F);
  1058. {
  1059. emitline(J, F, stm);
  1060. emit(J, F, OP_NEXTITER);
  1061. end = emitjump(J, F, OP_JFALSE);
  1062. cassignforin(J, F, stm);
  1063. if (F->script) {
  1064. emit(J, F, OP_ROT2);
  1065. cstm(J, F, stm->c);
  1066. emit(J, F, OP_ROT2);
  1067. } else {
  1068. cstm(J, F, stm->c);
  1069. }
  1070. emitline(J, F, stm);
  1071. emitjumpto(J, F, OP_JUMP, loop);
  1072. }
  1073. label(J, F, end);
  1074. labeljumps(J, F, stm, here(J,F), loop);
  1075. break;
  1076. case STM_SWITCH:
  1077. cswitch(J, F, stm->a, stm->b);
  1078. labeljumps(J, F, stm, here(J,F), 0);
  1079. break;
  1080. case STM_LABEL:
  1081. cstm(J, F, stm->b);
  1082. /* skip consecutive labels */
  1083. while (stm->type == STM_LABEL)
  1084. stm = stm->b;
  1085. /* loops and switches have already been labelled */
  1086. if (!isloop(stm->type) && stm->type != STM_SWITCH)
  1087. labeljumps(J, F, stm, here(J,F), 0);
  1088. break;
  1089. case STM_BREAK:
  1090. if (stm->a) {
  1091. checkfutureword(J, F, stm->a);
  1092. target = breaktarget(J, F, stm->parent, stm->a->string);
  1093. if (!target)
  1094. jsC_error(J, stm, "break label '%s' not found", stm->a->string);
  1095. } else {
  1096. target = breaktarget(J, F, stm->parent, NULL);
  1097. if (!target)
  1098. jsC_error(J, stm, "unlabelled break must be inside loop or switch");
  1099. }
  1100. cexit(J, F, STM_BREAK, stm, target);
  1101. emitline(J, F, stm);
  1102. addjump(J, F, STM_BREAK, target, emitjump(J, F, OP_JUMP));
  1103. break;
  1104. case STM_CONTINUE:
  1105. if (stm->a) {
  1106. checkfutureword(J, F, stm->a);
  1107. target = continuetarget(J, F, stm->parent, stm->a->string);
  1108. if (!target)
  1109. jsC_error(J, stm, "continue label '%s' not found", stm->a->string);
  1110. } else {
  1111. target = continuetarget(J, F, stm->parent, NULL);
  1112. if (!target)
  1113. jsC_error(J, stm, "continue must be inside loop");
  1114. }
  1115. cexit(J, F, STM_CONTINUE, stm, target);
  1116. emitline(J, F, stm);
  1117. addjump(J, F, STM_CONTINUE, target, emitjump(J, F, OP_JUMP));
  1118. break;
  1119. case STM_RETURN:
  1120. if (stm->a)
  1121. cexp(J, F, stm->a);
  1122. else
  1123. emit(J, F, OP_UNDEF);
  1124. target = returntarget(J, F, stm->parent);
  1125. if (!target)
  1126. jsC_error(J, stm, "return not in function");
  1127. cexit(J, F, STM_RETURN, stm, target);
  1128. emitline(J, F, stm);
  1129. emit(J, F, OP_RETURN);
  1130. break;
  1131. case STM_THROW:
  1132. cexp(J, F, stm->a);
  1133. emitline(J, F, stm);
  1134. emit(J, F, OP_THROW);
  1135. break;
  1136. case STM_WITH:
  1137. F->lightweight = 0;
  1138. if (F->strict)
  1139. jsC_error(J, stm->a, "'with' statements are not allowed in strict mode");
  1140. cexp(J, F, stm->a);
  1141. emitline(J, F, stm);
  1142. emit(J, F, OP_WITH);
  1143. cstm(J, F, stm->b);
  1144. emitline(J, F, stm);
  1145. emit(J, F, OP_ENDWITH);
  1146. break;
  1147. case STM_TRY:
  1148. emitline(J, F, stm);
  1149. if (stm->b && stm->c) {
  1150. F->lightweight = 0;
  1151. if (stm->d)
  1152. ctrycatchfinally(J, F, stm->a, stm->b, stm->c, stm->d);
  1153. else
  1154. ctrycatch(J, F, stm->a, stm->b, stm->c);
  1155. } else {
  1156. ctryfinally(J, F, stm->a, stm->d);
  1157. }
  1158. break;
  1159. case STM_DEBUGGER:
  1160. emitline(J, F, stm);
  1161. emit(J, F, OP_DEBUGGER);
  1162. break;
  1163. default:
  1164. if (F->script) {
  1165. emitline(J, F, stm);
  1166. emit(J, F, OP_POP);
  1167. cexp(J, F, stm);
  1168. } else {
  1169. cexp(J, F, stm);
  1170. emitline(J, F, stm);
  1171. emit(J, F, OP_POP);
  1172. }
  1173. break;
  1174. }
  1175. }
  1176. static void cstmlist(JF, js_Ast *list)
  1177. {
  1178. while (list) {
  1179. cstm(J, F, list->a);
  1180. list = list->b;
  1181. }
  1182. }
  1183. /* Declarations and programs */
  1184. static int listlength(js_Ast *list)
  1185. {
  1186. int n = 0;
  1187. while (list) ++n, list = list->b;
  1188. return n;
  1189. }
  1190. static void cparams(JF, js_Ast *list, js_Ast *fname)
  1191. {
  1192. F->numparams = listlength(list);
  1193. while (list) {
  1194. checkfutureword(J, F, list->a);
  1195. addlocal(J, F, list->a, 0);
  1196. list = list->b;
  1197. }
  1198. }
  1199. static void cvardecs(JF, js_Ast *node)
  1200. {
  1201. if (node->type == AST_LIST) {
  1202. while (node) {
  1203. cvardecs(J, F, node->a);
  1204. node = node->b;
  1205. }
  1206. return;
  1207. }
  1208. if (isfun(node->type))
  1209. return; /* stop at inner functions */
  1210. if (node->type == EXP_VAR) {
  1211. checkfutureword(J, F, node->a);
  1212. addlocal(J, F, node->a, 1);
  1213. }
  1214. if (node->a) cvardecs(J, F, node->a);
  1215. if (node->b) cvardecs(J, F, node->b);
  1216. if (node->c) cvardecs(J, F, node->c);
  1217. if (node->d) cvardecs(J, F, node->d);
  1218. }
  1219. static void cfundecs(JF, js_Ast *list)
  1220. {
  1221. while (list) {
  1222. js_Ast *stm = list->a;
  1223. if (stm->type == AST_FUNDEC) {
  1224. emitline(J, F, stm);
  1225. emitfunction(J, F, newfun(J, stm->line, stm->a, stm->b, stm->c, 0, F->strict, 0));
  1226. emitline(J, F, stm);
  1227. emit(J, F, OP_SETLOCAL);
  1228. emitarg(J, F, addlocal(J, F, stm->a, 1));
  1229. emit(J, F, OP_POP);
  1230. }
  1231. list = list->b;
  1232. }
  1233. }
  1234. static void cfunbody(JF, js_Ast *name, js_Ast *params, js_Ast *body, int is_fun_exp)
  1235. {
  1236. F->lightweight = 1;
  1237. F->arguments = 0;
  1238. if (F->script)
  1239. F->lightweight = 0;
  1240. /* Check if first statement is 'use strict': */
  1241. if (body && body->type == AST_LIST && body->a && body->a->type == EXP_STRING)
  1242. if (!strcmp(body->a->string, "use strict"))
  1243. F->strict = 1;
  1244. F->lastline = F->line;
  1245. cparams(J, F, params, name);
  1246. if (body) {
  1247. cvardecs(J, F, body);
  1248. cfundecs(J, F, body);
  1249. }
  1250. if (name) {
  1251. checkfutureword(J, F, name);
  1252. if (is_fun_exp) {
  1253. if (findlocal(J, F, name->string) < 0) {
  1254. /* TODO: make this binding immutable! */
  1255. emit(J, F, OP_CURRENT);
  1256. emit(J, F, OP_SETLOCAL);
  1257. emitarg(J, F, addlocal(J, F, name, 1));
  1258. emit(J, F, OP_POP);
  1259. }
  1260. }
  1261. }
  1262. if (F->script) {
  1263. emit(J, F, OP_UNDEF);
  1264. cstmlist(J, F, body);
  1265. emit(J, F, OP_RETURN);
  1266. } else {
  1267. cstmlist(J, F, body);
  1268. emit(J, F, OP_UNDEF);
  1269. emit(J, F, OP_RETURN);
  1270. }
  1271. }
  1272. js_Function *jsC_compilefunction(js_State *J, js_Ast *prog)
  1273. {
  1274. return newfun(J, prog->line, prog->a, prog->b, prog->c, 0, J->default_strict, 1);
  1275. }
  1276. js_Function *jsC_compilescript(js_State *J, js_Ast *prog, int default_strict)
  1277. {
  1278. return newfun(J, prog ? prog->line : 0, NULL, NULL, prog, 1, default_strict, 0);
  1279. }