Coverage Report

Created: 2026-11-30 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cairo/util/cairo-script/cairo-script-objects.c
Line
Count
Source
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
35
#include "config.h"
36
37
#include "cairo-script-private.h"
38
39
#include <limits.h> /* INT_MAX */
40
#include <string.h>
41
42
csi_status_t
43
csi_array_new (csi_t *ctx,
44
         csi_integer_t initial_size,
45
         csi_object_t *obj)
46
47
3.17M
{
48
3.17M
    csi_array_t *array;
49
50
3.17M
    if (ctx->free_array == NULL ||
51
1.77k
  ctx->free_array->stack.size <= initial_size)
52
3.17M
    {
53
3.17M
  csi_status_t status;
54
55
3.17M
  array = _csi_slab_alloc (ctx, sizeof (csi_array_t));
56
3.17M
  if (_csi_unlikely (array == NULL))
57
0
      return _csi_error (CSI_STATUS_NO_MEMORY);
58
59
3.17M
  status = _csi_stack_init (ctx, &array->stack,
60
3.17M
          initial_size ? initial_size : 32);
61
3.17M
  if (_csi_unlikely (status)) {
62
0
      _csi_slab_free (ctx, array, sizeof (csi_array_t));
63
0
      return status;
64
0
  }
65
3.17M
    } else {
66
1.30k
  array = ctx->free_array;
67
1.30k
  ctx->free_array = NULL;
68
1.30k
    }
69
70
3.17M
    array->base.type = CSI_OBJECT_TYPE_ARRAY;
71
3.17M
    array->base.ref = 1;
72
73
3.17M
    obj->type = CSI_OBJECT_TYPE_ARRAY;
74
3.17M
    obj->datum.array = array;
75
76
3.17M
    return CSI_STATUS_SUCCESS;
77
3.17M
}
78
79
csi_status_t
80
csi_array_put (csi_t *ctx,
81
         csi_array_t *array,
82
         csi_integer_t elem,
83
         csi_object_t *value)
84
1
{
85
1
    if (_csi_unlikely (elem < 0))
86
1
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
87
88
0
    if (_csi_unlikely (elem >= array->stack.len)) {
89
0
  csi_status_t status;
90
91
0
  status = _csi_stack_grow (ctx, &array->stack, elem + 1);
92
0
  if (_csi_unlikely (status))
93
0
      return status;
94
95
0
  memset (array->stack.objects + array->stack.len,
96
0
    0, (elem - array->stack.len + 1) * sizeof (csi_object_t));
97
0
  array->stack.len = elem + 1;
98
0
    }
99
100
0
    csi_object_free (ctx, &array->stack.objects[elem]);
101
0
    array->stack.objects[elem] = *csi_object_reference (value);
102
103
0
    return CSI_STATUS_SUCCESS;
104
0
}
105
106
csi_status_t
107
csi_array_get (csi_t *ctx,
108
         csi_array_t *array,
109
         csi_integer_t elem,
110
         csi_object_t *value)
111
1
{
112
1
    if (_csi_unlikely (elem < 0))
113
0
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
114
115
1
    if (_csi_unlikely (elem > array->stack.len))
116
1
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
117
118
0
    *value = array->stack.objects[elem];
119
0
    return CSI_STATUS_SUCCESS;
120
1
}
121
122
csi_status_t
123
csi_array_append (csi_t *ctx,
124
      csi_array_t *array,
125
      csi_object_t *obj)
