| 1 | /* |
| 2 | * Copyright (C) 2019 Igalia S.L. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "JSCOptions.h" |
| 22 | |
| 23 | #include "Options.h" |
| 24 | #include <glib/gi18n-lib.h> |
| 25 | #include <wtf/Vector.h> |
| 26 | #include <wtf/glib/GUniquePtr.h> |
| 27 | |
| 28 | /** |
| 29 | * SECTION: JSCOptions |
| 30 | * @short_description: JavaScript options |
| 31 | * @title: JSCOptions |
| 32 | * |
| 33 | * JavaScript options allow changing the behavior of the JavaScript engine. |
| 34 | * They affect the way the engine works, so it's encouraged to set the options |
| 35 | * at the very beginning of the program execution, before any other JavaScript |
| 36 | * API call. Most of the options are only useful for testing and debugging. |
| 37 | * Only a few of them are documented; you can use the undocumented options at |
| 38 | * your own risk. (You can find the list of options in the WebKit source code). |
| 39 | * |
| 40 | * The API allows to set and get any option using the types defined in #JSCOptionType. |
| 41 | * You can also iterate all the available options using jsc_options_foreach() and |
| 42 | * passing a #JSCOptionsFunc callback. If your application uses #GOptionContext to handle |
| 43 | * command line arguments, you can easily integrate the JSCOptions by adding the |
| 44 | * #GOptionGroup returned by jsc_options_get_option_group(). |
| 45 | * |
| 46 | * Since: 2.24 |
| 47 | */ |
| 48 | |
| 49 | using namespace JSC; |
| 50 | |
| 51 | using int32 = int32_t; |
| 52 | using size = size_t; |
| 53 | |
| 54 | static bool valueFromGValue(const GValue* gValue, bool& value) |
| 55 | { |
| 56 | value = g_value_get_boolean(gValue); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | static void valueToGValue(bool value, GValue* gValue) |
| 61 | { |
| 62 | g_value_set_boolean(gValue, value); |
| 63 | } |
| 64 | |
| 65 | static bool valueFromGValue(const GValue* gValue, int32_t& value) |
| 66 | { |
| 67 | value = g_value_get_int(gValue); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | static void valueToGValue(int32_t value, GValue* gValue) |
| 72 | { |
| 73 | g_value_set_int(gValue, value); |
| 74 | } |
| 75 | |
| 76 | #if CPU(ADDRESS64) |
| 77 | static bool valueFromGValue(const GValue* gValue, unsigned& value) |
| 78 | { |
| 79 | value = g_value_get_uint(gValue); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | static void valueToGValue(unsigned value, GValue* gValue) |
| 84 | { |
| 85 | g_value_set_uint(gValue, value); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | static bool valueFromGValue(const GValue* gValue, size_t& value) |
| 90 | { |
| 91 | value = GPOINTER_TO_SIZE(g_value_get_pointer(gValue)); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | static void valueToGValue(size_t value, GValue* gValue) |
| 96 | { |
| 97 | g_value_set_pointer(gValue, GSIZE_TO_POINTER(value)); |
| 98 | } |
| 99 | |
| 100 | static bool valueFromGValue(const GValue* gValue, const char*& value) |
| 101 | { |
| 102 | value = g_value_dup_string(gValue); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static void valueToGValue(const char* value, GValue* gValue) |
| 107 | { |
| 108 | g_value_set_string(gValue, value); |
| 109 | } |
| 110 | |
| 111 | static bool valueFromGValue(const GValue* gValue, double& value) |
| 112 | { |
| 113 | value = g_value_get_double(gValue); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | static void valueToGValue(double value, GValue* gValue) |
| 118 | { |
| 119 | g_value_set_double(gValue, value); |
| 120 | } |
| 121 | |
| 122 | static bool valueFromGValue(const GValue* gValue, OptionRange& value) |
| 123 | { |
| 124 | return value.init(g_value_get_string(gValue) ? g_value_get_string(gValue) : "<null>" ); |
| 125 | } |
| 126 | |
| 127 | static void valueToGValue(const OptionRange& value, GValue* gValue) |
| 128 | { |
| 129 | const char* rangeString = value.rangeString(); |
| 130 | g_value_set_string(gValue, !g_strcmp0(rangeString, "<null>" ) ? nullptr : rangeString); |
| 131 | } |
| 132 | |
| 133 | static bool valueFromGValue(const GValue* gValue, GCLogging::Level& value) |
| 134 | { |
| 135 | switch (g_value_get_uint(gValue)) { |
| 136 | case 0: |
| 137 | value = GCLogging::Level::None; |
| 138 | return true; |
| 139 | case 1: |
| 140 | value = GCLogging::Level::Basic; |
| 141 | return true; |
| 142 | case 2: |
| 143 | value = GCLogging::Level::Verbose; |
| 144 | return true; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | static void valueToGValue(GCLogging::Level value, GValue* gValue) |
| 153 | { |
| 154 | switch (value) { |
| 155 | case GCLogging::Level::None: |
| 156 | g_value_set_uint(gValue, 0); |
| 157 | break; |
| 158 | case GCLogging::Level::Basic: |
| 159 | g_value_set_uint(gValue, 1); |
| 160 | break; |
| 161 | case GCLogging::Level::Verbose: |
| 162 | g_value_set_uint(gValue, 2); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static gboolean jscOptionsSetValue(const char* option, const GValue* value) |
| 168 | { |
| 169 | #define FOR_EACH_OPTION(type_, name_, defaultValue_, availability_, description_) \ |
| 170 | if (!g_strcmp0(#name_, option)) { \ |
| 171 | type_ |
|---|