Skip to content

Commit 8736d80

Browse files
authored
Merge pull request #154 from space-wizards/25-10-09-wesl-c-resolve
wesl-c: Support for providing a custom resolve implementation
2 parents 7190f33 + 1ac6c3e commit 8736d80

5 files changed

Lines changed: 574 additions & 254 deletions

File tree

Cargo.lock

Lines changed: 111 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wesl-c/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ crate-type = ["cdylib", "staticlib"]
1313
[dependencies]
1414
wesl = { workspace = true }
1515

16+
[build-dependencies]
17+
bindgen = "0.72"
18+
1619
[features]
1720
eval = ["wesl/eval"]
1821
generics = ["wesl/generics"]

crates/wesl-c/build.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
4+
fn main() {
5+
let handles = ["WeslCompiler", "WeslTranslationUnit"];
6+
7+
let mut builder = bindgen::Builder::default()
8+
.header("include/wesl.h")
9+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
10+
.allowlist_item("wesl.*")
11+
.allowlist_item("Wesl.*")
12+
.prepend_enum_name(false)
13+
.ignore_functions();
14+
15+
for handle in handles {
16+
builder = builder
17+
.blocklist_item(handle)
18+
.raw_line(format!("type {handle} = crate::{handle};"));
19+
}
20+
21+
let bindings = builder.generate().expect("Unable to generate bindings");
22+
23+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
24+
bindings
25+
.write_to_file(out_path.join("bindings.rs"))
26+
.expect("Couldn't write bindings!");
27+
}

crates/wesl-c/include/wesl.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111

1212
// -- handles
1313
typedef struct WeslCompiler WeslCompiler;
14+
typedef struct WeslTranslationUnit WeslTranslationUnit;
1415

1516
// -- enums
1617
typedef enum WeslManglerKind {
@@ -45,6 +46,35 @@ typedef struct WeslBinding {
4546
size_t data_len;
4647
} WeslBinding;
4748

49+
typedef struct WeslResolveSourceResult {
50+
bool success;
51+
const char* source;
52+
} WeslResolveSourceResult;
53+
54+
typedef struct WeslResolveModuleResult {
55+
bool success;
56+
WeslTranslationUnit* module;
57+
} WeslResolveModuleResult;
58+
59+
typedef const WeslResolveSourceResult* (*WeslResolveSourceFunction)(const char* path, void* userdata);
60+
typedef const WeslResolveModuleResult* (*WeslResolveModuleFunction)(const char* path, void* userdata);
61+
typedef void (*WeslResolveSourceFreeFunction)(const WeslResolveSourceResult* result, void* userdata);
62+
typedef void (*WeslResolveModuleFreeFunction)(const WeslResolveModuleResult* result, void* userdata);
63+
typedef const char* (*WeslResolveStringFunction)(const char* path, void* userdata);
64+
typedef void (*WeslResolveFreeStringFunction)(const char* string, void* userdata);
65+
66+
typedef struct WeslResolverOptions {
67+
void* userdata;
68+
WeslResolveSourceFunction resolve_source;
69+
WeslResolveSourceFreeFunction resolve_source_free;
70+
WeslResolveModuleFunction resolve_module;
71+
WeslResolveModuleFreeFunction resolve_module_free;
72+
WeslResolveStringFunction display_name;
73+
WeslResolveFreeStringFunction free_display_name;
74+
WeslResolveStringFunction fs_path;
75+
WeslResolveFreeStringFunction free_fs_path;
76+
} WeslResolverOptions;
77+
4878
typedef struct WeslCompileOptions {
4979
WeslManglerKind mangler;
5080
bool sourcemap;
@@ -58,6 +88,7 @@ typedef struct WeslCompileOptions {
5888
bool lazy;
5989
bool keep_root;
6090
bool mangle_root;
91+
WeslResolverOptions* resolver;
6192
} WeslCompileOptions;
6293

6394
typedef struct WeslStringMap {
@@ -102,6 +133,12 @@ typedef struct WeslResult {
102133
WeslError error;
103134
} WeslResult;
104135

136+
typedef struct WeslParseResult {
137+
bool success;
138+
WeslTranslationUnit* data;
139+
WeslError error;
140+
} WeslParseResult;
141+
105142
typedef struct WeslExecOptions {
106143
WeslCompileOptions compile;
107144
const char* entrypoint;
@@ -127,6 +164,10 @@ WeslResult wesl_compile(
127164
const WeslBoolMap* features
128165
);
129166

167+
WeslParseResult wesl_parse(
168+
const char* source
169+
);
170+
130171
WeslResult wesl_eval(
131172
const WeslStringMap* files,
132173
const char* root,
@@ -150,6 +191,11 @@ void wesl_free_string(const char* ptr);
150191
void wesl_free_result(WeslResult* result);
151192
void wesl_free_exec_result(WeslExecResult* result);
152193

194+
// Free a WeslParseResult filled by wesl_parse.
195+
// This does NOT free the WeslTranslationUnit* inside the result, if the parsing succeeded!
196+
void wesl_free_parse_result(WeslParseResult* result);
197+
void wesl_free_translation_unit(WeslTranslationUnit* unit);
198+
153199
// -- utility
154200

155201
// note: results from this function must not be freed

0 commit comments

Comments
 (0)