Web interfaces – exposed as JavaScript objects – are generally specified in Web IDL (Interface Definition Language), a declarative language (sometimes written without the space as WebIDL). This is the language used in standard specifications, and Blink uses IDL files to specify the interface and generate JavaScript bindings (formally, C++ code that the V8 JavaScript virtual machine uses to call Blink itself). Web IDL in Blink is close to the standard, and the resulting bindings use standard conventions to call Blink code, but there are additional features to specify implementation details, primarily Blink IDL extended attributes. To implement a new Web IDL interface in Blink:
IDL
See Blink IDL: Style for style guide.
IDL files contain two types of data:
Note that if Blink behavior differs from the spec, the Blink IDL file should reflect Blink behavior. This makes interface differences visible, rather than hiding them in the C++ implementation or bindings generator.
Also as a rule, nop data should not be included: if Blink (bindings generator) ignores an IDL keyword or extended attribute, do not include it, as it suggests a difference in behavior when there is none. If this results in a difference from the spec, this is good, as it makes the difference in behavior visible.
Nulls and non-finite numbersTwo points to be careful of, and which are often incorrect in specs, particularly older specs, are nullability and non-finite values (infinities and NaN). These are both to ensure correct type checking. If these are incorrect in the spec – for example, a prose specifying behavior on non-finite values, but the IDL not reflecting this – please file a spec bug upstream, and link to it in the IDL file.
If null values are valid (for attributes, argument types, or method return values), the type MUST be marked with a ? to indicate nullable, as in attribute Foo? foo; Note that for arguments (but not attributes or method return values), optional is preferred to nullable (see Re: removeEventListener with only one passed parameter...).
Similarly, IEEE floating point allows non-finite numbers (infinities and NaN); if these are valid, the floating point type –
float or double – MUST be marked as unrestricted as in unrestricted float or unrestricted double – the bare float or double means finite floating point.Union typesMany older specs use overloading when a union type argument would be clearer. Please match spec, but file a spec bug for these and link to it. For example:
// FIXME: should be void bar((long or Foo) foo); https://www.w3.org/Bugs/Public/show_bug.cgi?id=123 void bar(long foo); void bar(Foo foo); Also, beware that you can't have multiple nullable arguments in the distinguishing position in an overload, as these are not distinguishing (what does null resolve to?). This is best resolved by using a union type if possible; otherwise make sure to mark only one overload as having a nullable argument in that position.Don't do this: void zork(Foo? x); void zork(Bar? x); // What does zork(null) resolve to? Instead do this: void zork(Foo? x); void zork(Bar x); ...but preferably this: void zork((Foo or Bar)? x); Extended attributesYou will often need to add Blink-specific extended attributes to specify implementation details.
Please comment extended attributes – why do you need special behavior?
BindingsSee Web IDL in Blink.
C++Bindings code assumes that a C++ class exists, with methods for each attribute or operation (with some exceptions). Attributes are implemented as properties, meaning that while in the JavaScript interface these are read and written as attributes, in C++ these are read and written by getter and setter methods.
For cases where an IDL attribute reflects a content attribute, you do not need to write boilerplate methods to call
getAttribute() and setAttribute(). Instead, use the [Reflect] extended attribute, and these calls will automatically be generated inline in the bindings code, with optimizations in some cases. However, if you wish to access these attributes from C++ code (say in another class), not just from JavaScript, you will need to write a getter and/or a setter, as necessary.NamesThe class and methods have default names, which can be overridden by the
[ImplementedAs] extended attribute; this is strongly discouraged, and method names should align with the spec unless there is very good reason for them to differ (this is sometimes necessary when there is a conflict, say when inheriting another interface).Given an IDL file Foo.idl:
interface Foo { attribute long a; attribute DOMString cssText; void f(); void f(long arg); void g(optional long arg); }; ...a minimal header file Foo.h illustrating this is:
class Foo { public: int a(); void setA(int); String cssText(); void setCSSText(const String&); void f(); void f(int); void g(); void g(int); // Alternatively, can use default arguments: // void f(int arg=0); };
Type information ("ScriptWrappable")Blink objects that are visible in JavaScript need type information, fundamentally because JavaScript is dynamically typed (so values have type), concretely because the bindings code uses type introspection for dynamic dispatch (function resolution of bindings functions): given a C++ object (representing the implementation of a JavaScript object), accessing it from V8 requires calling the correct C++ binding methods, which requires knowing its JavaScript type (i.e., the IDL interface type).
Blink does not use C++ run-time type information (RTTI), and thus the type information must be stored separately.
There are various ways this is done, most simply (for Blink developers) by the C++ class inheriting
ScriptWrappable and placing DEFINE_WRAPPERTYPEINFO in the class declaration. Stylistically ScriptWrappable should be the last class, or at least after more interesting classes, and should be directly inherited by the class (not indirectly from a more distant ancestor).Explicitly:
Foo.h:
#ifndef Foo_h #define Foo_h #include "bindings/v8/ScriptWrappable.h" namespace WebCore { class Foo FINAL : /* maybe others */ public ScriptWrappable { DEFINE_WRAPPERTYPEINFO(); // ... }; } // namespace WebCore #endif Foo_h In case of C++ inheritance, it's preferable to avoid inheriting ScriptWrappable indirectly, most simply because this creates overhead on a redundant write. In many cases this can be avoided by having an abstract base class that both concrete classes inherit. Stylistically, FIXME
However, in some cases – notably if both a base class and a derived class implement JS interface types (say, if there is IDL inheritance and the C++ inheritance is the same) – you will need to call
ScriptWrappable::init both in the base class and the derived class.Thus, to avoid this:
Foo.h:
class Foo FINAL : public Bar, public ScriptWrappable { /* ... */ };Bar.h:
class Bar : public ScriptWrappable { /* ... */ }; ...instead use an abstract base class, and have both concrete classes inherit
ScriptWrappable directly:Foo.h:
class Foo FINAL : public FooBarBase, public ScriptWrappable { /* ... */ };Bar.h: class Bar FINAL : public FooBarBase, public ScriptWrappable { /* ... */ };FooBarBase.h:
class FooBarBase { /* ... */ }; History (ScriptWrappable)
Garbage CollectionBuildYou need to list the
.idl file and .h/.cpp files in the correct GN variable so that they will be built (bindings generated, Blink code compiled.) IDL files to be processed are listed in .gni (GN Include) files. For core files, this is core_idl_files.gni.There are 3 dichotomies in these
.idl files, which affect where you list them in the build:
For core interfaces, the IDL files are listed in the
core_idl_files variable or in the core_dependency_idl_files variable, if the IDL file is a partial interface or the target (right side of) an implements statement. This distinction is because partial interfaces and implemented interfaces do not have their own bindings generated, so these IDL files are not directly compiled.Testing files are listed in the
core_testing_idl_files variable instead; there are currently no core testing dependency files.The C++ files should be listed in the
core_files variable or an appropriate core_*_files variable, depending on directory, or core_testing_files if a testing interface.Modules files are analogous, and placed in modules_idl_files.gni. There are currently no modules testing interface files, but there are modules testing dependency files, which are listed in
modules_dependency_idl_files and modules_testing_files .TestsMake sure to test:
SubtypingThere are three mechanisms for subtyping in IDL:
The corresponding C++ implementations are as follows, here illustrated for
attribute T foo;
IDL files SHOULD agree with spec, and almost always MUST do so. It is not ok to change the kind of subtyping or move members between interfaces, and violations SHOULD or MUST be fixed:
Technical detailsWhile members of an interface definition, members of implemented interface, and members of partial interfaces are identical for JavaScript, partial interface members – and members of certain implemented interfaces, namely those with the
[TreatAsPartial] extended attribute – are treated differently internally in Blink (see below).Inheritance and implements are both interface inheritance. JavaScript has single inheritance, and IDL inheritance corresponds to JavaScript inheritance, while IDL
implements provides multiple inheritance in IDL, which does not correspond to inheritance in JavaScript.In both cases, by spec, members of the inherited or implemented interface must be implemented on the JavaScript object implementing the interface. Concretely, members of inherited interfaces are implemented as properties on the prototype object of the parent interface, while members of implemented interfaces are implemented as properties of the implementing interface.
In C++, members of an interface definition and members of implemented interfaces are implemented on the C++ object (referred to as the parameter or variable
impl ) implementing the JavaScript object. Specifically this is done in the Blink class corresponding to the IDL interface or a base class – the C++ hierarchy is invisible to both JavaScript and the bindings.Implementation-wise, inheritance and implements differ in two ways:
If (IDL) interface A inherits from interface B, then usually (C++) class A inherits from class B, meaning that:
interface A : B { /* ... */ }; class A : B { /* ... */ }; class A : C { /* ... */ }; class C : B { /* ... */ }; However, the bindings are agnostic about this, and simply set the prototype in the wrapper object to be the inherited interface (concretely, sets the parentClass attribute in the WrapperTypeInfo of the class's bindings). Dispatch is thus done in JavaScript.
"A implements B;"
should mean that members declared in (IDL) interface B
are members of (C++) classes implementing A.
impl.
Partial interfaces formally are type extension (external type extension, since specified in a separate place from the original definition), and in principle are simply part of the interface, just defined separately, as a convenience for spec authors. However in practice, members of partial interfaces are not assumed to be implemented on the C++ object (
impl ), and are not defined in the Blink class implementing the interface. Instead, they are implemented as static members of a separate class, which take impl as their first argument. This is done because in practice, partial interfaces are type extensions, which often only used in subtypes or are deactivated (via conditionals or as runtime enabled features), and we do not want to bloat the main Blink class to include these.Further, in some cases we must use type extension (static methods) for implemented interfaces as well. This is due to componentization in Blink (see Browser Components), currently
core versus modules. Code in core cannot inherit from code in modules, and thus if an interface in core implements an interface in modules, this must be implemented via type extension (static methods in modules ). This is an exceptional case, and indicates that Blink's internal layering (componentization) disagrees with the layering implied by the IDL specs, and formally should be resolved by moving the relevant interface from modules to core. This is not always possible or desirable (for internal implementation reasons), and thus static methods can be specified via the [TreatAsPartial] extended attribute on the implemented interface.Inheritance and code reuseIDL has single inheritance, which maps directly to JavaScript inheritance (prototype chain). C++ has multiple inheritance, and the two hierarchies need not be related.
FIXME: There are issues if a C++ class inherits from another C++ class that implements an IDL interface, as .
downcasting
IDL has 3 mechanisms for combining interfaces:
Sharing code with a legacy interface (unprefixing)...
Changing inheritance → implementsConverting a parent to the target of an implements
See alsoOther Blink interfaces, not standard Web IDL interfaces:
External linksFor reference, documentation by other projects.
|
For Developers >