31 #ifdef KJSEMBED_FORMBUILDER_BINDING 57 #include <kjs/interpreter.h> 58 #include <kjs/completion.h> 60 #include <QtCore/QFile> 61 #include <QtCore/QTextStream> 62 #include <QtCore/QObject> 64 #include <QtCore/QDebug> 75 UString::UString(
const QString &d )
77 uint len = d.length();
78 UChar *dat =
static_cast<UChar*
>(fastMalloc(
sizeof(UChar)*len));
79 memcpy( dat, d.unicode(), len *
sizeof(UChar) );
80 m_rep = UString::Rep::create(dat, len);
83 QString UString::qstring()
const 85 return QString((QChar*)
data(), size());
88 QString Identifier::qstring()
const 90 return QString((QChar*)
data(), size());
101 m_interpreter =
new KJS::Interpreter( );
102 m_interpreter->initGlobalObject();
103 m_interpreter->ref();
107 m_interpreter->deref();
109 KJS::Interpreter *m_interpreter;
110 KJS::Completion m_currentResult;
111 bool m_bindingsEnabled;
137 QApplication* app = ::qobject_cast<QApplication*>(QCoreApplication::instance());
138 if (app && (app->type() != QApplication::Tty))
142 #ifdef KJSEMBED_FORMBUILDER_BINDING 164 dptr =
new EnginePrivate( );
165 if ( enableBindings )
166 setup( dptr->m_interpreter->globalExec(), dptr->m_interpreter->globalObject() );
167 dptr->m_bindingsEnabled = enableBindings;
177 return dptr->m_bindingsEnabled;
182 KJS::ExecState *exec = dptr->m_interpreter->globalExec();
186 parent->putDirect(jsName, returnObject, KJS::DontDelete|KJS::ReadOnly );
192 return addObject( obj, dptr->m_interpreter->globalObject(),
name );
197 return dptr->m_currentResult;
202 return dptr->m_interpreter;
205 KJS::Completion
Engine::runFile( KJS::Interpreter *interpreter,
const KJS::UString &fileName )
210 if(
file.open( QFile::ReadOnly ) )
212 QTextStream ts( &
file );
217 line = ts.readLine();
218 if( line[0] !=
'#' ) code +=
toUString(line +
'\n');
224 code =
"println('Could not open file.');";
225 qWarning() <<
"Could not open file " <<
toQString(fileName);
230 return interpreter->evaluate( fileName, 0, code, 0 );
235 dptr->m_currentResult =
runFile( dptr->m_interpreter, fileName );
237 if( dptr->m_currentResult.complType() == KJS::Normal )
239 else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
247 dptr->m_currentResult = dptr->m_interpreter->evaluate(KJS::UString(
""), 0, code, 0);
248 if( dptr->m_currentResult.complType() == KJS::Normal )
250 else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
258 KJS::JSObject *global = dptr->m_interpreter->globalObject();
259 KJS::ExecState *exec = dptr->m_interpreter->globalExec();
265 KJS::JSObject *global = dptr->m_interpreter->globalObject();
266 KJS::ExecState *exec = dptr->m_interpreter->globalExec();
268 KJS::Identifier
id = KJS::Identifier( KJS::UString( methodName ) );
269 KJS::JSObject *fun = global->get( exec,
id )->toObject( exec );
270 KJS::JSValue *retValue;
272 if ( !fun->implementsCall() ) {
273 QString msg = i18n(
"%1 is not a function and cannot be called.",
toQString(methodName) );
274 return throwError( exec, KJS::TypeError, msg );
277 retValue = fun->call( exec, global, args );
279 if( exec->hadException() )
280 return exec->exception();
286 const KJS::UString &methodName,
const KJS::List &args )
288 KJS::ExecState *exec = dptr->m_interpreter->globalExec();
290 KJS::Identifier
id = KJS::Identifier( methodName);
291 KJS::JSObject *fun =
parent->get( exec,
id )->toObject( exec );
292 KJS::JSValue *retValue;
294 if ( !fun->implementsCall() ) {
295 QString msg = i18n(
"%1 is not a function and cannot be called.",
toQString(methodName) );
296 return throwError( exec, KJS::TypeError, msg );
299 retValue = fun->call( exec,
parent, args );
301 if( exec->hadException() )
302 return exec->exception();
static const Method * methods()
ExitStatus execute(const KJS::UString &code)
Execute a code string using the current interpreter.
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
KJS::JSObject * construct(KJS::ExecState *exec, const KJS::List &args)
Calls the callback that will in turn create a new instance of this object with the arguments passed i...
KJS::JSObject * construct(const KJS::UString &className, const KJS::List &args=KJS::List()) const
Create a new instance of an object that the Javascript engine knows about.
bool isBindingsEnabled() const
Returns true if the Engine was created with the bindings enabled.
ExitStatus
Status codes for script execution.
END_QOBJECT_METHOD QByteArray className
ExitStatus runFile(const KJS::UString &file)
Execute the file with the specified name using the current interpreter.
static KJS::JSObject * add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor)
Add the constructor to an object.
END_VALUE_METHOD QString name
void setup(KJS::ExecState *exec, KJS::JSObject *parent)
KJSEMBED_EXPORT KJS::JSObject * createQObject(KJS::ExecState *exec, QObject *value, KJSEmbed::ObjectBinding::Ownership owner=KJSEmbed::ObjectBinding::JSOwned)
Returns a binding object for the specified QObject.
Engine(bool enableBindings=true)
Constructs an embedded JS engine.
static const Method * methods()
KJS::Interpreter * interpreter() const
Returns the current interpreter.
END_OBJECT_METHOD QFile * file
KJS::JSObject * addObject(QObject *obj, const KJS::UString &name=KJS::UString()) const
publishes a QObject to the global context of the javascript interpereter.
Implement QString-KJS::UString conversion methods.
KJS::JSValue * callMethod(const KJS::UString &methodName, const KJS::List &args=KJS::List())
Execute a method at the global scope of the javascript interperter.
virtual ~Engine()
Clean up.
KJS::UString toUString(const QString &qs)
static const Method * methods()
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
KJS::Completion completion() const
Returns the Completion object for the last script executed.
QString toQString(const KJS::UString &u)