You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section describes the host-related macros of XS in C (see Table 2). An annotated example that uses the host-related macros follows.
2062
2062
2063
-
The central concept for the macros below is a Host Object. A Host Object is an XS object that has data that can only be accessed in C. Host objects are created either using `xsNewHostObject`or using the XS `@` syntax in JavaScript `(class Foo @ "aDestructorFunction" {....`. Internally, a Host Object has a single dedicated slot which holds both the destructor and Host Data/Chunk. Non-host objects don't have space for Host Data or Host Chunk, so objects that have a JavaScript constructor but also have some methods implemented in C cannot host Host Data or a Host Chunk.
2063
+
A host object is an XS object that has a data pointer that can only be accessed in C and a native destructor that is invoked when the host object is garbage collected. Host objects are created in C using `xsNewHostObject`and in JavaScript using the [XS `@` syntax](#syntax-extension) in JavaScript `(class Foo @ "aDestructorFunction" {}`. Internally, a host object has a dedicated slot to hold its destructor and a data pointer; non-host objects don't have this slot. Consequently, only host objects have a native destructor and data pointer that is accessible only from C. This data pointer is either host data or a host chunk.
2064
2064
2065
-
Host Data is a pointer stored by XS in a Host Object. The pointer value and data it points to are managed entirely by the C code. XS doesn't do anything beyond storing the pointer. Host Data is usually allocated with malloc/calloc, but this isn't required. The Host Data is usually disposed in the object's destructor.
2065
+
Host data is a pointer stored by XS in a host object. The pointer and data it points to are managed entirely by the host object's C code. XS stores the pointer but does not access it in any way. Host data is usually allocated with `malloc`/`calloc`, but this isn't required. Host data is disposed by the host object's destructor.
2066
2066
2067
-
Host Chunk is memory allocated by XS in its chunk heap for use by native C code. XS garbage collects the storage when the object is garbage collected. The memory is relocatable (like all XS chunks). Unlike Host Data this can avoid loosing memory to fragmentation but it requires some extra attention because the pointer can be invalidated by GC due to compaction. Consequently, C code needs to refresh the pointer if it performs an operation which could trigger a GC. Also, because the block can move, it can only be used inside an XS callback; accessing it from an interrupt, for example, is unsafe because it could be moving.
2067
+
A host chunk is memory allocated by XS in its chunk heap for use by a host object from its native C code. XS garbage collects this storage when the host object is garbage collected. The memory is relocatable (like all XS chunks), so unlike Host Data it avoids losing memory to fragmentation. However, it requires some extra attention because the pointer may be invalidated when the garbage collector compacts memory. Therefore, C code needs to refetch the pointer after any operation which might trigger a garbage collection. Because the chunk pointer can move, it can only be used inside an XS callback; accessing it from an interrupt, for example, is unsafe because it could be moving. The [Rectangle example](#rectangle-example) shows how to use a host chunk.
2068
+
2069
+
Implementing a host object using host data is easier than a host chunk, but potentially less memory efficient.
2068
2070
2069
2071
> Note that an object has either host data or a host chunk but never both.
2070
2072
@@ -2200,7 +2202,7 @@ To get and set the data of a host object, use the `xsGetHostData` and `xsSetHost
2200
2202
| --- | :-- |
2201
2203
| `theThis` | A reference to a host object
2202
2204
2203
-
Returns the Host Data pointer.
2205
+
Returns the host data pointer.
2204
2206
2205
2207
***
2206
2208
@@ -2212,7 +2214,7 @@ Returns the Host Data pointer.
2212
2214
| `theThis` | A reference to a host object
2213
2215
| `theData` | The data to set
2214
2216
2215
-
Sets the Host Data pointer.
2217
+
Sets the host data pointer.
2216
2218
2217
2219
***
2218
2220
@@ -2227,7 +2229,7 @@ To get and set the data of a host object as a chunk, use the `xsGetHostChunk` an
2227
2229
| --- | :-- |
2228
2230
| `theThis` | A reference to a host object
2229
2231
2230
-
Returns a pointer to the Host Chunk data
2232
+
Returns a pointer to the host chunk data
2231
2233
2232
2234
***
2233
2235
@@ -2273,6 +2275,7 @@ The `xsBeginHost` macro sets up the stack, and the `xsEndHost` macro cleans up t
2273
2275
2274
2276
Uncaught exceptions that occur between the calls the `xsBeginHost` and `xsEndHost `do not propagate beyond `xsEndHost`.
2275
2277
2278
+
<aid="file-example"></a>
2276
2279
##### Example
2277
2280
2278
2281
This example creates a `File` class using the host macros of XS in C. This is a low-level technique that provides the most flexibility. Most projects do not create classes directly using XS in C, but instead use the [`@` syntax extension](#syntax-extension) to declare classes because it is simpler.
XS provides the `@` language syntax extension to implement JavaScript functions in C. The language extension is only recognized by the XS compiler. This section introduces the language extension with a JavaScript class that implements methods with C functions.
0 commit comments