From 6e7e3a1266271041ca794252cf0396764c5638f1 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sun, 12 Jan 2014 01:14:39 +0100 Subject: Moved JavaScript related function to own file. --- src/ex.c | 3 +- src/hints.c | 80 +------------------------- src/js.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/js.h | 34 +++++++++++ src/main.c | 58 +------------------ src/main.h | 1 - src/setting.c | 5 +- 7 files changed, 222 insertions(+), 138 deletions(-) create mode 100644 src/js.c create mode 100644 src/js.h (limited to 'src') diff --git a/src/ex.c b/src/ex.c index fd602ba..2b6c5ab 100644 --- a/src/ex.c +++ b/src/ex.c @@ -38,6 +38,7 @@ #include "bookmark.h" #include "shortcut.h" #include "map.h" +#include "js.h" typedef enum { EX_BMA, @@ -695,7 +696,7 @@ static gboolean ex_eval(const ExArg *arg) return false; } - success = vb_eval_script( + success = js_eval( webkit_web_view_get_main_frame(vb.gui.webview), arg->rhs->str, NULL, &value ); if (success) { diff --git a/src/hints.c b/src/hints.c index 07e4ffd..7e50c1c 100644 --- a/src/hints.c +++ b/src/hints.c @@ -29,6 +29,7 @@ #include "mode.h" #include "input.h" #include "map.h" +#include "js.h" #define HINT_FILE "hints.js" @@ -44,10 +45,7 @@ static struct { extern VbCore vb; -static JSObjectRef create_hints_object(WebKitWebFrame *frame, const char *script); static void call_hints_function(const char *func, int count, JSValueRef params[]); -static char *js_ref_to_string(JSContextRef context, JSValueRef ref); -static JSValueRef js_string_to_ref(JSContextRef ctx, const char *string); void hints_init(WebKitWebFrame *frame) @@ -57,7 +55,7 @@ void hints_init(WebKitWebFrame *frame) hints.obj = NULL; } if (!hints.obj) { - hints.obj = create_hints_object(frame, HINTS_JS); + hints.obj = js_create_object(frame, HINTS_JS); hints.ctx = webkit_web_frame_get_global_context(frame); } } @@ -246,59 +244,9 @@ gboolean hints_parse_prompt(const char *prompt, char *mode, gboolean *is_gmode) return res; } -static JSObjectRef create_hints_object(WebKitWebFrame *frame, const char *script) -{ - if (!script) { - return NULL; - } - - JSStringRef str, file; - JSValueRef result, exc = NULL; - JSObjectRef object; - - JSContextRef ctx = webkit_web_frame_get_global_context(frame); - str = JSStringCreateWithUTF8CString(script); - file = JSStringCreateWithUTF8CString(HINT_FILE); - result = JSEvaluateScript(ctx, str, NULL, file, 0, &exc); - JSStringRelease(str); - JSStringRelease(file); - if (exc) { - return NULL; - } - - object = JSValueToObject(ctx, result, &exc); - if (exc) { - return NULL; - } - JSValueProtect(ctx, result); - - return object; -} - static void call_hints_function(const char *func, int count, JSValueRef params[]) { - JSValueRef js_ret, function; - JSObjectRef function_object; - JSStringRef js_func = NULL; - char *value; - - if (!hints.obj) { - return; - } - - js_func = JSStringCreateWithUTF8CString(func); - - if (!JSObjectHasProperty(hints.ctx, hints.obj, js_func)) { - JSStringRelease(js_func); - return; - } - - function = JSObjectGetProperty(hints.ctx, hints.obj, js_func, NULL); - function_object = JSValueToObject(hints.ctx, function, NULL); - js_ret = JSObjectCallAsFunction(hints.ctx, function_object, NULL, count, params, NULL); - JSStringRelease(js_func); - - value = js_ref_to_string(hints.ctx, js_ret); + char *value = js_object_call_function(hints.ctx, hints.obj, func, count, params); if (!strncmp(value, "OVER:", 5)) { g_signal_emit_by_name( @@ -371,25 +319,3 @@ static void call_hints_function(const char *func, int count, JSValueRef params[] } g_free(value); } - -/* FIXME duplicate to main.c */ -static char *js_ref_to_string(JSContextRef ctx, JSValueRef ref) -{ - char *string; - JSStringRef str_ref = JSValueToStringCopy(ctx, ref, NULL); - size_t len = JSStringGetMaximumUTF8CStringSize(str_ref); - - string = g_new0(char, len); - JSStringGetUTF8CString(str_ref, string, len); - JSStringRelease(str_ref); - - return string; -} - -static JSValueRef js_string_to_ref(JSContextRef ctx, const char *string) -{ - JSStringRef js = JSStringCreateWithUTF8CString(string); - JSValueRef ret = JSValueMakeString(ctx, js); - JSStringRelease(js); - return ret; -} diff --git a/src/js.c b/src/js.c new file mode 100644 index 0000000..458fdb5 --- /dev/null +++ b/src/js.c @@ -0,0 +1,179 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2014 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 "config.h" +#include "js.h" + + +static gboolean evaluate_string(JSContextRef ctx, const char *script, + const char *file, JSValueRef *result); + +/** + * Run scripts out of given file in the given frame. + */ +gboolean js_eval_file(WebKitWebFrame *frame, const char *file) +{ + char *js = NULL, *value = NULL; + GError *error = NULL; + + if (g_file_test(file, G_FILE_TEST_IS_REGULAR) + && g_file_get_contents(file, &js, NULL, &error) + ) { + gboolean success = js_eval(frame, js, file, &value); + if (!success) { + fprintf(stderr, "%s", value); + } + g_free(value); + g_free(js); + + return success; + } + + return false; +} + +/** + * Evaluates given string as script and return if this call succeed or not. + * On success the given **value pointer is filles with the returned string, + * else with the exception message. In both cases this must be freed by the + * caller if no longer used. + */ +gboolean js_eval(WebKitWebFrame *frame, const char *script, const char *file, + char **value) +{ + gboolean success; + JSValueRef result = NULL; + JSContextRef ctx = webkit_web_frame_get_global_context(frame); + + success = evaluate_string(ctx, script, file, &result); + *value = js_ref_to_string(ctx, result); + + return success; +} + +/** + * Creates a JavaScript object in contect of given frame. + */ +JSObjectRef js_create_object(WebKitWebFrame *frame, const char *script) +{ + JSValueRef result = NULL, exc = NULL; + JSObjectRef object; + JSContextRef ctx = webkit_web_frame_get_global_context(frame); + + if (!evaluate_string(ctx, script, NULL, &result)) { + return NULL; + } + + object = JSValueToObject(ctx, result, &exc); + if (exc) { + return NULL; + } + JSValueProtect(ctx, result); + + return object; +} + +/** + * Calls a function on object and returns the result as newly allocates + * string. + * Returned string must be freed after use. + */ +char* js_object_call_function(JSContextRef ctx, JSObjectRef obj, + const char *func, int count, JSValueRef params[]) +{ + JSValueRef js_ret, function; + JSObjectRef function_object; + JSStringRef js_func = NULL; + char *value; + + if (!obj) { + return NULL; + } + + js_func = JSStringCreateWithUTF8CString(func); + if (!JSObjectHasProperty(ctx, obj, js_func)) { + JSStringRelease(js_func); + + return NULL; + } + + function = JSObjectGetProperty(ctx, obj, js_func, NULL); + function_object = JSValueToObject(ctx, function, NULL); + js_ret = JSObjectCallAsFunction(ctx, function_object, NULL, count, params, NULL); + JSStringRelease(js_func); + + value = js_ref_to_string(ctx, js_ret); + + return value; +} + +/** + * Retrune a new allocates string for given valeu reference. + * String must be freed if not used anymore. + */ +char* js_ref_to_string(JSContextRef ctx, JSValueRef ref) +{ + char *string; + JSStringRef str_ref = JSValueToStringCopy(ctx, ref, NULL); + size_t len = JSStringGetMaximumUTF8CStringSize(str_ref); + + string = g_new0(char, len); + JSStringGetUTF8CString(str_ref, string, len); + JSStringRelease(str_ref); + + return string; +} + +/** + * Retrieves a values reference for given string. + */ +JSValueRef js_string_to_ref(JSContextRef ctx, const char *string) +{ + JSStringRef js = JSStringCreateWithUTF8CString(string); + JSValueRef ref = JSValueMakeString(ctx, js); + JSStringRelease(js); + return ref; +} + +/** + * Runs a string as JavaScript and returns if the call succeed. + * In case the call succeed, the given *result is filled with the result + * value, else with the value reference of the exception. + */ +static gboolean evaluate_string(JSContextRef ctx, const char *script, + const char *file, JSValueRef *result) +{ + JSStringRef js_str, js_file; + JSValueRef exc = NULL, res = NULL; + + js_str = JSStringCreateWithUTF8CString(script); + js_file = JSStringCreateWithUTF8CString(file); + + res = JSEvaluateScript(ctx, js_str, JSContextGetGlobalObject(ctx), js_file, 0, &exc); + JSStringRelease(js_file); + JSStringRelease(js_str); + + if (exc) { + *result = exc; + return false; + } + + *result = res; + return true; +} diff --git a/src/js.h b/src/js.h new file mode 100644 index 0000000..7ab81c8 --- /dev/null +++ b/src/js.h @@ -0,0 +1,34 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2014 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/. + */ + +#ifndef _JS_H +#define _JS_H + +#include "main.h" + +gboolean js_eval_file(WebKitWebFrame *frame, const char *file); +gboolean js_eval(WebKitWebFrame *frame, const char *script, const char *file, + char **value); +JSObjectRef js_create_object(WebKitWebFrame *frame, const char *script); +char* js_object_call_function(JSContextRef ctx, JSObjectRef obj, + const char *func, int count, JSValueRef params[]); +char *js_ref_to_string(JSContextRef ctx, JSValueRef ref); +JSValueRef js_string_to_ref(JSContextRef ctx, const char *string); + +#endif /* end of include guard: _JS_H */ diff --git a/src/main.c b/src/main.c index 6161fcb..14d14ca 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,7 @@ #include "default.h" #include "pass.h" #include "bookmark.h" +#include "js.h" /* variables */ static char **args; @@ -68,8 +69,6 @@ static void download_progress_cp(WebKitDownload *download, GParamSpec *pspec); /* functions */ static void update_title(void); -static void run_user_script(WebKitWebFrame *frame); -static char *jsref_to_string(JSContextRef context, JSValueRef ref); static void init_core(void); static void read_config(void); static void setup_signals(); @@ -143,29 +142,6 @@ char *vb_get_input_text(void) return gtk_text_buffer_get_text(vb.gui.buffer, &start, &end, false); } -gboolean vb_eval_script(WebKitWebFrame *frame, char *script, char *file, char **value) -{ - JSStringRef str, file_name; - JSValueRef exception = NULL, result = NULL; - JSContextRef js; - - js = webkit_web_frame_get_global_context(frame); - str = JSStringCreateWithUTF8CString(script); - file_name = JSStringCreateWithUTF8CString(file); - - result = JSEvaluateScript(js, str, JSContextGetGlobalObject(js), file_name, 0, &exception); - JSStringRelease(file_name); - JSStringRelease(str); - - if (result) { - *value = jsref_to_string(js, result); - return true; - } - - *value = jsref_to_string(js, exception); - return false; -} - gboolean vb_load_uri(const Arg *arg) { char *uri = NULL, *rp, *path = NULL; @@ -421,7 +397,7 @@ static void webview_load_status_cb(WebKitWebView *view, GParamSpec *pspec) hints_init(frame); /* run user script file */ - run_user_script(frame); + js_eval_file(frame, vb.files[FILES_SCRIPT]); } /* if we load a page from a submitted form, leafe the insert mode */ @@ -522,36 +498,6 @@ static void set_status(const StatusType status) } } -static void run_user_script(WebKitWebFrame *frame) -{ - char *js = NULL, *value = NULL; - GError *error = NULL; - - if (g_file_test(vb.files[FILES_SCRIPT], G_FILE_TEST_IS_REGULAR) - && g_file_get_contents(vb.files[FILES_SCRIPT], &js, NULL, &error) - ) { - gboolean success = vb_eval_script(frame, js, vb.files[FILES_SCRIPT], &value); - if (!success) { - fprintf(stderr, "%s", value); - } - g_free(value); - g_free(js); - } -} - -static char *jsref_to_string(JSContextRef context, JSValueRef ref) -{ - char *string; - JSStringRef str_ref = JSValueToStringCopy(context, ref, NULL); - size_t len = JSStringGetMaximumUTF8CStringSize(str_ref); - - string = g_new0(char, len); - JSStringGetUTF8CString(str_ref, string, len); - JSStringRelease(str_ref); - - return string; -} - static void init_core(void) { Gui *gui = &vb.gui; diff --git a/src/main.h b/src/main.h index 2c44ac7..3148506 100644 --- a/src/main.h +++ b/src/main.h @@ -330,7 +330,6 @@ void vb_echo(const MessageType type, gboolean hide, const char *error, ...); void vb_set_input_text(const char *text); char *vb_get_input_text(void); void vb_input_activate(void); -gboolean vb_eval_script(WebKitWebFrame *frame, char *script, char *file, char **value); gboolean vb_load_uri(const Arg *arg); gboolean vb_set_clipboard(const Arg *arg); void vb_set_widget_font(GtkWidget *widget, const VbColor *fg, const VbColor *bg, PangoFontDescription *font); diff --git a/src/setting.c b/src/setting.c index bf03451..a94e417 100644 --- a/src/setting.c +++ b/src/setting.c @@ -21,6 +21,7 @@ #include "setting.h" #include "util.h" #include "completion.h" +#include "js.h" static GHashTable *settings; @@ -865,9 +866,7 @@ static gboolean validate_js_regexp_list(const char *pattern) char *js, *value = NULL; js = g_strdup_printf("var i;for(i=0;i<[%s].length;i++);", pattern); - result = vb_eval_script( - webkit_web_view_get_main_frame(vb.gui.webview), js, NULL, &value - ); + result = js_eval(webkit_web_view_get_main_frame(vb.gui.webview), js, NULL, &value); g_free(js); if (!result) { vb_echo(VB_MSG_ERROR, true, "%s", value); -- cgit v1.2.3