varnish-cache/lib/libvcc/vcc_vmod.c
0
/*-
1
 * Copyright (c) 2010-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Parse `import`, check metadata and versioning.
30
 *
31
 */
32
33
#include "config.h"
34
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include "vcc_compile.h"
39
40
#include "libvcc.h"
41
#include "vfil.h"
42
#include "vjsn.h"
43
#include "vmod_abi.h"
44
45
#include "vcc_vmod.h"
46
47
struct vmod_import {
48
        unsigned                        magic;
49
#define VMOD_IMPORT_MAGIC               0x31803a5d
50
        const char                      *err;
51
        struct vsb                      *json;
52
        char                            *path;
53
        VTAILQ_ENTRY(vmod_import)       list;
54
        int                             from_vext;
55
        int                             unimported_vext;
56
57
        // From $VMOD
58
        double                          vmod_syntax;
59
        char                            *name;
60
        char                            *func_name;
61
        char                            *file_id;
62
        char                            *abi;
63
        unsigned                        major;
64
        unsigned                        minor;
65
66
        struct symbol                   *sym;
67
        const struct token              *t_mod;
68
        struct vjsn                     *vj;
69
#define STANZA(UU, ll, ss)              int n_##ll;
70
        STANZA_TBL
71
#undef STANZA
72
};
73
74
static VTAILQ_HEAD(,vmod_import) imports = VTAILQ_HEAD_INITIALIZER(imports);
75
76
typedef void vcc_do_stanza_f(struct vcc *tl, const struct vmod_import *vim,
77
    const struct vjsn_val *vv);
78
79
static int
80 7826
vcc_Extract_JSON(struct vmod_import *vim, const char *filename)
81
{
82 7826
        const char *magic = "VMOD_JSON_SPEC\x02", *p;
83
        int c;
84
        FILE *f;
85
86 7826
        CHECK_OBJ_NOTNULL(vim, VMOD_IMPORT_MAGIC);
87 7826
        AN(filename);
88
89 7826
        f = fopen(filename, "rb");
90 7826
        if (f == NULL) {
91 13
                vim->err = strerror(errno);
92 13
                return (-1);
93
        }
94
95 7813
        p = magic;
96 7813
        vim->err = "No VMOD JSON found";
97 632073
        while (1) {
98 158215980
                c = getc(f);
99 158215980
                if (c == EOF) {
100 13
                        AZ(fclose(f));
101 13
                        vim->err = "No VMOD JSON found";
102 13
                        return (-1);
103
                }
104 158215967
                if (c != *p) {
105 157583907
                        p = magic;
106 157583907
                        continue;
107
                }
108 632060
                p++;
109 632060
                if (*p == '\0')
110 7800
                        break;
111
        }
112
113 7800
        vim->json = VSB_new_auto();
114 7800
        AN(vim->json);
115
116 156559819
        while (1) {
117 156559819
                c = getc(f);
118 156559819
                if (c == EOF) {
119 13
                        AZ(fclose(f));
120 13
                        vim->err = "Truncated VMOD JSON";
121 13
                        VSB_destroy(&vim->json);
122 13
                        return (-1);
123
                }
124 156559806
                if (c == '\x03')
125 7787
                        break;
126 156552019
                VSB_putc(vim->json, c);
127
        }
128 7787
        AZ(fclose(f));
129 7787
        AZ(VSB_finish(vim->json));
130 7787
        return (0);
131 7826
}
132
133
static const char *
134 7787
vcc_ParseJSON(const struct vcc *tl, const char *jsn, struct vmod_import *vim)
135
{
136
        const struct vjsn_val *vv, *vv2, *vv3;
137
        const char *err;
138
        char *p;
139
140 7787
        vim->vj = vjsn_parse(jsn, &err);
141 7787
        if (err != NULL)
142 13
                return (err);
143 7774
        AN(vim->vj);
144
145 7774
        vv = vim->vj->value;
146 7774
        if (!vjsn_is_array(vv))
147 13
                return ("Not array[0]");
148
149 7761
        vv2 = VTAILQ_FIRST(&vv->children);
150 7761
        AN(vv2);
151 7761
        if (!vjsn_is_array(vv2))
152 13
                return ("Not array[1]");
153 7748
        vv3 = VTAILQ_FIRST(&vv2->children);
154 7748
        AN(vv3);
155 7748
        if (!vjsn_is_string(vv3))
156 13
                return ("Not string[2]");
157 7735
        if (strcmp(vv3->value, "$VMOD"))
158 13
                return ("Not $VMOD[3]");
159
160 7722
        vv3 = VTAILQ_NEXT(vv3, list);
161 7722
        AN(vv3);
162 7722
        assert(vjsn_is_string(vv3));
163 7722
        vim->vmod_syntax = strtod(vv3->value, NULL);
164 7722
        if (vim->vmod_syntax != 2.0)
165 13
                return ("Syntax != 2.0");
166
167 7709
        vv3 = VTAILQ_NEXT(vv3, list);
168 7709
        AN(vv3);
169 7709
        assert(vjsn_is_string(vv3));
170 7709
        vim->name = vv3->value;
171
172 7709
        vv3 = VTAILQ_NEXT(vv3, list);
173 7709
        AN(vv3);
174 7709
        assert(vjsn_is_string(vv3));
175 7709
        vim->func_name = vv3->value;
176
177 7709
        vv3 = VTAILQ_NEXT(vv3, list);
178 7709
        AN(vv3);
179 7709
        assert(vjsn_is_string(vv3));
180 7709
        vim->file_id = vv3->value;
181
182 7709
        vv3 = VTAILQ_NEXT(vv3, list);
183 7709
        AN(vv3);
184 7709
        assert(vjsn_is_string(vv3));
185 7709
        vim->abi = vv3->value;
186
187 7709
        vv3 = VTAILQ_NEXT(vv3, list);
188 7709
        AN(vv3);
189 7709
        assert(vjsn_is_string(vv3));
190 7709
        vim->major = strtoul(vv3->value, &p, 10);
191 7709
        assert(p == NULL || *p == '\0' || *p == 'U');
192
193 7709
        vv3 = VTAILQ_NEXT(vv3, list);
194 7709
        AN(vv3);
195 7709
        assert(vjsn_is_string(vv3));
196 7709
        vim->minor = strtoul(vv3->value, &p, 10);
197 7709
        assert(p == NULL || *p == '\0' || *p == 'U');
198
199
200 7709
        if (vim->major == 0 && vim->minor == 0 &&
201 5928
            strcmp(vim->abi, VMOD_ABI_Version)) {
202 13
                VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(vim->t_mod));
203 13
                VSB_printf(tl->sb, "\tFile name: %s\n", vim->path);
204 26
                VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n",
205 13
                           VMOD_ABI_Version, vim->abi);
