summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.78.1/emscripten/postfix_module.js
blob: cbf8f57b8d9a34c91051f5c47008ffd2e8afa162 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    //! auto initializes the module
    Module["_initialize"](); //// execute this only one time,
    Module["_initialize"] = Function.prototype; // and replace it with a noop

    /**
     * Takes in a JS string with other params, converts it to UTF8 and
     * passes it to the actual _uncrustify function while also managing the
     * memory on the emscripten heap.
     */
    Module["uncrustify"] = function(str, langIDX, frag, defer)
    {
        if( !str || typeof(str) !== "string" || str.length === 0 ) {return "";}

        var nDataBytes = lengthBytesUTF8(str)+1; // +1 for \0
        var stringInputPtr = Module._malloc(nDataBytes);
        Module.stringToUTF8(str, stringInputPtr, nDataBytes);

        var retStringPointer = 0;

        switch(arguments.length)
        {
            // depending in the number of args the internal select_overload
            // function resolves the appropriate internal _uncrustify function
            case 2:
            {
                retStringPointer = Module["_uncrustify"](stringInputPtr, langIDX);
                break;
            }
            case 3:
            {
                retStringPointer = Module["_uncrustify"](stringInputPtr, langIDX, frag);
                break;
            }
//             case 4:
//             {
//                 retStringPointer = Module["_uncrustify"](stringInputPtr, langIDX, frag, defer);
//                 break;
//             }
            default:
            {
                break;
            }
        }

        Module._free(stringInputPtr);


        var retString = "";

        if(retStringPointer !== 0)
        {
            retString = Module.UTF8ToString(retStringPointer);
            Module._free(retStringPointer);
        }

        return retString;
    }

    /**
     * Takes in a JS string with other params, converts it to UTF8 and
     * passes it to the actual _debug function while also managing the
     * memory on the emscripten heap.
     */
    Module["debug"] = function(str, langIDX, frag)
    {
        if( !str || typeof(str) !== "string" || str.length === 0 ) {return "";}

        var nDataBytes = lengthBytesUTF8(str)+1; // +1 for \0
        var stringInputPtr = Module._malloc(nDataBytes);
        Module.stringToUTF8(str, stringInputPtr, nDataBytes);

        var retStringPointer = 0;

        switch(arguments.length)
        {
            // depending in the number of args the internal select_overload
            // function resolves the appropriate internal _uncrustify function
            case 2:
            {
                retStringPointer = Module["_debug"](stringInputPtr, langIDX);
                break;
            }
            case 3:
            {
                retStringPointer = Module["_debug"](stringInputPtr, langIDX, frag);
                break;
            }
            default:
            {
                break;
            }
        }

        Module._free(stringInputPtr);


        var retString = "";

        if(retStringPointer !== 0)
        {
            retString = Module.UTF8ToString(retStringPointer);
            Module._free(retStringPointer);
        }

        return retString;
    }

    /**
     * Takes in a JS string, removes non ascii chars (only those are needed
     * in a config) and passes it to the actual _loadConfig function while
     * also managing the memory on the emscripten heap.
     */
    Module.load_config = function(str)
    {
        // UTF8 functions return on empty string but they have to be accepted too
        // to reset the current config
        if( !str || typeof(str) !== "string" || str.length === 0) {str = " ";}

        //remove unneeded non asci chars in the config
        str.replace(/[^\x00-\x7F]/g, "");

        var nDataBytes = str.length+1; // +1 for \0
        var stringInputPtr = Module._malloc(nDataBytes);
        Module.writeAsciiToMemory(str, stringInputPtr);


        var retStringPointer = Module["_load_config"](stringInputPtr);
        Module._free(stringInputPtr);


        var retString = "";

        if(retStringPointer !== 0)
        {
            retString = Module.Pointer_stringify(retStringPointer);
            Module._free(retStringPointer);
        }

        return retString;
    }