testqtbindings.cpp 4.7 KB

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