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 can aid for common use by allowing string literals, rather than
61
62
requiring otherwise-unused locals
62
-
- if the primary purpose of a function is to mutate its parameters, this
63
-
should be considered case-by-case; for example, if we were to add something
64
-
similar to `array_pop()`, it probably should take an `inout` container - but
65
-
probably shouldn't be in `OS\`
63
+
- if the primary purpose of a set of functions is to create, mutate and
64
+
destroy a C data structure, whose reference would not be held by C
65
+
libraries, they should be exposed as a `vec`. See [Appendix:
66
+
short-lived C pointer encoding](#appendix-short-lived-c-pointer-encoding)
67
+
section for more detail.
66
68
67
69
## Implementation notes
68
70
@@ -104,3 +106,63 @@ inspiration for several decisions in this library.
104
106
- that said, if an operation is extremely efficient and almost always wanted,
105
107
consider doing it natively, automatically. For example, ints in HSL
106
108
`sockaddr` are always in host byte order, not network byte order.
109
+
110
+
## Appendix: C / Hack type mapping cheat sheet
111
+
112
+
| C type | Hack type |
113
+
|---|---|
114
+
|`int socket` or `int filedes`, or other long-lived system resources |`HH\Lib\OS\FileDescriptor` or other wrapper classes |
115
+
| Setters for `void *` or `struct *` whose reference would not be held by C libraries |`vec`, see [Appendix: encoding lightweight C data types with setters](#appendix-encoding-lightweight-c-data-types-with-setters)|
116
+
117
+
## Appendix: encoding lightweight C data types with setters
118
+
119
+
A common practice in C library is to expose a set of functions, including a
120
+
constructor, a destructor and several setters, to manipulate a C data type. If
121
+
the C data type represents a native resource, e.g. `int filedes`, we should
122
+
create a closeable Hack class wrappers for the C native resource, otherwise we
123
+
should a create a garbage-collectable mirror in Hack, which creates and destroys
124
+
the underlying C data type on demand. The rest of this section describes the
125
+
encoding of the garbage-collectable mirror corresponding to the C data type.
126
+
127
+
### Setters as a `vec` of an interface
128
+
129
+
The general way to encode a lightweight C data type and its setters is to
130
+
consider the data type as a `vec` of setter interface, and to create a class
131
+
implements the interface for each setter function. For example, given the
132
+
following short-lived C pointer and the related utility functions:
0 commit comments