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;
116 StaticBinding::publish( exec, parent, IoFactory::methods() );
117 StaticBinding::publish( exec, parent, FileDialog::methods() );
118 StaticBinding::publish( exec, parent, BuiltinsFactory::methods() );
119 StaticConstructor::add( exec, parent, FileIO::constructor() );
120 StaticConstructor::add( exec, parent, DomNode::constructor() );
121 StaticConstructor::add( exec, parent, DomDocument::constructor() );
122 StaticConstructor::add( exec, parent, DomElement::constructor() );
123 StaticConstructor::add( exec, parent, DomAttr::constructor() );
124 StaticConstructor::add( exec, parent, DomDocumentType::constructor() );
125 StaticConstructor::add( exec, parent, DomNodeList::constructor() );
126 StaticConstructor::add( exec, parent, DomNamedNodeMap::constructor() );
127 StaticConstructor::add( exec, parent, DomText::constructor() );
128 StaticConstructor::add( exec, parent, Url::constructor() );
129 StaticConstructor::add( exec, parent, SettingsBinding::constructor() );
130 StaticConstructor::add( exec, parent, CoreApplicationBinding::constructor() );
131 StaticConstructor::add( exec, parent, Point::constructor() );
132 StaticConstructor::add( exec, parent, Size::constructor() );
133 StaticConstructor::add( exec, parent, Rect::constructor() );
134 StaticConstructor::add( exec, parent, Color::constructor() );
137 QApplication* app = ::qobject_cast<QApplication*>(QCoreApplication::instance());
138 if (app && (app->type() != QApplication::Tty))
142 #ifdef KJSEMBED_FORMBUILDER_BINDING 143 StaticConstructor::add( exec, parent, FormBuilder::constructor() );
145 StaticConstructor::add( exec, parent, UiLoaderBinding::constructor() );
146 StaticConstructor::add( exec, parent, QWidgetBinding::constructor() );
147 StaticConstructor::add( exec, parent, Layout::constructor() );
148 StaticConstructor::add( exec, parent, Action::constructor() );
149 StaticConstructor::add( exec, parent, ActionGroup::constructor() );
150 StaticConstructor::add( exec, parent, Font::constructor() );
151 StaticConstructor::add( exec, parent, Pen::constructor() );
152 StaticConstructor::add( exec, parent, Brush::constructor() );
153 StaticConstructor::add( exec, parent, Image::constructor() );
154 StaticConstructor::add( exec, parent, Pixmap::constructor() );
155 StaticConstructor::add( exec, parent, Painter::constructor() );
156 StaticConstructor::add( exec, parent, SvgRenderer::constructor() );
157 StaticConstructor::add( exec, parent, SvgWidget::constructor() );
158 StaticConstructor::add( exec, parent, ApplicationBinding::constructor() );
162 Engine::Engine(
bool enableBindings )
164 dptr =
new EnginePrivate( );
165 if ( enableBindings )
166 setup( dptr->m_interpreter->globalExec(), dptr->m_interpreter->globalObject() );
167 dptr->m_bindingsEnabled = enableBindings;
175 bool Engine::isBindingsEnabled()
const 177 return dptr->m_bindingsEnabled;
180 KJS::JSObject *Engine::addObject(
QObject *obj, KJS::JSObject *
parent,
const KJS::UString &
name )
const 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 );
190 KJS::JSObject *Engine::addObject(
QObject *obj,
const KJS::UString &
name )
const 192 return addObject( obj, dptr->m_interpreter->globalObject(),
name );
195 KJS::Completion Engine::completion()
const 197 return dptr->m_currentResult;
200 KJS::Interpreter *Engine::interpreter()
const 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 )
238 return Engine::Success;
239 else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
240 return Engine::Success;
242 return Engine::Failure;
247 dptr->m_currentResult = dptr->m_interpreter->evaluate(KJS::UString(
""), 0, code, 0);
248 if( dptr->m_currentResult.complType() == KJS::Normal )
249 return Engine::Success;
250 else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
251 return Engine::Success;
253 return Engine::Failure;
258 KJS::JSObject *global = dptr->m_interpreter->globalObject();
259 KJS::ExecState *exec = dptr->m_interpreter->globalExec();
260 return StaticConstructor::construct( exec, global, className, args );
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();
ExitStatus
Status codes for script execution.
END_QOBJECT_METHOD QByteArray className
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.
KJS::JSValue *(* callMethod)(KJS::ExecState *, KJS::JSObject *, const KJS::List &)
Method callback signature.
END_OBJECT_METHOD QFile * file
Implement QString-KJS::UString conversion methods.
KJS::UString toUString(const QString &qs)
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
QString toQString(const KJS::UString &u)