summaryrefslogtreecommitdiff
path: root/webhelper/webextensions/wh2jscutil.c
blob: 34f325f6e6c16e4f2f8d472aabd289ab7345075b (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
/* -*- 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;
}