diff --git a/Makefile b/Makefile index 6a5fc09..a76d4e0 100644 --- a/Makefile +++ b/Makefile @@ -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 . diff --git a/context_test.go b/context_test.go index e08ef03..23cbd57 100644 --- a/context_test.go +++ b/context_test.go @@ -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() diff --git a/examples/simple/main.go b/examples/simple/main.go new file mode 100644 index 0000000..80485ad --- /dev/null +++ b/examples/simple/main.go @@ -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) +}