summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression_js_emit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline/expression_js_emit.cpp')
-rw-r--r--src/mongo/db/pipeline/expression_js_emit.cpp17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/mongo/db/pipeline/expression_js_emit.cpp b/src/mongo/db/pipeline/expression_js_emit.cpp
index dd43f587016..d19a9c53191 100644
--- a/src/mongo/db/pipeline/expression_js_emit.cpp
+++ b/src/mongo/db/pipeline/expression_js_emit.cpp
@@ -130,10 +130,10 @@ boost::intrusive_ptr<Expression> ExpressionInternalJsEmit::parse(ExpressionConte
return new ExpressionInternalJsEmit(expCtx, std::move(thisRef), std::move(funcSourceString));
}
-Value ExpressionInternalJsEmit::serialize(const SerializationOptions& options) const {
+Value ExpressionInternalJsEmit::serialize(bool explain) const {
return Value(
Document{{kExpressionName,
- Document{{"eval", _funcSource}, {"this", _thisRef->serialize(options)}}}});
+ Document{{"eval", _funcSource}, {"this", _thisRef->serialize(explain)}}}});
}
Value ExpressionInternalJsEmit::evaluate(const Document& root, Variables* variables) const {
@@ -145,14 +145,11 @@ Value ExpressionInternalJsEmit::evaluate(const Document& root, Variables* variab
ExpressionContext* expCtx = getExpressionContext();
auto jsExec = expCtx->getJsExecWithScope();
-
- // Inject the native "emit" function to be called from the user-defined map function.
- //
- // We reinject this function on every invocation of evaluate(), because there is a single
- // JsExecution instance for the OperationContext, which may be shared by multiple aggregation
- // pipelines and we need to ensure that the injected function still points to the valid
- // contextual data ('_emitState').
- jsExec->injectEmit(emitFromJS, &_emitState);
+ // Inject the native "emit" function to be called from the user-defined map function. This
+ // particular Expression/ExpressionContext may be reattached to a new OperationContext (and thus
+ // a new JS Scope) when used across getMore operations, so this method will handle that case for
+ // us by only injecting if we haven't already.
+ jsExec->injectEmitIfNecessary(emitFromJS, &_emitState);
// Although inefficient to "create" a new function every time we evaluate, this will usually end
// up being a simple cache lookup. This is needed because the JS Scope may have been recreated