summaryrefslogtreecommitdiff
path: root/gi/gtype.cpp
Commit message (Collapse)AuthorAgeFilesLines
* gtype: Move code into class bodyPhilip Chimento2021-01-301-112/+90
| | | | | This is split out into a separate commit so that it's easier to see what changed in "gtype: Remove JSClass macros".
* gtype: Remove JSClass macrosPhilip Chimento2021-01-301-67/+112
| | | | | | | | | | Port gtype.cpp to use the CWrapper template, instead of the GJS_DEFINE_PRIV_FROM_JS family of macros. This is slightly different than Ns because GTypeObj is a class with only static methods, whereas the actual data stored in the object's private pointer slot is the GType, stuffed into a void*. Also requires adding a debug topic for GTypes.
* gtype: Remove gjs_typecheck_gtype()Philip Chimento2020-12-031-8/+0
| | | | | | This function was only used in one place, and it was unnecessary because gjs_gtype_get_actual_gtype() is used on the same object later, which also checks the type. So, we can remove the function altogether.
* maint: Convert all existing license/copyright comments to SPDX formatPhilip Chimento2020-10-041-22/+3
| | | | | | | | The SPDX format is machine-readable, which should make it much easier to avoid mistakes with licensing and copyright using automated tools. This commit does not add any implicit information. It converts exactly what was already specified into SPDX format comments.
* CI: Fix IWYU includesincludesPhilip Chimento2020-09-201-2/+0
|
* maint: Use C++17 attributesPhilip Chimento2020-08-051-1/+1
| | | | | | | | | | | | | Now that we depend on C++17, we can use these attributes that are part of the language, instead of relying on macros to make them portable. (GJS_USE still needs to be defined for public header files, which may be compiled in C.) Attributes which are new in C++17 are [[maybe_unused]], [[nodiscard]], and [[fallthrough]]. [skip cpplint] because cpplint doesn't deal with C++ attributes: https://github.com/google/styleguide/issues/165
* js: Define Symbol.toStringTag names on all our custom classesEvan Welsh2020-07-271-0/+1
| | | | | | | | SpiderMonkey 78 will stop using the JSClass.name string as the string tag. For backwards compatibility, we should make sure the string tag doesn't change. See: GNOME/gjs#329
* js: Fix header includes according to IWYUPhilip Chimento2020-05-201-0/+2
| | | | | | | This updates all the includes according to what IWYU suggests. In a few cases we move a typedef down in order to avoid IWYU wanting a forward declaration, or use a JS::Heap<T>{braced initializer} in order to avoid an implicit std::move.
* gtype: Don't use lookupForAdd() when adding GType to tablePhilip Chimento2020-01-251-3/+7
| | | | | | | | | | | This was a bug exposed by more efficient garbage collection and better debug assertions in SpiderMonkey 68. We cannot use lookupForAdd() when checking if a GType is already present in the GType table (which we did to anticipate adding it if it wasn't present) because creating the GType wrapper object might trigger a GC, which could cause another GType to be garbage collected, which would remove it from the GType table. Mutating the table invalidates the AddPtr returned by lookupForAdd(), which is a bug.
* js: Remove jsapi-wrapper.hPhilip Chimento2020-01-191-2/+12
| | | | | | | | | | | | | With SpiderMonkey 68, it's no longer necessary to mark SpiderMonkey header files as system headers, and it's also no longer necessary to include <js-config.h> before any other header files, as SpiderMonkey has sorted that out internally. We remove jsapi-wrapper.h and define DEBUG directly in config.h instead of indirectly in jsapi-wrapper.h via HAVE_SPIDERMONKEY_DEBUG. This should reduce compile time, on average, because in most .h files we now only need to include <js/TypeDecls.h>.
* js: Remove requestsPhilip Chimento2020-01-191-3/+0
| | | | | | JSAutoRequest, JS_BeginRequest(), and JS_EndRequest() are no longer necessary in SpiderMonkey 68. We can just remove them wherever they appear.
* maint: Fix header includes once and for allPhilip Chimento2019-06-081-6/+6
| | | | | | | | | | | | | | | | | | Previously #include statements were a bit of a mess across the codebase. This commit is the result of a pass by the IWYU (Include What You Use) tool, which suggests headers to add or remove based on what is in the file, and can also suggest forward-declaring classes instead of including their headers, if they are only used as a pointer in a particular file. Cleaning this up should in general speed up compile times. IWYU isn't perfect, it produces a number of false positives, so we don't try to automate this process and we don't accept all of its recommendations. We do add a script and configuration file to the tools/ directory so that IWYU can be every so often in the future. We also clean up all the includes according to a consistent style, which is now described clearly in the C++ style guide.
* gtype: Make finalize function a no-opPhilip Chimento2019-05-071-9/+3
| | | | | | | | | | | GType objects don't allocate any memory for their private data. Instead, they just store their GType value in the JSObject's private data pointer slot. Since nothing is allocated, nothing needs to be freed in the finalize function. We are required to have a finalize function because of the GJS_DEFINE_PROTO_ABSTRACT macro used to define this JSClass. However, we can just make it empty. The statements currently in there seem useless.
* gtype: Port weak pointer hash table to WeakCachePhilip Chimento2019-04-131-46/+9
| | | | | | | | | | | | As in a previous change in fundamental.cpp, JS::WeakCache allows us to get rid of the weak pointer callback, as that is all implemented internally. The hash table is automatically swept when the GType wrapper objects are garbage collected. We move the table to GjsContextPrivate instead of using a static variable. Although there is probably little difference in practice, that is a more accurate place to store it, in case there is more than one GjsContext in use.
* gtype: Remove unused functionPhilip Chimento2019-01-021-22/+0
| | | | With the new GI wrapper framework, this function is now not used.
* atoms: Use more atoms to avoid converting stringsPhilip Chimento2018-11-051-8/+13
| | | | | | | | | | There are a number of places where we used JS_{Get,Set,Define}Property() or JS_DefineFunction() with a string literal. In these cases it would be better to use an atom so that the string doesn't have to be converted from UTF-8 internally each time. In some cases there was already an atom defined, and in others we add a new atom.
* gtype: use Atomized string for static onesMarco Trevisan (Treviño)2018-11-041-1/+2
| | | | We can avoid doing a copy of the string for a static one
* gtype: reuse heap wrapper from cached onesMarco Trevisan (Treviño)2018-11-041-32/+12
|
* gtype: track weak pointers using unique_ptrMarco Trevisan (Treviño)2018-11-041-13/+13
| | | | So we don't have to manage the deletion manually.
* gtype: delete heap wrappers on gtype removalMarco Trevisan (Treviño)2018-11-041-3/+12
|
* gtype: Fix error handling in gjs_gtype_get_actual_gtype()Philip Chimento2018-10-301-20/+22
| | | | | This makes gjs_gtype_get_actual_gtype() conform to the JSAPI return convention, so that it is clear when an exception is pending.
* js: Introduce annotations for using return valuesPhilip Chimento2018-10-301-1/+5
| | | | | | | | | | | | | | | | | | | This introduces two annotations to functions. GJS_USE is a macro for GCC and Clang's "warn_unused_result" annotation, meaning that the return value of a function must not be ignored. This allows us to catch some cases where we are neglecting to do error checking. The second annotation is GJS_JSAPI_RETURN_CONVENTION which for now is identical to GJS_USE, but could be made into a custom annotation in the future which a static analyzer plugin could use to enforce further checks at compile time. The "JSAPI return convention" is valid on functions that return bool or a pointer type, and whose first parameter is JSContext*. If false or nullptr is returned, then an exception must be pending at return time on the passed-in JSContext. If true or a non-nullptr value is returned, then no exception must be pending. This commit only has the addition of the attributes, and a few fixes mandated by the autoformatter, so it can be reviewed "lightly".
* js: Replace JS_FS with JS_FNPhilip Chimento2018-07-281-3/+2
| | | | | | The macro simply changed its name. See: #161
* arg: Marshal GType of 0 to nullPhilip Chimento2018-01-031-0/+3
| | | | | | | | Zero is a valid return value from e.g. GObject.type_from_name() in case the type in question is not registered. We marshal a value of 0 (G_TYPE_INVALID) into JS null. Closes #11.
* jsapi-util-string: Use mozjs UTF8-to-JSString conversionPhilip Chimento2017-11-191-15/+14
| | | | | | | | | | | | There are two paths for decoding UTF8 strings directly to JSString: JS_NewStringCopyUTF8N() and JS_NewStringCopyUTF8Z(). Mostly we have a zero-terminated string in GJS, so we shorten gjs_string_from_utf8() to remove its string length argument and use JS_NewStringCopyUTF8Z(). There are a few places where we have a non-zero-terminated string, so we introduce gjs_string_from_utf8_n() to handle that path. A few places we were doing a UTF8 conversion where it was not necessary.
* js: Unbarriered read while in weak ptr callbackPhilip Chimento2017-07-091-1/+4
| | | | | | | | | | | | | Inside the weak pointer callback, we only need to compare the pointer to nullptr. Since we don't actually use the pointer's location, no read barrier is needed. Previously this was not a problem, but SpiderMonkey 52 now asserts that the heap is not active when exposing a pointer to active JS through a read barrier, so the weak pointer callback will crash if we try to use a read barrier. https://bugzilla.gnome.org/show_bug.cgi?id=784196
* js: Replace JSRuntime APIs that now take JSContextPhilip Chimento2017-07-091-3/+2
| | | | | | | | | | | | Many APIs that adapted to the removal of JSRuntime can be trivially replaced with JSContext without any other code changes. JS_AbortIfWrongThread() also takes JSContext instead of JSRuntime, but we only called it to check that we were creating a JSContext on the correct thread. Before creating the JSContext, there is no point in calling it, so remove it. https://bugzilla.gnome.org/show_bug.cgi?id=784196
* js: Add JSCLASS_FOREGROUND_FINALIZE flagPhilip Chimento2017-07-091-1/+2
| | | | | | | | For all classes with a finalize hook, SpiderMonkey 52 requires that they either have JSCLASS_FOREGROUND_FINALIZE or JSCLASS_BACKGROUND_FINALIZE set. Previously, only the BACKGROUND flag existed and FOREGROUND was implicit. https://bugzilla.gnome.org/show_bug.cgi?id=784196
* js: Weak pointer callback API changePhilip Chimento2017-07-091-4/+6
| | | | | | | | Weak pointer callbacks now come in two flavours: per-zone and per-compartment. We do everything in one compartment, and don't care about zones, so we should use the per-compartment flavour. https://bugzilla.gnome.org/show_bug.cgi?id=784196
* js: Switch from JS::NullPtr() to nullptrPhilip Chimento2017-07-091-3/+2
| | | | | | | JS::NullPtr() is gone, and instead now functions that took JS::NullPtr() as a null handle value take nullptr_t, so we should use C++ nullptr. https://bugzilla.gnome.org/show_bug.cgi?id=784196
* jsapi-util: Add static function spec to GJS_DEFINE_PROTOPhilip Chimento2017-04-171-0/+2
| | | | | | | | | | | | This allows the GJS_DEFINE_PROTO_* family of macros to have a JSFunctionSpec array for functions defined on the constructor, rather than on the prototype: i.e. static functions. We clean up an incongruity in cairo-image-surface.cpp where you had to pass the constructor into gjs_cairo_image_surface_init() in order to get the static functions defined. https://bugzilla.gnome.org/show_bug.cgi?id=614413
* jsapi-class: GJS_DEFINE_PROTO stores protos in global slotsPhilip Chimento2017-04-171-14/+10
| | | | | | | | | | | | | | | | | | | | This refactors the GJS_DEFINE_PROTO family of macros to store the created prototype objects in slots on the global object. We split up the gjs_WHATEVER_create_proto() function into two functions: one that must be called at least once for each type to define the class, store the prototype in a global slot, and define the constructor as a property on a passed-in module or global object, gjs_WHATEVER_define_proto(); and one that retrieves the prototype from its global slot, gjs_WHATEVER_get_proto(). We also add two macros, GJS_DEFINE_PROTO_WITH_PARENT and GJS_DEFINE_PROTO_ABSTRACT_WITH_PARENT, which also move the definition of the parent to compile time rather than runtime, and keep it in the same place as the rest of each class's macro definition. Similarly, previously we had to pass the parent prototype object to gjs_WHATEVER_create_proto(). This mostly affects the cairo module. https://bugzilla.gnome.org/show_bug.cgi?id=614413
* jsapi-class: Get rid of "proto_name" argumentPhilip Chimento2017-04-161-2/+1
| | | | | | | | | | | | | | There's no need to have a "proto_name" that's different from the class's name. Getting rid of this distinction will allow us to refactor things so that JS_InitClass() defines the correct constructor directly on the global object or the module object. This is a small behavioural API break, since it changes the output of toString() on the various Cairo objects, but I would say that people should not be parsing the output of toString(). (I know we do in our tests, but that's terrible.) https://bugzilla.gnome.org/show_bug.cgi?id=614413
* maint: Use correct mode lines in all filesPhilip Chimento2017-04-161-1/+1
| | | | | | | | | I finally figured out why my highlighting was always messed up; the mode lines in all these files said "C" whereas they were C++ or JS files. This was in such a blind spot that I even copied it to new files that I had created in the meantime... Unreviewed.
* jsapi-util: Split off class stuff into jsapi-class.hPhilip Chimento2017-04-131-0/+1
| | | | | | | | | I've been wishing I had done this for three days now; jsapi-util.h is far too long, and it's no fun to recompile the entire program every time one of the macros is changed. The class macros plus the dynamic class functions make a fairly independent unit, so this seems logical. https://bugzilla.gnome.org/show_bug.cgi?id=614413
* jsapi-util: Return JSObject from create_protoPhilip Chimento2017-04-101-2/+2
| | | | | | | The return value is a prototype object, so there's no use in returning JS::Value here only to convert it to an object in the calling code. https://bugzilla.gnome.org/show_bug.cgi?id=614413
* js: Update weak pointers after GCPhilip Chimento2017-02-141-10/+42
| | | | | | | | | | | | | | | | | | | | There are two places where we maintain weak pointers to GC things: one is in object.cpp, where a GObject is associated with its JS wrapper object. The other is in gtype.cpp, where each GType is associated with its wrapper object. In SpiderMonkey 38, we have to call JS_UpdateWeakPointerAfterGC() on weak pointers, every garbage collection cycle, in case the garbage collector moves them. We considered adding a separate callback for each object with JS_AddWeakPointerCallback() instead of one callback per file that processes all the weak pointers it knows about, but that is prohibitive since SpiderMonkey doesn't take the user data into account when calling JS_RemoveWeakPointerCallback(), so you can't add and remove multiple instances of the same callback. https://bugzilla.gnome.org/show_bug.cgi?id=777962
* js: No need to use .address() in new APIPhilip Chimento2016-12-091-2/+2
| | | | | | | | | | | | | Since in mozjs31 all out parameters for GC things have been changed from pointers to MutableHandles, there is no need to get the underlying GC thing's address with JS::Handle::address(). Instead, we can simply use the unary & operator to get a MutableHandle from a Rooted. In some cases, such as JS_SetProperty(), the SpiderMonkey API changed a JS::Value* parameter to a JS::HandleValue, so we can just delete the .address() call and pass the RootedValue directly. https://bugzilla.gnome.org/show_bug.cgi?id=751252
* js: Pass JS::NullPtr() instead of NULLPhilip Chimento2016-12-091-1/+2
| | | | | | | In APIs that now take a JS::HandleObject, we must pass JS::NullPtr() since JS::HandleObject cannot directly be constructed from NULL. https://bugzilla.gnome.org/show_bug.cgi?id=751252
* js: Remove remaining usage of JSBoolPhilip Chimento2016-12-091-2/+2
| | | | | | | Since mozjs31 does away with JSBool entirely, we can now replace the remaining occurrences of it in the API with C++ bool. https://bugzilla.gnome.org/show_bug.cgi?id=751252
* gtype: Specify GType object prototype explicitlyPhilip Chimento2016-11-081-2/+3
| | | | | | | | | | | | | Previously, JS_NewObject() was able to infer the prototype of the GType object because the prototype was there in the global namespace. JS_NewObject() has stopped doing this in mozjs31, so we need to explicitly specify the prototype using JS_NewObjectWithGivenProto(). (I'm not sure if this needs to be fixed up anywhere else. The tests all pass without it, and in the importer case, the tests were relying on the importer object _not_ having the prototype of Object.) https://bugzilla.gnome.org/show_bug.cgi?id=742249
* js: Don't pass JSPROP_READONLY to JS_PSGPhilip Chimento2016-11-051-1/+1
| | | | | | | This is redundant, since you already can't set a property that doesn't have a setter. It will be illegal and caught at compile time in mozjs31. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* js: Root misc functionsPhilip Chimento2016-11-051-14/+10
| | | | | | | | | | This ports the rest of the main codebase to use exact GC rooting for everything that would otherwise have caused a compile error in mozjs31. There were only a few modifications per file still to do so I stuck them all in one commit. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* js: Root create_proto functionsPhilip Chimento2016-10-211-7/+6
| | | | | | | | This changes the gjs_whatever_create_proto() functions generated by the GJS_DEFINE_PROTO macros in jsapi-util.h to be correctly rooted, letting changes to those function prototypes cascade everywhere else. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* build: Fix various compiler warningsPhilip Chimento2016-10-211-0/+6
| | | | | | | Fixing various compiler warnings that are too small to have their own commit. https://bugzilla.gnome.org/show_bug.cgi?id=773297
* build: Make a jsapi.h "system header" wrapperPhilip Chimento2016-10-211-2/+1
| | | | | | | | | | | | | | | | | We got rid of all the "compat" stuff in compat.h already, before this commit it only included jsapi.h, jsdbgapi.h, and jsapi-util.h, wrapping the former two in some diagnostic pragmas. If we get rid of the jsapi-util.h include, then we can use a system_header pragma to mark it as a system header so that we don't have to painstakingly maintain all those diagnostic pragmas. (The system_header pragma ignores all warnings coming from that file: https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html) Since compat.h already wasn't a fitting name anymore, rename the header to jsapi-wrapper.h. https://bugzilla.gnome.org/show_bug.cgi?id=773297
* js: Root gjs_string_from_utf8()Philip Chimento2016-10-201-11/+2
| | | | | | | | | | | | | | | | | Starting with gjs_string_from_utf8(), we convert the JS::Value out-arg to JS::MutableHandleValue. Working backwards from there in a huge cascade, we change JS::Value * to JS::MutableHandleValue in many functions that call gjs_string_from_utf8(), and root the values with JS::RootedValue where they are created. This is mostly straightforward, though requires replacing a few instances of a stack-allocated JS::Value array created with g_newa(), with JS::AutoValueVector instead. In the course of moving to more RAII classes such as JS::Rooted<T>, we can get rid of some more gotos in favour of returns. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* js: Switch to JSNative property accessorsPhilip Chimento2016-10-191-10/+6
| | | | | | | | | | Per https://bugzilla.mozilla.org/show_bug.cgi?id=992977, JSPropertyOp-style property accessors are going to be removed in a future version of SpiderMonkey. Instead, we use JSNative property accessors. This allows us to use the JS_PSG and JS_PSGS macros in our JSPropertySpec arrays, making things safer by eliminating the callback casts. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* js: Add macros for 'this' and private dataPhilip Chimento2016-10-191-4/+2
| | | | | | | | | | | | | | | | | | This adds two macros, GJS_GET_THIS() to get a function's 'this' value as a rooted object (and if it was not an object, to box it into one), and GJS_GET_PRIV() to get our private C data from the 'this' object. This operation is done over and over again inside most JSNative functions, and SpiderMonkey code often uses convenience macros like this. These will come in handy even more when we switch to JSNative property accessors in a following commit. At the same time we make things more typesafe by doing a typecheck on each 'this' object in the native methods as part of GJS_GET_PRIV() and throwing an error if it is not the correct type. This gets rid of the args.thisv().toObjectOrNull() idiom which I'm pretty sure is wrong. https://bugzilla.gnome.org/show_bug.cgi?id=742249
* module: Get rid of internals APIPhilip Chimento2016-10-181-2/+1
| | | | | | | | | | | This removes gjs-internals-1.0.pc and gjs-module.h, and makes all the module header files private, that were previously installed. Removes "do not include separately" guards from those files, and makes sure they are included with quotes instead of angle brackets. Untangles any header inclusion problems resulting from the removal of gjs-module.h. https://bugzilla.gnome.org/show_bug.cgi?id=772386