Skip to content

Commit

Permalink
chore: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 24, 2024
1 parent 95fe190 commit 0c733ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ COMMANDS := $(notdir $(wildcard cmd/*))
generate: ## Generate code.
$(GO) generate ./...

.PHONY: bench
bench: ## Run benchmarks.
mkdir -p .test/reports
$(GO_TEST) --junitfile .test/reports/bench-test.xml -- -bench=. -benchmem ./...

.PHONY: fmt
fmt: ## Run go fmt against code.
$(GO_RUN_TOOLS) mvdan.cc/gofumpt -w .
Expand Down
14 changes: 14 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func BenchmarkContext(b *testing.B) {
}
}

func BenchmarkContextExample(b *testing.B) {
b.ReportAllocs()
iso := v8.NewIsolate()
defer iso.Dispose()
for n := 0; n < b.N; n++ {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
ctx.RunScript("const add = (a, b) => a + b", "math.js")
ctx.RunScript("const result = add(3, 4)", "main.js")
_, _ = ctx.RunScript("result", "value.js")
}
}

func ExampleContext() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
Expand Down
15 changes: 15 additions & 0 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"

v8 "github.com/zeiss/v8go"
)

func main() {
ctx := v8.NewContext() // creates a new V8 context with a new Isolate aka VM
ctx.RunScript("const add = (a, b) => a + b", "math.js") // executes a script on the global context
ctx.RunScript("const result = add(3, 4)", "main.js") // any functions previously added to the context can be called
val, _ := ctx.RunScript("result", "value.js") // return a value in JavaScript back to Go
fmt.Printf("addition result: %s", val)
}

0 comments on commit 0c733ec

Please sign in to comment.