[561] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[561] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[561] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[561] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
[2] | 41 | //! [0]
|
---|
| 42 | QScriptEngine myEngine;
|
---|
| 43 | QScriptValue three = myEngine.evaluate("1 + 2");
|
---|
| 44 | //! [0]
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | //! [1]
|
---|
[651] | 48 | QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
|
---|
[2] | 49 | QScriptValueList args;
|
---|
| 50 | args << 1 << 2;
|
---|
| 51 | QScriptValue threeAgain = fun.call(QScriptValue(), args);
|
---|
| 52 | //! [1]
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | //! [2]
|
---|
| 56 | QString fileName = "helloworld.qs";
|
---|
| 57 | QFile scriptFile(fileName);
|
---|
| 58 | if (!scriptFile.open(QIODevice::ReadOnly))
|
---|
| 59 | // handle error
|
---|
| 60 | QTextStream stream(&scriptFile);
|
---|
| 61 | QString contents = stream.readAll();
|
---|
| 62 | scriptFile.close();
|
---|
| 63 | myEngine.evaluate(contents, fileName);
|
---|
| 64 | //! [2]
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | //! [3]
|
---|
| 68 | myEngine.globalObject().setProperty("myNumber", 123);
|
---|
| 69 | ...
|
---|
| 70 | QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
|
---|
| 71 | //! [3]
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | //! [4]
|
---|
| 75 | QScriptValue result = myEngine.evaluate(...);
|
---|
| 76 | if (myEngine.hasUncaughtException()) {
|
---|
| 77 | int line = myEngine.uncaughtExceptionLineNumber();
|
---|
| 78 | qDebug() << "uncaught exception at line" << line << ":" << result.toString();
|
---|
| 79 | }
|
---|
| 80 | //! [4]
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | //! [5]
|
---|
| 84 | QPushButton button;
|
---|
| 85 | QScriptValue scriptButton = myEngine.newQObject(&button);
|
---|
| 86 | myEngine.globalObject().setProperty("button", scriptButton);
|
---|
| 87 |
|
---|
| 88 | myEngine.evaluate("button.checkable = true");
|
---|
| 89 |
|
---|
| 90 | qDebug() << scriptButton.property("checkable").toBoolean();
|
---|
| 91 | scriptButton.property("show").call(); // call the show() slot
|
---|
| 92 | //! [5]
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | //! [6]
|
---|
| 96 | QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
|
---|
| 97 | {
|
---|
| 98 | QScriptValue a = context->argument(0);
|
---|
| 99 | QScriptValue b = context->argument(1);
|
---|
| 100 | return a.toNumber() + b.toNumber();
|
---|
| 101 | }
|
---|
| 102 | //! [6]
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | //! [7]
|
---|
| 106 | QScriptValue fun = myEngine.newFunction(myAdd);
|
---|
| 107 | myEngine.globalObject().setProperty("myAdd", fun);
|
---|
| 108 | //! [7]
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | //! [8]
|
---|
| 112 | QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
|
---|
| 113 | //! [8]
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | //! [9]
|
---|
| 117 | QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
|
---|
| 118 | {
|
---|
| 119 | if (context->calledAsConstructor()) {
|
---|
| 120 | // initialize the new object
|
---|
| 121 | context->thisObject().setProperty("bar", ...);
|
---|
| 122 | // ...
|
---|
| 123 | // return a non-object value to indicate that the
|
---|
| 124 | // thisObject() should be the result of the "new Foo()" expression
|
---|
| 125 | return engine->undefinedValue();
|
---|
| 126 | } else {
|
---|
| 127 | // not called as "new Foo()", just "Foo()"
|
---|
| 128 | // create our own object and return that one
|
---|
| 129 | QScriptValue object = engine->newObject();
|
---|
| 130 | object.setPrototype(context->callee().property("prototype"));
|
---|
| 131 | object.setProperty("baz", ...);
|
---|
| 132 | return object;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | ...
|
---|
| 137 |
|
---|
| 138 | QScriptValue fooProto = engine->newObject();
|
---|
| 139 | fooProto.setProperty("whatever", ...);
|
---|
| 140 | engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
|
---|
| 141 | //! [9]
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | //! [10]
|
---|
| 145 | class Bar { ... };
|
---|
| 146 |
|
---|
| 147 | Q_DECLARE_METATYPE(Bar)
|
---|
| 148 |
|
---|
| 149 | QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
|
---|
| 150 | {
|
---|
| 151 | Bar bar;
|
---|
| 152 | // initialize from arguments in context, if desired
|
---|
| 153 | ...
|
---|
| 154 | return engine->toScriptValue(bar);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | class BarPrototype : public QObject, public QScriptable
|
---|
| 158 | {
|
---|
| 159 | // provide the scriptable interface of this type using slots and properties
|
---|
| 160 | ...
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | ...
|
---|
| 164 |
|
---|
| 165 | // create and register the Bar prototype and constructor in the engine
|
---|
| 166 | BarPrototype *barPrototypeObject = new BarPrototype(...);
|
---|
| 167 | QScriptValue barProto = engine->newQObject(barPrototypeObject);
|
---|
| 168 | engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
|
---|
| 169 | QScriptValue barCtor = engine->newFunction(constructBar, barProto);
|
---|
| 170 | engine->globalObject().setProperty("Bar", barCtor);
|
---|
| 171 | //! [10]
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | //! [11]
|
---|
| 175 | static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
|
---|
| 176 | {
|
---|
| 177 | QScriptValue callee = context->callee();
|
---|
| 178 | if (context->argumentCount() == 1) // writing?
|
---|
| 179 | callee.setProperty("value", context->argument(0));
|
---|
| 180 | return callee.property("value");
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | ....
|
---|
| 184 |
|
---|
| 185 | QScriptValue object = engine.newObject();
|
---|
| 186 | object.setProperty("foo", engine.newFunction(getSetFoo),
|
---|
| 187 | QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
|
---|
| 188 | //! [11]
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | //! [12]
|
---|
| 192 | QScriptValue object = engine.newObject();
|
---|
| 193 | object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
|
---|
| 194 | object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
|
---|
| 195 | //! [12]
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | //! [13]
|
---|
| 199 | Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
|
---|
| 200 |
|
---|
| 201 | ...
|
---|
| 202 |
|
---|
| 203 | QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
|
---|
| 204 | engine.globalObject().setProperty("QLineEdit", lineEditClass);
|
---|
| 205 | //! [13]
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | //! [14]
|
---|
| 209 | if (hello && world)
|
---|
| 210 | print("hello world");
|
---|
| 211 | //! [14]
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | //! [15]
|
---|
| 215 | if (hello &&
|
---|
| 216 | //! [15]
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | //! [16]
|
---|
| 220 | 0 = 0
|
---|
| 221 | //! [16]
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | //! [17]
|
---|
| 225 | ./test.js
|
---|
| 226 | //! [17]
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | //! [18]
|
---|
| 230 | foo["bar"]
|
---|
| 231 | //! [18]
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | //! [19]
|
---|
| 235 | QScriptEngine engine;
|
---|
| 236 | QScriptContext *context = engine.pushContext();
|
---|
| 237 | context->activationObject().setProperty("myArg", 123);
|
---|
| 238 | engine.evaluate("var tmp = myArg + 42");
|
---|
| 239 | ...
|
---|
| 240 | engine.popContext();
|
---|
| 241 | //! [19]
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | //! [20]
|
---|
| 245 | struct MyStruct {
|
---|
| 246 | int x;
|
---|
| 247 | int y;
|
---|
| 248 | };
|
---|
| 249 | //! [20]
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | //! [21]
|
---|
| 253 | Q_DECLARE_METATYPE(MyStruct)
|
---|
| 254 | //! [21]
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | //! [22]
|
---|
| 258 | QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
|
---|
| 259 | {
|
---|
| 260 | QScriptValue obj = engine->newObject();
|
---|
| 261 | obj.setProperty("x", s.x);
|
---|
| 262 | obj.setProperty("y", s.y);
|
---|
| 263 | return obj;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | void fromScriptValue(const QScriptValue &obj, MyStruct &s)
|
---|
| 267 | {
|
---|
| 268 | s.x = obj.property("x").toInt32();
|
---|
| 269 | s.y = obj.property("y").toInt32();
|
---|
| 270 | }
|
---|
| 271 | //! [22]
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | //! [23]
|
---|
| 275 | qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
|
---|
| 276 | //! [23]
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | //! [24]
|
---|
| 280 | MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
|
---|
| 281 | ...
|
---|
| 282 | MyStruct s2;
|
---|
| 283 | s2.x = s.x + 10;
|
---|
| 284 | s2.y = s.y + 20;
|
---|
| 285 | QScriptValue v = engine->toScriptValue(s2);
|
---|
| 286 | //! [24]
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | //! [25]
|
---|
| 290 | QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
|
---|
| 291 | {
|
---|
| 292 | MyStruct s;
|
---|
| 293 | s.x = 123;
|
---|
| 294 | s.y = 456;
|
---|
| 295 | return engine->toScriptValue(s);
|
---|
| 296 | }
|
---|
| 297 | ...
|
---|
| 298 | QScriptValue ctor = engine.newFunction(createMyStruct);
|
---|
| 299 | engine.globalObject().setProperty("MyStruct", ctor);
|
---|
| 300 | //! [25]
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | //! [26]
|
---|
| 304 | Q_DECLARE_METATYPE(QVector<int>)
|
---|
| 305 |
|
---|
| 306 | ...
|
---|
| 307 |
|
---|
| 308 | qScriptRegisterSequenceMetaType<QVector<int> >(engine);
|
---|
| 309 | ...
|
---|
| 310 | QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
|
---|
| 311 | qSort(v.begin(), v.end());
|
---|
| 312 | QScriptValue a = engine->toScriptValue(v);
|
---|
| 313 | qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
|
---|
| 314 | //! [26]
|
---|
| 315 |
|
---|
| 316 | //! [27]
|
---|
| 317 | QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
|
---|
| 318 | QScriptEngine *engine)
|
---|
| 319 | {
|
---|
| 320 | QObject *parent = context->argument(0).toQObject();
|
---|
| 321 | QObject *object = new QObject(parent);
|
---|
| 322 | return engine->newQObject(object, QScriptEngine::ScriptOwnership);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | ...
|
---|
| 326 |
|
---|
| 327 | QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
|
---|
| 328 | QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
|
---|
| 329 | engine.globalObject().setProperty("QObject", metaObject);
|
---|
| 330 |
|
---|
| 331 | QScriptValue result = engine.evaluate("new QObject()");
|
---|
| 332 | //! [27]
|
---|