Skip to content
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

Added js module: new JavaScript API supporting V8 #91

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/couchbase/sg-bucket
go 1.19

require (
github.com/pkg/errors v0.9.1
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
github.com/snej/v8go v1.7.3
github.com/stretchr/testify v1.7.1
golang.org/x/text v0.3.7
gopkg.in/couchbase/gocb.v1 v1.6.7
Expand All @@ -14,7 +16,6 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand All @@ -24,4 +25,5 @@ require (
gopkg.in/couchbaselabs/jsonx.v1 v1.0.1 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
rogchap.com/v8go v0.8.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f h1:a7clxaGmmqtdNTXyvrp/lVO/Gnkzlhc/+dLs5v965GM=
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f/go.mod h1:/mK7FZ3mFYEn9zvNPhpngTyatyehSwte5bJZ4ehL5Xw=
github.com/snej/v8go v1.7.3 h1:skliM+LRvRdLKwqJK4I+1BCQy4jTZs0u37R6b7aEUaU=
github.com/snej/v8go v1.7.3/go.mod h1:s7IVrqyNoVfwYhndECq3XJ+/y0uq/JUH0/ECsC3k/UQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -42,3 +44,5 @@ gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rogchap.com/v8go v0.8.0 h1:/crDEiga68kOtbIqw3K9Rt9OztYz0LhAPHm2e3wK7Q4=
rogchap.com/v8go v0.8.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs=
299 changes: 299 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
//go:build cb_sg_v8

/*
Copyright 2022-Present Couchbase, Inc.

Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/

package js

import (
"context"
"encoding/json"
"math"
"math/big"
"strconv"
"testing"
"time"

"github.com/snej/v8go"
"github.com/stretchr/testify/assert"
)

func TestSquare(t *testing.T) {
ctx := context.Background()
TestWithVMs(t, func(t *testing.T, vm VM) {
service := NewService(vm, "square", `function(n) {return n * n;}`)
assert.Equal(t, "square", service.Name())
assert.Equal(t, vm, service.Host())

// Test Run:
result, err := service.Run(ctx, 13)
assert.NoError(t, err)
assert.EqualValues(t, 169, result)

// Test WithRunner:
result, err = service.WithRunner(func(runner Runner) (any, error) {
assert.Nil(t, runner.Context())
assert.NotNil(t, runner.ContextOrDefault())
runner.SetContext(ctx)
assert.Equal(t, ctx, runner.Context())
assert.Equal(t, ctx, runner.ContextOrDefault())

return runner.Run(9)
})
assert.NoError(t, err)
assert.EqualValues(t, 81, result)
})
}

func TestSquareV8Args(t *testing.T) {
vm := V8.NewVM()
defer vm.Close()

service := NewService(vm, "square", `function(n) {return n * n;}`)
result, err := service.WithRunner(func(runner Runner) (any, error) {
v8Runner := runner.(*V8Runner)
result, err := v8Runner.RunWithV8Args(v8Runner.NewInt(9))
if err != nil {
return nil, err
}
return result.Integer(), nil
})
assert.NoError(t, err)
assert.EqualValues(t, 81, result)
}

func TestJSON(t *testing.T) {
ctx := context.Background()

var pool VMPool
pool.Init(V8, 4)
defer pool.Close()

service := NewService(&pool, "length", `function(v) {return v.length;}`)

result, err := service.Run(ctx, []string{"a", "b", "c"})
if assert.NoError(t, err) {
assert.EqualValues(t, 3, result)
}

result, err = service.Run(ctx, JSONString(`[1,2,3,4]`))
if assert.NoError(t, err) {
assert.EqualValues(t, 4, result)
}
}

func TestCallback(t *testing.T) {
ctx := context.Background()

vm := V8.NewVM()
defer vm.Close()

src := `(function() {
return hey(1234, "hey you guys!");
});`

var heyParam string

// A callback function that's callable from JS as hey(num, str)
hey := func(r *V8Runner, this *v8go.Object, args []*v8go.Value) (result any, err error) {
assert.Equal(t, len(args), 2)
assert.Equal(t, int64(1234), args[0].Integer())
heyParam = args[1].String()
return 5678, nil
}

service := NewCustomService(vm, "callbacks", func(tmpl *V8BasicTemplate) (V8Template, error) {
err := tmpl.SetScript(src)
tmpl.GlobalCallback("hey", hey)
return tmpl, err
})

result, err := service.Run(ctx)
assert.NoError(t, err)
assert.Equal(t, 5678, result)
assert.Equal(t, "hey you guys!", heyParam)
}

// Test conversion of numbers into/out of JavaScript.
func TestNumbers(t *testing.T) {
ctx := context.Background()

TestWithVMs(t, func(t *testing.T, vm VM) {
service := NewService(vm, "numbers", `function(n, expectedStr) {
if (typeof(n) != 'number' && typeof(n) != 'bigint') throw "Unexpectedly n is a " + typeof(n);
var str = n.toString();
console.info("n=",n,"str=",str);
if (str != expectedStr) throw "Got " + str + " instead of " + expectedStr;
return n;
}`)

t.Run("integers", func(t *testing.T) {
testInt := func(n int64) {
result, err := service.Run(ctx, n, strconv.FormatInt(n, 10))
if assert.NoError(t, err) {
assert.EqualValues(t, n, result)
}
}

testInt(-1)
testInt(0)
testInt(1)
testInt(math.MaxInt32)
testInt(math.MinInt32)
testInt(math.MaxInt64)
testInt(math.MinInt64)
testInt(math.MaxInt64 - 1)
testInt(math.MinInt64 + 1)
testInt(JavascriptMaxSafeInt)
testInt(JavascriptMinSafeInt)
testInt(JavascriptMaxSafeInt + 1)
testInt(JavascriptMinSafeInt - 1)
})

t.Run("floats", func(t *testing.T) {
testFloat := func(n float64) {
result, err := service.Run(ctx, n, strconv.FormatFloat(n, 'f', -1, 64))
if assert.NoError(t, err) {
assert.EqualValues(t, n, result)
}
}

testFloat(-1.0)
testFloat(0.0)
testFloat(0.001)
testFloat(1.0)
testFloat(math.MaxInt32)
testFloat(math.MinInt32)
testFloat(math.MaxInt64)
testFloat(math.MinInt64)
testFloat(float64(JavascriptMaxSafeInt))
testFloat(float64(JavascriptMinSafeInt))
testFloat(float64(JavascriptMaxSafeInt + 1))
testFloat(float64(JavascriptMinSafeInt - 1))
testFloat(12345678.12345678)
testFloat(22.0 / 7.0)
testFloat(0.1)
})

t.Run("json_Number_integer", func(t *testing.T) {
hugeInt := json.Number("123456789012345")
result, err := service.Run(ctx, hugeInt, string(hugeInt))
if assert.NoError(t, err) {
assert.EqualValues(t, 123456789012345, result)
}
})

if vm.Engine().languageVersion >= 11 { // (Otto does not support BigInts)
t.Run("json_Number_huge_integer", func(t *testing.T) {
hugeInt := json.Number("1234567890123456789012345678901234567890")
result, err := service.Run(ctx, hugeInt, string(hugeInt))
if assert.NoError(t, err) {
ibig := new(big.Int)
ibig, _ = ibig.SetString(string(hugeInt), 10)
assert.EqualValues(t, ibig, result)
}
})
}

t.Run("json_Number_float", func(t *testing.T) {
floatStr := json.Number("1234567890.123")
result, err := service.Run(ctx, floatStr, string(floatStr))
if assert.NoError(t, err) {
assert.EqualValues(t, 1234567890.123, result)
}
})
})
}

// For security purposes, verify that JS APIs to do network or file I/O are not present:
func TestNoIO(t *testing.T) {
ctx := context.Background()

vm := V8.NewVM() // Otto appears to have no way to refer to the global object...
defer vm.Close()

service := NewService(vm, "check", `function() {
// Ensure that global fns/classes enabling network or file access are missing:
if (globalThis.fetch !== undefined) throw "fetch exists";
if (globalThis.XMLHttpRequest !== undefined) throw "XMLHttpRequest exists";
if (globalThis.File !== undefined) throw "File exists";
if (globalThis.require !== undefined) throw "require exists";
// But the following should exist:
if (globalThis.Math === undefined) throw "Math is missing!";
if (globalThis.String === undefined) throw "String is missing!";
if (globalThis.Number === undefined) throw "Number is missing!";
if (globalThis.console === undefined) throw "console is missing!";
}`)
_, err := service.Run(ctx)
assert.NoError(t, err)
}

// Verify that ECMAScript modules can't be loaded. (The older `require` is checked in TestNoIO.)
func TestNoModules(t *testing.T) {
ctx := context.Background()

vm := V8.NewVM() // Otto doesn't support ES modules
defer vm.Close()

src := `import foo from 'foo';
(function() { });`

service := NewCustomService(vm, "check", func(tmpl *V8BasicTemplate) (V8Template, error) {
err := tmpl.SetScript(src)
return tmpl, err
})
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "Cannot use import statement outside a module")
}

func TestTimeout(t *testing.T) {
TestWithVMs(t, func(t *testing.T, vm VM) {
ctx := context.Background()

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

service := NewService(vm, "forever", `function() { while (true) ; }`)
start := time.Now()
_, err := service.Run(ctx)
assert.Less(t, time.Since(start), 4*time.Second)
assert.Equal(t, context.DeadlineExceeded, err)
})
}

func TestOutOfMemory(t *testing.T) {
vm := V8.NewVM()
defer vm.Close()
ctx := context.Background()

service := NewService(vm, "OOM", `
function() {
let a = ["supercalifragilisticexpialidocious"];
while (true) {
a = [a, a];
}
}`)
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "ExecutionTerminated: script execution has been terminated")
}

func TestStackOverflow(t *testing.T) {
vm := V8.NewVM()
defer vm.Close()
ctx := context.Background()

service := NewService(vm, "Overflow", `
function() {
function recurse(n) {console.log("level ", n); return recurse(n + 1) * recurse(n + 2);}
return recurse(0);
}`)
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "Maximum call stack size exceeded")
}
Loading