126
1.46M
{
127
1.46M
    return _csi_stack_push (ctx, &array->stack, csi_object_reference (obj));
128
1.46M
}
129
130
inline csi_status_t
131
_csi_array_execute (csi_t *ctx, csi_array_t *array)
132
0
{
133
0
    csi_integer_t i;
134
0
    csi_status_t status;
135
136
0
    if (_csi_unlikely (array->stack.len == 0))
137
0
  return CSI_STATUS_SUCCESS;
138
139
0
    for (i = 0; i < array->stack.len; i++) {
140
0
  csi_object_t *obj = &array->stack.objects[i];
141
142
0
  if (obj->type & CSI_OBJECT_ATTR_EXECUTABLE) {
143
0
      if (obj->type == (CSI_OBJECT_TYPE_ARRAY |
144
0
            CSI_OBJECT_ATTR_EXECUTABLE))
145
0
      {
146
0
    status = _csi_push_ostack_copy (ctx, &array->stack.objects[i]);
147
0
      }
148
0
      else
149
0
    status = csi_object_execute (ctx, &array->stack.objects[i]);
150
0
  } else
151
0
      status = _csi_push_ostack_copy (ctx, &array->stack.objects[i]);
152
0
  if (_csi_unlikely (status))
153
0
      return status;
154
0
    }
155
156
0
    return CSI_STATUS_SUCCESS;
157
0
}
158
159
void
160
csi_array_free (csi_t *ctx, csi_array_t *array)
161
3.15M
{
162
#if CSI_DEBUG_MALLOC
163
    _csi_stack_fini (ctx, &array->stack);
164
    _csi_slab_free (ctx, array, sizeof (csi_array_t));
165
#else
166
3.15M
    csi_integer_t n;
167
168
5.25M
    for (n = 0; n < array->stack.len; n++)
169
2.10M
  csi_object_free (ctx, &array->stack.objects[n]);
170
3.15M
    array->stack.len = 0;
171
172
3.15M
    if (ctx->free_array != NULL) {
173
3.15M
  if (array->stack.size > ctx->free_array->stack.size) {
174
70
      csi_array_t *tmp = ctx->free_array;
175
70
      ctx->free_array = array;
176
70
      array = tmp;
177
70
  }
178
179
3.15M
  _csi_stack_fini (ctx, &array->stack);
180
3.15M
  _csi_slab_free (ctx, array, sizeof (csi_array_t));
181
3.15M
    } else
182
2.14k
  ctx->free_array = array;
183
3.15M
#endif
184
3.15M
}
185
186
static cairo_bool_t
187
_dictionary_name_equal (const void *_a, const void *_b)
188
11.1M
{
189
11.1M
    return TRUE;
190
11.1M
}
191
192
csi_status_t
193
csi_dictionary_new (csi_t *ctx,
194
        csi_object_t *obj)
195
196
167k
{
197
167k
    csi_dictionary_t *dict;
198
199
167k
    if (ctx->free_dictionary != NULL) {
200
7.56k
  dict = ctx->free_dictionary;
201
7.56k
  ctx->free_dictionary = NULL;
202
160k
    } else {
203
160k
  csi_status_t status;
204
205
160k
  dict = _csi_slab_alloc (ctx, sizeof (csi_dictionary_t));
206
160k
  if (_csi_unlikely (dict == NULL))
207
0
      return _csi_error (CSI_STATUS_NO_MEMORY);
208
209
160k
  status = _csi_hash_table_init (&dict->hash_table,
210
160k
               _dictionary_name_equal);
211
160k
  if (_csi_unlikely (status)) {
212
0
      _csi_slab_free (ctx, dict, sizeof (csi_dictionary_t));
213
0
      return status;
214
0
  }
215
160k
    }
216
217
167k
    dict->base.type = CSI_OBJECT_TYPE_DICTIONARY;
218
167k
    dict->base.ref = 1;
219
220
167k
    obj->type = CSI_OBJECT_TYPE_DICTIONARY;
221
167k
    obj->datum.dictionary = dict;
222
223
167k
    return CSI_STATUS_SUCCESS;
224
167k
}
225
226
struct _dictionary_entry_pluck {
227
    csi_t *ctx;
228
    csi_hash_table_t *hash_table;
229
};
230
231
static void
232
_dictionary_entry_pluck (void *entry, void *data)
233
2.78M
{
234
2.78M
    csi_dictionary_entry_t *dict_entry;
235
2.78M
    struct _dictionary_entry_pluck *pluck_data;
236
237
2.78M
    dict_entry = entry;
238
2.78M
    pluck_data = data;
239
240
2.78M
    _csi_hash_table_remove (pluck_data->hash_table, entry);
241
2.78M
    csi_object_free (pluck_data->ctx, &dict_entry->value);
242
2.78M
    _csi_slab_free (pluck_data->ctx, entry, sizeof (csi_dictionary_entry_t));
243
2.78M
}
244
245
void
246
csi_dictionary_free (csi_t *ctx,
247
         csi_dictionary_t *dict)
248
174k
{
249
174k
    struct _dictionary_entry_pluck data;
250
251
174k
    data.ctx = ctx;
252
174k
    data.hash_table = &dict->hash_table;
253
174k
    _csi_hash_table_foreach (&dict->hash_table,
254
174k
           _dictionary_entry_pluck,
255
174k
           &data);
256
257
#if CSI_DEBUG_MALLOC
258
    _csi_hash_table_fini (&dict->hash_table);
259
    _csi_slab_free (ctx, dict, sizeof (csi_dictionary_t));
260
#else
261
174k
    if (ctx->free_dictionary != NULL) {
262
159k
  _csi_hash_table_fini (&dict->hash_table);
263
159k
  _csi_slab_free (ctx, dict, sizeof (csi_dictionary_t));
264
159k
    } else
265
14.1k
  ctx->free_dictionary = dict;
266
174k
#endif
267
174k
}
268
269
csi_status_t
270
csi_dictionary_put (csi_t *ctx,
271
        csi_dictionary_t *dict,
272
        csi_name_t name,
273
        csi_object_t *value)
