Skip to content

Commit 6388df2

Browse files
committed
qmlweb_parse: support assigning functions to properties
Declaring new properties (qmlpropdef) with functions worked already, but assiging functions to existing properties (qmlprop) didn't. This is the fix for those (js functions inside qmlprop statements). Fixes: #32
1 parent dd0dece commit 6388df2

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

src/api.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) {
141141

142142
var statement_js = statement;
143143
statement = function() {
144-
var in_qmlpropdef = !!statement.in_qmlpropdef;
145-
statement.in_qmlpropdef = false;
144+
var in_qmlprop = !!statement.in_qmlprop;
145+
statement.in_qmlprop = false;
146146
switch (S.token.type) {
147147
case "punc":
148148
switch (S.token.value) {
@@ -152,7 +152,7 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) {
152152
case "keyword":
153153
switch (S.token.value) {
154154
case "function":
155-
if (in_qmlpropdef) {
155+
if (in_qmlprop) {
156156
next();
157157
return function_(false);
158158
}
@@ -240,7 +240,7 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) {
240240
}
241241
if (is("punc", ":")) {
242242
next();
243-
statement.in_qmlpropdef = true;
243+
statement.in_qmlprop = true;
244244
return as_statement("qmlpropdef", name, type);
245245
} else if (is("punc", ";"))
246246
next();
@@ -318,6 +318,7 @@ function qmlweb_parse($TEXT, document_type, exigent_mode) {
318318
} else {
319319
// Evaluatable item
320320
expect(":");
321+
statement.in_qmlprop = true;
321322
return as_statement("qmlprop", propname);
322323
}
323324
} else if (is("keyword", "default")) {

tests/qml/ParseFunctionVar.qml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import QtQuick 2.0
22

3-
Item {
3+
MyItem {
44
property var aFunction: function(){}
55
property var bFunction: function(h) {
66
return 21;
77
}
8+
cFunction: function (a,b, c) {
9+
a /= 2;
10+
return a - b + c;
11+
}
812
}

tests/qml/ParseFunctionVar.qml.json

+56-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
[
1313
"qmlelem",
14-
"Item",
14+
"MyItem",
1515
null,
1616
[
1717
[
@@ -46,7 +46,61 @@
4646
]
4747
]
4848
],
49-
"function(h) {\n return 21;\n }\n"
49+
"function(h) {\n return 21;\n }\n "
50+
],
51+
[
52+
"qmlprop",
53+
"cFunction",
54+
[
55+
"function",
56+
null,
57+
[
58+
"a",
59+
"b",
60+
"c"
61+
],
62+
[
63+
[
64+
"stat",
65+
[
66+
"assign",
67+
"/",
68+
[
69+
"name",
70+
"a"
71+
],
72+
[
73+
"num",
74+
2
75+
]
76+
]
77+
],
78+
[
79+
"return",
80+
[
81+
"binary",
82+
"+",
83+
[
84+
"binary",
85+
"-",
86+
[
87+
"name",
88+
"a"
89+
],
90+
[
91+
"name",
92+
"b"
93+
]
94+
],
95+
[
96+
"name",
97+
"c"
98+
]
99+
]
100+
]
101+
]
102+
],
103+
"function (a,b, c) {\n a /= 2;\n return a - b + c;\n }\n"
50104
]
51105
]
52106
]

0 commit comments

Comments
 (0)