Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions compile_flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
-stdlib=libc++
-xc++
-nostdinc
-Ibazel-bin/external/com_cloudflare_lol_html/_virtual_includes/lolhtml
-Ibazel-bin/external/+_repo_rules+com_cloudflare_lol_html/_virtual_includes/lolhtml
-Ibazel-bin/external/perfetto+/
-Iexternal/ada-url+/
-Iexternal/abseil-cpp+/
-Iexternal/+http+simdutf/
-Iexternal/+nbytes+nbytes/include/
-Iexternal/codspeed/google_benchmark/include/
-Iexternal/+http+nbytes/include/
-Iexternal/google_benchmark++_repo_rules+codspeed/google_benchmark/include/
-Iexternal/perfetto+/include/
-Iexternal/perfetto+/include/perfetto/base/build_configs/bazel/
-Iexternal/boringssl+/include
-Iexternal/+ncrypto+ncrypto/include
-Iexternal/+http+ncrypto/include
-isystembazel-bin/external/sqlite3+
-Isrc
-isystem/usr/lib/llvm-21/include/c++/v1
Expand Down Expand Up @@ -40,30 +40,27 @@
-isystembazel-bin/external/+http+capnp-cpp/src/kj/compat/_virtual_includes/kj-gzip
-isystembazel-bin/external/+http+capnp-cpp/src/kj/compat/_virtual_includes/kj-http
-isystembazel-bin/external/+http+capnp-cpp/src/kj/compat/_virtual_includes/kj-tls
-isystembazel-bin/external/workerd-cxx/_virtual_includes/core/
-isystembazel-bin/external/workerd-cxx/kj-rs/_virtual_includes/kj-rs-lib/
-isystembazel-bin/external/+_repo_rules+v8
-isystembazel-bin/external/+_repo_rules+v8/icu
-isystembazel-bin/external/+http+workerd-cxx/_virtual_includes/core/
-isystembazel-bin/external/+http+workerd-cxx/kj-rs/_virtual_includes/kj-rs-lib/
-isystemexternal/+_repo_rules2+v8/include
-isystemexternal/+_repo_rules2+v8/include/cppgc
-isystembazel-bin/src
-isystemexternal/brotli+/c/include
-isystemexternal/+_repo_rules2+com_googlesource_chromium_icu/source/common
-isystemexternal/+_repo_rules3+com_googlesource_chromium_icu/source/common
-isystemexternal/zlib+
-isystembazel-bin/src/rust/cxx-integration/_virtual_includes/cxx-include/
-isystembazel-bin/src/rust/cxx-integration/_virtual_includes/cxx-integration@cxx
-isystembazel-bin/src/rust/cxx-integration-test/_virtual_includes/cxx-integration-test@cxx
-isystembazel-bin/src/rust/cxx-integration/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/cxx-integration-test/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/dns/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/kj/_virtual_includes/ffi
-isystembazel-bin/src/rust/kj/_virtual_includes/http.rs@cxx
-isystembazel-bin/src/rust/kj/_virtual_includes/io.rs@cxx
-isystembazel-bin/src/rust/kj/tests/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/python-parser/_virtual_includes/python-parser@cxx
-isystembazel-bin/src/rust/net/_virtual_includes/net@cxx
-isystembazel-bin/src/rust/transpiler/_virtual_includes/transpiler@cxx
-isystembazel-bin/src/rust/python-parser/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/net/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/transpiler/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/api/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/jsg/_virtual_includes/bridge
-isystembazel-bin/src/rust/jsg/_virtual_includes/ffi
-isystembazel-bin/src/rust/jsg/_virtual_includes/lib.rs@cxx
-isystembazel-bin/src/rust/jsg/_virtual_includes/modules.rs@cxx
-isystembazel-bin/src/rust/jsg/_virtual_includes/v8.rs@cxx
-isystembazel-bin/src/rust/jsg-test/_virtual_includes/ffi-hdrs
-isystembazel-bin/src/rust/jsg-test/_virtual_includes/lib.rs@cxx
Expand Down
24 changes: 16 additions & 8 deletions src/rust/api/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ pub enum DnsParserError {

impl From<DnsParserError> for jsg::Error {
fn from(val: DnsParserError) -> Self {
Self::new(jsg::ExceptionType::Error, val.to_string())
match val {
DnsParserError::InvalidHexString(msg) | DnsParserError::InvalidDnsResponse(msg) => {
Self::new_error(&msg)
}
DnsParserError::ParseIntError(msg) => Self::new_range_error(msg.to_string()),
DnsParserError::Unknown => Self::new_error("Unknown dns parser error"),
}
}
}

Expand Down Expand Up @@ -136,7 +142,7 @@ impl DnsUtil {
/// `DnsParserError::InvalidHexString`
/// `DnsParserError::ParseIntError`
#[jsg_method]
pub fn parse_caa_record(&self, record: &str) -> Result<CaaRecord, DnsParserError> {
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
// Let's remove "\\#" and the length of data from the beginning of the record
let data = record.split_ascii_whitespace().collect::<Vec<_>>()[2..].to_vec();
let critical = data[0].parse::<u8>()?;
Expand Down Expand Up @@ -191,7 +197,7 @@ impl DnsUtil {
/// `DnsParserError::InvalidHexString`
/// `DnsParserError::ParseIntError`
#[jsg_method]
pub fn parse_naptr_record(&self, record: &str) -> jsg::Result<NaptrRecord, DnsParserError> {
pub fn parse_naptr_record(&self, record: String) -> jsg::Result<NaptrRecord, DnsParserError> {
let data = record.split_ascii_whitespace().collect::<Vec<_>>()[1..].to_vec();

let order_str = data[1..3].to_vec();
Expand Down Expand Up @@ -262,7 +268,7 @@ mod tests {
_state: ResourceState::default(),
};
let record = dns_util
.parse_caa_record("\\# 15 00 05 69 73 73 75 65 70 6b 69 2e 67 6f 6f 67")
.parse_caa_record("\\# 15 00 05 69 73 73 75 65 70 6b 69 2e 67 6f 6f 67".to_owned())
.unwrap();

assert_eq!(record.critical, 0);
Expand All @@ -277,7 +283,8 @@ mod tests {
};
let record = dns_util
.parse_caa_record(
"\\# 21 00 09 69 73 73 75 65 77 69 6c 64 6c 65 74 73 65 6e 63 72 79 70 74",
"\\# 21 00 09 69 73 73 75 65 77 69 6c 64 6c 65 74 73 65 6e 63 72 79 70 74"
.to_owned(),
)
.unwrap();

Expand All @@ -291,8 +298,9 @@ mod tests {
let dns_util = DnsUtil {
_state: ResourceState::default(),
};
let result =
dns_util.parse_caa_record("\\# 15 00 05 69 6e 76 61 6c 69 64 70 6b 69 2e 67 6f 6f 67");
let result = dns_util.parse_caa_record(
"\\# 15 00 05 69 6e 76 61 6c 69 64 70 6b 69 2e 67 6f 6f 67".to_owned(),
);

assert!(result.is_err());
}
Expand All @@ -303,7 +311,7 @@ mod tests {
_state: ResourceState::default(),
};
let record = dns_util
.parse_naptr_record("\\# 37 15 b3 08 ae 01 73 0a 6d 79 2d 73 65 72 76 69 63 65 06 72 65 67 65 78 70 0b 72 65 70 6c 61 63 65 6d 65 6e 74 00")
.parse_naptr_record("\\# 37 15 b3 08 ae 01 73 0a 6d 79 2d 73 65 72 76 69 63 65 06 72 65 67 65 78 70 0b 72 65 70 6c 61 63 65 6d 65 6e 74 00".to_owned())
.unwrap();

assert_eq!(record.flags, "s");
Expand Down
10 changes: 5 additions & 5 deletions src/rust/api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ mod tests {
#[test]
fn test_wrap_resource_equality() {
let harness = Harness::new();
harness.run_in_context(|isolate, _ctx| unsafe {
let mut lock = jsg::Lock::from_isolate_ptr(isolate);
harness.run_in_context(|lock, _ctx| unsafe {
let dns_util = jsg::Ref::new(DnsUtil {
_state: ResourceState::default(),
});
let mut dns_util_template = DnsUtilTemplate::new(&mut lock);
let mut dns_util_template = DnsUtilTemplate::new(lock);

let lhs = jsg::wrap_resource(&mut lock, dns_util.clone(), &mut dns_util_template);
let rhs = jsg::wrap_resource(&mut lock, dns_util, &mut dns_util_template);
let lhs = jsg::wrap_resource(lock, dns_util.clone(), &mut dns_util_template);
let rhs = jsg::wrap_resource(lock, dns_util, &mut dns_util_template);

assert_eq!(lhs, rhs);
Ok(())
});
}
}
33 changes: 31 additions & 2 deletions src/rust/jsg-macros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Generates the `jsg::Struct` and `jsg::Type` implementations for data structures.
```rust
#[jsg_struct]
pub struct CaaRecord {
pub critical: u8,
pub critical: f64,
pub field: String,
pub value: String,
}
Expand All @@ -20,6 +20,35 @@ pub struct MyRecord {
}
```

## `#[jsg_method]`

Generates FFI callback functions for JSG resource methods. The `name` parameter is optional and defaults to converting the method name from `snake_case` to `camelCase`.

Parameters and return values are handled via the `jsg::Wrappable` trait. Any type implementing `Wrappable` can be used as a parameter or return value:

- `Option<T>` - accepts `T` or `undefined`, rejects `null`
- `Nullable<T>` - accepts `T`, `null`, or `undefined`
- `NonCoercible<T>` - rejects values that would require JavaScript coercion

```rust
impl DnsUtil {
#[jsg_method(name = "parseCaaRecord")]
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
// Errors are thrown as JavaScript exceptions
}

#[jsg_method]
pub fn get_name(&self) -> String {
self.name.clone()
}

#[jsg_method]
pub fn reset(&self) {
// Void methods return undefined in JavaScript
}
}
```

## `#[jsg_resource]`

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.
Expand All @@ -40,7 +69,7 @@ pub struct MyUtil {
#[jsg_resource]
impl DnsUtil {
#[jsg_method]
pub fn parse_caa_record(&self, record: &str) -> Result<CaaRecord, DnsParserError> {
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
// implementation
}
}
Expand Down
Loading
Loading