Skip to content

Commit

Permalink
Provide WebSocket ready state constants to follow the Web IDL definit…
Browse files Browse the repository at this point in the history
…ion (#1393)

* Add a macro to register a JSG static constant from an arbitrary value.

* Provide WebSocket ready state constants to follow the Web IDL definition
  • Loading branch information
bcaimano authored Dec 19, 2023
1 parent d8d662c commit 1c63020
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/workerd/api/web-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,16 @@ class WebSocket: public EventTarget {
JSG_METHOD(deserializeAttachment);

JSG_STATIC_CONSTANT(READY_STATE_CONNECTING);
JSG_STATIC_CONSTANT_NAMED(CONNECTING, WebSocket::READY_STATE_CONNECTING);

JSG_STATIC_CONSTANT(READY_STATE_OPEN);
JSG_STATIC_CONSTANT_NAMED(OPEN, WebSocket::READY_STATE_OPEN);

JSG_STATIC_CONSTANT(READY_STATE_CLOSING);
JSG_STATIC_CONSTANT_NAMED(CLOSING, WebSocket::READY_STATE_CLOSING);

JSG_STATIC_CONSTANT(READY_STATE_CLOSED);
JSG_STATIC_CONSTANT_NAMED(CLOSED, WebSocket::READY_STATE_CLOSED);

// Previously, we were setting all properties as instance properties,
// which broke the ability to subclass the Event object. With the
Expand Down
14 changes: 14 additions & 0 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ namespace workerd::jsg {
registry.template registerStaticConstant<NAME, decltype(Self::name)>(Self::name); \
} while (false)

// This works the same as JSG_STATIC_CONSTANT but allows us to provide an alias to an arbitrary c++
// constant instead. For example:
// struct Interface {
// static Interface constructor()
// JSG_RESOURCE_TYPE {
// JSG_STATIC_CONSTANT_NAMED(FOO_BAR, SOME_SYSTEM_CONSTANT);
// }
// };
#define JSG_STATIC_CONSTANT_NAMED(name, constant) \
do { \
static const char NAME[] = #name; \
registry.template registerStaticConstant<NAME, decltype(constant)>(constant); \
} while (false)

// Use inside a JSG_RESOURCE_TYPE block to declare that this type inherits from another type,
// which must also have a JSG_RESOURCE_TYPE block. This type must singly, non-virtually inherit
// from the specified type. (Multiple inheritance and virtual inheritance will not work since we
Expand Down

0 comments on commit 1c63020

Please sign in to comment.