js.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. js.h -- JavaScript header
  3. Copyright (c) All Rights Reserved. See details at the end of the file.
  4. */
  5. #ifndef _h_JS
  6. #define _h_JS 1
  7. /********************************* Includes ***********************************/
  8. #include "goahead.h"
  9. #if ME_GOAHEAD_JAVASCRIPT
  10. /********************************** Defines ***********************************/
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /*
  15. Constants
  16. */
  17. #define JS_INC 110 /* Growth for tags/tokens */
  18. #define JS_SCRIPT_INC 1023 /* Growth for ej scripts */
  19. #define JS_OFFSET 1 /* hAlloc doesn't like 0 entries */
  20. #define JS_MAX_RECURSE 100 /* Sanity for maximum recursion */
  21. /*
  22. Javascript Lexical analyser tokens
  23. */
  24. #define TOK_ERR -1 /* Any error */
  25. #define TOK_LPAREN 1 /* ( */
  26. #define TOK_RPAREN 2 /* ) */
  27. #define TOK_IF 3 /* if */
  28. #define TOK_ELSE 4 /* else */
  29. #define TOK_LBRACE 5 /* { */
  30. #define TOK_RBRACE 6 /* } */
  31. #define TOK_LOGICAL 7 /* ||, &&, ! */
  32. #define TOK_EXPR 8 /* +, -, /, % */
  33. #define TOK_SEMI 9 /* ; */
  34. #define TOK_LITERAL 10 /* literal string */
  35. #define TOK_FUNCTION 11 /* function name */
  36. #define TOK_NEWLINE 12 /* newline white space */
  37. #define TOK_ID 13 /* function name */
  38. #define TOK_EOF 14 /* End of script */
  39. #define TOK_COMMA 15 /* Comma */
  40. #define TOK_VAR 16 /* var */
  41. #define TOK_ASSIGNMENT 17 /* = */
  42. #define TOK_FOR 18 /* for */
  43. #define TOK_INC_DEC 19 /* ++, -- */
  44. #define TOK_RETURN 20 /* return */
  45. /*
  46. Expression operators
  47. */
  48. #define EXPR_LESS 1 /* < */
  49. #define EXPR_LESSEQ 2 /* <= */
  50. #define EXPR_GREATER 3 /* > */
  51. #define EXPR_GREATEREQ 4 /* >= */
  52. #define EXPR_EQ 5 /* == */
  53. #define EXPR_NOTEQ 6 /* != */
  54. #define EXPR_PLUS 7 /* + */
  55. #define EXPR_MINUS 8 /* - */
  56. #define EXPR_DIV 9 /* / */
  57. #define EXPR_MOD 10 /* % */
  58. #define EXPR_LSHIFT 11 /* << */
  59. #define EXPR_RSHIFT 12 /* >> */
  60. #define EXPR_MUL 13 /* * */
  61. #define EXPR_ASSIGNMENT 14 /* = */
  62. #define EXPR_INC 15 /* ++ */
  63. #define EXPR_DEC 16 /* -- */
  64. #define EXPR_BOOL_COMP 17 /* ! */
  65. /*
  66. Conditional operators
  67. */
  68. #define COND_AND 1 /* && */
  69. #define COND_OR 2 /* || */
  70. #define COND_NOT 3 /* ! */
  71. /*
  72. States
  73. */
  74. #define STATE_ERR -1 /* Error state */
  75. #define STATE_EOF 1 /* End of file */
  76. #define STATE_COND 2 /* Parsing a "(conditional)" stmt */
  77. #define STATE_COND_DONE 3
  78. #define STATE_RELEXP 4 /* Parsing a relational expr */
  79. #define STATE_RELEXP_DONE 5
  80. #define STATE_EXPR 6 /* Parsing an expression */
  81. #define STATE_EXPR_DONE 7
  82. #define STATE_STMT 8 /* Parsing General statement */
  83. #define STATE_STMT_DONE 9
  84. #define STATE_STMT_BLOCK_DONE 10 /* End of block "}" */
  85. #define STATE_ARG_LIST 11 /* Function arg list */
  86. #define STATE_ARG_LIST_DONE 12
  87. #define STATE_DEC_LIST 16 /* Declaration list */
  88. #define STATE_DEC_LIST_DONE 17
  89. #define STATE_DEC 18
  90. #define STATE_DEC_DONE 19
  91. #define STATE_RET 20 /* Return statement */
  92. #define STATE_BEGIN STATE_STMT
  93. /*
  94. Flags. Used in Js and as parameter to parse()
  95. */
  96. #define FLAGS_EXE 0x1 /* Execute statements */
  97. #define FLAGS_VARIABLES 0x2 /* Allocated variables store */
  98. #define FLAGS_FUNCTIONS 0x4 /* Allocated function store */
  99. /*
  100. Function call structure
  101. */
  102. typedef struct JsFun {
  103. char *fname; /* Function name */
  104. char **args; /* Args for function (halloc) */
  105. int nArgs; /* Number of args */
  106. } JsFun;
  107. /*
  108. Evaluation block structure
  109. */
  110. typedef struct JsInput {
  111. WebsBuf tokbuf; /* Current token */
  112. WebsBuf script; /* Input script for parsing */
  113. char *putBackToken; /* Putback token string */
  114. int putBackTokenId; /* Putback token ID */
  115. char *line; /* Current line */
  116. int lineLength; /* Current line length */
  117. int lineNumber; /* Parse line number */
  118. int lineColumn; /* Column in line */
  119. } JsInput;
  120. /**
  121. Javascript engine structure
  122. @defgroup Js Js
  123. */
  124. typedef struct Js {
  125. JsInput *input; /* Input evaluation block */
  126. WebsHash functions; /* Symbol table for functions */
  127. WebsHash *variables; /* hAlloc list of variables */
  128. int variableMax; /* Number of entries */
  129. JsFun *func; /* Current function */
  130. char *result; /* Current expression result */
  131. char *error; /* Error message */
  132. char *token; /* Pointer to token string */
  133. int tid; /* Current token id */
  134. int jid; /* Halloc handle */
  135. int flags; /* Flags */
  136. void *userHandle; /* User defined handle */
  137. } Js;
  138. /**
  139. Javascript function procedure
  140. @ingroup Js
  141. */
  142. typedef int (*JsProc)(int jid, void *handle, int argc, char **argv);
  143. /******************************** Prototypes **********************************/
  144. /**
  145. Utility routine to parse function arguments
  146. @param argc Count of arguments in argv
  147. @param argv Array of arguments
  148. @param fmt Printf style format string
  149. @return Count of the arguments parsed
  150. @ingroup Js
  151. */
  152. PUBLIC int jsArgs(int argc, char **argv, cchar *fmt, ...);
  153. /**
  154. Close a javascript engine
  155. @param jid Javascript ID allocated via jsOpenEngine
  156. @ingroup Js
  157. */
  158. PUBLIC void jsCloseEngine(int jid);
  159. /**
  160. Emit a parse error
  161. @param js Javascript engine object
  162. @param fmt Error message format string
  163. @ingroup Js
  164. */
  165. PUBLIC void jsError(Js *js, cchar *fmt, ...);
  166. /**
  167. Parse and evaluate a script. Return the last function return value.
  168. @param jid Javascript ID allocated via jsOpenEngine
  169. @param script Script to evaluate
  170. @param emsg Pointer to a string to receive any error message
  171. @param str String value to use as the result. Set to null for errors.
  172. @ingroup Js
  173. */
  174. PUBLIC char *jsEval(int jid, cchar *script, char **emsg);
  175. /**
  176. Get the function result value
  177. @param jid Javascript ID allocated via jsOpenEngine
  178. @return Function return value string. Caller must not free.
  179. @ingroup Js
  180. */
  181. PUBLIC cchar *jsGetResult(int jid);
  182. /**
  183. Get a variable value
  184. @param jid Javascript ID allocated via jsOpenEngine
  185. @param var Variable name
  186. @param value Returned value.
  187. @return If successful, a positive variable index, otherwise -1. This will be zero for global variables
  188. and > 0 for local variables.
  189. @ingroup Js
  190. */
  191. PUBLIC int jsGetVar(int jid, cchar *var, cchar **value);
  192. /**
  193. Open a new javascript engine
  194. @param variables Hash table of variables
  195. @param functions Hash table of functions
  196. @ingroup Js
  197. */
  198. PUBLIC int jsOpenEngine(WebsHash variables, WebsHash functions);
  199. /**
  200. Set a local variable
  201. @param jid Javascript ID allocated via jsOpenEngine
  202. @param var Variable name
  203. @param value Value to use
  204. @ingroup Js
  205. */
  206. PUBLIC void jsSetLocalVar(int jid, cchar *var, cchar *value);
  207. /**
  208. Set a global variable
  209. @param jid Javascript ID allocated via jsOpenEngine
  210. @param var Variable name
  211. @param value value to use
  212. @ingroup Js
  213. */
  214. PUBLIC void jsSetGlobalVar(int jid, cchar *var, cchar *value);
  215. /**
  216. Set the function return result
  217. @param jid Javascript ID allocated via jsOpenEngine
  218. @param str String value to use as the result
  219. @ingroup Js
  220. */
  221. PUBLIC void jsSetResult(int jid, cchar *str);
  222. /**
  223. Set a variable value in the top most variable frame
  224. @param jid Javascript ID allocated via jsOpenEngine
  225. @param var Variable name
  226. @param value Value to set
  227. @ingroup Js
  228. */
  229. PUBLIC void jsSetVar(int jid, cchar *var, cchar *value);
  230. /**
  231. Set a global function
  232. @param jid Javascript ID allocated via jsOpenEngine
  233. @param name Javascript function name
  234. @param fn C function providing the implementation.
  235. @ingroup Js
  236. */
  237. PUBLIC int jsSetGlobalFunction(int jid, cchar *name, JsProc fn);
  238. /*
  239. Internal API
  240. */
  241. PUBLIC int jsCloseBlock(int jid, int vid);
  242. PUBLIC char *jsEvalBlock(int jid, cchar *script, char **emsg);
  243. PUBLIC WebsHash jsGetFunctionTable(int jid);
  244. PUBLIC void *jsGetGlobalFunction(int jid, cchar *name);
  245. PUBLIC int jsGetLineNumber(int jid);
  246. PUBLIC void *jsGetUserHandle(int jid);
  247. PUBLIC WebsHash jsGetVariableTable(int jid);
  248. PUBLIC int jsLexOpen(Js *ep);
  249. PUBLIC void jsLexClose(Js *ep);
  250. PUBLIC int jsLexOpenScript(Js *ep, cchar *script);
  251. PUBLIC void jsLexCloseScript(Js *ep);
  252. PUBLIC void jsLexSaveInputState(Js *ep, JsInput *state);
  253. PUBLIC void jsLexFreeInputState(Js *ep, JsInput *state);
  254. PUBLIC void jsLexRestoreInputState(Js *ep, JsInput *state);
  255. PUBLIC int jsLexGetToken(Js *ep, int state);
  256. PUBLIC void jsLexPutbackToken(Js *ep, int tid, cchar *string);
  257. PUBLIC int jsOpenBlock(int jid);
  258. PUBLIC int jsRemoveGlobalFunction(int jid, cchar *name);
  259. PUBLIC int jsSetGlobalFunctionDirect(WebsHash functions, cchar *name, JsProc fn);
  260. PUBLIC void jsSetUserHandle(int jid, void *handle);
  261. #if ME_GOAHEAD_LEGACY
  262. typedef Js ej_t;
  263. typedef JsInput jsinput_t;
  264. typedef JsFun jsfunc_t;
  265. #define ejOpenBlock jsOpenBlock
  266. #define ejCloseBlock jsCloseBlock
  267. #define ejEvalBlock jsEvalBlock
  268. #define ejRemoveGlobalFunction jsRemoveGlobalFunction
  269. #define ejGetGlobalFunction jsGetGlobalFunction
  270. #define ejSetGlobalFunctionDirect jsSetGlobalFunctionDirect
  271. #define ejError jsError
  272. #define ejSetUserHandle jsSetUserHandle
  273. #define ejGetUserHandle jsGetUserHandle
  274. #define ejGetLineNumber jsGetLineNumber
  275. #define ejGetResult jsGetResult
  276. #define ejSetLocalVar jsSetLocalVar
  277. #define ejSetGlobalVar jsSetGlobalVar
  278. #define ejLexOpen jsLexOpen
  279. #define ejLexClose jsLexClose
  280. #define ejLexOpenScript jsLexOpenScript
  281. #define ejLexCloseScript jsLexCloseScript
  282. #define ejLexSaveInputState jsLexSaveInputState
  283. #define ejLexFreeInputState jsLexFreeInputState
  284. #define ejLexRestoreInputState jsLexRestoreInputState
  285. #define ejLexGetToken jsLexGetToken
  286. #define ejLexPutbackToken jsLexPutbackToken
  287. #define ejGetVariableTable jsGetVariableTable
  288. #define ejGetFunctionTable jsGetFunctionTable
  289. #define ejArgs jsArgs
  290. #define ejSetResult jsSetResult
  291. #define ejOpenEngine jsOpenEngine
  292. #define ejCloseEngine jsCloseEngine
  293. #define ejSetGlobalFunction jsSetGlobalFunction
  294. #define ejSetVar jsSetVar
  295. #define ejGetVar jsGetVar
  296. #define ejEval jsEval
  297. #endif
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif /* ME_GOAHEAD_JAVASCRIPT */
  302. #endif /* _h_JS */
  303. /*
  304. Copyright (c) Embedthis Software. All Rights Reserved.
  305. This software is distributed under commercial and open source licenses.
  306. You may use the Embedthis GoAhead open source license or you may acquire
  307. a commercial license from Embedthis Software. You agree to be fully bound
  308. by the terms of either license. Consult the LICENSE.md distributed with
  309. this software for full details and other copyrights.
  310. */