Skip to content

Instantly share code, notes, and snippets.

@rudolph9
Last active February 4, 2023 00:32
Show Gist options
  • Select an option

  • Save rudolph9/dd5e3dbcbf762abdb3f662e39dacfa7f to your computer and use it in GitHub Desktop.

Select an option

Save rudolph9/dd5e3dbcbf762abdb3f662e39dacfa7f to your computer and use it in GitHub Desktop.

Revisions

  1. rudolph9 revised this gist Sep 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Followed tutorial here: https://github.com/golang/go/wiki/WebAssembly
    ## Setup

    1. install compatible version of golang. Test with go1.12.8
    2. get cue: `go get -u go get cuelang.org/go/cue` NOTE: this installs the packages files, not the cmd
    2. get cue: `go get -u cuelang.org/go/cue` NOTE: this installs the packages files, not the cmd
    3. get the wasm exec_file `cp "$(go env GOROOT)/misc/wasm/wasm_exec.js`

    ## Build
  2. rudolph9 revised this gist Sep 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,11 @@ Followed tutorial here: https://github.com/golang/go/wiki/WebAssembly

    ## Build

    3. Build the wasm: `GOARCH=wasm GOOS=js go build -o lib.wasm main.go`
    4. Build the wasm: `GOARCH=wasm GOOS=js go build -o lib.wasm main.go`

    ## Serve files

    4. install `goexec` if needed (or serve the files some other way): `go get -u github.com/shurcooL/goexec`
    5. install `goexec` if needed (or serve the files some other way): `go get -u github.com/shurcooL/goexec`

    5. Serve files: `goexec 'http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))'`

  3. rudolph9 revised this gist Sep 3, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ Followed tutorial here: https://github.com/golang/go/wiki/WebAssembly

    1. install compatible version of golang. Test with go1.12.8
    2. get cue: `go get -u go get cuelang.org/go/cue` NOTE: this installs the packages files, not the cmd
    3. get the wasm exec_file `cp "$(go env GOROOT)/misc/wasm/wasm_exec.js`

    ## Build

  4. rudolph9 created this gist Sep 2, 2019.
    19 changes: 19 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # Cue Web Assembly

    Followed tutorial here: https://github.com/golang/go/wiki/WebAssembly

    ## Setup

    1. install compatible version of golang. Test with go1.12.8
    2. get cue: `go get -u go get cuelang.org/go/cue` NOTE: this installs the packages files, not the cmd

    ## Build

    3. Build the wasm: `GOARCH=wasm GOOS=js go build -o lib.wasm main.go`

    ## Serve files

    4. install `goexec` if needed (or serve the files some other way): `go get -u github.com/shurcooL/goexec`

    5. Serve files: `goexec 'http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))'`

    45 changes: 45 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <!DOCTYPE html>
    <!--
    Copyright 2018 The Go Authors. All rights reserved.
    Use of this source code is governed by a BSD-style
    license that can be found in the LICENSE file.
    -->
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Go wasm</title>
    </head>

    <body>
    <script src="wasm_exec.js"></script>

    <script>
    if (!WebAssembly.instantiateStreaming) {
    // polyfill
    WebAssembly.instantiateStreaming = async (resp, importObject) => {
    const source = await (await resp).arrayBuffer();
    return await WebAssembly.instantiate(source, importObject);
    };
    }

    const go = new Go();

    let mod, inst;

    WebAssembly.instantiateStreaming(fetch("lib.wasm"), go.importObject).then(
    result => {
    mod = result.module;
    inst = result.instance;
    document.getElementById("runButton").disabled = false;
    }
    );

    async function run() {
    await go.run(inst);
    inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
    }
    </script>

    <button onClick="run();" id="runButton" disabled>Run</button>
    </body>
    </html>
    29 changes: 29 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package main

    import (
    "fmt"
    "math/big"

    "cuelang.org/go/cue"
    )

    func main() {
    const config = `
    TimeSeries: {
    "2019-09-01T07:00:00Z": 36
    }
    TimeSeries: {
    "2019-09-01T07:10:59Z": 200
    }
    `
    var r cue.Runtime

    instance, err := r.Compile("test", config)
    if err != nil {
    fmt.Println(err)
    }

    var bigInt big.Int
    instance.Lookup("TimeSeries").Lookup("2019-09-01T07:10:59Z").Int(&bigInt)
    fmt.Println(bigInt.String())
    }
    1 change: 1 addition & 0 deletions wasm_exec.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    // get this file by running `cp "$(go env GOROOT)/misc/wasm/wasm_exec.js"`