#include <c_class.h>
#include <c_instance.h>
#include <c_runtime.h>
#include <c_utility.h>
#include <JavaScriptCore/npruntime_impl.h>
#include <JavaScriptCore/npruntime_priv.h>
#ifdef NDEBUG
#define C_LOG(formatAndArgs...) ((void)0)
#else
#define C_LOG(formatAndArgs...) { \
fprintf (stderr, "%s:%d -- %s: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, formatAndArgs); \
}
#endif
using namespace KJS::Bindings;
using namespace KJS;
CInstance::CInstance (NPObject *o)
{
_object = _NPN_RetainObject (o);
_class = 0;
setExecutionContext (0);
};
CInstance::~CInstance ()
{
_NPN_ReleaseObject (_object);
delete _class;
}
CInstance::CInstance (const CInstance &other) : Instance()
{
_object = _NPN_RetainObject (other._object);
_class = 0;
setExecutionContext (other.executionContext());
};
CInstance &CInstance::operator=(const CInstance &other){
if (this == &other)
return *this;
NPObject *_oldObject = _object;
_object= _NPN_RetainObject (other._object);
_NPN_ReleaseObject (_oldObject);
_class = 0;
return *this;
};
Class *CInstance::getClass() const
{
if (!_class) {
_class = new CClass (_object->_class);
}
return _class;
}
void CInstance::begin()
{
}
void CInstance::end()
{
}
Value CInstance::invokeMethod (KJS::ExecState *exec, const MethodList &methodList, const List &args)
{
Value resultValue;
assert (methodList.length() == 1);
CMethod *method = 0;
method = static_cast<CMethod*>(methodList.methodAt(0));
NPIdentifier ident = _NPN_GetStringIdentifier (method->name());
if (!_object->_class->hasMethod (_object, ident)) {
return Undefined();
}
unsigned i, count = args.size();
NPVariant *cArgs;
NPVariant localBuffer[128];
if (count > 128)
cArgs = (NPVariant *)malloc (sizeof(NPVariant)*count);
else
cArgs = localBuffer;
for (i = 0; i < count; i++) {
convertValueToNPVariant (exec, args.at(i), &cArgs[i]);
}
NPVariant resultVariant;
VOID_TO_NPVARIANT(resultVariant);
_object->_class->invoke (_object, ident, cArgs, count, &resultVariant);
for (i = 0; i < count; i++) {
_NPN_ReleaseVariantValue (&cArgs[i]);
}
if (cArgs != localBuffer)
free ((void *)cArgs);
if (!NPVARIANT_IS_VOID(resultVariant)) {
resultValue = convertNPVariantToValue (exec, &resultVariant);
_NPN_ReleaseVariantValue (&resultVariant);
return resultValue;
}
return Undefined();
}
Value CInstance::invokeDefaultMethod (KJS::ExecState *exec, const List &args)
{
Value resultValue;
if (_object->_class->invokeDefault) {
unsigned i, count = args.size();
NPVariant *cArgs;
NPVariant localBuffer[128];
if (count > 128)
cArgs = (NPVariant *)malloc (sizeof(NPVariant)*count);
else
cArgs = localBuffer;
for (i = 0; i < count; i++) {
convertValueToNPVariant (exec, args.at(i), &cArgs[i]);
}
NPVariant resultVariant;
VOID_TO_NPVARIANT(resultVariant);
_object->_class->invokeDefault (_object, cArgs, count, &resultVariant);
for (i = 0; i < count; i++) {
_NPN_ReleaseVariantValue (&cArgs[i]);
}
if (cArgs != localBuffer)
free ((void *)cArgs);
if (!NPVARIANT_IS_VOID(resultVariant)) {
resultValue = convertNPVariantToValue (exec, &resultVariant);
_NPN_ReleaseVariantValue (&resultVariant);
return resultValue;
}
}
return Undefined();
}
KJS::Value CInstance::defaultValue (KJS::Type hint) const
{
if (hint == KJS::StringType) {
return stringValue();
}
else if (hint == KJS::NumberType) {
return numberValue();
}
else if (hint == KJS::BooleanType) {
return booleanValue();
}
return valueOf();
}
KJS::Value CInstance::stringValue() const
{
char buf[1024];
snprintf (buf, 1024, "NPObject %p, NPClass %p", _object, _object->_class);
KJS::String v(buf);
return v;
}
KJS::Value CInstance::numberValue() const
{
KJS::Number v(0);
return v;
}
KJS::Value CInstance::booleanValue() const
{
KJS::Boolean v((bool)0);
return v;
}
KJS::Value CInstance::valueOf() const
{
return stringValue();
};