aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripting.c
blob: af5d99348a1ddb095a17f9caba6e0c48a8f2339d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "scripting.h"

void handle_tcc_error(void* opaque, const char* msg)
{
    TraceLog(LOG_ERROR, msg);
}

void initScripting(SldjScripting* scripting)
{
    scripting->state = tcc_new();
    scripting->lineScanner = NULL;
    scripting->fileBuf = NULL;
    
    if (!scripting->state)
    {
        TraceLog(LOG_ERROR, "Could not open tcc state");
        return;
    }

    tcc_set_error_func(scripting->state, stderr, handle_tcc_error);
}

void closeScripting(SldjScripting* scripting)
{
    tcc_delete(scripting->state);

    if (scripting->fileBuf != NULL)
    {
        free(scripting->fileBuf);
    }
}

void compileScript(SldjScripting* scripting, const char filePath[SLDJ_NAMEMAX])
{
    // Get file size.
    struct stat fileState;

    if (stat(filePath, &fileState) < 0)
    {
        TraceLog(LOG_ERROR, strerror(errno));
        return;
    }

    scripting->fileSize = fileState.st_size;

    // Load file information into a buffer.
    if (scripting->fileBuf == NULL)
    {
        scripting->fileBuf = (char*)malloc(scripting->fileSize);
    } else {
        scripting->fileBuf = (char*)realloc(scripting->fileBuf, scripting->fileSize);
    }

    if (scripting->fileBuf == NULL)
    {
        TraceLog(LOG_ERROR, strerror(errno));
        return;
    }

    FILE* file = fopen(filePath, "r");

    if (file == NULL)
    {
        TraceLog(LOG_ERROR, strerror(errno));
        return;
    }

    fread(scripting->fileBuf, scripting->fileSize, 1, file);
    scripting->fileBuf[scripting->fileSize - 1] = '\0';
    fclose(file);

    // Compile shit now.
    tcc_set_output_type(scripting->state, TCC_OUTPUT_MEMORY);

    if (tcc_compile_string(scripting->state, scripting->fileBuf) == -1)
    {
        return;
    }

    if (tcc_relocate(scripting->state) < 0)
    {
        return;
    }

    scripting->lineScanner = tcc_get_symbol(scripting->state, "lineScanner");
}