tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / bridge / testqtbindings.cpp
1 /*
2  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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
27 #include "BridgeJSC.h"
28 #include "JSObject.h"
29 #include "JSValue.h"
30 #include "interpreter.h"
31 #include "qdebug.h"
32 #include "qobject.h"
33 #include "runtime_object.h"
34 #include "types.h"
35 #include <assert.h>
36 #include <stdio.h>
37 #include <string.h>
38
39
40 class MyObject : public QObject
41 {
42     Q_OBJECT
43     Q_PROPERTY(QString testString READ testString WRITE setTestString)
44     Q_PROPERTY(int testInt READ testInt WRITE setTestInt)
45
46 public:
47     MyObject() : QObject(0), integer(0){}
48
49     void setTestString(const QString &str) {
50         qDebug() << "called setTestString" << str;
51         string = str;
52     }
53     void setTestInt(int i) {
54         qDebug() << "called setTestInt" << i;
55         integer = i;
56     }
57     QString testString() const {
58         qDebug() << "called testString" << string;
59         return string;
60     }
61     int testInt() const {
62         qDebug() << "called testInt" << integer;
63         return integer;
64     }
65     QString string;
66     int integer;
67
68 public slots:
69     void foo() { qDebug() << "foo invoked"; }
70 };
71
72 // --------------------------------------------------------
73
74 using namespace JSC;
75 using namespace JSC::Bindings;
76
77 class Global : public JSNonFinalObject {
78 public:
79     typedef JSNonFinalObject Base;
80
81     static UString className(const JSObject*) { return "global"; }
82 };
83
84 static char code[] =
85     "myInterface.foo();\n"
86     "myInterface.testString = \"Hello\";\n"
87     "str = myInterface.testString;\n"
88     "myInterface.testInt = 10;\n"
89     "i = myInterface.testInt;\n";
90
91 int main(int argc, char** argv)
92 {
93     // expecting a filename
94     bool ret = true;
95     {
96         JSLock lock;
97         
98         // create interpreter w/ global object
99         Global* global = new Global();
100
101         // create interpreter
102         RefPtr<Interpreter> interp = new Interpreter(global);
103         ExecState* exec = interp->globalExec();
104         
105         MyObject* myObject = new MyObject;
106         
107         global->methodTable()->put(global, exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::QtLanguage, (void*)myObject));
108         
109         
110         if (code) {
111             // run
112             Completion comp(interp->evaluate("", 0, code));
113             
114             if (comp.complType() == Throw) {
115                 qDebug() << "exception thrown";
116                 JSValue* exVal = comp.value();
117                 char* msg = exVal->toString(exec).ascii();
118                 int lineno = -1;
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));
123                 }
124                 if (lineno != -1)
125                     fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
126                 else
127                     fprintf(stderr,"Exception: %s\n",msg);
128                 ret = false;
129             }
130             else if (comp.complType() == ReturnValue) {
131                 char* msg = comp.value()->toString(interp->globalExec()).ascii();
132                 fprintf(stderr,"Return value: %s\n",msg);
133             }
134         }
135         
136     } // end block, so that Interpreter and global get deleted
137     
138     return ret ? 0 : 1;
139 }
140
141 #include "testqtbindings.moc"