Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Create WASM bindings and wire up into the vanilla JS Mapbox demo #106

Merged
merged 1 commit into from
Mar 21, 2022

Conversation

dabreegster
Copy link
Contributor

I believe this is the minimal work needed to call the Rust library from JS. It appears to work:
Screenshot from 2022-03-21 10-27-08

Still TODO: passing more options in, error handling, and an NPM package

@dabreegster
Copy link
Contributor Author

@@ -21,3 +21,8 @@ pub mod transform;
pub use transform::{lanes_to_tags, tags_to_lanes, LanesToTagsConfig, TagsToLanesConfig};

mod test;

#[cfg(target_arch = "wasm32")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think another crate is overkill; it's not too much work to feature-gate a few dependencies and this modul

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, I still think the separation helps in readability and separation of concerns?

I will do that work as part of #60, if I still think it is needed.
It also has the added benefit of being a home for node js based integration tests

<script type="module">
import init, { js_tags_to_lanes } from "./pkg/osm2lanes.js";

await init();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is correct; I think everything here needs to be in an async function. But it kind of works out as long as the user doesn't click a road too quickly before this happens to finish

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think top level await is a thing (though I personally haven't used module scripts directly in the browser before). So this should work, but at the cost of queuing the rest of the setup work behind init().

You could kick off init() here, store the promise, and await it just before you use the lib (on line 120).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, then we need extra state to avoid awaiting the promise multiple times. Cool that top-level await works directly though! I'll keep this in mind if we hit issues later

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, awaiting the promise multiple times is also safe, at the cost of yielding to the event loop (like setTimeout( _, 0)).

Copy link
Collaborator

@droogmic droogmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK, just need to figure out how to integrate this into the deployed website.

@@ -21,3 +21,8 @@ pub mod transform;
pub use transform::{lanes_to_tags, tags_to_lanes, LanesToTagsConfig, TagsToLanesConfig};

mod test;

#[cfg(target_arch = "wasm32")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, I still think the separation helps in readability and separation of concerns?

I will do that work as part of #60, if I still think it is needed.
It also has the added benefit of being a home for node js based integration tests

@dabreegster
Copy link
Contributor Author

Rebasing...

maybe, I still think the separation helps in readability and separation of concerns?

Sounds good to me. I'm still not sure what the node config looks like, or how to wire up end-to-end JS integration tests. This is just a small step in that direction

@BudgieInWA
Copy link
Collaborator

This breaks trunk serve in the rust dir for me:

~/c/osm2lanes  ➦ 098d9b7  rust  git co main
Previous HEAD position was 098d9b7 Rust benchmark (#105)
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
 ~/c/osm2lanes   main  rust  trunk serve
Mar 24 21:52:05.691  INFO 📦 starting build
Mar 24 21:52:05.692  INFO spawning asset pipelines
Mar 24 21:52:05.846  INFO copying & hashing css path="osm2lanes-web/assets/css/main.css"
Mar 24 21:52:05.846  INFO building osm2lanes-web
Mar 24 21:52:05.847  INFO finished copying & hashing css path="osm2lanes-web/assets/css/main.css"
   Compiling osm2lanes v0.1.0 (/home/ben/code/osm2lanes/rust/osm2lanes)
error[E0433]: failed to resolve: use of undeclared crate or module `wasm_bindgen`
 --> osm2lanes/src/wasm.rs:4:5
  |
4 | use wasm_bindgen::prelude::*;
  |     ^^^^^^^^^^^^ use of undeclared crate or module `wasm_bindgen`

error[E0432]: unresolved imports `crate::tags_to_lanes`, `crate::DrivingSide`, `crate::Locale`, `crate::TagsToLanesConfig`
 --> osm2lanes/src/wasm.rs:7:13
  |
7 | use crate::{tags_to_lanes, DrivingSide, Locale, TagsToLanesConfig};
  |             ^^^^^^^^^^^^^  ^^^^^^^^^^^  ^^^^^^  ^^^^^^^^^^^^^^^^^

error: cannot find attribute `wasm_bindgen` in this scope
  --> osm2lanes/src/wasm.rs:15:3
   |
15 | #[wasm_bindgen]
   |   ^^^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared crate or module `console_error_panic_hook`
  --> osm2lanes/src/wasm.rs:17:5
   |
17 |     console_error_panic_hook::set_once();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `console_error_panic_hook`

error[E0433]: failed to resolve: use of undeclared type `JsValue`
  --> osm2lanes/src/wasm.rs:38:5
   |
38 |     JsValue::from_serde(&lanes).unwrap()
   |     ^^^^^^^ use of undeclared type `JsValue`

error[E0412]: cannot find type `JsValue` in this scope
  --> osm2lanes/src/wasm.rs:16:31
   |
16 | pub fn js_tags_to_lanes(val: &JsValue) -> JsValue {
   |                               ^^^^^^^ not found in this scope

error[E0412]: cannot find type `JsValue` in this scope
  --> osm2lanes/src/wasm.rs:16:43
   |
16 | pub fn js_tags_to_lanes(val: &JsValue) -> JsValue {
   |                                           ^^^^^^^ not found in this scope

Some errors have detailed explanations: E0412, E0432, E0433.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `osm2lanes` due to 7 previous errors
Mar 24 21:52:06.590 ERROR ❌ error
error from HTML pipeline

Caused by:
    0: error from asset pipeline
    1: error during cargo build execution
    2: cargo call returned a bad status
Mar 24 21:52:06.591  INFO 📡 serving static assets at -> /
Mar 24 21:52:06.591  INFO 📡 server listening at 0.0.0.0:8080

@dabreegster
Copy link
Contributor Author

Argh, I failed to test everything. Feel free to revert temporarily to unblock working on that, if you need to. Work and travel stuff mean I probably can't touch anything in osm2lanes until after April 6 or so. :\

@droogmic
Copy link
Collaborator

Fixed in #110

I still don't know why it failed, probably trunk fiddling with wasm-bindgen and ony expecting it to exist in the root of the tree.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants