Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions exist-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@
<include>src/test/java/org/exist/xquery/ForwardReferenceTest.java</include>
<include>src/main/java/org/exist/xquery/Function.java</include>
<include>src/main/java/org/exist/xquery/FunctionFactory.java</include>
<include>src/main/java/org/exist/xquery/InlineFunction.java</include>
<include>src/test/java/org/exist/xquery/InternalModuleTest.java</include>
<include>src/main/java/org/exist/xquery/Intersect.java</include>
<include>src/test/java/org/exist/xquery/LexerTest.java</include>
Expand Down Expand Up @@ -1974,6 +1975,7 @@
<exclude>src/test/resources-filtered/org/exist/xquery/import-from-pkg-test.conf.xml</exclude>
<exclude>src/test/java/org/exist/xquery/ImportFromPkgTest.java</exclude>
<exclude>src/test/java/org/exist/xquery/ImportModuleTest.java</exclude>
<exclude>src/main/java/org/exist/xquery/InlineFunction.java</exclude>
<exclude>src/test/java/org/exist/xquery/InternalModuleTest.java</exclude>
<exclude>src/main/java/org/exist/xquery/Intersect.java</exclude>
<exclude>src/main/java/org/exist/xquery/JavaBinding.java</exclude>
Expand Down
125 changes: 73 additions & 52 deletions exist-core/src/main/java/org/exist/xquery/InlineFunction.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
/*
* Elemental
* Copyright (C) 2024, Evolved Binary Ltd
*
* admin@evolvedbinary.com
* https://www.evolvedbinary.com | https://www.elemental.xyz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
* The original license header is included below.
*
* =====================================================================
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand All @@ -21,7 +45,6 @@
*/
package org.exist.xquery;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Stack;

Expand All @@ -34,69 +57,67 @@

/**
* An XQuery 3.0 inline function expression.
*
* @author wolf
*
* @author wolf
* @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
*/
public class InlineFunction extends AbstractExpression {

public final static QName INLINE_FUNCTION_QNAME = QName.EMPTY_QNAME;

private UserDefinedFunction function;
private ArrayDeque<FunctionCall> calls = new ArrayDeque<>();

private AnalyzeContextInfo cachedContextInfo;
public static final QName INLINE_FUNCTION_QNAME = QName.EMPTY_QNAME;

public InlineFunction(XQueryContext context, UserDefinedFunction function) {
super(context);
this.function = function;
}
private final UserDefinedFunction function;

@Override
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
private AnalyzeContextInfo cachedContextInfo;

public InlineFunction(final XQueryContext context, final UserDefinedFunction function) {
super(context);
this.function = function;
}

@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
cachedContextInfo = new AnalyzeContextInfo(contextInfo);
cachedContextInfo.addFlag(SINGLE_STEP_EXECUTION);
cachedContextInfo.setParent(this);
}

@Override
public void dump(ExpressionDumper dumper) {
dumper.display("function");
function.dump(dumper);
}

/**
* Wraps a function call around the function and returns a
* reference to it. Make sure local variables in the context
* are visible.
*/
public Sequence eval(Sequence contextSequence, Item contextItem)
throws XPathException {
// local variable context is known within inline function
final List<ClosureVariable> closureVars = context.getLocalStack();

final FunctionCall call = new FunctionCall(context, function);
call.getFunction().setClosureVariables(closureVars);
call.setLocation(function.getLine(), function.getColumn());
call.analyze(new AnalyzeContextInfo(cachedContextInfo));

// push the created function call to the stack so we can clear
// it after execution
calls.push(call);

return new FunctionReference(this, call);
}

@Override
public int returnsType() {
return Type.FUNCTION_REFERENCE;
}
}

@Override
public void dump(final ExpressionDumper dumper) {
dumper.display("function");
function.dump(dumper);
}

/**
* Wraps a function call around the function and returns a
* reference to it. Make sure local variables in the context
* are visible.
*/
@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
// local variable context is known within inline function
final List<ClosureVariable> closureVars = context.getLocalStack();

final FunctionCall call = new FunctionCall(context, function);
call.getFunction().setClosureVariables(closureVars);
call.setLocation(function.getLine(), function.getColumn());
call.analyze(new AnalyzeContextInfo(cachedContextInfo));

return new FunctionReference(this, call);
}

@Override
public void resetState(boolean postOptimization) {
public int returnsType() {
return Type.FUNCTION_REFERENCE;
}

@Override
public void resetState(final boolean postOptimization) {
super.resetState(postOptimization);
calls.clear();

if (!postOptimization) {
function.setClosureVariables(null);
}

function.resetState(postOptimization);
}
}
}