diff --git a/internal/server/scripts.go b/internal/server/scripts.go index 587222c6..c30ad0a4 100644 --- a/internal/server/scripts.go +++ b/internal/server/scripts.go @@ -13,6 +13,7 @@ import ( "sync" "time" + "github.com/tidwall/geojson/geo" "github.com/tidwall/resp" "github.com/tidwall/tile38/internal/log" "github.com/yuin/gopher-lua" @@ -142,12 +143,22 @@ func (pl *lStatePool) New() *lua.LState { ls.Push(lua.LString(shaSum)) return 1 } + distanceTo := func(ls *lua.LState) int { + dt := geo.DistanceTo( + float64(ls.ToNumber(1)), + float64(ls.ToNumber(2)), + float64(ls.ToNumber(3)), + float64(ls.ToNumber(4))) + ls.Push(lua.LNumber(dt)) + return 1 + } var exports = map[string]lua.LGFunction{ "call": call, "pcall": pcall, "error_reply": errorReply, "status_reply": statusReply, "sha1hex": sha1hex, + "distance_to": distanceTo, } L.SetGlobal("tile38", L.SetFuncs(L.NewTable(), exports)) diff --git a/tests/scripts_test.go b/tests/scripts_test.go index 8a87ade3..e879cedf 100644 --- a/tests/scripts_test.go +++ b/tests/scripts_test.go @@ -24,6 +24,8 @@ func scripts_BASIC_test(mc *mockServer) error { {"EVAL", "return KEYS[1] .. ' only'", 1, "key1"}, {"key1 only"}, {"EVAL", "return KEYS[1] .. ' and ' .. ARGV[1]", 1, "key1", "arg1"}, {"key1 and arg1"}, {"EVAL", "return ARGV[1] .. ' and ' .. ARGV[2]", 0, "arg1", "arg2"}, {"arg1 and arg2"}, + {"EVAL", "return tile38.sha1hex('asdf')", 0}, {"3da541559918a808c2402bba5012f6c60b27661c"}, + {"EVAL", "return tile38.distance_to(37.7341129, -122.4408378, 37.733, -122.43)", 0}, {"961"}, }) }