summaryrefslogtreecommitdiff
path: root/src/couch/priv/couch_js/60/util.cpp
blob: c37c41f2faed517b674d57bbd8230c19467951f1 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include <stdlib.h>
#include <string.h>

#include <sstream>

#include <jsapi.h>
#include <js/Initialization.h>
#include <js/CharacterEncoding.h>
#include <js/Conversions.h>
#include <mozilla/Unused.h>

#include "help.h"
#include "util.h"

std::string
js_to_string(JSContext* cx, JS::HandleValue val)
{
    JS::AutoSaveExceptionState exc_state(cx);
    JS::RootedString sval(cx);
    sval = val.toString();

    JS::UniqueChars chars(JS_EncodeStringToUTF8(cx, sval));
    if(!chars) {
        JS_ClearPendingException(cx);
        return std::string();
    }

    return chars.get();
}

bool
js_to_string(JSContext* cx, JS::HandleValue val, std::string& str)
{
    if(!val.isString()) {
        return false;
    }

    if(JS_GetStringLength(val.toString()) == 0) {
        str = "";
        return true;
    }

    std::string conv = js_to_string(cx, val);
    if(!conv.size()) {
        return false;
    }

    str = conv;
    return true;
}

JSString*
string_to_js(JSContext* cx, const std::string& raw)
{
    JS::UTF8Chars utf8(raw.c_str(), raw.size());
    JS::UniqueTwoByteChars utf16;
    size_t len;

    utf16.reset(JS::UTF8CharsToNewTwoByteCharsZ(cx, utf8, &len).get());
    if(!utf16) {
        return nullptr;
    }

    JSString* ret = JS_NewUCString(cx, utf16.get(), len);

    if(ret) {
        // JS_NewUCString took ownership on success. We shift
        // the resulting pointer into Unused to silence the
        // compiler warning.
        mozilla::Unused << utf16.release();
    }

    return ret;
}

size_t
couch_readfile(const char* file, char** outbuf_p)
{
    FILE* fp;
    char fbuf[16384];
    char *buf = NULL;
    char* tmp;
    size_t nread = 0;
    size_t buflen = 0;

    if(strcmp(file, "-") == 0) {
        fp = stdin;
    } else {
        fp = fopen(file, "r");
        if(fp == NULL) {
            fprintf(stderr, "Failed to read file: %s\n", file);
            exit(3);
        }
    }

    while((nread = fread(fbuf, 1, 16384, fp)) > 0) {
        if(buf == NULL) {
            buf = new char[nread + 1];
            if(buf == NULL) {
                fprintf(stderr, "Out of memory.\n");
                exit(3);
            }
            memcpy(buf, fbuf, nread);
        } else {
            tmp = new char[buflen + nread + 1];
            if(tmp == NULL) {
                fprintf(stderr, "Out of memory.\n");
                exit(3);
            }
            memcpy(tmp, buf, buflen);
            memcpy(tmp+buflen, fbuf, nread);
            delete buf;
            buf = tmp;
        }
        buflen += nread;
        buf[buflen] = '\0';
    }
    *outbuf_p = buf;
    return buflen ;
}

couch_args*
couch_parse_args(int argc, const char* argv[])
{
    couch_args* args;
    int i = 1;

    args = new couch_args();
    if(args == NULL)
        return NULL;

    args->eval = 0;
    args->use_http = 0;
    args->use_test_funs = 0;
    args->stack_size = 64L * 1024L * 1024L;
    args->scripts = nullptr;
    args->uri_file = nullptr;
    args->uri = nullptr;

    while(i < argc) {
        if(strcmp("-h", argv[i]) == 0) {
            DISPLAY_USAGE;
            exit(0);
        } else if(strcmp("-V", argv[i]) == 0) {
            DISPLAY_VERSION;
            exit(0);
        } else if(strcmp("-H", argv[i]) == 0) {
            args->use_http = 1;
        } else if(strcmp("-T", argv[i]) == 0) {
            args->use_test_funs = 1;
        } else if(strcmp("-S", argv[i]) == 0) {
            args->stack_size = atoi(argv[++i]);
            if(args->stack_size <= 0) {
                fprintf(stderr, "Invalid stack size.\n");
                exit(2);
            }
        } else if(strcmp("-u", argv[i]) == 0) {
            args->uri_file = argv[++i];
        } else if(strcmp("--eval", argv[i]) == 0) {
            args->eval = 1;
        } else if(strcmp("--", argv[i]) == 0) {
            i++;
            break;
        } else {
            break;
        }
        i++;
    }

    if(i >= argc) {
        DISPLAY_USAGE;
        exit(3);
    }
    args->scripts = argv + i;

    return args;
}


