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
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,6 +125,25 @@ impl WebSocket {
125
125
126
126
Per Web IDL, constants are `{writable: false, enumerable: true, configurable: false}`.
127
127
128
+
## `#[jsg_constructor]`
129
+
130
+
Marks a static method as the JavaScript constructor for a `#[jsg_resource]`. When JavaScript calls `new MyClass(args)`, V8 invokes this method, creates a `jsg::Rc<Self>`, and attaches it to the `this` object.
131
+
132
+
```rust
133
+
#[jsg_resource]
134
+
implMyResource {
135
+
#[jsg_constructor]
136
+
fnconstructor(name:String) ->Self {
137
+
Self { name }
138
+
}
139
+
}
140
+
// JS: let r = new MyResource("hello");
141
+
```
142
+
143
+
The method must be static (no `self` receiver) and must return `Self`. Only one `#[jsg_constructor]` is allowed per impl block. The first parameter may be `&mut Lock` if the constructor needs isolate access — it is not exposed as a JS argument.
144
+
145
+
If no `#[jsg_constructor]` is present, `new MyClass()` throws an `Illegal constructor` error.
146
+
128
147
## `#[jsg_oneof]`
129
148
130
149
Generates `jsg::Type` and `jsg::FromJS` implementations for union types. Use this to accept parameters that can be one of several JavaScript types.
0 commit comments