274
2.80M
{
275
2.80M
    csi_dictionary_entry_t *entry;
276
2.80M
    csi_status_t status;
277
278
2.80M
    entry = _csi_hash_table_lookup (&dict->hash_table,
279
2.80M
            (csi_hash_entry_t *) &name);
280
2.80M
    if (entry != NULL) {
281
  /* replace the existing entry */
282
13.6k
  csi_object_free (ctx, &entry->value);
283
13.6k
  entry->value = *csi_object_reference (value);
284
13.6k
  return CSI_STATUS_SUCCESS;
285
13.6k
    }
286
287
2.78M
    entry = _csi_slab_alloc (ctx, sizeof (*entry));
288
2.78M
    if (_csi_unlikely (entry == NULL))
289
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
290
291
2.78M
    entry->hash_entry.hash = name;
292
2.78M
    status = _csi_hash_table_insert (&dict->hash_table, &entry->hash_entry);
293
2.78M
    if (_csi_unlikely (status)) {
294
0
  _csi_slab_free (ctx, entry, sizeof (*entry));
295
0
  return status;
296
0
    }
297
298
2.78M
    entry->value = *csi_object_reference (value);
299
300
2.78M
    return CSI_STATUS_SUCCESS;
301
2.78M
}
302
303
csi_status_t
304
csi_dictionary_get (csi_t *ctx,
305
        csi_dictionary_t *dict,
306
        csi_name_t name,
307
        csi_object_t *value)
308
4
{
309
4
    csi_dictionary_entry_t *entry;
310
311
4
    entry = _csi_hash_table_lookup (&dict->hash_table,
312
4
            (csi_hash_entry_t *) &name);
313
4
    if (_csi_unlikely (entry == NULL))
314
4
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
315
316
0
    *value = entry->value;
317
0
    return CSI_STATUS_SUCCESS;
318
4
}
319
320
csi_boolean_t
321
csi_dictionary_has (csi_dictionary_t *dict,
322
        csi_name_t name)
323
1.27M
{
324
1.27M
    return _csi_hash_table_lookup (&dict->hash_table,
325
1.27M
           (csi_hash_entry_t *) &name) != NULL;
326
1.27M
}
327
328
void
329
csi_dictionary_remove (csi_t *ctx,
330
           csi_dictionary_t *dict,
331
           csi_name_t name)
332
0
{
333
0
    csi_dictionary_entry_t *entry;
334
335
0
    entry = _csi_hash_table_lookup (&dict->hash_table,
336
0
            (csi_hash_entry_t *) &name);
337
0
    if (entry != NULL) {
338
0
  _csi_hash_table_remove (&dict->hash_table, &entry->hash_entry);
339
0
  csi_object_free (ctx, &entry->value);
340
0
  _csi_slab_free (ctx, entry, sizeof (csi_dictionary_entry_t));
341
0
    }
342
0
}
343
344
csi_status_t
345
csi_matrix_new (csi_t *ctx,
346
    csi_object_t *obj)
347
2.28k
{
348
2.28k
    csi_matrix_t *matrix;
349
350
2.28k
    matrix = _csi_slab_alloc (ctx, sizeof (csi_matrix_t));
351
2.28k
    if (_csi_unlikely (matrix == NULL))
352
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
353
354
2.28k
    matrix->base.type = CSI_OBJECT_TYPE_MATRIX;
355
2.28k
    matrix->base.ref = 1;
356
2.28k
    cairo_matrix_init_identity (&matrix->matrix);
357
358
2.28k
    obj->type = CSI_OBJECT_TYPE_MATRIX;
359
2.28k
    obj->datum.matrix = matrix;
360
361
2.28k
    return CSI_STATUS_SUCCESS;
362
2.28k
}
363
364
csi_status_t
365
csi_matrix_new_from_array (csi_t *ctx,
366
         csi_object_t *obj,
367
         csi_array_t *array)
368
1
{
369
1
    csi_matrix_t *matrix;
370
371
1
    if (_csi_unlikely (array->stack.len != 6))
372
1
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
373
374
0
    matrix = _csi_slab_alloc (ctx, sizeof (csi_matrix_t));
375
0
    if (_csi_unlikely (matrix == NULL))
376
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
377
378
0
    matrix->base.type = CSI_OBJECT_TYPE_MATRIX;
379
0
    matrix->base.ref = 1;
380
0
    cairo_matrix_init (&matrix->matrix,
381
0
           csi_number_get_value (&array->stack.objects[0]),
382
0
           csi_number_get_value (&array->stack.objects[1]),
383
0
           csi_number_get_value (&array->stack.objects[2]),
384
0
           csi_number_get_value (&array->stack.objects[3]),
385
0
           csi_number_get_value (&array->stack.objects[4]),
386
0
           csi_number_get_value (&array->stack.objects[5]));
387
388
0
    obj->type = CSI_OBJECT_TYPE_MATRIX;
389
0
    obj->datum.matrix = matrix;
390
391
0
    return CSI_STATUS_SUCCESS;
392
0
}
393
394
csi_status_t
395
csi_matrix_new_from_matrix (csi_t *ctx,
396
          csi_object_t *obj,
397
          const cairo_matrix_t *m)
