-
Notifications
You must be signed in to change notification settings - Fork 0
Stevenmasley/wasm #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
GO_SRC_FILES := $(shell find . $(FIND_EXCLUSIONS) -type f -name '*.go' -not -name '*_test.go') | ||
|
||
.PHONY: gen | ||
gen: | ||
@echo "Generating code..." | ||
go generate ./... | ||
|
||
.PHONY: clean-testdata | ||
clean-testdata: | ||
git clean -xfd testdata | ||
git clean -xfd testdata | ||
|
||
.PHONY: build-wasm | ||
build-wasm: site/public/build/preview.wasm | ||
mkdir -p ./site/public/build/ | ||
|
||
site/public/build/preview.wasm: $(GO_SRC_FILES) | ||
GOOS=js GOARCH=wasm go build -o site/public/build/preview.wasm ./cmd/wasm |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||
//go:build js && wasm | ||||||
|
||||||
package main | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"io/fs" | ||||||
"path/filepath" | ||||||
"syscall/js" | ||||||
|
||||||
"github.com/spf13/afero" | ||||||
|
||||||
"github.com/coder/preview" | ||||||
"github.com/coder/preview/types" | ||||||
) | ||||||
|
||||||
func main() { | ||||||
// Create a channel to keep the Go program alive | ||||||
done := make(chan struct{}, 0) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
it's fine for unbuffered to be implied imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey this is a draft for a reason! To be 100% transparent, this was almost all written by cursor lol |
||||||
|
||||||
// Expose the Go function `fibonacciSum` to JavaScript | ||||||
js.Global().Set("go_preview", js.FuncOf(tfpreview)) | ||||||
js.Global() | ||||||
|
||||||
// Block the program from exiting | ||||||
<-done | ||||||
} | ||||||
|
||||||
func tfpreview(this js.Value, p []js.Value) any { | ||||||
tf, err := fileTreeFS(p[0]) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
output, diags := preview.Preview(context.Background(), preview.Input{ | ||||||
PlanJSONPath: "", | ||||||
PlanJSON: nil, | ||||||
ParameterValues: nil, | ||||||
Owner: types.WorkspaceOwner{}, | ||||||
}, tf) | ||||||
|
||||||
data, _ := json.Marshal(map[string]any{ | ||||||
"output": output, | ||||||
"diags": diags, | ||||||
}) | ||||||
return js.ValueOf(string(data)) | ||||||
} | ||||||
|
||||||
func fileTreeFS(value js.Value) (fs.FS, error) { | ||||||
data := js.Global().Get("JSON").Call("stringify", value).String() | ||||||
var filetree map[string]any | ||||||
if err := json.Unmarshal([]byte(data), &filetree); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
mem := afero.NewMemMapFs() | ||||||
loadTree(mem, filetree) | ||||||
|
||||||
return afero.NewIOFS(mem), nil | ||||||
} | ||||||
|
||||||
func loadTree(mem afero.Fs, fileTree map[string]any, path ...string) { | ||||||
dir := filepath.Join(path...) | ||||||
err := mem.MkdirAll(dir, 0755) | ||||||
if err != nil { | ||||||
fmt.Printf("error creating directory %q: %v\n", dir, err) | ||||||
} | ||||||
for k, v := range fileTree { | ||||||
switch vv := v.(type) { | ||||||
case string: | ||||||
fn := filepath.Join(dir, k) | ||||||
f, err := mem.Create(fn) | ||||||
if err != nil { | ||||||
fmt.Printf("error creating file %q: %v\n", fn, err) | ||||||
continue | ||||||
} | ||||||
_, _ = f.WriteString(vv) | ||||||
f.Close() | ||||||
case map[string]any: | ||||||
loadTree(mem, vv, append(path, k)...) | ||||||
default: | ||||||
fmt.Printf("unknown type %T for %q\n", v, k) | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,3 +22,6 @@ dist-ssr | |||||||||
*.njsproj | ||||||||||
*.sln | ||||||||||
*.sw? | ||||||||||
|
||||||||||
# wasm | ||||||||||
public/build/preview.wasm | ||||||||||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
<link href="/src/index.css" rel="stylesheet"> | ||
<title>Conditional Parameters</title> | ||
</head> | ||
<body> | ||
<body> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👻 |
||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { usePreview } from '../contexts/PreviewContext/PreviewContext'; | ||
import { useState } from 'react'; | ||
|
||
export function Live() { | ||
const { isWasmLoaded, preview } = usePreview(); | ||
const [previewResult, setPreviewResult] = useState<string | null>(null); | ||
|
||
const handlePreviewClick = async () => { | ||
try { | ||
const result = await preview(); | ||
setPreviewResult(result); | ||
} catch (error) { | ||
console.error('Error calling preview:', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
{isWasmLoaded && <p>Wasm Loaded</p>} | ||
{!isWasmLoaded && <p>Wasm not Loaded</p>} | ||
|
||
<button onClick={handlePreviewClick}>Run Preview</button> | ||
{previewResult !== null && ( | ||
<div> | ||
<p>Preview Result: {previewResult}</p> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { createContext, FC, PropsWithChildren, useContext, useEffect, useState } from "react"; | ||
import "./wasm_exec.js"; | ||
|
||
export interface PreviewContextValue { | ||
isWasmLoaded: boolean; | ||
preview?: GoPreviewDef; | ||
} | ||
|
||
export const PreviewContext = createContext<PreviewContextValue | undefined>( | ||
undefined, | ||
); | ||
|
||
export const PreviewProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [isWasmLoaded, setIsWasmLoaded] = useState(false); | ||
|
||
// useEffect hook to load WebAssembly when the component mounts | ||
useEffect(() => { | ||
// Function to asynchronously load WebAssembly | ||
async function loadWasm(): Promise<void> { | ||
// Create a new Go object | ||
const goWasm = new window.Go(); | ||
const result = await WebAssembly.instantiateStreaming( | ||
// Fetch and instantiate the main.wasm file | ||
fetch('build/preview.wasm'), | ||
// Provide the import object to Go for communication with JavaScript | ||
goWasm.importObject | ||
); | ||
// Run the Go program with the WebAssembly instance | ||
goWasm.run(result.instance); | ||
setIsWasmLoaded(true); | ||
} | ||
|
||
loadWasm(); | ||
}, []); | ||
|
||
|
||
|
||
return ( | ||
<PreviewContext.Provider | ||
value={{ | ||
isWasmLoaded, | ||
preview: window.go_preview, | ||
}} | ||
> | ||
{children} | ||
</PreviewContext.Provider> | ||
); | ||
} | ||
|
||
export const usePreview = (): PreviewContextValue => { | ||
const context = useContext(PreviewContext); | ||
|
||
if (!context) { | ||
throw new Error("usePreview should be used inside of <PreviewProvider />"); | ||
} | ||
|
||
return context; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a trailing newline to appease github (and me) pls