206 13
                return ("");
207
        }
208 9464
        if (vim->major != 0 &&
209 1781
            (vim->major != VRT_MAJOR_VERSION ||
210 1768
            vim->minor > VRT_MINOR_VERSION)) {
211 13
                VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(vim->t_mod));
212 13
                VSB_printf(tl->sb, "\tFile name: %s\n", vim->path);
213 26
                VSB_printf(tl->sb, "\tVMOD wants ABI version %u.%u\n",
214 13
                    vim->major, vim->minor);
215 13
                VSB_printf(tl->sb, "\tvarnishd provides ABI version %u.%u\n",
216
                    VRT_MAJOR_VERSION, VRT_MINOR_VERSION);
217 13
                return ("");
218
        }
219
220
221 282620
        VTAILQ_FOREACH(vv2, &vv->children, list) {
222 274950
                assert (vjsn_is_array(vv2));
223 274950
                vv3 = VTAILQ_FIRST(&vv2->children);
224 274950
                assert(vjsn_is_string(vv3));
225 274950
                assert(vv3->value[0] == '$');
226
#define STANZA(UU, ll, ss) \
227
    if (!strcmp(vv3->value, "$" #UU)) {vim->n_##ll++; continue;}
228 274950
                STANZA_TBL
229
#undef STANZA
230 13
                return ("Unknown metadata stanza.");
231
        }
232 7670
        if (vim->n_cproto != 1)
233 13
                return ("Bad cproto stanza(s)");
234 7657
        if (vim->n_vmod != 1)
235 13
                return ("Bad vmod stanza(s)");
236 7644
        return (NULL);
237 7787
}
238
239
/*
240
 * Load and check the metadata from the objectfile containing the vmod
241
 */
242
243
static int
244 7787
vcc_VmodLoad(struct vcc *tl, struct vmod_import *vim)
245
{
246
        static const char *err;
247
        struct vmod_import *vim2;
248
249 7787
        CHECK_OBJ_NOTNULL(vim, VMOD_IMPORT_MAGIC);
250
251 7787
        err = vcc_ParseJSON(tl, VSB_data(vim->json), vim);
252 7787
        if (err != NULL && *err != '\0') {
253 234
                VSB_printf(tl->sb,
254 117
                    "VMOD %.*s: bad metadata\n", PF(vim->t_mod));
255 117
                VSB_printf(tl->sb, "\t(%s)\n", err);
256 117
                VSB_printf(tl->sb, "\tFile name: %s\n", vim->path);
257 117
        }
258
259 7787
        if (err != NULL)
260 143
                return (-1);
261
262 9542
        VTAILQ_FOREACH(vim2, &imports, list) {
263 1950
                if (strcmp(vim->name, vim2->name))
264 1898
                        continue;
265 52
                if (!strcmp(vim->file_id, vim2->file_id)) {
266
                        // (Truly) duplicate imports are OK
267 39
                        return (0);
268
                }
269 26
                VSB_printf(tl->sb,
270
                    "Different version of VMOD %.*s already loaded\n",
271 13
                    PF(vim->t_mod));
272 13
                vcc_ErrWhere(tl, vim->t_mod);
273 13
                VSB_cat(tl->sb, "Previous import at:\n");
274 13
                vcc_ErrWhere(tl, vim2->t_mod);
275 13
                vcc_Warn(tl);
276 13
                break;
277
        }
278 7605
        VTAILQ_INSERT_TAIL(&imports, vim, list);
279
280 7605
        return (0);
281 7787
}
282
283
static void v_matchproto_(vcc_do_stanza_f)
284 1638
vcc_do_event(struct vcc *tl, const struct vmod_import *vim,
285
    const struct vjsn_val *vv)
286
{
287
        struct inifin *ifp;
288
289 1638
        ifp = New_IniFin(tl);
290 3276
        VSB_printf(ifp->ini,
291
            "\tif (%s(ctx, &vmod_priv_%s, VCL_EVENT_LOAD))\n"
292
            "\t\treturn(1);",
293 1638
            vv->value, vim->sym->vmod_name);
294 3276
        VSB_printf(ifp->fin,
295
            "\t\t(void)%s(ctx, &vmod_priv_%s,\n"
296
            "\t\t\t    VCL_EVENT_DISCARD);",
297 1638
            vv->value, vim->sym->vmod_name);
298 3276
        VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%s, ev)",
299 1638
            vv->value, vim->sym->vmod_name);
