Skip to content

Commit

Permalink
use wasmbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna committed May 5, 2024
1 parent c531029 commit 7bb82c2
Show file tree
Hide file tree
Showing 11 changed files with 526 additions and 321 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ jobs:
if: contains(matrix.os, 'ubuntu')
run: diff <(./target/release/examples/dlint rules --json) www/static/docs.json

- name: Check if wasm is up-to-date
if : contains(matrix.os, 'ubuntu')
run: deno task wasmbuild --check

- name: Benchmarks
if: contains(matrix.os, 'ubuntu')
run: deno run -A --quiet benchmarks/benchmarks.ts
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ once_cell = "1.19.0"
derive_more = { version = "0.99.17", features = ["display"] }
anyhow = "1.0.79"
if_chain = "1.0.2"
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen = { version = "0.2.92", optional = true }
wee_alloc = { version = "0.4", optional = true }
miette = { version = "4.3.0", features = ["fancy-no-backtrace"], optional = true }

Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"lock": false,
"tasks": {
"wasmbuild": "deno run -A jsr:@deno/[email protected] --features wasm --out www/static",
"update-docs": "cargo run --features=docs --example dlint rules --json > www/static/docs.json"
},
"exclude": [
Expand Down
30 changes: 2 additions & 28 deletions www/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,5 @@ can write random source code and see the diagnostics right away.
#### How to update wasm files used in playground

This feature is realized using wasm and its glue code put in the `static`
directory. To update these files, follow the steps below. (Note: These steps
assume that you are in the root directory of this repository)

1. Make sure you have `wasm-bindgen-cli` v0.2.92 installed:

```shell
cargo install -f wasm-bindgen-cli --version 0.2.92
```

2. Build deno_lint for the wasm target.

```shell
cargo build --target wasm32-unknown-unknown --features wasm --release
```

3. Run `wasm-bindgen-cli` to generate the glue code and to put everything needed
into the `static` directory:

```shell
wasm-bindgen --out-dir=www/static --target=web ./target/wasm32-unknown-unknown/release/deno_lint.wasm
```

4. (Optional, but preferable) Reduce the size of the wasm binary using
[the binaryen toolkit](https://github.com/WebAssembly/binaryen):

```shell
wasm-opt -Os -o www/static/deno_lint_bg.wasm www/static/deno_lint_bg.wasm
```
directory. To update these files, run `deno task wasmbuild` at the repository
root.
10 changes: 6 additions & 4 deletions www/islands/Linter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @deno-types="../static/deno_lint.d.ts"
import init, { run } from "../static/deno_lint.js";
import { instantiate, run } from "../static/deno_lint.generated.js";
import { useEffect, useMemo } from "preact/hooks";
import { type Signal, useComputed, useSignal } from "@preact/signals";
import Convert from "https://esm.sh/v130/[email protected]";
Expand All @@ -22,7 +21,10 @@ export default function Linter(props: Props) {
const initialized = useSignal(false);

useEffect(() => {
init(fetch("/deno_lint_bg.wasm")).then(() => {
const origin = new URL(location.href).origin;
instantiate({
url: new URL("deno_lint_bg.wasm", origin),
}).then(() => {
initialized.value = true;
});
}, []);
Expand Down Expand Up @@ -55,7 +57,7 @@ export default function Linter(props: Props) {
<div class="h-full">
<div class="border border-gray-300 dark:border-gray-700 dark:bg-[#1e1e1e] p-4 overflow-x-auto h-full">
{display.value.kind === "Loading"
? <p>Loading...</p>
? <p>wasm is being loaded...</p>
: display.value.kind === "LintError"
? (
<>
Expand Down
40 changes: 0 additions & 40 deletions www/static/deno_lint.d.ts

This file was deleted.

37 changes: 37 additions & 0 deletions www/static/deno_lint.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// deno-lint-ignore-file
// deno-fmt-ignore-file

export interface InstantiateResult {
instance: WebAssembly.Instance;
exports: {
run: typeof run
};
}

/** Gets if the Wasm module has been instantiated. */
export function isInstantiated(): boolean;

/** Options for instantiating a Wasm instance. */
export interface InstantiateOptions {
/** Optional url to the Wasm file to instantiate. */
url?: URL;
/** Callback to decompress the raw Wasm file bytes before instantiating. */
decompress?: (bytes: Uint8Array) => Uint8Array;
}

/** Instantiates an instance of the Wasm module returning its functions.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object. */
export function instantiate(opts?: InstantiateOptions): Promise<InstantiateResult["exports"]>;

/** Instantiates an instance of the Wasm module along with its exports.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object. */
export function instantiateWithInstance(opts?: InstantiateOptions): Promise<InstantiateResult>;

/**
* @param {string} filename
* @param {string} source_code
* @returns {string}
*/
export function run(filename: string, source_code: string): string;
Loading

0 comments on commit 7bb82c2

Please sign in to comment.