1 | //! [0]
|
---|
2 | QScriptEngine myEngine;
|
---|
3 | QScriptValue three = myEngine.evaluate("1 + 2");
|
---|
4 | //! [0]
|
---|
5 |
|
---|
6 |
|
---|
7 | //! [1]
|
---|
8 | QScriptValue fun = myEngine.evaluate("function(a, b) { return a + b; }");
|
---|
9 | QScriptValueList args;
|
---|
10 | args << 1 << 2;
|
---|
11 | QScriptValue threeAgain = fun.call(QScriptValue(), args);
|
---|
12 | //! [1]
|
---|
13 |
|
---|
14 |
|
---|
15 | //! [2]
|
---|
16 | QString fileName = "helloworld.qs";
|
---|
17 | QFile scriptFile(fileName);
|
---|
18 | if (!scriptFile.open(QIODevice::ReadOnly))
|
---|
19 | // handle error
|
---|
20 | QTextStream stream(&scriptFile);
|
---|
21 | QString contents = stream.readAll();
|
---|
22 | scriptFile.close();
|
---|
23 | myEngine.evaluate(contents, fileName);
|
---|
24 | //! [2]
|
---|
25 |
|
---|
26 |
|
---|
27 | //! [3]
|
---|
28 | myEngine.globalObject().setProperty("myNumber", 123);
|
---|
29 | ...
|
---|
30 | QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
|
---|
31 | //! [3]
|
---|
32 |
|
---|
33 |
|
---|
34 | //! [4]
|
---|
35 | QScriptValue result = myEngine.evaluate(...);
|
---|
36 | if (myEngine.hasUncaughtException()) {
|
---|
37 | int line = myEngine.uncaughtExceptionLineNumber();
|
---|
38 | qDebug() << "uncaught exception at line" << line << ":" << result.toString();
|
---|
39 | }
|
---|
40 | //! [4]
|
---|
41 |
|
---|
42 |
|
---|
43 | //! [5]
|
---|
44 | QPushButton button;
|
---|
45 | QScriptValue scriptButton = myEngine.newQObject(&button);
|
---|
46 | myEngine.globalObject().setProperty("button", scriptButton);
|
---|
47 |
|
---|
48 | myEngine.evaluate("button.checkable = true");
|
---|
49 |
|
---|
50 | qDebug() << scriptButton.property("checkable").toBoolean();
|
---|
51 | scriptButton.property("show").call(); // call the show() slot
|
---|
52 | //! [5]
|
---|
53 |
|
---|
54 |
|
---|
55 | //! [6]
|
---|
56 | QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
|
---|
57 | {
|
---|
58 | QScriptValue a = context->argument(0);
|
---|
59 | QScriptValue b = context->argument(1);
|
---|
60 | return a.toNumber() + b.toNumber();
|
---|
61 | }
|
---|
62 | //! [6]
|
---|
63 |
|
---|
64 |
|
---|
65 | //! [7]
|
---|
66 | QScriptValue fun = myEngine.newFunction(myAdd);
|
---|
67 | myEngine.globalObject().setProperty("myAdd", fun);
|
---|
68 | //! [7]
|
---|
69 |
|
---|
70 |
|
---|
71 | //! [8]
|
---|
72 | QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
|
---|
73 | //! [8]
|
---|
74 |
|
---|
75 |
|
---|
76 | //! [9]
|
---|
77 | QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
|
---|
78 | {
|
---|
79 | if (context->calledAsConstructor()) {
|
---|
80 | // initialize the new object
|
---|
81 | context->thisObject().setProperty("bar", ...);
|
---|
82 | // ...
|
---|
83 | // return a non-object value to indicate that the
|
---|
84 | // thisObject() should be the result of the "new Foo()" expression
|
---|
85 | return engine->undefinedValue();
|
---|
86 | } else {
|
---|
87 | // not called as "new Foo()", just "Foo()"
|
---|
88 | // create our own object and return that one
|
---|
89 | QScriptValue object = engine->newObject();
|
---|
90 | object.setPrototype(context->callee().property("prototype"));
|
---|
91 | object.setProperty("baz", ...);
|
---|
92 | return object;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | ...
|
---|
97 |
|
---|
98 | QScriptValue fooProto = engine->newObject();
|
---|
99 | fooProto.setProperty("whatever", ...);
|
---|
100 | engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
|
---|
101 | //! [9]
|
---|
102 |
|
---|
103 |
|
---|
104 | //! [10]
|
---|
105 | class Bar { ... };
|
---|
106 |
|
---|
107 | Q_DECLARE_METATYPE(Bar)
|
---|
108 |
|
---|
109 | QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
|
---|
110 | {
|
---|
111 | Bar bar;
|
---|
112 | // initialize from arguments in context, if desired
|
---|
113 | ...
|
---|
114 | return engine->toScriptValue(bar);
|
---|
115 | }
|
---|
116 |
|
---|
117 | class BarPrototype : public QObject, public QScriptable
|
---|
118 | {
|
---|
119 | // provide the scriptable interface of this type using slots and properties
|
---|
120 | ...
|
---|
121 | };
|
---|
122 |
|
---|
123 | ...
|
---|
124 |
|
---|
125 | // create and register the Bar prototype and constructor in the engine
|
---|
126 | BarPrototype *barPrototypeObject = new BarPrototype(...);
|
---|
127 | QScriptValue barProto = engine->newQObject(barPrototypeObject);
|
---|
128 | engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
|
---|
129 | QScriptValue barCtor = engine->newFunction(constructBar, barProto);
|
---|
130 | engine->globalObject().setProperty("Bar", barCtor);
|
---|
131 | //! [10]
|
---|
132 |
|
---|
133 |
|
---|
134 | //! [11]
|
---|
135 | static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
|
---|
136 | {
|
---|
137 | QScriptValue callee = context->callee();
|
---|
138 | if (context->argumentCount() == 1) // writing?
|
---|
139 | callee.setProperty("value", context->argument(0));
|
---|
140 | return callee.property("value");
|
---|
141 | }
|
---|
142 |
|
---|
143 | ....
|
---|
144 |
|
---|
145 | QScriptValue object = engine.newObject();
|
---|
146 | object.setProperty("foo", engine.newFunction(getSetFoo),
|
---|
147 | QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
|
---|
148 | //! [11]
|
---|
149 |
|
---|
150 |
|
---|
151 | //! [12]
|
---|
152 | QScriptValue object = engine.newObject();
|
---|
153 | object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
|
---|
154 | object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
|
---|
155 | //! [12]
|
---|
156 |
|
---|
157 |
|
---|
158 | //! [13]
|
---|
159 | Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
|
---|
160 |
|
---|
161 | ...
|
---|
162 |
|
---|
163 | QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
|
---|
164 | engine.globalObject().setProperty("QLineEdit", lineEditClass);
|
---|
165 | //! [13]
|
---|
166 |
|
---|
167 |
|
---|
168 | //! [14]
|
---|
169 | if (hello && world)
|
---|
170 | print("hello world");
|
---|
171 | //! [14]
|
---|
172 |
|
---|
173 |
|
---|
174 | //! [15]
|
---|
175 | if (hello &&
|
---|
176 | //! [15]
|
---|
177 |
|
---|
178 |
|
---|
179 | //! [16]
|
---|
180 | 0 = 0
|
---|
181 | //! [16]
|
---|
182 |
|
---|
183 |
|
---|
184 | //! [17]
|
---|
185 | ./test.js
|
---|
186 | //! [17]
|
---|
187 |
|
---|
188 |
|
---|
189 | //! [18]
|
---|
190 | foo["bar"]
|
---|
191 | //! [18]
|
---|
192 |
|
---|
193 |
|
---|
194 | //! [19]
|
---|
195 | QScriptEngine engine;
|
---|
196 | QScriptContext *context = engine.pushContext();
|
---|
197 | context->activationObject().setProperty("myArg", 123);
|
---|
198 | engine.evaluate("var tmp = myArg + 42");
|
---|
199 | ...
|
---|
200 | engine.popContext();
|
---|
201 | //! [19]
|
---|
202 |
|
---|
203 |
|
---|
204 | //! [20]
|
---|
205 | struct MyStruct {
|
---|
206 | int x;
|
---|
207 | int y;
|
---|
208 | };
|
---|
209 | //! [20]
|
---|
210 |
|
---|
211 |
|
---|
212 | //! [21]
|
---|
213 | Q_DECLARE_METATYPE(MyStruct)
|
---|
214 | //! [21]
|
---|
215 |
|
---|
216 |
|
---|
217 | //! [22]
|
---|
218 | QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
|
---|
219 | {
|
---|
220 | QScriptValue obj = engine->newObject();
|
---|
221 | obj.setProperty("x", s.x);
|
---|
222 | obj.setProperty("y", s.y);
|
---|
223 | return obj;
|
---|
224 | }
|
---|
225 |
|
---|
226 | void fromScriptValue(const QScriptValue &obj, MyStruct &s)
|
---|
227 | {
|
---|
228 | s.x = obj.property("x").toInt32();
|
---|
229 | s.y = obj.property("y").toInt32();
|
---|
230 | }
|
---|
231 | //! [22]
|
---|
232 |
|
---|
233 |
|
---|
234 | //! [23]
|
---|
235 | qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
|
---|
236 | //! [23]
|
---|
237 |
|
---|
238 |
|
---|
239 | //! [24]
|
---|
240 | MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
|
---|
241 | ...
|
---|
242 | MyStruct s2;
|
---|
243 | s2.x = s.x + 10;
|
---|
244 | s2.y = s.y + 20;
|
---|
245 | QScriptValue v = engine->toScriptValue(s2);
|
---|
246 | //! [24]
|
---|
247 |
|
---|
248 |
|
---|
249 | //! [25]
|
---|
250 | QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
|
---|
251 | {
|
---|
252 | MyStruct s;
|
---|
253 | s.x = 123;
|
---|
254 | s.y = 456;
|
---|
255 | return engine->toScriptValue(s);
|
---|
256 | }
|
---|
257 | ...
|
---|
258 | QScriptValue ctor = engine.newFunction(createMyStruct);
|
---|
259 | engine.globalObject().setProperty("MyStruct", ctor);
|
---|
260 | //! [25]
|
---|
261 |
|
---|
262 |
|
---|
263 | //! [26]
|
---|
264 | Q_DECLARE_METATYPE(QVector<int>)
|
---|
265 |
|
---|
266 | ...
|
---|
267 |
|
---|
268 | qScriptRegisterSequenceMetaType<QVector<int> >(engine);
|
---|
269 | ...
|
---|
270 | QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
|
---|
271 | qSort(v.begin(), v.end());
|
---|
272 | QScriptValue a = engine->toScriptValue(v);
|
---|
273 | qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
|
---|
274 | //! [26]
|
---|
275 |
|
---|
276 | //! [27]
|
---|
277 | QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
|
---|
278 | QScriptEngine *engine)
|
---|
279 | {
|
---|
280 | QObject *parent = context->argument(0).toQObject();
|
---|
281 | QObject *object = new QObject(parent);
|
---|
282 | return engine->newQObject(object, QScriptEngine::ScriptOwnership);
|
---|
283 | }
|
---|
284 |
|
---|
285 | ...
|
---|
286 |
|
---|
287 | QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
|
---|
288 | QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
|
---|
289 | engine.globalObject().setProperty("QObject", metaObject);
|
---|
290 |
|
---|
291 | QScriptValue result = engine.evaluate("new QObject()");
|
---|
292 | //! [27]
|
---|