1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright (C) 2004 Ian Reinhart Geiser <geiseri@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <kdebug.h>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kjs/interpreter.h>
#include <kjsembed/jsconsolewidget.h>
#include <kjsembed/jsobjectproxy.h>
#include <kjsembed/jsfactory.h>
#include <kjsembed/kjsembedpart.h>
#include <kjsembed/jssecuritypolicy.h>
using namespace KJSEmbed;
int main( int argc, char **argv )
{
KAboutData about( "test-kjsembed", I18N_NOOP("KJS Embed Test For C++ access of variables and functions."), "0.1",
I18N_NOOP("Test"),
KAboutData::License_LGPL, I18N_NOOP("(c) 2004 Ian Reinhart Geiser") );
KCmdLineArgs::init( argc, argv, &about );
KApplication app;
// Setup Interpreter
KJSEmbed::JSSecurityPolicy::setDefaultPolicy( KJSEmbed::JSSecurityPolicy::CapabilityAll );
KJSEmbed::KJSEmbedPart *part = new KJSEmbed::KJSEmbedPart;
KJS::Interpreter *js = part->interpreter();
KJS::ExecState *exec = js->globalExec();
TQString script = "var foobar = \"test from javascript.\";"
"function testNoArgsFunction( ){ return foobar; }"
"function testNoReturnFunction( value ){ foobar = value; }"
"function testFunction( value ){ return \"testFunction dorks with \" + value;}";
KJS::Value returnValue = part->evaluate(script,js->globalObject());
if( returnValue.isValid() && !returnValue.toBoolean(exec ) )
kdWarning() << "Script failed to run." << endl;
/**
* Test extraction of a Variant from javascript
*/
TQString value = part->getVariant("foobar").toString();
if( value != "test from javascript." )
kdWarning() << "Get variant failed with: " << value << endl;
/**
* Test manipulation of a Variant from C++
*/
part->putVariant("foobar", "test from C++.");
value = part->getVariant("foobar").toString();
if( value != "test from C++." )
kdWarning() << "Put variant failed with: " << value << endl;
/**
* Test to see if a function is present
*/
if( !part->hasMethod("testFunction") )
kdWarning() << "error finding testFunction" << endl;
if( part->hasMethod("foobar") )
kdWarning() << "error i found a value and was looking for a method" << endl;
/**
* Test calling function that takes an arg, and returns a value.
*/
KJS::List args;
args.append(KJS::String("C++ String"));
value = part->callMethod("testFunction", args).toString(exec).qstring();
if ( value != "testFunction dorks with C++ String" )
kdWarning() << "error calling testFunction, wanted \"testFunction dorks with C++ String\" but got " << value << endl;
/**
* Test calling function that takes an arg, and returns nothing.
*/
args.clear();
args.append(KJS::String("C++ String"));
returnValue = part->callMethod("testNoReturnFunction", args);
if ( returnValue.isNull() )
kdWarning() << "error calling testNoReturnFunction is suppose to be null and was " << returnValue.toString(exec).qstring() << endl;
/**
* Test calling function that takes no args, and returns a value
*/
args.clear();
value = part->callMethod("testNoArgsFunction", args).toString(exec).qstring();
if ( value != "C++ String" )
kdWarning() << "error calling testNoArgsFunction, wanted \"C++ String\" but got " << value << endl;
return 1;
}
|