300 1638
}
301
302
static void v_matchproto_(vcc_do_stanza_f)
303 7553
vcc_do_cproto(struct vcc *tl, const struct vmod_import *vim,
304
    const struct vjsn_val *vv)
305
{
306 7553
        (void)vim;
307 7553
        do {
308 1479062
                assert (vjsn_is_string(vv));
309 1479062
                Fh(tl, 0, "%s\n", vv->value);
310 1479062
                vv = VTAILQ_NEXT(vv, list);
311 1479062
        } while(vv != NULL);
312 7553
}
313
314
static void
315 15106
vcc_vj_foreach(struct vcc *tl, const struct vmod_import *vim,
316
    const char *stanza, vcc_do_stanza_f *func)
317
{
318
        const struct vjsn_val *vv, *vv2, *vv3;
319
320 15106
        vv = vim->vj->value;
321 15106
        assert (vjsn_is_array(vv));
322 554346
        VTAILQ_FOREACH(vv2, &vv->children, list) {
323 539240
                assert (vjsn_is_array(vv2));
324 539240
                vv3 = VTAILQ_FIRST(&vv2->children);
325 539240
                assert (vjsn_is_string(vv3));
326 539240
                if (!strcmp(vv3->value, stanza))
327 9191
                        func(tl, vim, VTAILQ_NEXT(vv3, list));
328 539240
        }
329 15106
}
330
331
static void
332 7553
vcc_emit_setup(struct vcc *tl, const struct vmod_import *vim)
333
{
334
        struct inifin *ifp;
335 7553
        const struct token *mod = vim->t_mod;
336
337 7553
        ifp = New_IniFin(tl);
338
339 7553
        VSB_cat(ifp->ini, "\tif (VPI_Vmod_Init(ctx,\n");
340 7553
        VSB_printf(ifp->ini, "\t    &VGC_vmod_%.*s,\n", PF(mod));
341 7553
        VSB_printf(ifp->ini, "\t    %u,\n", tl->vmod_count++);
342 7553
        VSB_printf(ifp->ini, "\t    &%s,\n", vim->func_name);
343 7553
        VSB_printf(ifp->ini, "\t    sizeof(%s),\n", vim->func_name);
344 7553
        VSB_printf(ifp->ini, "\t    \"%.*s\",\n", PF(mod));
345 7553
        VSB_cat(ifp->ini, "\t    ");
346 7553
        VSB_quote(ifp->ini, vim->path, -1, VSB_QUOTE_CSTR);
347 7553
        VSB_cat(ifp->ini, ",\n");
348 7553
        AN(vim->file_id);
349 7553
        VSB_printf(ifp->ini, "\t    \"%s\",\n", vim->file_id);
350 7553
        if (vim->from_vext) {
351 13
                VSB_cat(ifp->ini, "\t    ");
352 13
                VSB_quote(ifp->ini, vim->path, -1, VSB_QUOTE_CSTR);
353 13
                VSB_cat(ifp->ini, "\n");
354 13
        } else {
355 15080
                VSB_printf(ifp->ini, "\t    \"./vmod_cache/_vmod_%.*s.%s\"\n",
356 7540
                    PF(mod), vim->file_id);
357
        }
358 7553
        VSB_cat(ifp->ini, "\t    ))\n");
359 7553
        VSB_cat(ifp->ini, "\t\treturn(1);");
360
361 7553
        VSB_cat(tl->symtab, ",\n    {\n");
362 7553
        VSB_cat(tl->symtab, "\t\"dir\": \"import\",\n");
363 7553
        VSB_cat(tl->symtab, "\t\"type\": \"$VMOD\",\n");
364 7553
        VSB_printf(tl->symtab, "\t\"name\": \"%.*s\",\n", PF(mod));
365 7553
        if (vim->from_vext)
366 13
                VSB_cat(tl->symtab, "\t\"vext\": true,\n");
367
        else
368 7540
                VSB_cat(tl->symtab, "\t\"vext\": false,\n");
369 7553
        VSB_printf(tl->symtab, "\t\"file\": \"%s\",\n", vim->path);
370 15106
        VSB_printf(tl->symtab, "\t\"dst\": \"./vmod_cache/_vmod_%.*s.%s\"\n",
371 7553
            PF(mod), vim->file_id);
372 7553
        VSB_cat(tl->symtab, "    }");
373
374
        /* XXX: zero the function pointer structure ?*/
375 15106
        VSB_printf(ifp->fin, "\t\tVRT_priv_fini(ctx, &vmod_priv_%.*s);",
376 7553
            PF(mod));
377 15106
        VSB_printf(ifp->final, "\t\tVPI_Vmod_Unload(ctx, &VGC_vmod_%.*s);",
378 7553
            PF(mod));
379
380 7553
        vcc_vj_foreach(tl, vim, "$EVENT", vcc_do_event);
381
382 7553
        Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod));
