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
40class 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
46public:
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
68public slots:
69    void foo() { qDebug() << "foo invoked"; }
70};
71
72// --------------------------------------------------------
73
74using namespace JSC;
75using namespace JSC::Bindings;
76
77class Global : public JSNonFinalObject {
78public:
79  virtual UString className() const { return "global"; }
80};
81
82static char code[] =
83    "myInterface.foo();\n"
84    "myInterface.testString = \"Hello\";\n"
85    "str = myInterface.testString;\n"
86    "myInterface.testInt = 10;\n"
87    "i = myInterface.testInt;\n";
88
89int main(int argc, char** argv)
90{
91    // expecting a filename
92    bool ret = true;
93    {
94        JSLock lock;
95
96        // create interpreter w/ global object
97        Global* global = new Global();
98
99        // create interpreter
100        RefPtr<Interpreter> interp = new Interpreter(global);
101        ExecState* exec = interp->globalExec();
102
103        MyObject* myObject = new MyObject;
104
105        global->put(exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::QtLanguage, (void*)myObject));
106
107
108        if (code) {
109            // run
110            Completion comp(interp->evaluate("", 0, code));
111
112            if (comp.complType() == Throw) {
113                qDebug() << "exception thrown";
114                JSValue* exVal = comp.value();
115                char* msg = exVal->toString(exec).ascii();
116                int lineno = -1;
117                if (exVal->type() == ObjectType) {
118                    JSValue* lineVal = exVal->getObject()->get(exec, Identifier("line"));
119                    if (lineVal->type() == NumberType)
120                        lineno = int(lineVal->toNumber(exec));
121                }
122                if (lineno != -1)
123                    fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
124                else
125                    fprintf(stderr,"Exception: %s\n",msg);
126                ret = false;
127            }
128            else if (comp.complType() == ReturnValue) {
129                char* msg = comp.value()->toString(interp->globalExec()).ascii();
130                fprintf(stderr,"Return value: %s\n",msg);
131            }
132        }
133
134    } // end block, so that Interpreter and global get deleted
135
136    return ret ? 0 : 1;
137}
138
139#include "testqtbindings.moc"
140