398
1
{
399
1
    csi_matrix_t *matrix;
400
401
1
    matrix = _csi_slab_alloc (ctx, sizeof (csi_matrix_t));
402
1
    if (_csi_unlikely (matrix == NULL))
403
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
404
405
1
    matrix->base.type = CSI_OBJECT_TYPE_MATRIX;
406
1
    matrix->base.ref = 1;
407
1
    matrix->matrix = *m;
408
409
1
    obj->type = CSI_OBJECT_TYPE_MATRIX;
410
1
    obj->datum.matrix = matrix;
411
412
1
    return CSI_STATUS_SUCCESS;
413
1
}
414
415
csi_status_t
416
csi_matrix_new_from_values (csi_t *ctx,
417
          csi_object_t *obj,
418
          double v[6])
419
1.78k
{
420
1.78k
    csi_matrix_t *matrix;
421
422
1.78k
    matrix = _csi_slab_alloc (ctx, sizeof (csi_matrix_t));
423
1.78k
    if (_csi_unlikely (matrix == NULL))
424
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
425
426
1.78k
    matrix->base.type = CSI_OBJECT_TYPE_MATRIX;
427
1.78k
    matrix->base.ref = 1;
428
1.78k
    cairo_matrix_init (&matrix->matrix, v[0], v[1], v[2], v[3], v[4], v[5]);
429
430
1.78k
    obj->type = CSI_OBJECT_TYPE_MATRIX;
431
1.78k
    obj->datum.matrix = matrix;
432
433
1.78k
    return CSI_STATUS_SUCCESS;
434
1.78k
}
435
436
void
437
csi_matrix_free (csi_t *ctx,
438
     csi_matrix_t *obj)
439
4.07k
{
440
4.07k
    _csi_slab_free (ctx, obj, sizeof (csi_matrix_t));
441
4.07k
}
442
443
444
csi_status_t
445
csi_name_new (csi_t *ctx,
446
        csi_object_t *obj,
447
        const char *str,
448
        int len)
449
12.2M
{
450
12.2M
    csi_status_t status;
451
452
12.2M
    status = _csi_intern_string (ctx, &str, len);
453
12.2M
    if (_csi_unlikely (status))
454
0
  return status;
455
456
12.2M
    obj->type = CSI_OBJECT_TYPE_NAME;
457
12.2M
    obj->datum.name = (csi_name_t) str;
458
459
12.2M
    return CSI_STATUS_SUCCESS;
460
12.2M
}
461
462
csi_status_t
463
csi_name_new_static (csi_t *ctx,
464
         csi_object_t *obj,
465
         const char *str)
466
1.86M
{
467
1.86M
    csi_status_t status;
468
469
1.86M
    status = _csi_intern_string (ctx, &str, strlen (str));
470
1.86M
    if (_csi_unlikely (status))
471
0
  return status;
472
473
1.86M
    obj->type = CSI_OBJECT_TYPE_NAME;
474
1.86M
    obj->datum.name = (csi_name_t) str;
475
476
1.86M
    return CSI_STATUS_SUCCESS;
477
1.86M
}
478
479
csi_status_t
480
csi_string_new (csi_t *ctx,
481
    csi_object_t *obj,
482
    const char *str,
483
    int len)
484
583k
{
485
583k
    csi_string_t *string;
486
487
583k
    if (len < 0)
488
603
  len = strlen (str);
489
583k
    if (_csi_unlikely (len >= INT_MAX))
490
1
  return _csi_error (CSI_STATUS_NO_MEMORY);
491
492
583k
    if (ctx->free_string == NULL || ctx->free_string->len <= len) {
493
578k
  string = _csi_slab_alloc (ctx, sizeof (csi_string_t));
494
578k
  if (_csi_unlikely (string == NULL))
495
0
      return _csi_error (CSI_STATUS_NO_MEMORY);
496
497
578k
  string->string = _csi_alloc (ctx, len + 1);
498
578k
  if (_csi_unlikely (string->string == NULL)) {
499
0
      _csi_slab_free (ctx, string, sizeof (csi_string_t));
500
0
      return _csi_error (CSI_STATUS_NO_MEMORY);
501
0
  }
502
578k
    } else {
503
4.49k
  string = ctx->free_string;
504
4.49k
  ctx->free_string = NULL;
505
4.49k
    }
506
507
583k
    if (str != NULL) {
508
579k
  memcpy (string->string, str, len);
509
579k
  string->string[len] = '\0';
510
579k
    }
511
583k
    string->len = len;
512
583k
    string->deflate = 0;
513
583k
    string->method = NONE;
514
515
583k
    string->base.type = CSI_OBJECT_TYPE_STRING;
516
583k
    string->base.ref = 1;
517
518
583k
    obj->type = CSI_OBJECT_TYPE_STRING;
519
583k
    obj->datum.string = string;
520
521
583k
    return CSI_STATUS_SUCCESS;
522
583k
}
523
524
csi_status_t
525
csi_string_deflate_new (csi_t *ctx,
526
      csi_object_t *obj,
527
      void *bytes,
528
      int in_len,
529
      int out_len)
