Skip to content

Commit

Permalink
update codegen templates, build nodejs
Browse files Browse the repository at this point in the history
now uses codegen bin to generate rust and js files
wasm bindgen now imports node classes instead of relying on globals
breaks web / no-modules building, but that's because we're optimizing
for nodejs (commonjs) importing
room for improvement might be having wasm-bindgen export the rust node
structs themselves isntead of templating the JS for those (generated
types would be nice as well)
  • Loading branch information
jbielick committed Dec 19, 2022
1 parent 7544efb commit 3acd335
Show file tree
Hide file tree
Showing 29 changed files with 6,170 additions and 817 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ node_modules
yarn.lock
Cargo.lock
*.wasm
codegen/codegen

# Rust codegen
js/nodes.js
js/messages.js
js/index.d.ts
bindings/src/nodes.rs
bindings/src/messages.rs

Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
include codegen/build.mk
include build/build.mk
include tests/build.mk
include gh-pages/build.mk
include benchmark/build.mk

build/nodejs: $(CODEGEN_FILES)
wasm-pack build --release --target nodejs --no-typescript --out-dir ../build/nodejs bindings
cp js/* build/nodejs

CLEAN += build/nodejs

clean:
rm -rf $(CLEAN)
touch codegen/build.rs
2 changes: 1 addition & 1 deletion bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]

[dependencies]
js-sys = "0.3.55"
lib-ruby-parser = {version = "3.0.12"}
lib-ruby-parser = {version = "4.0"}
wasm-bindgen = "0.2.63"

# The `console_error_panic_hook` crate provides better debugging of panics by
Expand Down
33 changes: 12 additions & 21 deletions bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = Loc)]
pub type JsLoc;
Expand Down Expand Up @@ -111,29 +111,20 @@ impl IntoJs for u8 {
}
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = Bytes)]
pub type JsBytes;

#[wasm_bindgen(constructor, js_class = Bytes)]
fn new(raw: Vec<u8>) -> JsBytes;
}

impl IntoJs for RustBytes {
type Output = JsBytes;
fn into_js(self) -> JsBytes {
JsBytes::new(self.raw)
type Output = Vec<u8>;
fn into_js(self) -> Vec<u8> {
self.raw
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = Token)]
pub type JsToken;

#[wasm_bindgen(constructor, js_class = Token)]
fn new(token_type: i32, token_value: JsBytes, loc: JsLoc) -> JsToken;
fn new(token_type: i32, token_value: Vec<u8>, loc: JsLoc) -> JsToken;
}

impl IntoJs for RustToken {
Expand All @@ -147,7 +138,7 @@ impl IntoJs for RustToken {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = Diagnostic)]
pub type JsDiagnostic;
Expand Down Expand Up @@ -179,7 +170,7 @@ impl IntoJs for RustErrorLevel {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = Comment)]
pub type JsComment;
Expand Down Expand Up @@ -207,7 +198,7 @@ impl IntoJs for RustCommentType {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = MagicComment)]
pub type JsMagicComment;
Expand Down Expand Up @@ -240,7 +231,7 @@ impl IntoJs for RustMagicCommentKind {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = DecodedInput)]
pub type JsDecodedInput;
Expand All @@ -263,7 +254,7 @@ impl IntoJs for RustDecodedInput {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = SourceLine)]
pub type JsSourceLine;
Expand All @@ -279,7 +270,7 @@ impl IntoJs for RustSourceLine {
}
}

#[wasm_bindgen]
#[wasm_bindgen(raw_module = "./types")]
extern "C" {
#[wasm_bindgen(js_name = ParserResult)]
pub type JsParserResult;
Expand Down
46 changes: 0 additions & 46 deletions build/build.mk

This file was deleted.

63 changes: 0 additions & 63 deletions build/scripts/merge.js

This file was deleted.

9 changes: 0 additions & 9 deletions codegen/Cargo.toml

This file was deleted.

38 changes: 26 additions & 12 deletions codegen/build.mk
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
CODEGEN_DEPS = $(wildcard codegen/codegen/*.rs)
CODEGEN_DEPS += codegen/Cargo.toml
CODEGEN_DEPS += codegen/build.rs
DO_CODEGEN = cd codegen && cargo build
define download_file
curl "$(1)" --fail-with-body -LSso "$(2).tmp" && mv "$(2).tmp" "$(2)"
endef

js/nodes.js: $(CODEGEN_DEPS)
$(DO_CODEGEN)
CODEGEN_EXE = codegen/codegen$(EXE)
$(CODEGEN_EXE):
$(call download_file,https://github.com/lib-ruby-parser/lib-ruby-parser/releases/download/codegen-v1.0.1/codegen-x86_64-apple-darwin,$@)
chmod +x $(CODEGEN_EXE)
CLEAN += $(CODEGEN_EXE)

js/index.d.ts: codegen/types.d.ts.liquid $(CODEGEN_EXE)
$(CODEGEN_EXE) --template $< --write-to $@
CLEAN += js/index.d.ts
CODEGEN_FILES += js/index.d.ts

js/nodes.js: codegen/nodes.js.liquid $(CODEGEN_EXE)
$(CODEGEN_EXE) --template $< --write-to $@
CLEAN += js/nodes.js
CODEGEN_FILES += js/nodes.js

js/messages.js: $(CODEGEN_DEPS)
$(DO_CODEGEN)
js/messages.js: codegen/messages.js.liquid $(CODEGEN_EXE)
$(CODEGEN_EXE) --template $< --write-to $@
CLEAN += js/messages.js
CODEGEN_FILES += js/messages.js

bindings/src/nodes.rs: $(CODEGEN_DEPS)
$(DO_CODEGEN)
bindings/src/nodes.rs: codegen/nodes.rs.liquid $(CODEGEN_EXE)
$(CODEGEN_EXE) --template $< --write-to $@
CLEAN += bindings/src/nodes.rs
CODEGEN_FILES += bindings/src/nodes.rs

bindings/src/messages.rs: $(CODEGEN_DEPS)
$(DO_CODEGEN)
bindings/src/messages.rs: codegen/messages.rs.liquid $(CODEGEN_EXE)
$(CODEGEN_EXE) --template $< --write-to $@
CLEAN += bindings/src/messages.rs
CODEGEN_FILES += bindings/src/messages.rs
7 changes: 0 additions & 7 deletions codegen/build.rs

This file was deleted.

Loading

0 comments on commit 3acd335

Please sign in to comment.