]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 2006 Trolltech ASA | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | #include "config.h" | |
26 | #include <assert.h> | |
27 | #include <stdio.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "value.h" | |
31 | #include "object.h" | |
32 | #include "types.h" | |
33 | #include "interpreter.h" | |
34 | ||
35 | #include "qobject.h" | |
36 | #include "qdebug.h" | |
37 | ||
38 | #include "runtime.h" | |
39 | #include "runtime_object.h" | |
40 | ||
41 | ||
42 | ||
43 | class MyObject : public QObject | |
44 | { | |
45 | Q_OBJECT | |
46 | Q_PROPERTY(QString testString READ testString WRITE setTestString) | |
47 | Q_PROPERTY(int testInt READ testInt WRITE setTestInt) | |
48 | ||
49 | public: | |
50 | MyObject() : QObject(0), integer(0){} | |
51 | ||
52 | void setTestString(const QString &str) { | |
53 | qDebug() << "called setTestString" << str; | |
54 | string = str; | |
55 | } | |
56 | void setTestInt(int i) { | |
57 | qDebug() << "called setTestInt" << i; | |
58 | integer = i; | |
59 | } | |
60 | QString testString() const { | |
61 | qDebug() << "called testString" << string; | |
62 | return string; | |
63 | } | |
64 | int testInt() const { | |
65 | qDebug() << "called testInt" << integer; | |
66 | return integer; | |
67 | } | |
68 | QString string; | |
69 | int integer; | |
70 | ||
71 | public slots: | |
72 | void foo() { qDebug() << "foo invoked"; } | |
73 | }; | |
74 | ||
75 | // -------------------------------------------------------- | |
76 | ||
77 | using namespace KJS; | |
78 | using namespace KJS::Bindings; | |
79 | ||
80 | class Global : public JSObject { | |
81 | public: | |
82 | virtual UString className() const { return "global"; } | |
83 | }; | |
84 | ||
85 | static char code[] = | |
86 | "myInterface.foo();\n" | |
87 | "myInterface.testString = \"Hello\";\n" | |
88 | "str = myInterface.testString;\n" | |
89 | "myInterface.testInt = 10;\n" | |
90 | "i = myInterface.testInt;\n"; | |
91 | ||
92 | int main(int argc, char** argv) | |
93 | { | |
94 | // expecting a filename | |
95 | bool ret = true; | |
96 | { | |
97 | JSLock lock; | |
98 | ||
99 | // create interpreter w/ global object | |
100 | Global* global = new Global(); | |
101 | ||
102 | // create interpreter | |
103 | RefPtr<Interpreter> interp = new Interpreter(global); | |
104 | ExecState* exec = interp->globalExec(); | |
105 | ||
106 | MyObject* myObject = new MyObject; | |
107 | ||
108 | global->put(exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::QtLanguage, (void*)myObject)); | |
109 | ||
110 | ||
111 | if (code) { | |
112 | // run | |
113 | Completion comp(interp->evaluate("", 0, code)); | |
114 | ||
115 | if (comp.complType() == Throw) { | |
116 | qDebug() << "exception thrown"; | |
117 | JSValue* exVal = comp.value(); | |
118 | char* msg = exVal->toString(exec).ascii(); | |
119 | int lineno = -1; | |
120 | if (exVal->type() == ObjectType) { | |
121 | JSValue* lineVal = exVal->getObject()->get(exec, Identifier("line")); | |
122 | if (lineVal->type() == NumberType) | |
123 | lineno = int(lineVal->toNumber(exec)); | |
124 | } | |
125 | if (lineno != -1) | |
126 | fprintf(stderr,"Exception, line %d: %s\n",lineno,msg); | |
127 | else | |
128 | fprintf(stderr,"Exception: %s\n",msg); | |
129 | ret = false; | |
130 | } | |
131 | else if (comp.complType() == ReturnValue) { | |
132 | char* msg = comp.value()->toString(interp->globalExec()).ascii(); | |
133 | fprintf(stderr,"Return value: %s\n",msg); | |
134 | } | |
135 | } | |
136 | ||
137 | } // end block, so that Interpreter and global get deleted | |
138 | ||
139 | return ret ? 0 : 1; | |
140 | } | |
141 | ||
142 | #include "testqtbindings.moc" |