KJsEmbed

kjsembed.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef KJSEMBED_H
24 #define KJSEMBED_H
25 
26 class QObject;
27 
28 #include <kjsembed/kjsembed_export.h>
29 #include <kjs/object.h>
30 
31 namespace KJS
32 {
33 class Interpreter;
34 class Object;
35 class Completion;
36 }
37 
38 namespace KJSEmbed
39 {
40 
41 /**
42  * The main interface for running embedded Javascript.
43  *
44  * \code
45  * KJSEmbed::Engine *engine = new KJSEmbed::Engine();
46  * KJS::Interpreter *interpreter = engine->interpreter();
47  * interpreter->setShouldPrintExceptions(true);
48  * KJS::ExecState *exec = interpreter->globalExec();
49  * KJS::UString code("print(\"Hello World\")");
50  * KJSEmbed::Engine::ExitStatus exitstatus = engine->execute(code);
51  * KJS::Completion completion = engine->completion();
52  * if(exitstatus != KJSEmbed::Engine::Success) {
53  * KJS::JSValue* value = completion.value();
54  * qDebug() << value->toString(exec).qstring();
55  * }
56  * \endcode
57  */
58 class KJSEMBED_EXPORT Engine
59 {
60 public:
61  /**
62  * Status codes for script execution. Note that you can access the completion
63  * object itself with completion() for more detail.
64  */
65  enum ExitStatus { Success = 0, Failure = 1 };
66 
67  /**
68  * Constructs an embedded JS engine.
69  *
70  * @param enableBindings If true then the bindings will be added to the interpreter
71  * created. Otherwise a plain interpreter with nothing beyond the JS built in functions
72  * and objects is created. This allows users who want to run untrusted scripts to choose
73  * exactly which bindings they offer.
74  */
75  Engine(bool enableBindings = true);
76  /** Clean up. */
77  virtual ~Engine();
78 
79  /** Returns true if the Engine was created with the bindings enabled. */
80  bool isBindingsEnabled() const;
81 
82  /**
83  * Execute the file with the specified name using the current interpreter.
84  * @param file Filename to execute.
85  */
86  ExitStatus runFile(const KJS::UString &file);
87 
88  /**
89  * Execute the file with the specified name using the specified interpreter.
90  * @param interpreter Interpreter to use.
91  * @param file Filename to execute.
92  */
93  static KJS::Completion runFile(KJS::Interpreter *interpreter, const KJS::UString &file);
94 
95  /**
96  * Execute a code string using the current interpreter.
97  * @param code The script code to execute.
98  */
99  ExitStatus execute(const KJS::UString &code);
100 
101  /**
102  * Returns the Completion object for the last script executed.
103  */
104  KJS::Completion completion() const;
105 
106  /**
107  * Returns the current interpreter.
108  */
109  KJS::Interpreter *interpreter() const;
110 
111  /**
112  * publishes a QObject to the global context of the javascript interpereter.
113  */
114  KJS::JSObject *addObject(QObject *obj, const KJS::UString &name = KJS::UString()) const;
115 
116  /**
117  * publishes a QObject to a parent object.
118  */
119  KJS::JSObject *addObject(QObject *obj, KJS::JSObject *parent, const KJS::UString &name = KJS::UString()) const;
120 
121  /**
122  * Create a new instance of an object that the Javascript engine knows about. If the object
123  * doesn't exist a KJS::jsNull() is returned and an exception thrown.
124  */
125  KJS::JSObject *construct(const KJS::UString &className, const KJS::List &args = KJS::List()) const;
126 
127  /**
128  * Execute a method at the global scope of the javascript interpreter.
129  */
130  KJS::JSValue *callMethod(const KJS::UString &methodName, const KJS::List &args = KJS::List());
131 
132  /**
133  * Execute a method on an object.
134  */
135  KJS::JSValue *callMethod(KJS::JSObject *parent, const KJS::UString &methodName, const KJS::List &args = KJS::List());
136 
137 private:
138  class EnginePrivate *dptr;
139 };
140 
141 }
142 
143 #endif
144 
ExitStatus
Status codes for script execution.
Definition: kjsembed.h:65
Implement QString-KJS::UString conversion methods.
The main interface for running embedded Javascript.
Definition: kjsembed.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 04:04:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.