int
couch_fgets(char* buf, int size, FILE* fp)
{
    int n, i, c;

    if(size <= 0) return -1;
    n = size - 1;

    for(i = 0; i < n && (c = getc(fp)) != EOF; i++) {
        buf[i] = c;
        if(c == '\n') {
            i++;
            break;
        }
    }

    buf[i] = '\0';
    return i;
}


JSString*
couch_readline(JSContext* cx, FILE* fp)
{
    JSString* str;
    char* bytes = NULL;
    char* tmp = NULL;
    size_t used = 0;
    size_t byteslen = 256;
    size_t oldbyteslen = 256;
    size_t readlen = 0;

    bytes = static_cast<char*>(JS_malloc(cx, byteslen));
    if(bytes == NULL) return NULL;
    
    while((readlen = couch_fgets(bytes+used, byteslen-used, fp)) > 0) {
        used += readlen;
        
        if(bytes[used-1] == '\n') {
            bytes[used-1] = '\0';
            break;
        }
        
        // Double our buffer and read more.
        oldbyteslen = byteslen;
        byteslen *= 2;
        tmp = static_cast<char*>(JS_realloc(cx, bytes, oldbyteslen, byteslen));
        if(!tmp) {
            JS_free(cx, bytes);
            return NULL;
        }
        
        bytes = tmp;
    }

    // Treat empty strings specially
    if(used == 0) {
        JS_free(cx, bytes);
        return JS_NewStringCopyZ(cx, nullptr);
    }

    // Shrink the buffer to the actual data size
    tmp = static_cast<char*>(JS_realloc(cx, bytes, byteslen, used));
    if(!tmp) {
        JS_free(cx, bytes);
        return NULL;
    }
    bytes = tmp;
    byteslen = used;

    str = string_to_js(cx, std::string(tmp));
    JS_free(cx, bytes);
    return str;
}


void
couch_print(JSContext* cx, JS::HandleValue obj, bool use_stderr)
{
    FILE* stream = stdout;

    if(use_stderr) {
        stream = stderr;
    }

    std::string val = js_to_string(cx, obj);
    fprintf(stream, "%s\n", val.c_str());
    fflush(stream);
}


void
couch_error(JSContext* cx, JSErrorReport* report)
{
    if(!report) {
        return;
    }

    if(JSREPORT_IS_WARNING(report->flags)) {
        return;
    }

    std::ostringstream msg;
    msg << "error: " << report->message().c_str();

    mozilla::Maybe<JSAutoCompartment> ac;
    JS::RootedValue exc(cx);
    JS::RootedObject exc_obj(cx);
    JS::RootedObject stack_obj(cx);
    JS::RootedString stack_str(cx);
    JS::RootedValue stack_val(cx);

    if(!JS_GetPendingException(cx, &exc)) {
        goto done;
    }

    // Clear the exception before an JS method calls or the result is
    // infinite, recursive error report generation.
    JS_ClearPendingException(cx);

    exc_obj.set(exc.toObjectOrNull());
    stack_obj.set(JS::ExceptionStackOrNull(exc_obj));

    if(!stack_obj) {
        // Compilation errors don't have a stack

        msg << " at ";

        if(report->filename) {
            msg << report->filename;
        } else {
            msg << "<unknown>";
        }

        if(report->lineno) {
            msg << ':' << report->lineno << ':' << report->column;
        }

        goto done;
    }

    if(!JS::BuildStackString(cx, stack_obj, &stack_str, 2)) {
        goto done;
    }

    stack_val.set(JS::StringValue(stack_str));
    msg << std::endl << std::endl << js_to_string(cx, stack_val).c_str();

done:
    msg << std::endl;
    fprintf(stderr, "%s", msg.str().c_str());
}


void
couch_oom(JSContext* cx, void* data)
{
    fprintf(stderr, "out of memory\n");
    exit(1);
}


bool
couch_load_funcs(JSContext* cx, JS::HandleObject obj, JSFunctionSpec* funcs)
{
    JSFunctionSpec* f;
    for(f = funcs; f->name != NULL; f++) {
        if(!JS_DefineFunction(cx, obj, f->name, f->call.op, f->nargs, f->flags)) {
            fprintf(stderr, "Failed to create function: %s\n", f->name);
            return false;
        }
    }
    return true;
}