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();
184 KJS::Identifier jsName( !name.isEmpty() ? name :
toUString(obj->objectName()) );
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();