@@ -4302,20 +4302,17 @@ bool QgsWithVariableExpressionFunction::isStatic( const QgsExpressionNodeFunctio
4302
4302
if ( args->count () < 3 )
4303
4303
return false ;
4304
4304
4305
+ // We only need to check if the node evaluation is static, if both - name and value - are static.
4305
4306
if ( args->at ( 0 )->isStatic ( parent, context ) && args->at ( 1 )->isStatic ( parent, context ) )
4306
4307
{
4307
4308
QVariant name = args->at ( 0 )->eval ( parent, context );
4308
4309
QVariant value = args->at ( 1 )->eval ( parent, context );
4309
4310
4310
- QgsExpressionContextScope *scope = new QgsExpressionContextScope ();
4311
- scope->setVariable ( name.toString (), value );
4312
-
4313
- QgsExpressionContext *updatedContext = const_cast <QgsExpressionContext *>( context );
4314
- updatedContext->appendScope ( scope );
4315
-
4316
- if ( args->at ( 2 )->isStatic ( parent, updatedContext ) )
4311
+ // Temporarily append a new scope to provide the variable
4312
+ appendTemporaryVariable ( context, name.toString (), value );
4313
+ if ( args->at ( 2 )->isStatic ( parent, context ) )
4317
4314
isStatic = true ;
4318
- delete updatedContext-> popScope ( );
4315
+ popTemporaryVariable ( context );
4319
4316
}
4320
4317
4321
4318
return false ;
@@ -4332,15 +4329,13 @@ QVariant QgsWithVariableExpressionFunction::run( QgsExpressionNode::NodeList *ar
4332
4329
QVariant name = args->at ( 0 )->eval ( parent, context );
4333
4330
QVariant value = args->at ( 1 )->eval ( parent, context );
4334
4331
4335
- QgsExpressionContextScope *scope = new QgsExpressionContextScope ();
4336
- scope->setVariable ( name.toString (), value );
4337
-
4338
4332
QgsExpressionContext *updatedContext = const_cast <QgsExpressionContext *>( context );
4339
4333
if ( !context )
4340
4334
updatedContext = new QgsExpressionContext ();
4341
- updatedContext->appendScope ( scope );
4335
+
4336
+ appendTemporaryVariable ( updatedContext, name.toString (), value );
4342
4337
result = args->at ( 2 )->eval ( parent, updatedContext );
4343
- delete updatedContext-> popScope ( );
4338
+ popTemporaryVariable ( updatedContext );
4344
4339
if ( !context )
4345
4340
delete updatedContext;
4346
4341
@@ -4369,13 +4364,24 @@ bool QgsWithVariableExpressionFunction::prepare( const QgsExpressionNodeFunction
4369
4364
QVariant name = args->at ( 0 )->prepare ( parent, context );
4370
4365
QVariant value = args->at ( 1 )->prepare ( parent, context );
4371
4366
4372
- QgsExpressionContextScope *scope = new QgsExpressionContextScope ();
4373
- scope->setVariable ( name.toString (), value );
4367
+ appendTemporaryVariable ( context, name.toString (), value );
4368
+ args->at ( 2 )->prepare ( parent, context );
4369
+ popTemporaryVariable ( context );
4370
+
4371
+ return true ;
4372
+ }
4374
4373
4374
+ void QgsWithVariableExpressionFunction::popTemporaryVariable ( const QgsExpressionContext *context ) const
4375
+ {
4375
4376
QgsExpressionContext *updatedContext = const_cast <QgsExpressionContext *>( context );
4376
- updatedContext->appendScope ( scope );
4377
- args->at ( 2 )->prepare ( parent, updatedContext );
4378
4377
delete updatedContext->popScope ();
4378
+ }
4379
4379
4380
- return true ;
4380
+ void QgsWithVariableExpressionFunction::appendTemporaryVariable ( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const
4381
+ {
4382
+ QgsExpressionContextScope *scope = new QgsExpressionContextScope ();
4383
+ scope->setVariable ( name, value );
4384
+
4385
+ QgsExpressionContext *updatedContext = const_cast <QgsExpressionContext *>( context );
4386
+ updatedContext->appendScope ( scope );
4381
4387
}
0 commit comments