Skip to content

Commit

Permalink
Merge pull request #11 from leagris/dev-build-patch
Browse files Browse the repository at this point in the history
Update compile and instructions
  • Loading branch information
robertaboukhalil authored Dec 1, 2023
2 parents a3985aa + 62747d4 commit 70b67e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ A [jq](https://github.com/stedolan/jq/) playground, powered by WebAssembly.
* [jq Tutorial](https://sandbox.bio/tutorials?id=jq-intro)
* [Use jq in your own web apps](https://github.com/biowasm/biowasm/tree/main/tools/jq#jqwasm)


## How?

jqkungfu was built by compiling [jq](https://github.com/stedolan/jq/) to WebAssembly, so that it runs in the browser.

The advantages of this approach are:

- **Speed**: After the initial load time, jq queries are very fast because there are no round trips to a server
- **Security**: This approach runs jq within the browser; otherwise, we would need to carefully secure the app so that users can't run arbitrary commands on the server!
- **Convenience**: The app is purely front-end and is hosted as static files on a cloud storage provider
* **Speed**: After the initial load time, jq queries are very fast because there are no round trips to a server
* **Security**: This approach runs jq within the browser; otherwise, we would need to carefully secure the app so that users can't run arbitrary commands on the server!
* **Convenience**: The app is purely front-end and is hosted as static files on a cloud storage provider

## Launch locally

Expand All @@ -30,22 +29,21 @@ python3 -m http.server 9999

Then open [http://localhost:9999](http://localhost:9999) in your browser.


## Compile to WebAssembly (optional)

To compile jq to WebAssembly, run the `compile.sh` code within an environment that includes [Emscripten](https://github.com/emscripten-core/emscripten).

To set up your environment:

```bash
```sh
# Make sure to use "--recursive" so the jq submodule is initialized
$ git clone --recursive https://github.com/robertaboukhalil/jqkungfu.git

# Build the Docker image with needed dependencies
$ docker build -t jqkungfu .
$ docker buildx build -t jqkungfu .

# Compile to WebAssembly
$ docker run --rm -it -v $(pwd):/src --entrypoint ./compile.sh jqkungfu
$ docker run --rm -it -v .:/src --entrypoint ./compile.sh jqkungfu
```

## Learn More
Expand Down
18 changes: 11 additions & 7 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#!/bin/bash
#!/usr/bin/env sh

# Echo function that behaves consistently across shells
echo() { printf '%s\n' "$*";}

# Compile jq to WebAssembly

# Need Emscripten installed
[[ ! $(command -v emcc --version) ]] && \
echo "You need Emscripten to compile jq to WebAssembly. See the setup instructions in the README file." && \
exit
if ! command -v emcc > /dev/null 2>&1; then
echo 'You need Emscripten to compile jq to WebAssembly. See the setup instructions in the README file.'
exit 2
fi >&2

# Update jq repo and its submodules
cd jq
cd jq || exit 2
git submodule update --init --recursive

# Edit main.c so that reset option flags to 0 every time we call the main() function
# This is needed because we're not exiting after each main() call; otherwise, the
# "Sort Keys" feature wouldn't work
git apply ../jq.patch
git apply ../jq.patch 2> /dev/null

# Generate ./configure file
autoreconf -fi
Expand All @@ -30,4 +34,4 @@ emmake make \
CFLAGS="-O2 -s EXPORTED_RUNTIME_METHODS=['callMain']"

mkdir -p ../build/
mv jq.{js,wasm} ../build/
mv jq.js jq.wasm ../build/

0 comments on commit 70b67e6

Please sign in to comment.