Skip to content

Commit

Permalink
do function coercion even if strict type checking is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
benibela committed Jun 30, 2024
1 parent 5c39807 commit ae313a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions data/tests/xpath3_1_tests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,17 @@ procedure unittests(testerrors: boolean);
t('sort(map:remove( map {"a": 1, "b": 2, "c": 3, "d": 4}, ("b", "d", "e") ) ? *)', '1 3');
t('sort(map:for-each(map{"a": 2, "b": 3}, function($k, $v){$k ||$v}))', 'a2 b3');

tester.ps.StaticContext.strictTypeChecking := false;
t('for-each("a", map { "a": 123 } )', '123');
t('for-each(2, [ "1", 9 ])', '9');
t('array:for-each(["a"], map { "a": 123 } )', '123');
t('array:for-each([2], [ "1", 9 ])', '9');
//f('for-each("2", [ "1", 9 ])', 'XPTY0004');
tester.ps.StaticContext.strictTypeChecking := true;
t('for-each("a", map { "a": 123 } )', '123');
t('for-each(2, [ "1", 9 ])', '9');
t('array:for-each(["a"], map { "a": 123 } )', '123');
t('array:for-each([2], [ "1", 9 ])', '9');
f('for-each("2", [ "1", 9 ])', 'XPTY0004');
t('count(function-name([]))', '0');
t('function-arity([])', '1');
Expand Down
14 changes: 13 additions & 1 deletion data/xquery__parse.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4924,11 +4924,23 @@ function TFinalNamespaceResolving.visit(t: PXQTerm): TXQTerm_VisitAction;
end;

function visitNamedFunction(var f: TXQTermNamedFunction): TXQTerm;
var
v: PXQFunctionParameterTypes;
checkTypes: Boolean;
i: Integer;
begin
lookupNamedFunction(f);
result := f;
if (result.ClassType = TXQTermNamedFunction) and (f.func <> nil) then begin
if staticContext.strictTypeChecking then f.version := f.func.getVersion(length(f.children));
v := f.func.getVersion(length(f.children));
if v <> nil then begin
checkTypes := staticContext.strictTypeChecking;
if not checkTypes then for i := 0 to high(v^.types) do if v^.types[i].kind = tikFunctionTest then begin
checkTypes := true; //force type checking, so array/map coercion is used
break;
end;
if checkTypes then f.version := v;
end;
end else if (result.ClassType = TXQTermNamedFunctionTypeConstructor) and (length(f.children) = 1) then
result := staticallyCastQNameAndNotation(TXQTermNamedFunctionTypeConstructor(result), TXSType(TObject(f.func)), staticContext);
end;
Expand Down
1 change: 0 additions & 1 deletion data/xquery_terms.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,6 @@ begin
for i := 0 to high(fresult.parameters) do begin
str(i, temp);
tempvar := TXQTermVariable.create(temp+'.');
writeln(tempvar.value);
tempvar.index := high(fresult.parameters);
TXQTermDynamicFunctionCall(fresult.body).push(tempvar);
fresult.parameters[i].variable := TXQTermVariable(TXQTermDynamicFunctionCall(fresult.body).children[high(TXQTermDynamicFunctionCall(fresult.body).children)].clone);
Expand Down

0 comments on commit ae313a0

Please sign in to comment.