530
603
{
531
603
    csi_status_t status;
532
603
    csi_string_t *string;
533
534
603
    status = csi_string_new (ctx, obj, bytes, in_len);
535
603
    if (_csi_unlikely (status))
536
0
  return status;
537
538
603
    string = obj->datum.string;
539
603
    string->deflate = out_len;
540
603
    string->method = ZLIB;
541
542
603
    return CSI_STATUS_SUCCESS;
543
603
}
544
545
csi_status_t
546
csi_string_new_from_bytes (csi_t *ctx,
547
                     csi_object_t *obj,
548
         char *bytes,
549
         unsigned int len)
550
0
{
551
0
    csi_string_t *string;
552
553
0
    if (_csi_unlikely (len >= INT_MAX))
554
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
555
556
0
    string = _csi_slab_alloc (ctx, sizeof (csi_string_t));
557
0
    if (_csi_unlikely (string == NULL))
558
0
  return _csi_error (CSI_STATUS_NO_MEMORY);
559
560
0
    string->string = bytes;
561
0
    string->len = len;
562
0
    string->deflate = 0;
563
0
    string->method = NONE;
564
565
0
    string->base.type = CSI_OBJECT_TYPE_STRING;
566
0
    string->base.ref = 1;
567
568
0
    obj->type = CSI_OBJECT_TYPE_STRING;
569
0
    obj->datum.string = string;
570
571
0
    return CSI_STATUS_SUCCESS;
572
0
}
573
574
static inline csi_status_t
575
_csi_string_execute (csi_t *ctx, csi_string_t *string)
576
0
{
577
0
    csi_status_t status;
578
0
    csi_object_t obj;
579
580
0
    if (_csi_unlikely (string->len == 0))
581
0
  return CSI_STATUS_SUCCESS;
582
583
0
    status = csi_file_new_for_bytes (ctx, &obj, string->string, string->len);
584
0
    if (_csi_unlikely (status))
585
0
  return status;
586
587
0
    status = _csi_scan_file (ctx, obj.datum.file);
588
0
    csi_object_free (ctx, &obj);
589
590
0
    return status;
591
0
}
592
593
void
594
csi_string_free (csi_t *ctx, csi_string_t *string)
595
577k
{
596
#if CSI_DEBUG_MALLOC
597
    _csi_free (ctx, string->string);
598
    _csi_slab_free (ctx, string, sizeof (csi_string_t));
599
#else
600
577k
    if (ctx->free_string != NULL) {
601
572k
  if (string->len > ctx->free_string->len) {
602
1.31k
      csi_string_t *tmp = ctx->free_string;
603
1.31k
      ctx->free_string = string;
604
1.31k
      string = tmp;
605
1.31k
  }
606
607
572k
  _csi_free (ctx, string->string);
608
572k
  _csi_slab_free (ctx, string, sizeof (csi_string_t));
609
572k
    } else
610
5.10k
  ctx->free_string = string;
611
577k
#endif
612
577k
}
613
614
csi_status_t
615
csi_object_execute (csi_t *ctx, csi_object_t *obj)
616
12.0M
{
617
12.0M
    csi_status_t status;
618
12.0M
    csi_object_t indirect;
619
620
22.7M
 INDIRECT:
621
22.7M
    switch (obj->type & CSI_OBJECT_TYPE_MASK) {
622
10.7M
    case CSI_OBJECT_TYPE_NAME:
623
10.7M
  status = _csi_name_lookup (ctx, obj->datum.name, &indirect);
624
10.7M
  if (_csi_unlikely (status))
625
1.11k
      return status;
626
10.7M
  if (indirect.type & CSI_OBJECT_ATTR_EXECUTABLE) {
627
10.7M
      obj = &indirect;
628
10.7M
      goto INDIRECT;
629
10.7M
  } else
630
41
      return _csi_push_ostack_copy (ctx, &indirect);
631
632
11.9M
    case CSI_OBJECT_TYPE_OPERATOR:
633
11.9M
  return obj->datum.op (ctx);
634
635
0
    case CSI_OBJECT_TYPE_ARRAY:
636
0
  return _csi_array_execute (ctx, obj->datum.array);
637
6.55k
    case CSI_OBJECT_TYPE_FILE:
638
6.55k
  return _csi_file_execute (ctx, obj->datum.file);
639
0
    case CSI_OBJECT_TYPE_STRING:
640
0
  return _csi_string_execute (ctx, obj->datum.string);
641
642
0
    default:
643
0
  return _csi_push_ostack_copy (ctx, obj);
644
22.7M
    }
645
22.7M
}
646
647
csi_object_t *
648
csi_object_reference (csi_object_t *obj)
649
4.29M
{
650
4.29M
    if (CSI_OBJECT_IS_CAIRO (obj)) {
651
58
  switch (obj->type & CSI_OBJECT_TYPE_MASK) {
652
0
  case CSI_OBJECT_TYPE_CONTEXT:
653
0
      cairo_reference (obj->datum.cr);
654
0
      break;
655
0
  case CSI_OBJECT_TYPE_FONT:
656
0
      cairo_font_face_reference (obj->datum.font_face);
657
0
      break;
658
58
  case CSI_OBJECT_TYPE_PATTERN:
659
58
      cairo_pattern_reference (obj->datum.pattern);
660
58
      break;
661
0
  case CSI_OBJECT_TYPE_SCALED_FONT:
662
0
      cairo_scaled_font_reference (obj->datum.scaled_font);
663
0
      break;
664
0
  case CSI_OBJECT_TYPE_SURFACE:
665
0
      cairo_surface_reference (obj->datum.surface);
666
0
      break;
667
58
  }
668
4.29M
    } else if (CSI_OBJECT_IS_COMPOUND (obj)) {
669
33.4k
  obj->datum.object->ref++;
670
33.4k
    }
671
672
4.29M
    return obj;
673
4.29M
}
674
675
void
676
csi_object_free (csi_t *ctx,
677
     csi_object_t *obj)
