summaryrefslogtreecommitdiff
path: root/src/js.c
blob: 4251f0380c203d37f6b68a71d015307929a0a263 (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
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2018 Daniel Carl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

#include <JavaScriptCore/JavaScript.h>
#include <glib.h>
#include <webkit2/webkit2.h>

#include "js.h"
#include "config.h"

/**
 * Returns a new allocates string for given value javascript result.
 * String must be freed if not used anymore.
 */
char *js_result_as_string(WebKitJavascriptResult *res)
{
    JSGlobalContextRef cr;
    JSStringRef jsstring;
    JSValueRef jsvalue;
    gsize max;

    g_return_val_if_fail(res != NULL, NULL);

    jsvalue  = webkit_javascript_result_get_value(res);
    cr       = webkit_javascript_result_get_global_context(res);
    jsstring = JSValueToStringCopy(cr, jsvalue, NULL);
    max      = JSStringGetMaximumUTF8CStringSize(jsstring);
    if (max > 0) {
        char *string = g_new(char, max);
        JSStringGetUTF8CString(jsstring, string, max);

        return string;
    }
    return NULL;
}