| 28 | #include "util/log.h" |
| 29 | |
| 30 | GJS_JSAPI_RETURN_CONVENTION |
| 31 | static bool gjs_define_enum_value(JSContext* cx, JS::HandleObject in_object, |
| 32 | const GI::ValueInfo& info) { |
| 33 | const char* value_name = info.name(); |
| 34 | int64_t value_val = info.value(); |
| 35 | |
| 36 | /* g-i converts enum members such as GDK_GRAVITY_SOUTH_WEST to |
| 37 | * Gdk.GravityType.south-west (where 'south-west' is value_name) Convert |
| 38 | * back to all SOUTH_WEST. |
| 39 | */ |
| 40 | Gjs::AutoChar fixed_name{g_ascii_strup(value_name, -1)}; |
| 41 | for (size_t i = 0; fixed_name[i]; ++i) { |
| 42 | char c = fixed_name[i]; |
| 43 | if (!(g_ascii_isupper(c) || g_ascii_isdigit(c))) |
| 44 | fixed_name[i] = '_'; |
| 45 | } |
| 46 | |
| 47 | gjs_debug(GJS_DEBUG_GENUM, |
| 48 | "Defining enum value %s (fixed from %s) %" PRId64, |
| 49 | fixed_name.get(), value_name, value_val); |
| 50 | |
| 51 | if (!JS_DefineProperty(cx, in_object, fixed_name, |
| 52 | static_cast<double>(value_val), |
| 53 | GJS_MODULE_PROP_FLAGS)) { |
| 54 | gjs_throw(cx, |
| 55 | "Unable to define enumeration value %s %" PRId64 |
| 56 | " (no memory most likely)", |
| 57 | fixed_name.get(), value_val); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool gjs_define_enum_values(JSContext* cx, JS::HandleObject in_object, |
| 65 | const GI::EnumInfo& info) { |