-
Notifications
You must be signed in to change notification settings - Fork 547
Automatic TypeScript definition generation from JSG RTTI #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad63b12
Add RTTI export script
mrbbot 8192143
Setup Bazel for JavaScript and compile `rtti.capnp` to JavaScript
mrbbot 7fc1fc5
Generate TypeScript types from RTTI
mrbbot b0db6e6
Replace `Iterator`-like interfaces with built-in `Iterator`s
mrbbot f297730
Extract global types in global scope
mrbbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| .idea | ||
| .DS_Store | ||
|
|
||
| /rust-deps/target | ||
| /rust-deps/Cargo.toml | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """ | ||
| Bazel rule to compile .capnp files into JavaScript using capnp-ts. | ||
| Based on https://github.com/capnproto/capnproto/blob/3b2e368cecc4b1419b40c5970d74a7a342224fac/c++/src/capnp/cc_capnp_library.bzl. | ||
| """ | ||
|
|
||
| load("@aspect_rules_js//js:defs.bzl", "js_library") | ||
|
|
||
| capnp_provider = provider("Capnproto Provider", fields = { | ||
| "includes": "includes for this target (transitive)", | ||
| "inputs": "src + data for the target", | ||
| "src_prefix": "src_prefix of the target", | ||
| }) | ||
|
|
||
| def _workspace_path(label, path): | ||
| if label.workspace_root == "": | ||
| return path | ||
| return label.workspace_root + "/" + path | ||
|
|
||
| def _capnp_gen_impl(ctx): | ||
| label = ctx.label | ||
| src_prefix = _workspace_path(label, ctx.attr.src_prefix) | ||
| includes = [] | ||
|
|
||
| inputs = ctx.files.srcs + ctx.files.data | ||
| for dep_target in ctx.attr.deps: | ||
| includes += dep_target[capnp_provider].includes | ||
| inputs += dep_target[capnp_provider].inputs | ||
|
|
||
| if src_prefix != "": | ||
| includes.append(src_prefix) | ||
|
|
||
| system_include = ctx.files._capnp_system[0].dirname.removesuffix("/capnp") | ||
|
|
||
| out_dir = ctx.var["GENDIR"] | ||
| if src_prefix != "": | ||
| out_dir = out_dir + "/" + src_prefix | ||
|
|
||
| js_out = "-o%s:%s" % (ctx.executable._capnpc_ts.path, out_dir) | ||
| args = ctx.actions.args() | ||
| args.add_all(["compile", "--verbose", js_out]) | ||
| args.add_all(["-I" + inc for inc in includes]) | ||
| args.add_all(["-I", system_include]) | ||
| if src_prefix != "": | ||
| args.add_all(["--src-prefix", src_prefix]) | ||
|
|
||
| args.add_all([s for s in ctx.files.srcs]) | ||
|
|
||
| ctx.actions.run( | ||
| inputs = inputs + ctx.files._capnpc_ts + ctx.files._capnpc_capnp + ctx.files._capnp_system, | ||
| tools = [ctx.executable._capnpc_ts], # Include required js_binary runfiles | ||
| outputs = ctx.outputs.outs, | ||
| executable = ctx.executable._capnpc, | ||
| arguments = [args], | ||
| mnemonic = "GenCapnp", | ||
| ) | ||
|
|
||
| return [ | ||
| capnp_provider( | ||
| includes = includes, | ||
| inputs = inputs, | ||
| src_prefix = src_prefix, | ||
| ), | ||
| ] | ||
|
|
||
| _capnp_gen = rule( | ||
| attrs = { | ||
| "srcs": attr.label_list(allow_files = True), | ||
| "deps": attr.label_list(providers = [capnp_provider]), | ||
| "data": attr.label_list(allow_files = True), | ||
| "outs": attr.output_list(), | ||
| "src_prefix": attr.string(), | ||
| "_capnpc": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnp_tool"), | ||
| "_capnpc_ts": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "//:capnpc_ts"), | ||
| "_capnpc_capnp": attr.label(executable = True, allow_single_file = True, cfg = "exec", default = "@capnp-cpp//src/capnp:capnpc-capnp"), | ||
| "_capnp_system": attr.label(default = "@capnp-cpp//src/capnp:capnp_system_library"), | ||
| }, | ||
| output_to_genfiles = True, | ||
| implementation = _capnp_gen_impl, | ||
| ) | ||
|
|
||
| def js_capnp_library( | ||
| name, | ||
| srcs = [], | ||
| data = [], | ||
| deps = [], | ||
| src_prefix = "", | ||
| visibility = None, | ||
| target_compatible_with = None, | ||
| **kwargs): | ||
| """Bazel rule to create a JavaScript capnproto library from capnp source files | ||
|
|
||
| Args: | ||
| name: library name | ||
| srcs: list of files to compile | ||
| data: additional files to provide to the compiler - data files and includes that need not to | ||
| be compiled | ||
| deps: other js_capnp_library rules to depend on | ||
| src_prefix: src_prefix for capnp compiler to the source root | ||
| visibility: rule visibility | ||
| target_compatible_with: target compatibility | ||
| **kwargs: rest of the arguments to js_library rule | ||
| """ | ||
|
|
||
| js_files = [s + ".js" for s in srcs] | ||
| d_ts_files = [s + ".d.ts" for s in srcs] | ||
|
|
||
| _capnp_gen( | ||
| name = name + "_gen", | ||
| srcs = srcs, | ||
| deps = [s + "_gen" for s in deps], | ||
| data = data, | ||
| outs = js_files + d_ts_files, | ||
| src_prefix = src_prefix, | ||
| visibility = visibility, | ||
| target_compatible_with = target_compatible_with, | ||
| ) | ||
| js_library( | ||
| name = name, | ||
| srcs = js_files + d_ts_files, | ||
| deps = deps + ["//:node_modules/capnp-ts"], | ||
| visibility = visibility, | ||
| target_compatible_with = target_compatible_with, | ||
| **kwargs | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "@cloudflare/workerd-root", | ||
| "private": true, | ||
| "scripts": { | ||
| "lint": "eslint types/src" | ||
| }, | ||
| "dependencies": { | ||
| "capnp-ts": "^0.7.0", | ||
| "prettier": "^2.7.1", | ||
| "typescript": "~4.7.4" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/debug": "^4.1.7", | ||
| "@types/node": "^18.7.18", | ||
| "@types/prettier": "^2.7.1", | ||
| "@typescript-eslint/eslint-plugin": "^5.37.0", | ||
| "@typescript-eslint/parser": "^5.37.0", | ||
| "capnpc-ts": "^0.7.0", | ||
| "esbuild": "^0.15.7", | ||
| "eslint": "^8.22.0", | ||
| "eslint-config-prettier": "^8.5.0", | ||
| "eslint-plugin-import": "^2.26.0", | ||
| "eslint-plugin-prettier": "^4.2.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- a/src/serialization/pointers/struct.ts | ||
| +++ b/src/serialization/pointers/struct.ts | ||
| @@ -107,8 +107,6 @@ export class Struct extends Pointer { | ||
| static readonly setText = setText; | ||
| static readonly testWhich = testWhich; | ||
|
|
||
| - readonly _capnp!: _Struct; | ||
mrbbot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - | ||
| /** | ||
| * Create a new pointer to a struct. | ||
| * | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.