Calls a javaScriptRunner's JavaScript function.
(db *db.Database, ctx context.Context, jsArgs ...any)
| 118 | |
| 119 | // Calls a javaScriptRunner's JavaScript function. |
| 120 | func (runner *jsRunner) CallWithDB(db *db.Database, ctx context.Context, jsArgs ...any) (result any, err error) { |
| 121 | if ctx == nil { |
| 122 | panic("missing context to userJSRunner") |
| 123 | } |
| 124 | |
| 125 | var timeout time.Duration |
| 126 | if deadline, exists := ctx.Deadline(); exists { |
| 127 | timeout = time.Until(deadline) |
| 128 | if timeout <= 0 { |
| 129 | return nil, sgbucket.ErrJSTimeout |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | var depth int |
| 134 | var ok bool |
| 135 | if depth, ok = ctx.Value(ctxJSCallDepthKey).(int); ok { |
| 136 | if depth >= kUserFunctionMaxCallDepth { |
| 137 | base.ErrorfCtx(ctx, "User function recursion too deep, calling %s", runner.name) |
| 138 | return nil, base.HTTPErrorf(http.StatusLoopDetected, "User function recursion too deep") |
| 139 | } |
| 140 | } |
| 141 | ctx = context.WithValue(ctx, ctxJSCallDepthKey, depth+1) |
| 142 | |
| 143 | runner.SetTimeout(timeout) |
| 144 | runner.currentDB = db |
| 145 | runner.ctx = ctx |
| 146 | return runner.Call(ctx, jsArgs...) |
| 147 | } |
| 148 | |
| 149 | // JavaScript error returned by a jsRunner |
| 150 | type jsError struct { |
no test coverage detected