383 7553
        Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod));
384 7553
        Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod));
385
386 7553
        vcc_vj_foreach(tl, vim, "$CPROTO", vcc_do_cproto);
387
388 7553
        Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod));
389 7553
}
390
391
static void
392 273
vcc_vim_destroy(struct vmod_import **vimp)
393
{
394
        struct vmod_import *vim;
395
396 273
        TAKE_OBJ_NOTNULL(vim, vimp, VMOD_IMPORT_MAGIC);
397 273
        if (vim->path)
398 260
                free(vim->path);
399 273
        if (vim->vj)
400 208
                vjsn_delete(&vim->vj);
401 273
        if (vim->json)
402 221
                VSB_destroy(&vim->json);
403 273
        FREE_OBJ(vim);
404 273
}
405
406
static int
407 7813
vcc_path_open(void *priv, const char *fn)
408
{
409
        struct vmod_import *vim;
410
411 7813
        CAST_OBJ_NOTNULL(vim, priv, VMOD_IMPORT_MAGIC);
412 7813
        AN(fn);
413
414 7813
        return (vcc_Extract_JSON(vim, fn));
415
}
416
417
void
418 7878
vcc_ParseImport(struct vcc *tl)
419
{
420
        char fn[1024];
421
        const char *p;
422
        struct token *mod, *tmod, *t1;
423
        struct symbol *msym, *vsym;
424 7878
        struct vmod_import *vim = NULL;
425
        const struct vmod_import *vimold;
426
427 7878
        t1 = tl->t;
428 7878
        SkipToken(tl, ID);              /* "import" */
429
430 7878
        ExpectErr(tl, ID);              /* "vmod_name" */
431 7878
        mod = tl->t;
432 7878
        tmod = vcc_PeekTokenFrom(tl, mod);
433 7878
        AN(tmod);
434 7878
        if (tmod->tok == ID && vcc_IdIs(tmod, "as")) {
435 65
                vcc_NextToken(tl);              /* "vmod_name" */
436 65
                vcc_NextToken(tl);              /* "as" */
437 65
                ExpectErr(tl, ID);              /* "vcl_name" */
438 65
        }
439 7878
        tmod = tl->t;
440
441 7878
        msym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VMOD, SYMTAB_CREATE, XREF_NONE);
