2 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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.
27 #include "BridgeJSC.h"
30 #include "interpreter.h"
33 #include "runtime_object.h"
40 class MyObject : public QObject
43 Q_PROPERTY(QString testString READ testString WRITE setTestString)
44 Q_PROPERTY(int testInt READ testInt WRITE setTestInt)
47 MyObject() : QObject(0), integer(0){}
49 void setTestString(const QString &str) {
50 qDebug() << "called setTestString" << str;
53 void setTestInt(int i) {
54 qDebug() << "called setTestInt" << i;
57 QString testString() const {
58 qDebug() << "called testString" << string;
62 qDebug() << "called testInt" << integer;
69 void foo() { qDebug() << "foo invoked"; }
72 // --------------------------------------------------------
75 using namespace JSC::Bindings;
77 class Global : public JSNonFinalObject {
79 typedef JSNonFinalObject Base;
81 static UString className(const JSObject*) { return "global"; }
85 "myInterface.foo();\n"
86 "myInterface.testString = \"Hello\";\n"
87 "str = myInterface.testString;\n"
88 "myInterface.testInt = 10;\n"
89 "i = myInterface.testInt;\n";
91 int main(int argc, char** argv)
93 // expecting a filename
98 // create interpreter w/ global object
99 Global* global = new Global();
101 // create interpreter
102 RefPtr<Interpreter> interp = new Interpreter(global);
103 ExecState* exec = interp->globalExec();
105 MyObject* myObject = new MyObject;
107 global->methodTable()->put(global, exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::QtLanguage, (void*)myObject));
112 Completion comp(interp->evaluate("", 0, code));
114 if (comp.complType() == Throw) {
115 qDebug() << "exception thrown";
116 JSValue* exVal = comp.value();
117 char* msg = exVal->toString(exec).ascii();
119 if (exVal->type() == ObjectType) {
120 JSValue* lineVal = exVal->getObject()->get(exec, Identifier("line"));
121 if (lineVal->type() == NumberType)
122 lineno = int(lineVal->toNumber(exec));
125 fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
127 fprintf(stderr,"Exception: %s\n",msg);
130 else if (comp.complType() == ReturnValue) {
131 char* msg = comp.value()->toString(interp->globalExec()).ascii();
132 fprintf(stderr,"Return value: %s\n",msg);
136 } // end block, so that Interpreter and global get deleted
141 #include "testqtbindings.moc"