678
32.0M
{
679
32.0M
    if (CSI_OBJECT_IS_CAIRO (obj)) {
680
104k
  switch (obj->type & CSI_OBJECT_TYPE_MASK) {
681
0
  case CSI_OBJECT_TYPE_CONTEXT:
682
0
      cairo_destroy (obj->datum.cr);
683
0
      break;
684
0
  case CSI_OBJECT_TYPE_FONT:
685
0
      cairo_font_face_destroy (obj->datum.font_face);
686
0
      break;
687
104k
  case CSI_OBJECT_TYPE_PATTERN:
688
104k
      cairo_pattern_destroy (obj->datum.pattern);
689
104k
      break;
690
0
  case CSI_OBJECT_TYPE_SCALED_FONT:
691
0
      cairo_scaled_font_destroy (obj->datum.scaled_font);
692
0
      break;
693
7
  case CSI_OBJECT_TYPE_SURFACE:
694
7
      cairo_surface_destroy (obj->datum.surface);
695
7
      break;
696
104k
  }
697
31.9M
    } else if (CSI_OBJECT_IS_COMPOUND (obj)) {
698
3.93M
  if (--obj->datum.object->ref)
699
33.1k
      return;
700
701
3.90M
  switch (obj->type & CSI_OBJECT_TYPE_MASK) {
702
3.15M
  case CSI_OBJECT_TYPE_ARRAY:
703
3.15M
      csi_array_free (ctx, obj->datum.array);
704
3.15M
      break;
705
161k
  case CSI_OBJECT_TYPE_DICTIONARY:
706
161k
      csi_dictionary_free (ctx, obj->datum.dictionary);
707
161k
      break;
708
6.55k
  case CSI_OBJECT_TYPE_FILE:
709
6.55k
      _csi_file_free (ctx, obj->datum.file);
710
6.55k
      break;
711
4.07k
  case CSI_OBJECT_TYPE_MATRIX:
712
4.07k
      csi_matrix_free (ctx, obj->datum.matrix);
713
4.07k
      break;
714
577k
  case CSI_OBJECT_TYPE_STRING:
715
577k
      csi_string_free (ctx, obj->datum.string);
716
577k
      break;
717
0
  default:
718
0
      break;
719
3.90M
  }
720
3.90M
    }
721
32.0M
}
722
723
csi_status_t
724
csi_object_as_file (csi_t *ctx,
725
        csi_object_t *src,
726
        csi_object_t *file)
727
0
{
728
0
    int type = csi_object_get_type (src);
729
0
    switch (type) {
730
0
    case CSI_OBJECT_TYPE_FILE:
731
0
  *file = *csi_object_reference (src);
732
0
  return CSI_STATUS_SUCCESS;
733
0
    case CSI_OBJECT_TYPE_STRING:
734
0
   return csi_file_new_from_string (ctx, file, src->datum.string);
735
0
    case CSI_OBJECT_TYPE_ARRAY:
736
#if 0
737
  if (src->type & CSI_OBJECT_ATTR_EXECUTABLE)
738
      return _csi_file_new_from_procedure (cs, src);
739
#endif
740
0
    default:
741
0
  return _csi_error (CSI_STATUS_INVALID_SCRIPT);
742
0
    }
743
0
}
744
745
static int
746
lexcmp (void const *a, size_t alen,
747
  void const *b, size_t blen)
