summaryrefslogtreecommitdiff
path: root/webhelper/webextensions/wh2jscutil.c
diff options
context:
space:
mode:
authorPhilip Chimento <philip@endlessm.com>2015-05-19 11:31:25 -0700
committerPhilip Chimento <philip@endlessm.com>2015-05-21 13:22:23 -0700
commita58f5454c8b046ba4f17534a88db1c19ac22b822 (patch)
treeb25eca159bc2d6f54882e9f29cf0818ad15f9c7b /webhelper/webextensions/wh2jscutil.c
parente2729f045a9459c9a320c617ac6e599814744e56 (diff)
Expose gettext() to client-side JS
This exposes the function set by webhelper.set_gettext() to the client- side Javascript as a gettext() function, defined on the global window object. This allows apps to translate messages that are generated at runtime, not just messages in static HTML. Some often-used JavaScriptCore operations can be turned into separate functions, which we can put in a separate source file. This is in anticipation of the next commit where we will define another function property of the global object. [endlessm/eos-sdk#291]
Diffstat (limited to 'webhelper/webextensions/wh2jscutil.c')
-rw-r--r--webhelper/webextensions/wh2jscutil.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/webhelper/webextensions/wh2jscutil.c b/webhelper/webextensions/wh2jscutil.c
new file mode 100644
index 0000000..34f325f
--- /dev/null
+++ b/webhelper/webextensions/wh2jscutil.c
@@ -0,0 +1,65 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/* Copyright 2015 Endless Mobile, Inc. */
+
+#include <glib.h>
+#include <JavaScriptCore/JavaScript.h>
+
+#include "wh2jscutil.h"
+
+G_GNUC_INTERNAL
+gboolean
+set_object_property (JSContextRef js,
+ JSObjectRef object,
+ const gchar *property_name,
+ JSValueRef property_value,
+ JSPropertyAttributes flags)
+{
+ JSValueRef exception = NULL;
+ JSStringRef property_name_ref = JSStringCreateWithUTF8CString (property_name);
+ JSObjectSetProperty (js, object, property_name_ref, property_value, flags,
+ &exception);
+ JSStringRelease (property_name_ref);
+ if (exception != NULL)
+ {
+ g_critical ("There was a problem setting the property '%s'.",
+ property_name);
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/* Returns a newly allocated string. */
+G_GNUC_INTERNAL
+gchar *
+string_ref_to_string (JSStringRef string_ref)
+{
+ size_t bufsize = JSStringGetMaximumUTF8CStringSize (string_ref);
+ gchar *string = g_new0 (gchar, bufsize);
+ JSStringGetUTF8CString (string_ref, string, bufsize);
+ return string;
+}
+
+G_GNUC_INTERNAL
+JSValueRef
+string_to_value_ref (JSContextRef js,
+ const gchar *string)
+{
+ JSStringRef string_ref = JSStringCreateWithUTF8CString (string);
+ JSValueRef value_ref = JSValueMakeString (js, string_ref);
+ /* value_ref owns string_ref now */
+ return value_ref;
+}
+
+G_GNUC_INTERNAL
+JSValueRef
+throw_exception (JSContextRef js,
+ const gchar *message)
+{
+ JSValueRef msgval = string_to_value_ref (js, message);
+ JSValueRef inner_error = NULL;
+ JSObjectRef exception = JSObjectMakeError (js, 1, &msgval, &inner_error);
+ if (inner_error != NULL)
+ return inner_error;
+ return (JSValueRef) exception;
+}