diff options
-rw-r--r-- | src/dwb.c | 2 | ||||
-rw-r--r-- | src/js.c | 17 | ||||
-rw-r--r-- | src/js.h | 6 | ||||
-rw-r--r-- | src/scripts.c | 131 | ||||
-rw-r--r-- | src/util.c | 3 |
5 files changed, 118 insertions, 41 deletions
@@ -1656,7 +1656,7 @@ dwb_execute_script(WebKitWebFrame *frame, const char *com, gboolean ret) { return NULL; if (eval_ret && ret) { - return js_value_to_char(context, eval_ret); + return js_value_to_char(context, eval_ret, JS_STRING_MAX); } return NULL; } @@ -20,7 +20,6 @@ #include "dwb.h" #include "util.h" #include "js.h" -#define JS_STRING_MAX 1024 /* js_get_object_property {{{*/ JSObjectRef @@ -56,7 +55,7 @@ js_get_string_property(JSContextRef ctx, JSObjectRef arg, const char *name) { JSStringRelease(buffer); if (exc != NULL || !JSValueIsString(ctx, val) ) return NULL; - return js_value_to_char(ctx, val); + return js_value_to_char(ctx, val, JS_STRING_MAX); }/*}}}*/ /* js_get_double_property {{{*/ @@ -79,8 +78,12 @@ js_get_double_property(JSContextRef ctx, JSObjectRef arg, const char *name) { * Converts a JSStringRef, return a newly allocated char. * {{{*/ char * -js_string_to_char(JSContextRef ctx, JSStringRef jsstring) { - size_t length = MIN(JSStringGetLength(jsstring), JS_STRING_MAX) + 1; +js_string_to_char(JSContextRef ctx, JSStringRef jsstring, size_t size) { + size_t length; + if (size > 0) + length = MIN(JSStringGetLength(jsstring), size) + 1; + else + length = JSStringGetLength(jsstring)+1; char *ret = g_new(char, length); size_t written = JSStringGetUTF8CString(jsstring, ret, length); @@ -162,7 +165,7 @@ js_call_as_function(WebKitWebFrame *frame, JSObjectRef obj, const char *string, js_ret = JSObjectCallAsFunction(ctx, function_object, NULL, 0, NULL, NULL); } if (char_ret != NULL) { - ret = js_value_to_char(ctx, js_ret); + ret = js_value_to_char(ctx, js_ret, JS_STRING_MAX); } error_out: if (js_name) @@ -174,7 +177,7 @@ error_out: /*{{{*/ char * -js_value_to_char(JSContextRef ctx, JSValueRef value) { +js_value_to_char(JSContextRef ctx, JSValueRef value, size_t limit) { JSValueRef exc = NULL; if (value == NULL) return NULL; @@ -184,7 +187,7 @@ js_value_to_char(JSContextRef ctx, JSValueRef value) { if (exc != NULL) return NULL; - char *ret = js_string_to_char(ctx, jsstring); + char *ret = js_string_to_char(ctx, jsstring, limit); JSStringRelease(jsstring); return ret; }/*}}}*/ @@ -19,8 +19,8 @@ #ifndef JS_H #define JS_H -char * js_string_to_char(JSContextRef ctx, JSStringRef jsstring); -char * js_value_to_char(JSContextRef ctx, JSValueRef value); +char * js_string_to_char(JSContextRef ctx, JSStringRef jsstring, size_t ); +char * js_value_to_char(JSContextRef ctx, JSValueRef value, size_t limit); JSObjectRef js_get_object_property(JSContextRef ctx, JSObjectRef arg, const char *name); JSObjectRef js_get_object_property(JSContextRef ctx, JSObjectRef arg, const char *name); char * js_get_string_property(JSContextRef ctx, JSObjectRef arg, const char *name); @@ -29,4 +29,6 @@ JSObjectRef js_create_object(WebKitWebFrame *, const char *); char * js_call_as_function(WebKitWebFrame *, JSObjectRef, const char *string, const char *args, char **char_ret); JSValueRef js_char_to_value(JSContextRef ctx, const char *text); +#define JS_STRING_MAX 1024 + #endif diff --git a/src/scripts.c b/src/scripts.c index 7541bf33..1c08be99 100644 --- a/src/scripts.c +++ b/src/scripts.c @@ -21,6 +21,7 @@ #include <JavaScriptCore/JavaScript.h> #include "dwb.h" #include "scripts.h" +#include "util.h" #include "js.h" //#define kJSDefaultFunction (kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum) #define kJSDefaultFunction (kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete) @@ -35,12 +36,15 @@ static JSValueRef scripts_execute(JSContextRef ctx, JSObjectRef function, JSObje static JSValueRef scripts_spawn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSValueRef scripts_set(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSValueRef scripts_get(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); -static JSValueRef scripts_print(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); + +static JSValueRef scripts_io_print(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); +static JSValueRef scripts_io_get_file_content(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); +static JSValueRef scripts_io_set_file_content(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSStaticFunction static_functions[] = { { "execute", scripts_execute, kJSDefaultFunction }, { "spawn", scripts_spawn, kJSDefaultFunction }, - { "print", scripts_print, kJSDefaultFunction }, + //{ "print", scripts_print, kJSDefaultFunction }, { 0, 0, 0 }, }; static JSStaticValue static_values[] = { @@ -53,17 +57,46 @@ static Sigmap _sigmap[] = { { SCRIPT_SIG_DOWNLOAD, "onDownload" } }; static JSObjectRef _sigObjects[SCRIPT_SIG_LAST]; -static JSContextRef _globalContext; +static JSContextRef _global_context; +static JSObjectRef _signals; static JSClassRef _viewClass; static void scripts_create_global_object() { - JSClassDefinition cd = kJSClassDefinitionEmpty; + JSClassDefinition cd; + JSStringRef name; + JSClassRef class; + /* Global object */ + cd = kJSClassDefinitionEmpty; cd.className = "dwb"; cd.staticFunctions = static_functions; cd.staticValues = static_values; - JSClassRef class = JSClassCreate(&cd); - _globalContext = JSGlobalContextCreate(class); + class = JSClassCreate(&cd); + _global_context = JSGlobalContextCreate(class); + + name = JSStringCreateWithUTF8CString("signals"); + cd = kJSClassDefinitionEmpty; + cd.className = "signals"; + class = JSClassCreate(&cd); + _signals = JSObjectMake(_global_context, class, NULL); + JSObjectSetProperty(_global_context, JSContextGetGlobalObject(_global_context), name, _signals, kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete, NULL); + JSStringRelease(name); + + name = JSStringCreateWithUTF8CString("io"); + cd = kJSClassDefinitionEmpty; + cd.className = "io"; + JSStaticFunction io_functions[] = { + { "print", scripts_io_print, kJSDefaultFunction }, + { "getFile", scripts_io_get_file_content, kJSDefaultFunction }, + { "setFile", scripts_io_set_file_content, kJSDefaultFunction }, + { 0, 0, 0 }, + }; + cd.staticFunctions = io_functions; + class = JSClassCreate(&cd); + JSObjectRef io = JSObjectMake(_global_context, class, NULL); + JSObjectSetProperty(_global_context, JSContextGetGlobalObject(_global_context), name, io, kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete, NULL); + JSStringRelease(name); + } static void @@ -87,7 +120,7 @@ static JSValueRef scripts_get_generic(JSContextRef ctx, GObject *o, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { if (argc < 1) return JSValueMakeBoolean(ctx, false); - char *name = js_value_to_char(ctx, argv[0]); + char *name = js_value_to_char(ctx, argv[0], -1); GValue v = G_VALUE_INIT; g_value_init(&v, G_TYPE_STRING); g_object_get_property(o, name, &v); @@ -102,7 +135,7 @@ scripts_get(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t arg return scripts_get_generic(ctx, G_OBJECT(s), function, argc, argv, exc); } static JSValueRef -scripts_set_generic(JSContextRef ctx, GObject *o, JSObjectRef function, size_t argc, const JSValueRef argv[], JSValueRef* exc) { +scripts_set_generic(JSContextRef ctx, GObject *o, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { char *sval = NULL; char dval; char *propname = NULL; @@ -122,7 +155,7 @@ scripts_set_generic(JSContextRef ctx, GObject *o, JSObjectRef function, size_t a int type = JSValueGetType(ctx, value); switch (type) { case kJSTypeString : - sval = js_value_to_char(ctx, value); + sval = js_value_to_char(ctx, value, -1); if (sval == NULL) return JSValueMakeBoolean(ctx, false); g_value_init(&v, G_TYPE_STRING); @@ -142,7 +175,7 @@ scripts_set_generic(JSContextRef ctx, GObject *o, JSObjectRef function, size_t a break; default : return JSValueMakeBoolean(ctx, false); } - propname = js_string_to_char(ctx, js_propname); + propname = js_string_to_char(ctx, js_propname, JS_STRING_MAX); g_object_set_property(o, propname, &v); g_free(propname); } @@ -153,23 +186,58 @@ static JSValueRef scripts_set(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { GList *gl = JSObjectGetPrivate(this); WebKitWebSettings *s = webkit_web_view_get_settings(WEBVIEW(gl)); - return scripts_set_generic(ctx, G_OBJECT(s), function, argc, argv, exc); + return scripts_set_generic(ctx, G_OBJECT(s), function, this, argc, argv, exc); } static JSValueRef scripts_spawn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { return NULL; } static JSValueRef -scripts_print(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { +scripts_io_get_file_content(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + JSValueRef ret = NULL; + char *path = NULL, *content = NULL; + if (argc < 1) + return JSValueMakeNull(ctx); + path = js_value_to_char(ctx, argv[0], PATH_MAX); + if (path == NULL) + goto error_out; + content = util_get_file_content(path); + if (content == NULL) + goto error_out; + ret = js_char_to_value(ctx, content); + +error_out: + g_free(path); + g_free(content); + if (ret == NULL) + return JSValueMakeNull(ctx); + return ret; + +} +static JSValueRef +scripts_io_set_file_content(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + char *path = NULL, *content = NULL; + gboolean ret = false; + if (argc < 2) + return JSValueMakeBoolean(ctx, false); + path = js_value_to_char(ctx, argv[0], PATH_MAX); + content = js_value_to_char(ctx, argv[1], -1); + ret = util_set_file_content(path, content); + g_free(path); + g_free(content); + return JSValueMakeBoolean(ctx, ret); +} +static JSValueRef +scripts_io_print(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { if (argc == 0) - return JSValueMakeUndefined(_globalContext); + return JSValueMakeUndefined(_global_context); char *out; double dout; int type = JSValueGetType(ctx, argv[0]); switch (type) { case kJSTypeString : - out = js_value_to_char(ctx, argv[0]); + out = js_value_to_char(ctx, argv[0], -1); if (out != NULL) { puts(out); g_free(out); @@ -185,7 +253,7 @@ scripts_print(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, si break; default : break; } - return JSValueMakeUndefined(_globalContext); + return JSValueMakeUndefined(ctx); } gboolean @@ -195,51 +263,52 @@ scripts_emit(JSObjectRef obj, int signal, const char *json) { return false; JSStringRef js_json = JSStringCreateWithUTF8CString(json); - JSValueRef val[] = { obj, JSValueMakeFromJSONString(_globalContext, js_json) }; + JSValueRef val[] = { obj, JSValueMakeFromJSONString(_global_context, js_json) }; JSStringRelease(js_json); - JSValueRef js_ret = JSObjectCallAsFunction(_globalContext, function, NULL, 2, val, NULL); - if (JSValueIsBoolean(_globalContext, js_ret)) - return JSValueToBoolean(_globalContext, js_ret); + JSValueRef js_ret = JSObjectCallAsFunction(_global_context, function, NULL, 2, val, NULL); + if (JSValueIsBoolean(_global_context, js_ret)) + return JSValueToBoolean(_global_context, js_ret); return false; } void scripts_create_tab(GList *gl) { - if (_globalContext == NULL ) { + if (_global_context == NULL ) { VIEW(gl)->script = NULL; return; } if (_viewClass == NULL ) scripts_create_view_class(); - if (_globalContext == NULL ) + if (_global_context == NULL ) scripts_create_global_object(); - VIEW(gl)->script = JSObjectMake(_globalContext, _viewClass, gl); + VIEW(gl)->script = JSObjectMake(_global_context, _viewClass, gl); } void scripts_init_script(const char *script) { - if (_globalContext == NULL) + if (_global_context == NULL) scripts_create_global_object(); JSStringRef js_script = JSStringCreateWithUTF8CString(script); - JSEvaluateScript(_globalContext, js_script, NULL, NULL, 0, NULL); + JSEvaluateScript(_global_context, js_script, NULL, NULL, 0, NULL); JSStringRelease(js_script); } void scripts_init() { dwb.misc.script_signals = 0; - if (_globalContext == NULL) + if (_global_context == NULL) return; - JSObjectRef globalObject = JSContextGetGlobalObject(_globalContext); + //JSObjectRef global_object = JSContextGetGlobalObject(_global_context); + for (int i=SCRIPT_SIG_FIRST; i<SCRIPT_SIG_LAST; i++) { JSStringRef name = JSStringCreateWithUTF8CString(_sigmap[i].name); - if (!JSObjectHasProperty(_globalContext, globalObject, name)) + if (!JSObjectHasProperty(_global_context, _signals, name)) goto release; - JSValueRef val = JSObjectGetProperty(_globalContext, globalObject, name, NULL); - if (!JSValueIsObject(_globalContext, val)) + JSValueRef val = JSObjectGetProperty(_global_context, _signals, name, NULL); + if (!JSValueIsObject(_global_context, val)) goto release; - JSValueProtect(_globalContext, val); - JSObjectRef o = JSValueToObject(_globalContext, val, NULL); + JSValueProtect(_global_context, val); + JSObjectRef o = JSValueToObject(_global_context, val, NULL); if (o != NULL) { _sigObjects[i] = o; dwb.misc.script_signals |= (1<<i); @@ -339,6 +339,9 @@ util_set_file_content(const char *filename, const char *content) { char *dname = NULL; char *realpath = NULL; char buffer[PATH_MAX]; + if (content == NULL || filename == NULL) + return false; + filename = util_expand_home(buffer, filename, PATH_MAX); if (g_file_test(filename, G_FILE_TEST_IS_SYMLINK)) { link = g_file_read_link(filename, &error); |