748
9.17k
{
749
9.17k
    size_t len = alen < blen ? alen : blen;
750
9.17k
    int cmp = memcmp (a, b, len);
751
9.17k
    if (cmp)
752
2.36k
  return cmp;
753
6.80k
    if (alen == blen)
754
3.35k
  return 0;
755
3.45k
    return alen < blen ? -1 : +1;
756
6.80k
}
757
758
csi_boolean_t
759
csi_object_eq (csi_object_t *a,
760
         csi_object_t *b)
761
443k
{
762
443k
    csi_object_type_t atype = csi_object_get_type (a);
763
443k
    csi_object_type_t btype = csi_object_get_type (b);
764
765
443k
    if (atype == btype) {
766
388k
  switch (atype) {
767
25.3k
  case CSI_OBJECT_TYPE_BOOLEAN:
768
25.3k
      return a->datum.boolean == b->datum.boolean;
769
63.4k
  case CSI_OBJECT_TYPE_INTEGER:
770
63.4k
      return a->datum.integer == b->datum.integer;
771
290k
  case CSI_OBJECT_TYPE_REAL:
772
290k
      return a->datum.real == b->datum.real;
773
81
  case CSI_OBJECT_TYPE_NAME:
774
81
      return a->datum.name == b->datum.name;
775
7.33k
  case CSI_OBJECT_TYPE_STRING:
776
7.33k
      return 0 == lexcmp (a->datum.string->string,
777
7.33k
        a->datum.string->len,
778
7.33k
        b->datum.string->string,
779
7.33k
        b->datum.string->len);
780
10
  case CSI_OBJECT_TYPE_NULL:
781
286
  case CSI_OBJECT_TYPE_MARK:
782
286
      return TRUE;
783
22
  case CSI_OBJECT_TYPE_OPERATOR:
784
22
      return a->datum.op == b->datum.op;
785
497
  case CSI_OBJECT_TYPE_ARRAY:
786
1.32k
  case CSI_OBJECT_TYPE_DICTIONARY:
787
1.32k
  case CSI_OBJECT_TYPE_FILE:
788
1.32k
  case CSI_OBJECT_TYPE_MATRIX:
789
1.32k
  case CSI_OBJECT_TYPE_CONTEXT:
790
1.32k
  case CSI_OBJECT_TYPE_FONT:
791
1.68k
  case CSI_OBJECT_TYPE_PATTERN:
792
1.68k
  case CSI_OBJECT_TYPE_SCALED_FONT:
793
1.68k
  case CSI_OBJECT_TYPE_SURFACE:
794
1.68k
      return a->datum.ptr == b->datum.ptr;
795
388k
  }
796
388k
    }
797
798
55.1k
    if (atype < btype) {
799
35.7k
  csi_object_t *c;
800
35.7k
  csi_object_type_t ctype;
801
35.7k
  c = a; a = b; b = c;
802
35.7k
  ctype = atype; atype = btype; btype = ctype;
803
35.7k
    }
804
805
55.1k
    switch ((int) atype) {
806
10.1k
    case CSI_OBJECT_TYPE_INTEGER:
807
10.1k
  if (btype == CSI_OBJECT_TYPE_BOOLEAN) {
808
10.1k
      return a->datum.integer == b->datum.boolean;
809
10.1k
  }
810
1
  break;
811
36.2k
    case CSI_OBJECT_TYPE_REAL:
812
36.2k
  if (btype == CSI_OBJECT_TYPE_INTEGER) {
813
14.6k
      return a->datum.real == b->datum.integer;
814
14.6k
  }
815
21.6k
  else if (btype == CSI_OBJECT_TYPE_BOOLEAN) {
816
18.7k
      return a->datum.real == b->datum.boolean;
817
18.7k
  }
818
2.90k
  break;
819
820
6.89k
    case CSI_OBJECT_TYPE_STRING:
821
6.89k
  if (btype == CSI_OBJECT_TYPE_NAME) {
822
1.65k
      const char *bstr = (const char *) b->datum.name;
823
1.65k
      return 0 == lexcmp (a->datum.string->string,
824
1.65k
        a->datum.string->len,
825
1.65k
        bstr,
826
1.65k
        strlen (bstr));
827
1.65k
  }
828
5.24k
  break;
829
830
5.24k
    default:
831
1.79k
  break;
832
55.1k
    }
833
834
9.94k
    return FALSE;
835
55.1k
}
836
837
csi_status_t
838
csi_object_compare (csi_object_t *a,
839
        csi_object_t *b,
840
        int *out)