442 7878
        ERRCHK(tl);
443 7865
        AN(msym);
444
445 7865
        bprintf(fn, "libvmod_%.*s.so", PF(mod));
446 7865
        if (tl->t->tok == ID) {
447 65
                if (!vcc_IdIs(tl->t, "from")) {
448 13
                        VSB_cat(tl->sb, "Expected 'from path ...'\n");
449 13
                        vcc_ErrWhere(tl, tl->t);
450 13
                        return;
451
                }
452 52
                vcc_NextToken(tl);
453 52
                if (!tl->unsafe_path && strchr(tl->t->dec, '/')) {
454 13
                        VSB_cat(tl->sb,
455
                            "'import ... from path ...' is unsafe.\nAt:");
456 13
                        vcc_ErrToken(tl, tl->t);
457 13
                        vcc_ErrWhere(tl, tl->t);
458 13
                        return;
459
                }
460 39
                ExpectErr(tl, CSTR);
461 39
                p = strrchr(tl->t->dec, '/');
462 39
                if (p != NULL && p[1] == '\0')
463 13
                        bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod));
464
                else
465 26
                        bprintf(fn, "%s", tl->t->dec);
466 39
                vcc_NextToken(tl);
467 39
        } else {
468 9776
                VTAILQ_FOREACH(vim, &imports, list) {
469 1989
                        if (!vcc_IdIs(mod, vim->name))
470 1937
                                continue;
471 52
                        if (!vim->unimported_vext)
472 39
                                continue;
473 13
                        fprintf(stderr, "IMPORT %s from VEXT\n", vim->name);
474 13
                        vim->unimported_vext = 0;
475 13
                        vim->t_mod = mod;
476 13
                        vim->sym = msym;
477 13
                        break;
478
                }
479
        }
480
481 7839
        SkipToken(tl, ';');
482
483 7839
        if (vim == NULL) {
484 7826
                ALLOC_OBJ(vim, VMOD_IMPORT_MAGIC);
485 7826
                AN(vim);
486 7826
                vim->t_mod = mod;
487 7826
                vim->sym = msym;
488
489 7826
                if (VFIL_searchpath(tl->vmod_path, vcc_path_open, vim, fn, &vim->path)) {
490 52
                        if (vim->err == NULL) {
491 26
                                VSB_printf(tl->sb,
492 13
                                    "Could not find VMOD %.*s\n", PF(mod));
493 13
                        } else {
494 78
                                VSB_printf(tl->sb,
495 39
                                    "Could not open VMOD %.*s\n", PF(mod));
496 78
                                VSB_printf(tl->sb, "\tFile name: %s\n",
497 39
                                    vim->path != NULL ? vim->path : fn);
498 39
                                VSB_printf(tl->sb, "\tError: %s\n", vim->err);
499
                        }
500 52
                        vcc_ErrWhere(tl, mod);
501 52
                        vcc_vim_destroy(&vim);
502 52
                        return;
503
                }
504
505 7774
                if (vcc_VmodLoad(tl, vim) < 0 || tl->err) {
506 143
                        vcc_ErrWhere(tl, vim->t_mod);
507 143
                        vcc_vim_destroy(&vim);
508 143
                        return;
509
                }
510 7631
        }
