| 1 | # JavaScript |
| 2 | |
| 3 | Prism provides bindings to JavaScript out of the box. |
| 4 | |
| 5 | ## Node |
| 6 | |
| 7 | To use the package from node, install the `@ruby/prism` dependency: |
| 8 | |
| 9 | ```sh |
| 10 | npm install @ruby/prism |
| 11 | ``` |
| 12 | |
| 13 | Then import the package: |
| 14 | |
| 15 | ```js |
| 16 | import { loadPrism } from "@ruby/prism"; |
| 17 | ``` |
| 18 | |
| 19 | Then call the load function to get a parse function: |
| 20 | |
| 21 | ```js |
| 22 | const parse = await loadPrism(); |
| 23 | ``` |
| 24 | |
| 25 | ## Browser |
| 26 | |
| 27 | To use the package from the browser, you will need to do some additional work. The [javascript/example.html](../javascript/example.html) file shows an example of running Prism in the browser. You will need to instantiate the WebAssembly module yourself and then pass it to the `parsePrism` function. |
| 28 | |
| 29 | First, get a shim for WASI since not all browsers support it yet. |
| 30 | |
| 31 | ```js |
| 32 | import { WASI } from "https://unpkg.com/@bjorn3/browser_wasi_shim@latest/dist/index.js"; |
| 33 | ``` |
| 34 | |
| 35 | Next, import the `parsePrism` function from `@ruby/prism`, either through a CDN or by bundling it with your application. |
| 36 | |
| 37 | ```js |
| 38 | import { parsePrism } from "https://unpkg.com/@ruby/prism@latest/src/parsePrism.js"; |
| 39 | ``` |
| 40 | |
| 41 | Next, fetch and instantiate the WebAssembly module. You can access it through a CDN or by bundling it with your application. |
| 42 | |
| 43 | ```js |
| 44 | const wasm = await WebAssembly.compileStreaming(fetch("https://unpkg.com/@ruby/prism@latest/src/prism.wasm")); |
| 45 | ``` |
| 46 | |
| 47 | Next, instantiate the module and initialize WASI. |
| 48 | |
| 49 | ```js |
| 50 | const wasi = new WASI([], [], []); |
| 51 | const instance = await WebAssembly.instantiate(wasm, { wasi_snapshot_preview1: wasi.wasiImport }); |
| 52 | wasi.initialize(instance); |
| 53 | ``` |
| 54 | |
| 55 | Finally, you can create a function that will parse a string of Ruby code. |
| 56 | |
| 57 | ```js |
| 58 | function parse(source) { |
| 59 | return parsePrism(instance.exports, source); |
| 60 | } |
| 61 | ``` |
| 62 | |
| 63 | ## API |
| 64 | |
| 65 | Now that we have access to a `parse` function, we can use it to parse Ruby code: |
| 66 | |
| 67 | ```js |
| 68 | const parseResult = parse("1 + 2"); |
| 69 | ``` |
| 70 | |
| 71 | A ParseResult object is very similar to the Prism::ParseResult object from Ruby. It has the same properties: `value`, `comments`, `magicComments`, `errors`, and `warnings`. Here we can serialize the AST to JSON. |
| 72 | |
| 73 | ```js |
| 74 | console.log(JSON.stringify(parseResult.value, null, 2)); |
| 75 | ``` |
| 76 | |
| 77 | ## Visitors |
| 78 | |
| 79 | Prism allows you to traverse the AST of parsed Ruby code using visitors. |
| 80 | |
| 81 | Here's an example of a custom `FooCalls` visitor: |
| 82 | |
| 83 | ```js |
| 84 | import { loadPrism, Visitor } from "@ruby/prism" |
| 85 | |
| 86 | const parse = await loadPrism(); |
| 87 | const parseResult = parse("foo()"); |
| 88 | |
| 89 | class FooCalls extends Visitor { |
| 90 | visitCallNode(node) { |
| 91 | if (node.name === "foo") { |
| 92 | // Do something with the node |
| 93 | } |
| 94 | |
| 95 | // Call super so that the visitor continues walking the tree |
| 96 | super.visitCallNode(node); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | const fooVisitor = new FooCalls(); |
| 101 | |
| 102 | parseResult.value.accept(fooVisitor); |
| 103 | ``` |
| 104 | |
| 105 | ## Building |
| 106 | |
| 107 | To build the WASM package yourself, first obtain a copy of `wasi-sdk`. You can retrieve this here: <https://github.com/WebAssembly/wasi-sdk>. Next, run: |
| 108 | |
| 109 | ```sh |
| 110 | make wasm WASI_SDK_PATH=path/to/wasi-sdk |
| 111 | ``` |
| 112 | |
| 113 | This will generate `javascript/src/prism.wasm`. From there, you can run the tests to verify everything was generated correctly. |
| 114 | |
| 115 | ```sh |
| 116 | cd javascript |
| 117 | node test |
| 118 | ``` |