jsi.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. };
  143. /* Exception handling */
  144. struct js_Jumpbuf
  145. {
  146. jmp_buf buf;
  147. js_Environment *E;
  148. int envtop;
  149. int tracetop;
  150. int top, bot;
  151. int strict;
  152. js_Instruction *pc;
  153. };
  154. void *js_savetrypc(js_State *J, js_Instruction *pc);
  155. #define js_trypc(J, PC) \
  156. setjmp(js_savetrypc(J, PC))
  157. /* String buffer */
  158. typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
  159. void js_putc(js_State *J, js_Buffer **sbp, int c);
  160. void js_puts(js_State *J, js_Buffer **sb, const char *s);
  161. void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e);
  162. /* State struct */
  163. struct js_State
  164. {
  165. void *actx;
  166. void *uctx;
  167. js_Alloc alloc;
  168. js_Report report;
  169. js_Panic panic;
  170. js_StringNode *strings;
  171. int default_strict;
  172. int strict;
  173. /* parser input source */
  174. const char *filename;
  175. const char *source;
  176. int line;
  177. /* lexer state */
  178. struct { char *text; int len, cap; } lexbuf;
  179. int lexline;
  180. int lexchar;
  181. int lasttoken;
  182. int newline;
  183. /* parser state */
  184. int astdepth;
  185. int lookahead;
  186. const char *text;
  187. double number;
  188. js_Ast *gcast; /* list of allocated nodes to free after parsing */
  189. /* runtime environment */
  190. js_Object *Object_prototype;
  191. js_Object *Array_prototype;
  192. js_Object *Function_prototype;
  193. js_Object *Boolean_prototype;
  194. js_Object *Number_prototype;
  195. js_Object *String_prototype;
  196. js_Object *RegExp_prototype;
  197. js_Object *Date_prototype;
  198. js_Object *Error_prototype;
  199. js_Object *EvalError_prototype;
  200. js_Object *RangeError_prototype;
  201. js_Object *ReferenceError_prototype;
  202. js_Object *SyntaxError_prototype;
  203. js_Object *TypeError_prototype;
  204. js_Object *URIError_prototype;
  205. unsigned int seed; /* Math.random seed */
  206. char scratch[12]; /* scratch buffer for iterating over array indices */
  207. int nextref; /* for js_ref use */
  208. js_Object *R; /* registry of hidden values */
  209. js_Object *G; /* the global object */
  210. js_Environment *E; /* current environment scope */
  211. js_Environment *GE; /* global environment scope (at the root) */
  212. /* execution stack */
  213. int top, bot;
  214. js_Value *stack;
  215. /* garbage collector list */
  216. int gcpause;
  217. int gcmark;
  218. unsigned int gccounter, gcthresh;
  219. js_Environment *gcenv;
  220. js_Function *gcfun;
  221. js_Object *gcobj;
  222. js_String *gcstr;
  223. js_Object *gcroot; /* gc scan list */
  224. /* environments on the call stack but currently not in scope */
  225. int envtop;
  226. js_Environment *envstack[JS_ENVLIMIT];
  227. /* debug info stack trace */
  228. int tracetop;
  229. js_StackTrace trace[JS_ENVLIMIT];
  230. /* exception stack */
  231. int trytop;
  232. js_Jumpbuf trybuf[JS_TRYLIMIT];
  233. };
  234. /* Values */
  235. typedef struct js_Property js_Property;
  236. typedef struct js_Iterator js_Iterator;
  237. /* Hint to ToPrimitive() */
  238. enum {
  239. JS_HNONE,
  240. JS_HNUMBER,
  241. JS_HSTRING
  242. };
  243. enum js_Type {
  244. JS_TSHRSTR, /* type tag doubles as string zero-terminator */
  245. JS_TUNDEFINED,
  246. JS_TNULL,
  247. JS_TBOOLEAN,
  248. JS_TNUMBER,
  249. JS_TLITSTR,
  250. JS_TMEMSTR,
  251. JS_TOBJECT,
  252. };
  253. enum js_Class {
  254. JS_COBJECT,
  255. JS_CARRAY,
  256. JS_CFUNCTION,
  257. JS_CSCRIPT, /* function created from global/eval code */
  258. JS_CCFUNCTION, /* built-in function */
  259. JS_CERROR,
  260. JS_CBOOLEAN,
  261. JS_CNUMBER,
  262. JS_CSTRING,
  263. JS_CREGEXP,
  264. JS_CDATE,
  265. JS_CMATH,
  266. JS_CJSON,
  267. JS_CARGUMENTS,
  268. JS_CITERATOR,
  269. JS_CUSERDATA,
  270. };
  271. /*
  272. Short strings abuse the js_Value struct. By putting the type tag in the
  273. last byte, and using 0 as the tag for short strings, we can use the
  274. entire js_Value as string storage by letting the type tag serve double
  275. purpose as the string zero terminator.
  276. */
  277. union js_Value
  278. {
  279. struct {
  280. char pad[15];
  281. char type; /* type tag overlaps with final byte of shrstr */
  282. } t;
  283. union {
  284. char shrstr[16];
  285. int boolean;
  286. double number;
  287. const char *litstr;
  288. js_String *memstr;
  289. js_Object *object;
  290. } u;
  291. };
  292. struct js_String
  293. {
  294. js_String *gcnext;
  295. char gcmark;
  296. char p[1];
  297. };
  298. struct js_Regexp
  299. {
  300. void *prog;
  301. char *source;
  302. unsigned short flags;
  303. unsigned short last;
  304. };
  305. struct js_Object
  306. {
  307. enum js_Class type;
  308. int extensible;
  309. js_Property *properties;
  310. int count; /* number of properties, for array sparseness check */
  311. js_Object *prototype;
  312. union {
  313. int boolean;
  314. double number;
  315. struct {
  316. int length;
  317. char *string;
  318. char shrstr[16];
  319. } s;
  320. struct {
  321. int length; /* actual length */
  322. int simple; /* true if array has only non-sparse array properties */
  323. int flat_length; /* used length of simple array part */
  324. int flat_capacity; /* allocated length of simple array part */
  325. js_Value *array;
  326. } a;
  327. struct {
  328. js_Function *function;
  329. js_Environment *scope;
  330. } f;
  331. struct {
  332. const char *name;
  333. js_CFunction function;
  334. js_CFunction constructor;
  335. int length;
  336. void *data;
  337. js_Finalize finalize;
  338. } c;
  339. js_Regexp r;
  340. struct {
  341. js_Object *target;
  342. int i, n; /* for array part */
  343. js_Iterator *head, *current; /* for object part */
  344. } iter;
  345. struct {
  346. const char *tag;
  347. void *data;
  348. js_HasProperty has;
  349. js_Put put;
  350. js_Delete delete;
  351. js_Finalize finalize;
  352. } user;
  353. } u;
  354. js_Object *gcnext; /* allocation list */
  355. js_Object *gcroot; /* scan list */
  356. int gcmark;
  357. };
  358. struct js_Property
  359. {
  360. js_Property *left, *right;
  361. int level;
  362. int atts;
  363. js_Value value;
  364. js_Object *getter;
  365. js_Object *setter;
  366. char name[1];
  367. };
  368. struct js_Iterator
  369. {
  370. js_Iterator *next;
  371. char name[1];
  372. };
  373. struct js_Environment
  374. {
  375. js_Environment *outer;
  376. js_Object *variables;
  377. js_Environment *gcnext;
  378. int gcmark;
  379. };
  380. /* jsrun.c */
  381. js_Environment *jsR_newenvironment(js_State *J, js_Object *variables, js_Environment *outer);
  382. js_String *jsV_newmemstring(js_State *J, const char *s, int n);
  383. js_Value *js_tovalue(js_State *J, int idx);
  384. void js_toprimitive(js_State *J, int idx, int hint);
  385. js_Object *js_toobject(js_State *J, int idx);
  386. void js_pushvalue(js_State *J, js_Value v);
  387. void js_pushobject(js_State *J, js_Object *v);
  388. void jsR_unflattenarray(js_State *J, js_Object *obj);
  389. /* jsvalue.c */
  390. int jsV_toboolean(js_State *J, js_Value *v);
  391. double jsV_tonumber(js_State *J, js_Value *v);
  392. double jsV_tointeger(js_State *J, js_Value *v);
  393. const char *jsV_tostring(js_State *J, js_Value *v);
  394. js_Object *jsV_toobject(js_State *J, js_Value *v);
  395. void jsV_toprimitive(js_State *J, js_Value *v, int preferred);
  396. const char *js_itoa(char *buf, int a);
  397. double js_stringtofloat(const char *s, char **ep);
  398. int jsV_numbertointeger(double n);
  399. int jsV_numbertoint32(double n);
  400. unsigned int jsV_numbertouint32(double n);
  401. short jsV_numbertoint16(double n);
  402. unsigned short jsV_numbertouint16(double n);
  403. const char *jsV_numbertostring(js_State *J, char buf[32], double number);
  404. double jsV_stringtonumber(js_State *J, const char *string);
  405. /* jsproperty.c */
  406. js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype);
  407. js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name);
  408. js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own);
  409. js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name);
  410. js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name);
  411. js_Property *jsV_nextproperty(js_State *J, js_Object *obj, const char *name);
  412. void jsV_delproperty(js_State *J, js_Object *obj, const char *name);
  413. js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own);
  414. const char *jsV_nextiterator(js_State *J, js_Object *iter);
  415. void jsV_resizearray(js_State *J, js_Object *obj, int newlen);
  416. void jsV_unflattenarray(js_State *J, js_Object *obj);
  417. void jsV_growarray(js_State *J, js_Object *obj);
  418. /* Lexer */
  419. enum
  420. {
  421. TK_IDENTIFIER = 256,
  422. TK_NUMBER,
  423. TK_STRING,
  424. TK_REGEXP,
  425. /* multi-character punctuators */
  426. TK_LE,
  427. TK_GE,
  428. TK_EQ,
  429. TK_NE,
  430. TK_STRICTEQ,
  431. TK_STRICTNE,
  432. TK_SHL,
  433. TK_SHR,
  434. TK_USHR,
  435. TK_AND,
  436. TK_OR,
  437. TK_ADD_ASS,
  438. TK_SUB_ASS,
  439. TK_MUL_ASS,
  440. TK_DIV_ASS,
  441. TK_MOD_ASS,
  442. TK_SHL_ASS,
  443. TK_SHR_ASS,
  444. TK_USHR_ASS,
  445. TK_AND_ASS,
  446. TK_OR_ASS,
  447. TK_XOR_ASS,
  448. TK_INC,
  449. TK_DEC,
  450. /* keywords */
  451. TK_BREAK,
  452. TK_CASE,
  453. TK_CATCH,
  454. TK_CONTINUE,
  455. TK_DEBUGGER,
  456. TK_DEFAULT,
  457. TK_DELETE,
  458. TK_DO,
  459. TK_ELSE,
  460. TK_FALSE,
  461. TK_FINALLY,
  462. TK_FOR,
  463. TK_FUNCTION,
  464. TK_IF,
  465. TK_IN,
  466. TK_INSTANCEOF,
  467. TK_NEW,
  468. TK_NULL,
  469. TK_RETURN,
  470. TK_SWITCH,
  471. TK_THIS,
  472. TK_THROW,
  473. TK_TRUE,
  474. TK_TRY,
  475. TK_TYPEOF,
  476. TK_VAR,
  477. TK_VOID,
  478. TK_WHILE,
  479. TK_WITH,
  480. };
  481. int jsY_iswhite(int c);
  482. int jsY_isnewline(int c);
  483. int jsY_ishex(int c);
  484. int jsY_tohex(int c);
  485. const char *jsY_tokenstring(int token);
  486. int jsY_findword(const char *s, const char **list, int num);
  487. void jsY_initlex(js_State *J, const char *filename, const char *source);
  488. int jsY_lex(js_State *J);
  489. int jsY_lexjson(js_State *J);
  490. /* Parser */
  491. enum js_AstType
  492. {
  493. AST_LIST,
  494. AST_FUNDEC,
  495. AST_IDENTIFIER,
  496. EXP_IDENTIFIER,
  497. EXP_NUMBER,
  498. EXP_STRING,
  499. EXP_REGEXP,
  500. /* literals */
  501. EXP_ELISION, /* for array elisions */
  502. EXP_NULL,
  503. EXP_TRUE,
  504. EXP_FALSE,
  505. EXP_THIS,
  506. EXP_ARRAY,
  507. EXP_OBJECT,
  508. EXP_PROP_VAL,
  509. EXP_PROP_GET,
  510. EXP_PROP_SET,
  511. EXP_FUN,
  512. /* expressions */
  513. EXP_INDEX,
  514. EXP_MEMBER,
  515. EXP_CALL,
  516. EXP_NEW,
  517. EXP_POSTINC,
  518. EXP_POSTDEC,
  519. EXP_DELETE,
  520. EXP_VOID,
  521. EXP_TYPEOF,
  522. EXP_PREINC,
  523. EXP_PREDEC,
  524. EXP_POS,
  525. EXP_NEG,
  526. EXP_BITNOT,
  527. EXP_LOGNOT,
  528. EXP_MOD,
  529. EXP_DIV,
  530. EXP_MUL,
  531. EXP_SUB,
  532. EXP_ADD,
  533. EXP_USHR,
  534. EXP_SHR,
  535. EXP_SHL,
  536. EXP_IN,
  537. EXP_INSTANCEOF,
  538. EXP_GE,
  539. EXP_LE,
  540. EXP_GT,
  541. EXP_LT,
  542. EXP_STRICTNE,
  543. EXP_STRICTEQ,
  544. EXP_NE,
  545. EXP_EQ,
  546. EXP_BITAND,
  547. EXP_BITXOR,
  548. EXP_BITOR,
  549. EXP_LOGAND,
  550. EXP_LOGOR,
  551. EXP_COND,
  552. EXP_ASS,
  553. EXP_ASS_MUL,
  554. EXP_ASS_DIV,
  555. EXP_ASS_MOD,
  556. EXP_ASS_ADD,
  557. EXP_ASS_SUB,
  558. EXP_ASS_SHL,
  559. EXP_ASS_SHR,
  560. EXP_ASS_USHR,
  561. EXP_ASS_BITAND,
  562. EXP_ASS_BITXOR,
  563. EXP_ASS_BITOR,
  564. EXP_COMMA,
  565. EXP_VAR, /* var initializer */
  566. /* statements */
  567. STM_BLOCK,
  568. STM_EMPTY,
  569. STM_VAR,
  570. STM_IF,
  571. STM_DO,
  572. STM_WHILE,
  573. STM_FOR,
  574. STM_FOR_VAR,
  575. STM_FOR_IN,
  576. STM_FOR_IN_VAR,
  577. STM_CONTINUE,
  578. STM_BREAK,
  579. STM_RETURN,
  580. STM_WITH,
  581. STM_SWITCH,
  582. STM_THROW,
  583. STM_TRY,
  584. STM_DEBUGGER,
  585. STM_LABEL,
  586. STM_CASE,
  587. STM_DEFAULT,
  588. };
  589. typedef struct js_JumpList js_JumpList;
  590. struct js_JumpList
  591. {
  592. enum js_AstType type;
  593. int inst;
  594. js_JumpList *next;
  595. };
  596. struct js_Ast
  597. {
  598. enum js_AstType type;
  599. int line;
  600. js_Ast *parent, *a, *b, *c, *d;
  601. double number;
  602. const char *string;
  603. js_JumpList *jumps; /* list of break/continue jumps to patch */
  604. int casejump; /* for switch case clauses */
  605. js_Ast *gcnext; /* next in alloc list */
  606. };
  607. js_Ast *jsP_parsefunction(js_State *J, const char *filename, const char *params, const char *body);
  608. js_Ast *jsP_parse(js_State *J, const char *filename, const char *source);
  609. void jsP_freeparse(js_State *J);
  610. /* Compiler */
  611. enum js_OpCode
  612. {
  613. OP_POP, /* A -- */
  614. OP_DUP, /* A -- A A */
  615. OP_DUP2, /* A B -- A B A B */
  616. OP_ROT2, /* A B -- B A */
  617. OP_ROT3, /* A B C -- C A B */
  618. OP_ROT4, /* A B C D -- D A B C */
  619. OP_INTEGER, /* -K- (number-32768) */
  620. OP_NUMBER, /* -N- <number> */
  621. OP_STRING, /* -S- <string> */
  622. OP_CLOSURE, /* -F- <closure> */
  623. OP_NEWARRAY,
  624. OP_NEWOBJECT,
  625. OP_NEWREGEXP, /* -S,opts- <regexp> */
  626. OP_UNDEF,
  627. OP_NULL,
  628. OP_TRUE,
  629. OP_FALSE,
  630. OP_THIS,
  631. OP_CURRENT, /* currently executing function object */
  632. OP_GETLOCAL, /* -K- <value> */
  633. OP_SETLOCAL, /* <value> -K- <value> */
  634. OP_DELLOCAL, /* -K- false */
  635. OP_HASVAR, /* -S- ( <value> | undefined ) */
  636. OP_GETVAR, /* -S- <value> */
  637. OP_SETVAR, /* <value> -S- <value> */
  638. OP_DELVAR, /* -S- <success> */
  639. OP_IN, /* <name> <obj> -- <exists?> */
  640. OP_SKIPARRAY, /* <obj> -- <obj> */
  641. OP_INITARRAY, /* <obj> <val> -- <obj> */
  642. OP_INITPROP, /* <obj> <key> <val> -- <obj> */
  643. OP_INITGETTER, /* <obj> <key> <closure> -- <obj> */
  644. OP_INITSETTER, /* <obj> <key> <closure> -- <obj> */
  645. OP_GETPROP, /* <obj> <name> -- <value> */
  646. OP_GETPROP_S, /* <obj> -S- <value> */
  647. OP_SETPROP, /* <obj> <name> <value> -- <value> */
  648. OP_SETPROP_S, /* <obj> <value> -S- <value> */
  649. OP_DELPROP, /* <obj> <name> -- <success> */
  650. OP_DELPROP_S, /* <obj> -S- <success> */
  651. OP_ITERATOR, /* <obj> -- <iobj> */
  652. OP_NEXTITER, /* <iobj> -- ( <iobj> <name> true | false ) */
  653. OP_EVAL, /* <args...> -(numargs)- <returnvalue> */
  654. OP_CALL, /* <closure> <this> <args...> -(numargs)- <returnvalue> */
  655. OP_NEW, /* <closure> <args...> -(numargs)- <returnvalue> */
  656. OP_TYPEOF,
  657. OP_POS,
  658. OP_NEG,
  659. OP_BITNOT,
  660. OP_LOGNOT,
  661. OP_INC, /* <x> -- ToNumber(x)+1 */
  662. OP_DEC, /* <x> -- ToNumber(x)-1 */
  663. OP_POSTINC, /* <x> -- ToNumber(x)+1 ToNumber(x) */
  664. OP_POSTDEC, /* <x> -- ToNumber(x)-1 ToNumber(x) */
  665. OP_MUL,
  666. OP_DIV,
  667. OP_MOD,
  668. OP_ADD,
  669. OP_SUB,
  670. OP_SHL,
  671. OP_SHR,
  672. OP_USHR,
  673. OP_LT,
  674. OP_GT,
  675. OP_LE,
  676. OP_GE,
  677. OP_EQ,
  678. OP_NE,
  679. OP_STRICTEQ,
  680. OP_STRICTNE,
  681. OP_JCASE,
  682. OP_BITAND,
  683. OP_BITXOR,
  684. OP_BITOR,
  685. OP_INSTANCEOF,
  686. OP_THROW,
  687. OP_TRY, /* -ADDR- /jump/ or -ADDR- <exception> */
  688. OP_ENDTRY,
  689. OP_CATCH, /* push scope chain with exception variable */
  690. OP_ENDCATCH,
  691. OP_WITH,
  692. OP_ENDWITH,
  693. OP_DEBUGGER,
  694. OP_JUMP,
  695. OP_JTRUE,
  696. OP_JFALSE,
  697. OP_RETURN,
  698. };
  699. struct js_Function
  700. {
  701. const char *name;
  702. int script;
  703. int lightweight;
  704. int strict;
  705. int arguments;
  706. int numparams;
  707. js_Instruction *code;
  708. int codecap, codelen;
  709. js_Function **funtab;
  710. int funcap, funlen;
  711. const char **vartab;
  712. int varcap, varlen;
  713. const char *filename;
  714. int line, lastline;
  715. js_Function *gcnext;
  716. int gcmark;
  717. };
  718. js_Function *jsC_compilefunction(js_State *J, js_Ast *prog);
  719. js_Function *jsC_compilescript(js_State *J, js_Ast *prog, int default_strict);
  720. /* Builtins */
  721. void jsB_init(js_State *J);
  722. void jsB_initobject(js_State *J);
  723. void jsB_initarray(js_State *J);
  724. void jsB_initfunction(js_State *J);
  725. void jsB_initboolean(js_State *J);
  726. void jsB_initnumber(js_State *J);
  727. void jsB_initstring(js_State *J);
  728. void jsB_initregexp(js_State *J);
  729. void jsB_initerror(js_State *J);
  730. void jsB_initmath(js_State *J);
  731. void jsB_initjson(js_State *J);
  732. void jsB_initdate(js_State *J);
  733. void jsB_propf(js_State *J, const char *name, js_CFunction cfun, int n);
  734. void jsB_propn(js_State *J, const char *name, double number);
  735. void jsB_props(js_State *J, const char *name, const char *string);
  736. #endif