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