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
Copy file name to clipboardExpand all lines: src/rust/jsg-macros/README.md
+50-7Lines changed: 50 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,17 +53,13 @@ impl DnsUtil {
53
53
54
54
Generates boilerplate for JSG resources. Applied to both struct definitions and impl blocks. Automatically implements `jsg::Type::class_name()` using the struct name, or a custom name if provided via the `name` parameter.
55
55
56
-
**Important:** Resource structs must include a `_state: jsg::ResourceState` field for internal JSG state management.
57
-
58
56
```rust
59
57
#[jsg_resource]
60
-
pubstructDnsUtil {
61
-
pub_state:jsg::ResourceState,
62
-
}
58
+
pubstructDnsUtil {}
63
59
64
60
#[jsg_resource(name ="CustomUtil")]
65
61
pubstructMyUtil {
66
-
pub_state:jsg::ResourceState,
62
+
pubvalue:u32,
67
63
}
68
64
69
65
#[jsg_resource]
@@ -75,7 +71,12 @@ impl DnsUtil {
75
71
}
76
72
```
77
73
78
-
On struct definitions, generates `jsg::Type`, wrapper struct, and `ResourceTemplate` implementations. On impl blocks, scans for `#[jsg_method]` attributes and generates the `Resource` trait implementation.
- Wrapper struct and `ResourceTemplate` implementations
78
+
79
+
On impl blocks, scans for `#[jsg_method]` attributes and generates the `Resource` trait implementation.
79
80
80
81
## `#[jsg_oneof]`
81
82
@@ -105,3 +106,45 @@ impl MyResource {
105
106
```
106
107
107
108
The macro generates type-checking code that matches JavaScript values to enum variants without coercion. If no variant matches, a `TypeError` is thrown listing all expected types.
109
+
110
+
### Garbage Collection
111
+
112
+
Resources are automatically integrated with V8's garbage collector through the C++ `Wrappable` base class. The macro automatically generates a `GarbageCollected` implementation that traces fields requiring GC integration:
113
+
114
+
-`Ref<T>` fields - traces the underlying resource
115
+
-`TracedReference<T>` fields - traces the JavaScript handle
116
+
-`Option<Ref<T>>` and `Option<TracedReference<T>>` - conditionally traces
117
+
-`RefCell<Option<Ref<T>>>` - supports cyclic references through interior mutability
118
+
119
+
```rust
120
+
#[jsg_resource]
121
+
pubstructMyResource {
122
+
// Automatically traced
123
+
js_callback:Option<TracedReference<Object>>,
124
+
child_resource:Option<Ref<ChildResource>>,
125
+
126
+
// Not traced (plain data)
127
+
name:String,
128
+
}
129
+
130
+
// Cyclic references using RefCell
131
+
#[jsg_resource]
132
+
pubstructNode {
133
+
name:String,
134
+
next:RefCell<Option<Ref<Node>>>,
135
+
}
136
+
```
137
+
138
+
For complex cases or custom tracing logic, you can manually implement `GarbageCollected` without using the jsg_resource macro:
0 commit comments