jsi.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #ifndef jsi_h
  2. #define jsi_h
  3. #include "mujs.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stddef.h>
  7. #include <stdarg.h>
  8. #include <string.h>
  9. #include <setjmp.h>
  10. #include <math.h>
  11. #include <float.h>
  12. #include <limits.h>
  13. /* NOTE: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103052 */
  14. #ifdef __GNUC__
  15. #if (__GNUC__ >= 6)
  16. #pragma GCC optimize ("no-ipa-pure-const")
  17. #endif
  18. #endif
  19. /* Microsoft Visual C */
  20. #ifdef _MSC_VER
  21. #pragma warning(disable:4996) /* _CRT_SECURE_NO_WARNINGS */
  22. #pragma warning(disable:4244) /* implicit conversion from double to int */
  23. #pragma warning(disable:4267) /* implicit conversion of int to smaller int */
  24. #pragma warning(disable:4090) /* broken const warnings */
  25. #define inline __inline
  26. #if _MSC_VER < 1900 /* MSVC 2015 */
  27. #define snprintf jsW_snprintf
  28. #define vsnprintf jsW_vsnprintf
  29. static int jsW_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
  30. {
  31. int n;
  32. n = _vsnprintf(str, size, fmt, ap);
  33. str[size-1] = 0;
  34. return n;
  35. }
  36. static int jsW_snprintf(char *str, size_t size, const char *fmt, ...)
  37. {
  38. int n;
  39. va_list ap;
  40. va_start(ap, fmt);
  41. n = jsW_vsnprintf(str, size, fmt, ap);
  42. va_end(ap);
  43. return n;
  44. }
  45. #endif
  46. #if _MSC_VER <= 1700 /* <= MSVC 2012 */
  47. #define isnan(x) _isnan(x)
  48. #define isinf(x) (!_finite(x))
  49. #define isfinite(x) _finite(x)
  50. static __inline int signbit(double x) { __int64 i; memcpy(&i, &x, 8); return i>>63; }
  51. #define INFINITY (DBL_MAX+DBL_MAX)
  52. #define NAN (INFINITY-INFINITY)
  53. #endif
  54. #endif
  55. #define soffsetof(x,y) ((int)offsetof(x,y))
  56. #define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
  57. void *js_malloc(js_State *J, int size);
  58. void *js_realloc(js_State *J, void *ptr, int size);
  59. void js_free(js_State *J, void *ptr);
  60. typedef union js_Value js_Value;
  61. typedef struct js_Regexp js_Regexp;
  62. typedef struct js_Object js_Object;
  63. typedef struct js_String js_String;
  64. typedef struct js_Ast js_Ast;
  65. typedef struct js_Function js_Function;
  66. typedef struct js_Environment js_Environment;
  67. typedef struct js_StringNode js_StringNode;
  68. typedef struct js_Jumpbuf js_Jumpbuf;
  69. typedef struct js_StackTrace js_StackTrace;
  70. /* Limits */
  71. #ifndef JS_STACKSIZE
  72. #define JS_STACKSIZE 4096 /* value stack size */
  73. #endif
  74. #ifndef JS_ENVLIMIT
  75. #define JS_ENVLIMIT 1024 /* environment stack size */
  76. #endif
  77. #ifndef JS_TRYLIMIT
  78. #define JS_TRYLIMIT 64 /* exception stack size */
  79. #endif
  80. #ifndef JS_ARRAYLIMIT
  81. #define JS_ARRAYLIMIT (1<<26) /* limit arrays to 64M entries (1G of flat array data) */
  82. #endif
  83. #ifndef JS_GCFACTOR
  84. /*
  85. * GC will try to trigger when memory usage is this value times the minimum
  86. * needed memory. E.g. if there are 100 remaining objects after GC and this
  87. * value is 5.0, then the next GC will trigger when the overall number is 500.
  88. * I.e. a value of 5.0 aims at 80% garbage, 20% remain-used on each GC.
  89. * The bigger the value the less impact GC has on overall performance, but more
  90. * memory is used and individual GC pauses are longer (but fewer).
  91. */
  92. #define JS_GCFACTOR 5.0 /* memory overhead factor >= 1.0 */
  93. #endif
  94. #ifndef JS_ASTLIMIT
  95. #define JS_ASTLIMIT 400 /* max nested expressions */
  96. #endif
  97. #ifndef JS_STRLIMIT
  98. #define JS_STRLIMIT (1<<28) /* max string length */
  99. #endif
  100. /* instruction size -- change to int if you get integer overflow syntax errors */
  101. #ifdef JS_INSTRUCTION
  102. typedef JS_INSTRUCTION js_Instruction;
  103. #else
  104. typedef unsigned short js_Instruction;
  105. #endif
  106. /* String interning */
  107. char *js_strdup(js_State *J, const char *s);
  108. const char *js_intern(js_State *J, const char *s);
  109. void jsS_dumpstrings(js_State *J);
  110. void jsS_freestrings(js_State *J);
  111. /* Portable strtod and printf float formatting */
  112. void js_fmtexp(char *p, int e);
  113. int js_grisu2(double v, char *buffer, int *K);
  114. double js_strtod(const char *as, char **aas);
  115. double js_strtol(const char *s, char **ep, int radix);
  116. /* Private stack functions */
  117. void js_newarguments(js_State *J);
  118. void js_newfunction(js_State *J, js_Function *function, js_Environment *scope);
  119. void js_newscript(js_State *J, js_Function *function, js_Environment *scope);
  120. void js_loadeval(js_State *J, const char *filename, const char *source);
  121. js_Regexp *js_toregexp(js_State *J, int idx);
  122. int js_isarrayindex(js_State *J, const char *str, int *idx);
  123. int js_runeat(js_State *J, const char *s, int i);
  124. int js_utflen(const char *s);
  125. int js_utfptrtoidx(const char *s, const char *p);
  126. void js_dup(js_State *J);
  127. void js_dup2(js_State *J);
  128. void js_rot2(js_State *J);
  129. void js_rot3(js_State *J);
  130. void js_rot4(js_State *J);
  131. void js_rot2pop1(js_State *J);
  132. void js_rot3pop2(js_State *J);
  133. void js_dup1rot3(js_State *J);
  134. void js_dup1rot4(js_State *J);
  135. void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
  136. void js_trap(js_State *J, int pc); /* dump stack and environment to stdout */
  137. struct js_StackTrace
  138. {
  139. const char *name;
  140. const char *file;
  141. int line;
  142. int stack;
  143. };
  144. /* Exception handling */
  145. struct js_Jumpbuf
  146. {
  147. jmp_buf buf;
  148. js_Environment *E;
  149. int envtop;
  150. int tracetop;
  151. int top, bot;
  152. int strict;
  153. js_Instruction *pc;
  154. };
  155. void *js_savetrypc(js_State *J, js_Instruction *pc);
  156. #define js_trypc(J, PC) \
  157. setjmp(js_savetrypc(J, PC))
  158. /* String buffer */
  159. typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
  160. void js_putc(js_State *J, js_Buffer **sbp, int c);
  161. void js_puts(js_State *J, js_Buffer **sb, const char *s);
  162. void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e);
  163. /* State struct */
  164. struct js_State
  165. {
  166. void *actx;
  167. void *uctx;
  168. js_Alloc alloc;
  169. js_Report report;
  170. js_Panic panic;
  171. js_StringNode *strings;
  172. int default_strict;
  173. int strict;
  174. /* parser input source */
  175. const char *filename;
  176. const char *source;
  177. int line;
  178. /* lexer state */
  179. struct { char *text; int len, cap; } lexbuf;
  180. int lexline;
  181. int lexchar;
  182. int lasttoken;
  183. int newline;
  184. /* '(' context stack: whether each open paren followed if/for/while/
  185. * with/switch/catch, so a regex literal can start the body statement. */
  186. int paren_control[64];
  187. int paren_top;
  188. int last_paren_control;
  189. /* parser state */
  190. int astdepth;
  191. int lookahead;
  192. const char *text;
  193. double number;
  194. js_Ast *gcast; /* list of allocated nodes to free after parsing */
  195. /* runtime environment */
  196. js_Object *Object_prototype;
  197. js_Object *Array_prototype;
  198. js_Object *Function_prototype;
  199. js_Object *Boolean_prototype;
  200. js_Object *Number_prototype;
  201. js_Object *String_prototype;
  202. js_Object *RegExp_prototype;
  203. js_Object *Date_prototype;
  204. js_Object *Error_prototype;
  205. js_Object *EvalError_prototype;
  206. js_Object *RangeError_prototype;
  207. js_Object *ReferenceError_prototype;
  208. js_Object *SyntaxError_prototype;
  209. js_Object *TypeError_prototype;
  210. js_Object *URIError_prototype;
  211. unsigned int seed; /* Math.random seed */
  212. char scratch[12]; /* scratch buffer for iterating over array indices */
  213. int nextref; /* for js_ref use */
  214. js_Object *R; /* registry of hidden values */
  215. js_Object *G; /* the global object */
  216. js_Environment *E; /* current environment scope */
  217. js_Environment *GE; /* global environment scope (at the root) */
  218. /* execution stack */
  219. int top, bot;
  220. js_Value *stack;
  221. /* garbage collector list */
  222. int gcmark;
  223. unsigned int gccounter, gcthresh;
  224. js_Environment *gcenv;
  225. js_Function *gcfun;
  226. js_Object *gcobj;
  227. js_String *gcstr;
  228. js_Object *gcroot; /* gc scan list */
  229. int runlimit;
  230. int memlimit;
  231. /* environments on the call stack but currently not in scope */
  232. int envtop;
  233. js_Environment *envstack[JS_ENVLIMIT];
  234. /* debug info stack trace */
  235. int tracetop;
  236. js_StackTrace trace[JS_ENVLIMIT];
  237. /* exception stack */
  238. int trytop;
  239. js_Jumpbuf trybuf[JS_TRYLIMIT];
  240. };
  241. /* Values */
  242. typedef struct js_Property js_Property;
  243. typedef struct js_Iterator js_Iterator;
  244. /* Hint to ToPrimitive() */
  245. enum {
  246. JS_HNONE,
  247. JS_HNUMBER,
  248. JS_HSTRING
  249. };
  250. enum js_Type {
  251. JS_TSHRSTR, /* type tag doubles as string zero-terminator */
  252. JS_TUNDEFINED,
  253. JS_TNULL,
  254. JS_TBOOLEAN,
  255. JS_TNUMBER,
  256. JS_TLITSTR,
  257. JS_TMEMSTR,
  258. JS_TOBJECT,
  259. };
  260. enum js_Class {
  261. JS_COBJECT,
  262. JS_CARRAY,
  263. JS_CFUNCTION,
  264. JS_CSCRIPT, /* function created from global/eval code */
  265. JS_CCFUNCTION, /* built-in function */
  266. JS_CERROR,
  267. JS_CBOOLEAN,
  268. JS_CNUMBER,
  269. JS_CSTRING,
  270. JS_CREGEXP,
  271. JS_CDATE,
  272. JS_CMATH,
  273. JS_CJSON,
  274. JS_CARGUMENTS,
  275. JS_CITERATOR,
  276. JS_CUSERDATA,
  277. };
  278. /*
  279. Short strings abuse the js_Value struct. By putting the type tag in the
  280. last byte, and using 0 as the tag for short strings, we can use the
  281. entire js_Value as string storage by letting the type tag serve double
  282. purpose as the string zero terminator.
  283. */
  284. union js_Value
  285. {
  286. struct {
  287. char pad[15];
  288. char type; /* type tag overlaps with final byte of shrstr */
  289. } t;
  290. union {
  291. char shrstr[16];
  292. int boolean;
  293. double number;
  294. const char *litstr;
  295. js_String *memstr;
  296. js_Object *object;
  297. } u;
  298. };
  299. struct js_String
  300. {
  301. js_String *gcnext;
  302. char gcmark;
  303. char p[1];
  304. };
  305. struct js_Regexp
  306. {
  307. void *prog;
  308. char *source;
  309. int flags;
  310. int last;
  311. };
  312. struct js_Object
  313. {
  314. enum js_Class type;
  315. int extensible;
  316. js_Property *properties;
  317. int count; /* number of properties, for array sparseness check */
  318. js_Object *prototype;
  319. union {
  320. int boolean;
  321. double number;
  322. struct {
  323. int length;
  324. char *string;
  325. char shrstr[16];
  326. } s;
  327. struct {
  328. int length; /* actual length */
  329. int simple; /* true if array has only non-sparse array properties */
  330. int flat_length; /* used length of simple array part */
  331. int flat_capacity; /* allocated length of simple array part */
  332. js_Value *array;
  333. } a;
  334. struct {
  335. js_Function *function;
  336. js_Environment *scope;
  337. } f;
  338. struct {
  339. const char *name;
  340. js_CFunction function;
  341. js_CFunction constructor;
  342. int length;
  343. void *data;
  344. js_Finalize finalize;
  345. } c;
  346. js_Regexp r;
  347. struct {
  348. js_Object *target;
  349. int i, n; /* for array part */
  350. js_Iterator *head, *current; /* for object part */
  351. } iter;
  352. struct {
  353. const char *tag;
  354. void *data;
  355. js_HasProperty has;
  356. js_Put put;
  357. js_Delete delete;
  358. js_Finalize finalize;
  359. } user;
  360. } u;
  361. js_Object *gcnext; /* allocation list */
  362. js_Object *gcroot; /* scan list */
  363. int gcmark;
  364. };
  365. struct js_Property
  366. {
  367. js_Property *left, *right;
  368. int level;
  369. int atts;
  370. js_Value value;
  371. js_Object *getter;
  372. js_Object *setter;
  373. char name[1];
  374. };
  375. struct js_Iterator
  376. {
  377. js_Iterator *next;
  378. char name[1];
  379. };
  380. struct js_Environment
  381. {
  382. js_Environment *outer;
  383. js_Object *variables;
  384. js_Environment *gcnext;
  385. int gcmark;
  386. };
  387. /* jsrun.c */
  388. js_Environment *jsR_newenvironment(js_State *J, js_Object *variables, js_Environment *outer);
  389. js_String *jsV_newmemstring(js_State *J, const char *s, int n);
  390. js_Value *js_tovalue(js_State *J, int idx);
  391. void js_toprimitive(js_State *J, int idx, int hint);
  392. js_Object *js_toobject(js_State *J, int idx);
  393. void js_pushvalue(js_State *J, js_Value v);
  394. void js_pushobject(js_State *J, js_Object *v);
  395. void jsR_unflattenarray(js_State *J, js_Object *obj);
  396. /* jsvalue.c */
  397. int jsV_toboolean(js_State *J, js_Value *v);
  398. double jsV_tonumber(js_State *J, js_Value *v);
  399. double jsV_tointeger(js_State *J, js_Value *v);
  400. const char *jsV_tostring(js_State *J, js_Value *v);
  401. js_Object *jsV_toobject(js_State *J, js_Value *v);
  402. void jsV_toprimitive(js_State *J, js_Value *v, int preferred);
  403. const char *js_itoa(char *buf, int a);
  404. double js_stringtofloat(const char *s, char **ep);
  405. int jsV_numbertointeger(double n);
  406. int jsV_numbertoint32(double n);
  407. unsigned int jsV_numbertouint32(double n);
  408. short jsV_numbertoint16(double n);
  409. unsigned short jsV_numbertouint16(double n);
  410. const char *jsV_numbertostring(js_State *J, char buf[32], double number);
  411. double jsV_stringtonumber(js_State *J, const char *string);
  412. /* jsproperty.c */
  413. js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype);
  414. js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name);
  415. js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own);
  416. js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name);
  417. js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name);
  418. js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name);
  419. void jsV_delproperty(js_State *J, js_Object *obj, const char *name);
  420. js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own);
  421. const char *jsV_nextiterator(js_State *J, js_Object *iter);
  422. void jsV_resizearray(js_State *J, js_Object *obj, int newlen);
  423. void jsV_unflattenarray(js_State *J, js_Object *obj);
  424. void jsV_growarray(js_State *J, js_Object *obj);
  425. /* Lexer */
  426. enum
  427. {
  428. TK_IDENTIFIER = 256,
  429. TK_NUMBER,
  430. TK_STRING,
  431. TK_REGEXP,
  432. /* multi-character punctuators */
  433. TK_LE,
  434. TK_GE,
  435. TK_EQ,
  436. TK_NE,
  437. TK_STRICTEQ,
  438. TK_STRICTNE,
  439. TK_SHL,
  440. TK_SHR,
  441. TK_USHR,
  442. TK_AND,
  443. TK_OR,
  444. TK_ADD_ASS,
  445. TK_SUB_ASS,
  446. TK_MUL_ASS,
  447. TK_DIV_ASS,
  448. TK_MOD_ASS,
  449. TK_SHL_ASS,
  450. TK_SHR_ASS,
  451. TK_USHR_ASS,
  452. TK_AND_ASS,
  453. TK_OR_ASS,
  454. TK_XOR_ASS,
  455. TK_INC,
  456. TK_DEC,
  457. /* keywords */
  458. TK_BREAK,
  459. TK_CASE,
  460. TK_CATCH,
  461. TK_CONTINUE,
  462. TK_DEBUGGER,
  463. TK_DEFAULT,
  464. TK_DELETE,
  465. TK_DO,
  466. TK_ELSE,
  467. TK_FALSE,
  468. TK_FINALLY,
  469. TK_FOR,
  470. TK_FUNCTION,
  471. TK_IF,
  472. TK_IN,
  473. TK_INSTANCEOF,
  474. TK_NEW,
  475. TK_NULL,
  476. TK_RETURN,
  477. TK_SWITCH,
  478. TK_THIS,
  479. TK_THROW,
  480. TK_TRUE,
  481. TK_TRY,
  482. TK_TYPEOF,
  483. TK_VAR,
  484. TK_VOID,
  485. TK_WHILE,
  486. TK_WITH,
  487. };
  488. int jsY_iswhite(int c);
  489. int jsY_isnewline(int c);
  490. int jsY_ishex(int c);
  491. int jsY_tohex(int c);
  492. const char *jsY_tokenstring(int token);
  493. int jsY_findword(const char *s, const char **list, int num);
  494. void jsY_initlex(js_State *J, const char *filename, const char *source);
  495. int jsY_lex(js_State *J);
  496. int jsY_lexjson(js_State *J);
  497. /* Parser */
  498. enum js_AstType
  499. {
  500. AST_LIST,
  501. AST_FUNDEC,
  502. AST_IDENTIFIER,
  503. EXP_IDENTIFIER,
  504. EXP_NUMBER,
  505. EXP_STRING,
  506. EXP_REGEXP,
  507. /* literals */
  508. EXP_ELISION, /* for array elisions */
  509. EXP_NULL,
  510. EXP_TRUE,
  511. EXP_FALSE,
  512. EXP_THIS,
  513. EXP_ARRAY,
  514. EXP_OBJECT,
  515. EXP_PROP_VAL,
  516. EXP_PROP_GET,
  517. EXP_PROP_SET,
  518. EXP_FUN,
  519. /* expressions */
  520. EXP_INDEX,
  521. EXP_MEMBER,
  522. EXP_CALL,
  523. EXP_NEW,
  524. EXP_POSTINC,
  525. EXP_POSTDEC,
  526. EXP_DELETE,
  527. EXP_VOID,
  528. EXP_TYPEOF,
  529. EXP_PREINC,
  530. EXP_PREDEC,
  531. EXP_POS,
  532. EXP_NEG,
  533. EXP_BITNOT,
  534. EXP_LOGNOT,
  535. EXP_MOD,
  536. EXP_DIV,
  537. EXP_MUL,
  538. EXP_SUB,
  539. EXP_ADD,
  540. EXP_USHR,
  541. EXP_SHR,
  542. EXP_SHL,
  543. EXP_IN,
  544. EXP_INSTANCEOF,
  545. EXP_GE,
  546. EXP_LE,
  547. EXP_GT,
  548. EXP_LT,
  549. EXP_STRICTNE,
  550. EXP_STRICTEQ,
  551. EXP_NE,
  552. EXP_EQ,
  553. EXP_BITAND,
  554. EXP_BITXOR,
  555. EXP_BITOR,
  556. EXP_LOGAND,
  557. EXP_LOGOR,
  558. EXP_COND,
  559. EXP_ASS,
  560. EXP_ASS_MUL,
  561. EXP_ASS_DIV,
  562. EXP_ASS_MOD,
  563. EXP_ASS_ADD,
  564. EXP_ASS_SUB,
  565. EXP_ASS_SHL,
  566. EXP_ASS_SHR,
  567. EXP_ASS_USHR,
  568. EXP_ASS_BITAND,
  569. EXP_ASS_BITXOR,
  570. EXP_ASS_BITOR,
  571. EXP_COMMA,
  572. EXP_VAR, /* var initializer */
  573. /* statements */
  574. STM_BLOCK,
  575. STM_EMPTY,
  576. STM_VAR,
  577. STM_IF,
  578. STM_DO,
  579. STM_WHILE,
  580. STM_FOR,
  581. STM_FOR_VAR,
  582. STM_FOR_IN,
  583. STM_FOR_IN_VAR,
  584. STM_CONTINUE,
  585. STM_BREAK,
  586. STM_RETURN,
  587. STM_WITH,
  588. STM_SWITCH,
  589. STM_THROW,
  590. STM_TRY,
  591. STM_DEBUGGER,
  592. STM_LABEL,
  593. STM_CASE,
  594. STM_DEFAULT,
  595. };
  596. typedef struct js_JumpList js_JumpList;
  597. struct js_JumpList
  598. {
  599. enum js_AstType type;
  600. int inst;
  601. js_JumpList *next;
  602. };
  603. struct js_Ast
  604. {
  605. enum js_AstType type;
  606. int line;
  607. js_Ast *parent, *a, *b, *c, *d;
  608. double number;
  609. const char *string;
  610. js_JumpList *jumps; /* list of break/continue jumps to patch */
  611. int casejump; /* for switch case clauses */
  612. js_Ast *gcnext; /* next in alloc list */
  613. };
  614. js_Ast *jsP_parsefunction(js_State *J, const char *filename, const char *params, const char *body);
  615. js_Ast *jsP_parse(js_State *J, const char *filename, const char *source);
  616. void jsP_freeparse(js_State *J);
  617. /* Compiler */
  618. enum js_OpCode
  619. {
  620. OP_POP, /* A -- */
  621. OP_DUP, /* A -- A A */
  622. OP_DUP2, /* A B -- A B A B */
  623. OP_ROT2, /* A B -- B A */
  624. OP_ROT3, /* A B C -- C A B */
  625. OP_ROT4, /* A B C D -- D A B C */
  626. OP_INTEGER, /* -K- (number-32768) */
  627. OP_NUMBER, /* -N- <number> */
  628. OP_STRING, /* -S- <string> */
  629. OP_CLOSURE, /* -F- <closure> */
  630. OP_NEWARRAY,
  631. OP_NEWOBJECT,
  632. OP_NEWREGEXP, /* -S,opts- <regexp> */
  633. OP_UNDEF,
  634. OP_NULL,
  635. OP_TRUE,
  636. OP_FALSE,
  637. OP_THIS,
  638. OP_CURRENT, /* currently executing function object */
  639. OP_GETLOCAL, /* -K- <value> */
  640. OP_SETLOCAL, /* <value> -K- <value> */
  641. OP_DELLOCAL, /* -K- false */
  642. OP_HASVAR, /* -S- ( <value> | undefined ) */
  643. OP_GETVAR, /* -S- <value> */
  644. OP_SETVAR, /* <value> -S- <value> */
  645. OP_DELVAR, /* -S- <success> */
  646. OP_IN, /* <name> <obj> -- <exists?> */
  647. OP_SKIPARRAY, /* <obj> -- <obj> */
  648. OP_INITARRAY, /* <obj> <val> -- <obj> */
  649. OP_INITPROP, /* <obj> <key> <val> -- <obj> */
  650. OP_INITGETTER, /* <obj> <key> <closure> -- <obj> */
  651. OP_INITSETTER, /* <obj> <key> <closure> -- <obj> */
  652. OP_GETPROP, /* <obj> <name> -- <value> */
  653. OP_GETPROP_S, /* <obj> -S- <value> */
  654. OP_SETPROP, /* <obj> <name> <value> -- <value> */
  655. OP_SETPROP_S, /* <obj> <value> -S- <value> */
  656. OP_DELPROP, /* <obj> <name> -- <success> */
  657. OP_DELPROP_S, /* <obj> -S- <success> */
  658. OP_ITERATOR, /* <obj> -- <iobj> */
  659. OP_NEXTITER, /* <iobj> -- ( <iobj> <name> true | false ) */
  660. OP_EVAL, /* <args...> -(numargs)- <returnvalue> */
  661. OP_CALL, /* <closure> <this> <args...> -(numargs)- <returnvalue> */
  662. OP_NEW, /* <closure> <args...> -(numargs)- <returnvalue> */
  663. OP_TYPEOF,
  664. OP_POS,
  665. OP_NEG,
  666. OP_BITNOT,
  667. OP_LOGNOT,
  668. OP_INC, /* <x> -- ToNumber(x)+1 */
  669. OP_DEC, /* <x> -- ToNumber(x)-1 */
  670. OP_POSTINC, /* <x> -- ToNumber(x)+1 ToNumber(x) */
  671. OP_POSTDEC, /* <x> -- ToNumber(x)-1 ToNumber(x) */
  672. OP_MUL,
  673. OP_DIV,
  674. OP_MOD,
  675. OP_ADD,
  676. OP_SUB,
  677. OP_SHL,
  678. OP_SHR,
  679. OP_USHR,
  680. OP_LT,
  681. OP_GT,
  682. OP_LE,
  683. OP_GE,
  684. OP_EQ,
  685. OP_NE,
  686. OP_STRICTEQ,
  687. OP_STRICTNE,
  688. OP_JCASE,
  689. OP_BITAND,
  690. OP_BITXOR,
  691. OP_BITOR,
  692. OP_INSTANCEOF,
  693. OP_THROW,
  694. OP_TRY, /* -ADDR- /jump/ or -ADDR- <exception> */
  695. OP_ENDTRY,
  696. OP_CATCH, /* push scope chain with exception variable */
  697. OP_ENDCATCH,
  698. OP_WITH,
  699. OP_ENDWITH,
  700. OP_DEBUGGER,
  701. OP_JUMP,
  702. OP_JTRUE,
  703. OP_JFALSE,
  704. OP_RETURN,
  705. };
  706. struct js_Function
  707. {
  708. const char *name;
  709. int script;
  710. int lightweight;
  711. int strict;
  712. int arguments;
  713. int numparams;
  714. js_Instruction *code;
  715. int codecap, codelen;
  716. js_Function **funtab;
  717. int funcap, funlen;
  718. const char **vartab;
  719. int varcap, varlen;
  720. const char *filename;
  721. int line, lastline;
  722. js_Function *gcnext;
  723. int gcmark;
  724. };
  725. js_Function *jsC_compilefunction(js_State *J, js_Ast *prog);
  726. js_Function *jsC_compilescript(js_State *J, js_Ast *prog, int default_strict);
  727. /* Builtins */
  728. void jsB_init(js_State *J);
  729. void jsB_initobject(js_State *J);
  730. void jsB_initarray(js_State *J);
  731. void jsB_initfunction(js_State *J);
  732. void jsB_initboolean(js_State *J);
  733. void jsB_initnumber(js_State *J);
  734. void jsB_initstring(js_State *J);
  735. void jsB_initregexp(js_State *J);
  736. void jsB_initerror(js_State *J);
  737. void jsB_initmath(js_State *J);
  738. void jsB_initjson(js_State *J);
  739. void jsB_initdate(js_State *J);
  740. void jsB_propf(js_State *J, const char *name, js_CFunction cfun, int n);
  741. void jsB_propn(js_State *J, const char *name, double number);
  742. void jsB_props(js_State *J, const char *name, const char *string);
  743. #endif