511
512 7644
        if (!vcc_IdIs(vim->t_mod, vim->name)) {
513 26
                vcc_ErrWhere(tl, vim->t_mod);
514 52
                VSB_printf(tl->sb, "Wrong file for VMOD %.*s\n",
515 26
                    PF(vim->t_mod));
516 26
                VSB_printf(tl->sb, "\tFile name: %s\n", vim->path);
517 26
                VSB_printf(tl->sb, "\tContains vmod \"%s\"\n", vim->name);
518 26
                vcc_vim_destroy(&vim);
519 26
                return;
520
        }
521
522 7618
        vimold = msym->import;
523 7618
        if (vimold != NULL) {
524 39
                CHECK_OBJ(vimold, VMOD_IMPORT_MAGIC);
525 39
                if (!strcmp(vimold->file_id, vim->file_id)) {
526
                        /* Identical import is OK */
527 26
                } else {
528 26
                        VSB_printf(tl->sb,
529
                            "Another module already imported as %.*s.\n",
530 13
                            PF(tmod));
531 13
                        vcc_ErrWhere2(tl, t1, tl->t);
532
                }
533 39
                vcc_vim_destroy(&vim);
534 39
                return;
535
        }
536 7579
        msym->def_b = t1;
537 7579
        msym->def_e = tl->t;
538
539 9451
        VTAILQ_FOREACH(vsym, &tl->sym_vmods, sideways) {
540 1885
                assert(vsym->kind == SYM_VMOD);
541 1885
                vimold = vsym->import;
542 1885
                CHECK_OBJ_NOTNULL(vimold, VMOD_IMPORT_MAGIC);
543 1885
                if (!strcmp(vimold->file_id, vim->file_id)) {
544
                        /* Already loaded under different name */
545 13
                        msym->eval_priv = vsym->eval_priv;
546 13
                        msym->import = vsym->import;
547 13
                        msym->vmod_name = vsym->vmod_name;
548 13
                        vcc_VmodSymbols(tl, msym);
549 13
                        AZ(tl->err);
550
                        // XXX: insert msym in sideways ?
551 13
                        vcc_vim_destroy(&vim);
552 13
                        return;
553
                }
554 1872
        }
555
556 7566
        VTAILQ_INSERT_TAIL(&tl->sym_vmods, msym, sideways);
557
558 7566
        msym->eval_priv = vim->vj;
559 7566
        msym->import = vim;
560 7566
        msym->vmod_name = TlDup(tl, vim->name);
561 7566
        vcc_VmodSymbols(tl, msym);
562 7566
        ERRCHK(tl);
563
564 7553
        vcc_emit_setup(tl, vim);
565 7878
}
566
567
void
568 13
vcc_ImportVext(struct vcc *tl, const char *filename)
569
{
570
        struct vmod_import *vim;
571
572 13
        ALLOC_OBJ(vim, VMOD_IMPORT_MAGIC);
573 13
        AN(vim);
574
575 13
        if (vcc_Extract_JSON(vim, filename)) {
576 0
                FREE_OBJ(vim);
577 0
                return;
578
        }
579 13
        fprintf(stderr, "FOUND VMOD in VEXT %s\n", filename);
580 13
        if (vcc_VmodLoad(tl, vim) < 0 || tl->err) {
581
                // vcc_ErrWhere(tl, vim->t_mod);
582 0
                vcc_vim_destroy(&vim);
583 0
                return;
584
        }
585 13
        vim->from_vext = 1;
586 13
        vim->unimported_vext = 1;
587 13
        vim->path = strdup(filename);
588 13
        vim->path += 1;
589 13
        AN(vim->path);
590 13
        fprintf(stderr, "GOOD VMOD %s in VEXT %s\n", vim->name, filename);
591 13
}