KJsEmbed

static_binding.cpp
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 #include "static_binding.h"
23 #include <kjs/interpreter.h>
24 #include <kjs/function_object.h>
25 #include <QDebug>
26 
27 namespace KJSEmbed
28 {
29 static QHash<QString, const Constructor *> g_ctorHash;
30 }
31 
32 using namespace KJSEmbed;
33 
35  : KJS::InternalFunctionImp(static_cast<KJS::FunctionPrototype *>(exec->lexicalInterpreter()->builtinFunctionPrototype()),
36  method->name),
37  m_method(method)
38 {
39  putDirect(exec->propertyNames().length, m_method->argc, LengthFlags);
40 }
41 
42 KJS::JSValue *StaticBinding::callAsFunction(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
43 {
44  if (m_method->call == nullptr) {
45  //throwError(exec, "Bad method id"); // NOTE: fix
46  KJS::throwError(exec, KJS::GeneralError, "Bad method id");
47  return KJS::jsNull();
48  }
49 
50  KJS::JSValue *retValue = (*m_method->call)(exec, self, args);
51 
52  if (exec->hadException()) {
53  return KJS::jsNull();
54  }
55  return retValue;
56 
57 }
58 
59 void StaticBinding::publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
60 {
61  int idx = 0;
62  while (methods[idx].name != nullptr) {
63  object->put(exec, methods[idx].name, new StaticBinding(exec, &methods[idx]), methods[idx].flags);
64  idx++;
65  }
66 }
67 
68 StaticConstructor::StaticConstructor(KJS::ExecState *exec, const Constructor *constructor)
69  : KJS::InternalFunctionImp(static_cast<KJS::FunctionPrototype *>(exec->lexicalInterpreter()->builtinFunctionPrototype()),
70  constructor->name),
71  m_constructor(constructor)
72 {
73  putDirect(exec->propertyNames().length, m_constructor->argc, LengthFlags);
74  m_default = KJS::jsNull();
75 }
76 
77 KJS::JSObject *StaticConstructor::construct(KJS::ExecState *exec, const KJS::List &args)
78 {
79  return (*m_constructor->construct)(exec, args);
80 }
81 
82 void StaticConstructor::setDefaultValue(KJS::JSValue *value)
83 {
84  m_default = value;
85 }
86 
87 KJS::JSValue *StaticConstructor::defaultValue(KJS::ExecState *exec, KJS::JSType hint) const
88 {
89  Q_UNUSED(exec);
90  Q_UNUSED(hint);
91  return m_default;
92 }
93 
94 KJS::JSObject *StaticConstructor::add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor)
95 {
96  KJS::JSObject *obj = new StaticConstructor(exec, constructor);
97  object->put(exec, constructor->name, obj);
98  if (constructor->staticMethods) {
99  StaticBinding::publish(exec, obj, constructor->staticMethods);
100  }
101  /* crashes for some reason */
102  if (constructor->enumerators) {
103  int idx = 0;
104  while (constructor->enumerators[idx].name != nullptr) {
105  obj->put(exec, constructor->enumerators[idx].name,
106  KJS::jsNumber(constructor->enumerators[idx].value), KJS::DontDelete | KJS::ReadOnly);
107  idx++;
108  }
109  }
110  // publish methods
111  KJSEmbed::g_ctorHash[constructor->name] = constructor;
112  return obj;
113 }
114 
115 const Method *StaticConstructor::methods(const KJS::UString &className)
116 {
117  return KJSEmbed::g_ctorHash[toQString(className)]->methods;
118 }
119 
120 const Constructor *StaticConstructor::constructor(const KJS::UString &className)
121 {
122  return KJSEmbed::g_ctorHash[toQString(className)];
123 }
124 
125 KJS::JSObject *StaticConstructor::bind(KJS::ExecState *exec, const QString &className, PointerBase &objPtr)
126 {
127  KJSEmbed::callBind mybind = KJSEmbed::g_ctorHash[className]->bind;
128 // qDebug() << "StaticConstructor::bind() className=" << className << " mybind=" << mybind;
129  if (mybind) {
130  return (*mybind)(exec, objPtr);
131  }
132 
133  return nullptr;
134 }
135 
136 KJS::JSObject *StaticConstructor::construct(KJS::ExecState *exec, KJS::JSObject *parent, const KJS::UString &className, const KJS::List &args)
137 {
138 // qDebug("StaticConstructor::construct('%s')", className.ascii() );
139  if (parent->hasProperty(exec, className.ascii())) {
140  KJS::JSObject *ctor = parent->get(exec, className.ascii())->toObject(exec);
141  if (ctor) {
142  return ctor->construct(exec, args);
143  }
144  }
145  qDebug("cannot create '%s'", className.ascii());
146  return KJS::throwError(exec, KJS::TypeError, toUString(QString("Cannot create %1 objects from javascript.").arg(toQString(className))));
147 }
148 
KJS::JSValue * callAsFunction(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args) override
Executes the callback for this method.
const callMethod call
The callback for the method.
Implement QString-KJS::UString conversion methods.
bool hadException() const
StaticConstructor(KJS::ExecState *exec, const Constructor *constructor)
Create a new constructor.
const int argc
Number of arguments.
static KJS::JSObject * add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor)
Add the constructor to an object.
StaticBinding(KJS::ExecState *exec, const Method *method)
Create a new method.
KJS::JSObject * construct(KJS::ExecState *exec, const KJS::List &args) override
Calls the callback that will in turn create a new instance of this object with the arguments passed i...
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
Method structure.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.