2 * Copyright (C) 2006 Trolltech ASA
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
33 #include "interpreter.h"
39 #include "runtime_object.h"
43 class MyObject
: public QObject
46 Q_PROPERTY(QString testString READ testString WRITE setTestString
)
47 Q_PROPERTY(int testInt READ testInt WRITE setTestInt
)
50 MyObject() : QObject(0), integer(0){}
52 void setTestString(const QString
&str
) {
53 qDebug() << "called setTestString" << str
;
56 void setTestInt(int i
) {
57 qDebug() << "called setTestInt" << i
;
60 QString
testString() const {
61 qDebug() << "called testString" << string
;
65 qDebug() << "called testInt" << integer
;
72 void foo() { qDebug() << "foo invoked"; }
75 // --------------------------------------------------------
78 using namespace KJS::Bindings
;
80 class Global
: public JSObject
{
82 virtual UString
className() const { return "global"; }
86 "myInterface.foo();\n"
87 "myInterface.testString = \"Hello\";\n"
88 "str = myInterface.testString;\n"
89 "myInterface.testInt = 10;\n"
90 "i = myInterface.testInt;\n";
92 int main(int argc
, char** argv
)
94 // expecting a filename
99 // create interpreter w/ global object
100 Global
* global
= new Global();
102 // create interpreter
103 RefPtr
<Interpreter
> interp
= new Interpreter(global
);
104 ExecState
* exec
= interp
->globalExec();
106 MyObject
* myObject
= new MyObject
;
108 global
->put(exec
, Identifier("myInterface"), Instance::createRuntimeObject(Instance::QtLanguage
, (void*)myObject
));
113 Completion
comp(interp
->evaluate("", 0, code
));
115 if (comp
.complType() == Throw
) {
116 qDebug() << "exception thrown";
117 JSValue
* exVal
= comp
.value();
118 char* msg
= exVal
->toString(exec
).ascii();
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
));
126 fprintf(stderr
,"Exception, line %d: %s\n",lineno
,msg
);
128 fprintf(stderr
,"Exception: %s\n",msg
);
131 else if (comp
.complType() == ReturnValue
) {
132 char* msg
= comp
.value()->toString(interp
->globalExec()).ascii();
133 fprintf(stderr
,"Return value: %s\n",msg
);
137 } // end block, so that Interpreter and global get deleted
142 #include "testqtbindings.moc"