cairo-script-interpreter.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it either under the terms of the GNU Lesser General Public
  6. * License version 2.1 as published by the Free Software Foundation
  7. * (the "LGPL") or, at your option, under the terms of the Mozilla
  8. * Public License Version 1.1 (the "MPL"). If you do not alter this
  9. * notice, a recipient may use your version of this file under either
  10. * the MPL or the LGPL.
  11. *
  12. * You should have received a copy of the LGPL along with this library
  13. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  15. * You should have received a copy of the MPL along with this library
  16. * in the file COPYING-MPL-1.1
  17. *
  18. * The contents of this file are subject to the Mozilla Public License
  19. * Version 1.1 (the "License"); you may not use this file except in
  20. * compliance with the License. You may obtain a copy of the License at
  21. * http://www.mozilla.org/MPL/
  22. *
  23. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  24. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  25. * the specific language governing rights and limitations.
  26. *
  27. * The Original Code is the cairo graphics library.
  28. *
  29. * The Initial Developer of the Original Code is Chris Wilson.
  30. *
  31. * Contributor(s):
  32. * Chris Wilson <chris@chris-wilson.co.uk>
  33. */
  34. #include "config.h"
  35. #include "cairo-script-private.h"
  36. #include "cairo.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <math.h>
  41. #include <assert.h>
  42. #ifndef MAX
  43. #define MAX(a,b) (((a)>=(b))?(a):(b))
  44. #endif
  45. csi_status_t
  46. _csi_error (csi_status_t status)
  47. {
  48. return status;
  49. }
  50. /* XXX track global/local memory, cap etc, mark/sweep GC */
  51. void *
  52. _csi_alloc (csi_t *ctx, int size)
  53. {
  54. return malloc (size);
  55. }
  56. void *
  57. _csi_alloc0 (csi_t *ctx, int size)
  58. {
  59. void *ptr;
  60. ptr = _csi_alloc (ctx, size);
  61. if (_csi_likely (ptr != NULL))
  62. memset (ptr, 0, size);
  63. return ptr;
  64. }
  65. void *
  66. _csi_realloc (csi_t *ctx, void *ptr, int size)
  67. {
  68. return realloc (ptr, size);
  69. }
  70. void
  71. _csi_free (csi_t *ctx, void *ptr)
  72. {
  73. if (_csi_unlikely (ptr == NULL))
  74. return;
  75. free (ptr);
  76. }
  77. void *
  78. _csi_perm_alloc (csi_t *ctx, int size)
  79. {
  80. csi_chunk_t *chunk;
  81. void *ptr;
  82. size = (size + sizeof (void *)-1) & -sizeof (void *);
  83. chunk = ctx->perm_chunk;
  84. if (chunk == NULL || chunk->rem < size) {
  85. int chunk_size = (size + 8191) & -8192;
  86. chunk = _csi_alloc (ctx, sizeof (csi_chunk_t) + chunk_size);
  87. if (_csi_unlikely (chunk == NULL))
  88. return NULL;
  89. chunk->rem = chunk_size;
  90. chunk->ptr = (char *) (chunk + 1);
  91. chunk->next = ctx->perm_chunk;
  92. ctx->perm_chunk = chunk;
  93. }
  94. ptr = chunk->ptr;
  95. chunk->ptr += size;
  96. chunk->rem -= size;
  97. return ptr;
  98. }
  99. void *
  100. _csi_slab_alloc (csi_t *ctx, int size)
  101. {
  102. #if CSI_DEBUG_MALLOC
  103. return malloc (size);
  104. #else
  105. int chunk_size;
  106. csi_chunk_t *chunk;
  107. void *ptr;
  108. chunk_size = 2 * sizeof (void *);
  109. chunk_size = (size + chunk_size - 1) / chunk_size;
  110. if (ctx->slabs[chunk_size].free_list) {
  111. ptr = ctx->slabs[chunk_size].free_list;
  112. ctx->slabs[chunk_size].free_list = *(void **) ptr;
  113. return ptr;
  114. }
  115. chunk = ctx->slabs[chunk_size].chunk;
  116. if (chunk == NULL || ! chunk->rem) {
  117. int cnt = MAX (128, 8192 / (chunk_size * 2 * sizeof (void *)));
  118. chunk = _csi_alloc (ctx,
  119. sizeof (csi_chunk_t) +
  120. cnt * chunk_size * 2 * sizeof (void *));
  121. if (_csi_unlikely (chunk == NULL))
  122. return NULL;
  123. chunk->rem = cnt;
  124. chunk->ptr = (char *) (chunk + 1);
  125. chunk->next = ctx->slabs[chunk_size].chunk;
  126. ctx->slabs[chunk_size].chunk = chunk;
  127. }
  128. ptr = chunk->ptr;
  129. chunk->ptr += chunk_size * 2 * sizeof (void *);
  130. chunk->rem--;
  131. return ptr;
  132. #endif
  133. }
  134. void
  135. _csi_slab_free (csi_t *ctx, void *ptr, int size)
  136. {
  137. int chunk_size;
  138. void **free_list;
  139. if (_csi_unlikely (ptr == NULL))
  140. return;
  141. #if CSI_DEBUG_MALLOC
  142. free (ptr);
  143. #else
  144. chunk_size = 2 * sizeof (void *);
  145. chunk_size = (size + chunk_size - 1) / chunk_size;
  146. free_list = ptr;
  147. *free_list = ctx->slabs[chunk_size].free_list;
  148. ctx->slabs[chunk_size].free_list = ptr;
  149. #endif
  150. }
  151. csi_status_t
  152. _csi_stack_push (csi_t *ctx, csi_stack_t *stack,
  153. const csi_object_t *obj)
  154. {
  155. if (_csi_unlikely (stack->len == stack->size))
  156. return _csi_stack_push_internal (ctx, stack, obj);
  157. stack->objects[stack->len++] = *obj;
  158. return CSI_STATUS_SUCCESS;
  159. }
  160. static void
  161. _csi_perm_fini (csi_t *ctx)
  162. {
  163. while (ctx->perm_chunk != NULL) {
  164. csi_chunk_t *chunk = ctx->perm_chunk;
  165. ctx->perm_chunk = chunk->next;
  166. _csi_free (ctx, chunk);
  167. }
  168. }
  169. static void
  170. _csi_slab_fini (csi_t *ctx)
  171. {
  172. unsigned int i;
  173. for (i = 0; i < sizeof (ctx->slabs) / sizeof (ctx->slabs[0]); i++) {
  174. while (ctx->slabs[i].chunk != NULL) {
  175. csi_chunk_t *chunk = ctx->slabs[i].chunk;
  176. ctx->slabs[i].chunk = chunk->next;
  177. _csi_free (ctx, chunk);
  178. }
  179. }
  180. }
  181. static csi_status_t
  182. _add_operator (csi_t *ctx,
  183. csi_dictionary_t *dict,
  184. const csi_operator_def_t *def)
  185. {
  186. csi_object_t name;
  187. csi_object_t operator;
  188. csi_status_t status;
  189. status = csi_name_new_static (ctx, &name, def->name);
  190. if (status)
  191. return status;
  192. csi_operator_new (&operator, def->op);
  193. return csi_dictionary_put (ctx, dict, name.datum.name, &operator);
  194. }
  195. static csi_status_t
  196. _add_integer_constant (csi_t *ctx,
  197. csi_dictionary_t *dict,
  198. const csi_integer_constant_def_t *def)
  199. {
  200. csi_object_t name;
  201. csi_object_t constant;
  202. csi_status_t status;
  203. status = csi_name_new_static (ctx, &name, def->name);
  204. if (status)
  205. return status;
  206. csi_integer_new (&constant, def->value);
  207. return csi_dictionary_put (ctx, dict, name.datum.name, &constant);
  208. }
  209. static csi_status_t
  210. _add_real_constant (csi_t *ctx,
  211. csi_dictionary_t *dict,
  212. const csi_real_constant_def_t *def)
  213. {
  214. csi_object_t name;
  215. csi_object_t constant;
  216. csi_status_t status;
  217. status = csi_name_new_static (ctx, &name, def->name);
  218. if (status)
  219. return status;
  220. csi_real_new (&constant, def->value);
  221. return csi_dictionary_put (ctx, dict, name.datum.name, &constant);
  222. }
  223. static csi_status_t
  224. _init_dictionaries (csi_t *ctx)
  225. {
  226. csi_status_t status;
  227. csi_stack_t *stack;
  228. csi_object_t obj;
  229. csi_dictionary_t *dict, *opcodes;
  230. const csi_operator_def_t *odef;
  231. const csi_integer_constant_def_t *idef;
  232. const csi_real_constant_def_t *rdef;
  233. unsigned n;
  234. stack = &ctx->dstack;
  235. status = _csi_stack_init (ctx, stack, 4);
  236. if (_csi_unlikely (status))
  237. return status;
  238. /* systemdict */
  239. status = csi_dictionary_new (ctx, &obj);
  240. if (_csi_unlikely (status))
  241. return status;
  242. status = _csi_stack_push (ctx, stack, &obj);
  243. if (_csi_unlikely (status))
  244. return status;
  245. dict = obj.datum.dictionary;
  246. status = csi_dictionary_new (ctx, &obj);
  247. if (_csi_unlikely (status))
  248. return status;
  249. opcodes = obj.datum.dictionary;
  250. n = 0;
  251. csi_integer_new (&obj, n);
  252. status = csi_dictionary_put (ctx, opcodes, 0, &obj);
  253. if (_csi_unlikely (status))
  254. return status;
  255. ctx->opcode[n++] = NULL;
  256. /* fill systemdict with operators */
  257. for (odef = _csi_operators (); odef->name != NULL; odef++) {
  258. status = _add_operator (ctx, dict, odef);
  259. if (_csi_unlikely (status))
  260. return status;
  261. if (! csi_dictionary_has (opcodes, (csi_name_t) odef->op)) {
  262. csi_integer_new (&obj, n);
  263. status = csi_dictionary_put (ctx,
  264. opcodes, (csi_name_t) odef->op, &obj);
  265. if (_csi_unlikely (status))
  266. return status;
  267. assert (n < sizeof (ctx->opcode) / sizeof (ctx->opcode[0]));
  268. ctx->opcode[n++] = odef->op;
  269. }
  270. }
  271. csi_dictionary_free (ctx, opcodes);
  272. /* add constants */
  273. for (idef = _csi_integer_constants (); idef->name != NULL; idef++) {
  274. status = _add_integer_constant (ctx, dict, idef);
  275. if (_csi_unlikely (status))
  276. return status;
  277. }
  278. for (rdef = _csi_real_constants (); rdef->name != NULL; rdef++) {
  279. status = _add_real_constant (ctx, dict, rdef);
  280. if (_csi_unlikely (status))
  281. return status;
  282. }
  283. /* and seal */
  284. //dict.type &= ~CSI_OBJECT_ATTR_WRITABLE;
  285. /* globaldict */
  286. status = csi_dictionary_new (ctx, &obj);
  287. if (_csi_unlikely (status))
  288. return status;
  289. status = _csi_stack_push (ctx, stack, &obj);
  290. if (_csi_unlikely (status))
  291. return status;
  292. /* userdict */
  293. status = csi_dictionary_new (ctx, &obj);
  294. if (_csi_unlikely (status))
  295. return status;
  296. status = _csi_stack_push (ctx, stack, &obj);
  297. if (_csi_unlikely (status))
  298. return status;
  299. return CSI_STATUS_SUCCESS;
  300. }
  301. /* intern string */
  302. typedef struct _cairo_intern_string {
  303. csi_hash_entry_t hash_entry;
  304. int len;
  305. char *string;
  306. } csi_intern_string_t;
  307. static unsigned long
  308. _intern_string_hash (const char *str, int len)
  309. {
  310. const signed char *p = (const signed char *) str;
  311. if (len > 0) {
  312. unsigned int h = *p;
  313. while (--len)
  314. h = (h << 5) - h + *++p;
  315. return h;
  316. }
  317. return 0;
  318. }
  319. static cairo_bool_t
  320. _intern_string_equal (const void *_a, const void *_b)
  321. {
  322. const csi_intern_string_t *a = _a;
  323. const csi_intern_string_t *b = _b;
  324. if (a->len != b->len)
  325. return FALSE;
  326. return memcmp (a->string, b->string, a->len) == 0;
  327. }
  328. static void
  329. _csi_init (csi_t *ctx)
  330. {
  331. csi_status_t status;
  332. memset (ctx, 0, sizeof (*ctx));
  333. ctx->status = CSI_STATUS_SUCCESS;
  334. ctx->ref_count = 1;
  335. ctx->scanner.line_number = -1;
  336. status = _csi_hash_table_init (&ctx->strings, _intern_string_equal);
  337. if (status)
  338. goto FAIL;
  339. status = _csi_stack_init (ctx, &ctx->ostack, 2048);
  340. if (status)
  341. goto FAIL;
  342. status = _init_dictionaries (ctx);
  343. if (status)
  344. goto FAIL;
  345. status = _csi_scanner_init (ctx, &ctx->scanner);
  346. if (status)
  347. goto FAIL;
  348. return;
  349. FAIL:
  350. if (ctx->status == CSI_STATUS_SUCCESS)
  351. ctx->status = status;
  352. }
  353. static void
  354. _csi_finish (csi_t *ctx)
  355. {
  356. _csi_stack_fini (ctx, &ctx->ostack);
  357. _csi_stack_fini (ctx, &ctx->dstack);
  358. _csi_scanner_fini (ctx, &ctx->scanner);
  359. _csi_hash_table_fini (&ctx->strings);
  360. }
  361. csi_status_t
  362. _csi_name_define (csi_t *ctx, csi_name_t name, csi_object_t *obj)
  363. {
  364. return csi_dictionary_put (ctx,
  365. ctx->dstack.objects[ctx->dstack.len-1].datum.dictionary,
  366. name,
  367. obj);
  368. }
  369. csi_status_t
  370. _csi_name_lookup (csi_t *ctx, csi_name_t name, csi_object_t *obj)
  371. {
  372. int i;
  373. for (i = ctx->dstack.len; i--; ) {
  374. csi_dictionary_t *dict;
  375. csi_dictionary_entry_t *entry;
  376. dict = ctx->dstack.objects[i].datum.dictionary;
  377. entry = _csi_hash_table_lookup (&dict->hash_table,
  378. (csi_hash_entry_t *) &name);
  379. if (entry != NULL) {
  380. *obj = entry->value;
  381. return CSI_STATUS_SUCCESS;
  382. }
  383. }
  384. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  385. }
  386. csi_status_t
  387. _csi_name_undefine (csi_t *ctx, csi_name_t name)
  388. {
  389. unsigned int i;
  390. for (i = ctx->dstack.len; --i; ) {
  391. if (csi_dictionary_has (ctx->dstack.objects[i].datum.dictionary,
  392. name))
  393. {
  394. csi_dictionary_remove (ctx,
  395. ctx->dstack.objects[i].datum.dictionary,
  396. name);
  397. return CSI_STATUS_SUCCESS;
  398. }
  399. }
  400. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  401. }
  402. csi_status_t
  403. _csi_intern_string (csi_t *ctx, const char **str_inout, int len)
  404. {
  405. char *str = (char *) *str_inout;
  406. csi_intern_string_t tmpl, *istring;
  407. csi_status_t status = CSI_STATUS_SUCCESS;
  408. tmpl.hash_entry.hash = _intern_string_hash (str, len);
  409. tmpl.len = len;
  410. tmpl.string = (char *) str;
  411. istring = _csi_hash_table_lookup (&ctx->strings, &tmpl.hash_entry);
  412. if (istring == NULL) {
  413. istring = _csi_perm_alloc (ctx,
  414. sizeof (csi_intern_string_t) + len + 1);
  415. if (istring != NULL) {
  416. istring->hash_entry.hash = tmpl.hash_entry.hash;
  417. istring->len = tmpl.len;
  418. istring->string = (char *) (istring + 1);
  419. memcpy (istring->string, str, len);
  420. istring->string[len] = '\0';
  421. status = _csi_hash_table_insert (&ctx->strings,
  422. &istring->hash_entry);
  423. if (_csi_unlikely (status)) {
  424. _csi_free (ctx, istring);
  425. return status;
  426. }
  427. } else
  428. return _csi_error (CSI_STATUS_NO_MEMORY);
  429. }
  430. *str_inout = istring->string;
  431. return CSI_STATUS_SUCCESS;
  432. }
  433. /* Public */
  434. static csi_t _csi_nil = { -1, CSI_STATUS_NO_MEMORY };
  435. csi_t *
  436. cairo_script_interpreter_create (void)
  437. {
  438. csi_t *ctx;
  439. ctx = malloc (sizeof (csi_t));
  440. if (ctx == NULL)
  441. return (csi_t *) &_csi_nil;
  442. _csi_init (ctx);
  443. return ctx;
  444. }
  445. void
  446. cairo_script_interpreter_install_hooks (csi_t *ctx,
  447. const csi_hooks_t *hooks)
  448. {
  449. if (ctx->status)
  450. return;
  451. ctx->hooks = *hooks;
  452. }
  453. cairo_status_t
  454. cairo_script_interpreter_run (csi_t *ctx, const char *filename)
  455. {
  456. csi_object_t file;
  457. if (ctx->status)
  458. return ctx->status;
  459. if (ctx->finished)
  460. return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
  461. ctx->status = csi_file_new (ctx, &file, filename, "r");
  462. if (ctx->status)
  463. return ctx->status;
  464. file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
  465. ctx->status = csi_object_execute (ctx, &file);
  466. csi_object_free (ctx, &file);
  467. return ctx->status;
  468. }
  469. cairo_status_t
  470. cairo_script_interpreter_feed_stream (csi_t *ctx, FILE *stream)
  471. {
  472. csi_object_t file;
  473. if (ctx->status)
  474. return ctx->status;
  475. if (ctx->finished)
  476. return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
  477. ctx->status = csi_file_new_for_stream (ctx, &file, stream);
  478. if (ctx->status)
  479. return ctx->status;
  480. file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
  481. ctx->status = csi_object_execute (ctx, &file);
  482. csi_object_free (ctx, &file);
  483. return ctx->status;
  484. }
  485. cairo_status_t
  486. cairo_script_interpreter_feed_string (csi_t *ctx, const char *line, int len)
  487. {
  488. csi_object_t file;
  489. if (ctx->status)
  490. return ctx->status;
  491. if (ctx->finished)
  492. return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
  493. if (len < 0)
  494. len = strlen (line);
  495. ctx->status = csi_file_new_for_bytes (ctx, &file, line, len);
  496. if (ctx->status)
  497. return ctx->status;
  498. file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
  499. ctx->status = csi_object_execute (ctx, &file);
  500. csi_object_free (ctx, &file);
  501. return ctx->status;
  502. }
  503. unsigned int
  504. cairo_script_interpreter_get_line_number (csi_t *ctx)
  505. {
  506. return ctx->scanner.line_number + 1; /* 1 index based */
  507. }
  508. csi_t *
  509. cairo_script_interpreter_reference (csi_t *ctx)
  510. {
  511. ctx->ref_count++;
  512. return ctx;
  513. }
  514. slim_hidden_def (cairo_script_interpreter_reference);
  515. cairo_status_t
  516. cairo_script_interpreter_finish (csi_t *ctx)
  517. {
  518. csi_status_t status;
  519. status = ctx->status;
  520. if (! ctx->finished) {
  521. _csi_finish (ctx);
  522. ctx->finished = 1;
  523. } else if (status == CSI_STATUS_SUCCESS) {
  524. status = ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
  525. }
  526. return status;
  527. }
  528. static void
  529. _csi_fini (csi_t *ctx)
  530. {
  531. if (! ctx->finished)
  532. _csi_finish (ctx);
  533. if (ctx->free_array != NULL)
  534. csi_array_free (ctx, ctx->free_array);
  535. if (ctx->free_dictionary != NULL)
  536. csi_dictionary_free (ctx, ctx->free_dictionary);
  537. if (ctx->free_string != NULL)
  538. csi_string_free (ctx, ctx->free_string);
  539. _csi_slab_fini (ctx);
  540. _csi_perm_fini (ctx);
  541. }
  542. cairo_status_t
  543. cairo_script_interpreter_destroy (csi_t *ctx)
  544. {
  545. csi_status_t status;
  546. status = ctx->status;
  547. if (--ctx->ref_count)
  548. return status;
  549. _csi_fini (ctx);
  550. free (ctx);
  551. return status;
  552. }
  553. slim_hidden_def (cairo_script_interpreter_destroy);
  554. cairo_status_t
  555. cairo_script_interpreter_translate_stream (FILE *stream,
  556. cairo_write_func_t write_func,
  557. void *closure)
  558. {
  559. csi_t ctx;
  560. csi_object_t src;
  561. csi_status_t status;
  562. _csi_init (&ctx);
  563. status = csi_file_new_for_stream (&ctx, &src, stream);
  564. if (status)
  565. goto BAIL;
  566. status = _csi_translate_file (&ctx, src.datum.file, write_func, closure);
  567. BAIL:
  568. csi_object_free (&ctx, &src);
  569. _csi_fini (&ctx);
  570. return status;
  571. }