841
66.3k
{
842
66.3k
    csi_object_type_t atype = csi_object_get_type (a);
843
66.3k
    csi_object_type_t btype = csi_object_get_type (b);
844
66.3k
    int sign;
845
846
66.3k
    if (csi_object_eq (a, b)){
847
12.0k
  *out = 0;
848
12.0k
  return CSI_STATUS_SUCCESS;
849
12.0k
    }
850
851
54.3k
#define CMP(x,y) ((x) < (y) ? -1 : +1)
852
853
54.3k
    if (atype == btype) {
854
51.8k
  switch (atype) {
855
289
  case CSI_OBJECT_TYPE_BOOLEAN:
856
289
      *out = CMP (a->datum.boolean, b->datum.boolean);
857
289
      return CSI_STATUS_SUCCESS;
858
51.3k
  case CSI_OBJECT_TYPE_INTEGER:
859
51.3k
      *out = CMP (a->datum.integer, b->datum.integer);
860
51.3k
      return CSI_STATUS_SUCCESS;
861
23
  case CSI_OBJECT_TYPE_REAL:
862
23
      *out = CMP (a->datum.real, b->datum.real);
863
23
      return CSI_STATUS_SUCCESS;
864
80
  case CSI_OBJECT_TYPE_NAME: {
865
80
      const char *x = (char const *) a->datum.name;
866
80
      const char *y = (char const *) b->datum.name;
867
80
      *out = lexcmp (x, strlen(x), y, strlen (y));
868
80
      return CSI_STATUS_SUCCESS;
869
0
  }
870
3
  case CSI_OBJECT_TYPE_STRING:
871
3
      *out = lexcmp (a->datum.string->string,
872
3
         a->datum.string->len,
873
3
         b->datum.string->string,
874
3
         b->datum.string->len);
875
3
      return CSI_STATUS_SUCCESS;
876
0
  case CSI_OBJECT_TYPE_NULL:
877
0
  case CSI_OBJECT_TYPE_MARK:
878
2
  case CSI_OBJECT_TYPE_OPERATOR:
879
3
  case CSI_OBJECT_TYPE_ARRAY:
880
6
  case CSI_OBJECT_TYPE_DICTIONARY:
881
6
  case CSI_OBJECT_TYPE_FILE:
882
7
  case CSI_OBJECT_TYPE_MATRIX:
883
7
  case CSI_OBJECT_TYPE_CONTEXT:
884
7
  case CSI_OBJECT_TYPE_FONT:
885
10
  case CSI_OBJECT_TYPE_PATTERN:
886
10
  case CSI_OBJECT_TYPE_SCALED_FONT:
887
10
  case CSI_OBJECT_TYPE_SURFACE:
888
10
      goto TYPE_CHECK_ERROR;
889
51.8k
  }
890
51.8k
    }
891
892
2.52k
    sign = +1;
893
2.52k
    if (atype < btype) {
894
1.85k
  csi_object_t *c;
895
1.85k
  csi_object_type_t ctype;
896
1.85k
  c = a; a = b; b = c;
897
1.85k
  ctype = atype; atype = btype; btype = ctype;
898
1.85k
  sign = -1;
899
1.85k
    }
900
901
2.52k
    switch ((int) atype) {
902
1.91k
    case CSI_OBJECT_TYPE_INTEGER:
903
1.91k
  if (btype == CSI_OBJECT_TYPE_BOOLEAN) {
904
1.91k
      *out = sign * CMP (a->datum.integer, !!b->datum.boolean);
905
1.91k
      return CSI_STATUS_SUCCESS;
906
1.91k
  }
907
1
  break;
908
489
    case CSI_OBJECT_TYPE_REAL:
909
489
  if (btype == CSI_OBJECT_TYPE_INTEGER) {
910
268
      *out = sign * CMP (a->datum.real, b->datum.integer);
911
268
      return CSI_STATUS_SUCCESS;
912
268
  }
913
221
  else if (btype == CSI_OBJECT_TYPE_BOOLEAN) {
914
219
      *out = sign * CMP (a->datum.real, !!b->datum.boolean);
915
219
      return CSI_STATUS_SUCCESS;
916
219
  }
917
2
  break;
918
919
106
    case CSI_OBJECT_TYPE_STRING:
920
106
  if (btype == CSI_OBJECT_TYPE_NAME) {
921
104
      const char *bstr = (const char *) b->datum.name;
922
104
      *out = sign * lexcmp (a->datum.string->string,
923
104
          a->datum.string->len,
924
104
          bstr,
925
104
          strlen (bstr));
926
104
      return CSI_STATUS_SUCCESS;
927
104
  }
928
2
  break;
929
930
14
    default:
931
14
  break;
932
2.52k
    }
933
934
19
#undef CMP
935
936
29
 TYPE_CHECK_ERROR:
937
29
    return _csi_error (CSI_STATUS_SCRIPT_INVALID_TYPE);
938
2.52k
}