#include "config.h"
#include "c_utility.h"
#include "NP_jsobject.h"
#include "c_instance.h"
#include "npruntime_impl.h"
#include "npruntime_priv.h"
#include "runtime_object.h"
#include "runtime_root.h"
#include <unicode/ucnv.h>
namespace KJS { namespace Bindings {
void convertNPStringToUTF16(const NPString *string, NPUTF16 **UTF16Chars, unsigned int *UTF16Length)
{
convertUTF8ToUTF16(string->UTF8Characters, string->UTF8Length, UTF16Chars, UTF16Length);
}
void convertUTF8ToUTF16(const NPUTF8 *UTF8Chars, int UTF8Length, NPUTF16 **UTF16Chars, unsigned int *UTF16Length)
{
assert(UTF8Chars || UTF8Length == 0);
assert(UTF16Chars);
if (UTF8Length == -1)
UTF8Length = strlen(UTF8Chars);
*UTF16Length = UTF8Length + 1;
*UTF16Chars = 0;
UErrorCode status = U_ZERO_ERROR;
UConverter* conv = ucnv_open("utf8", &status);
if (U_SUCCESS(status)) {
*UTF16Chars = (NPUTF16 *)malloc(sizeof(NPUTF16) * (*UTF16Length));
ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_STOP, 0, 0, 0, &status);
*UTF16Length = ucnv_toUChars(conv, *UTF16Chars, *UTF16Length, UTF8Chars, UTF8Length, &status);
ucnv_close(conv);
}
if (!U_SUCCESS(status)) {
*UTF16Length = UTF8Length;
if (!*UTF16Chars) *UTF16Chars = (NPUTF16 *)malloc(sizeof(NPUTF16) * (*UTF16Length));
for (unsigned i = 0; i < *UTF16Length; i++)
(*UTF16Chars)[i] = UTF8Chars[i] & 0xFF;
}
}
void coerceValueToNPVariantStringType(ExecState *exec, JSValue *value, NPVariant *result)
{
UString ustring = value->toString(exec);
CString cstring = ustring.UTF8String();
NPString string = { (const NPUTF8 *)cstring.c_str(), cstring.size() };
NPN_InitializeVariantWithStringCopy(result, &string);
}
void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result)
{
JSType type = value->type();
if (type == StringType) {
UString ustring = value->toString(exec);
CString cstring = ustring.UTF8String();
NPString string = { (const NPUTF8 *)cstring.c_str(), cstring.size() };
NPN_InitializeVariantWithStringCopy(result, &string);
} else if (type == NumberType) {
DOUBLE_TO_NPVARIANT(value->toNumber(exec), *result);
} else if (type == BooleanType) {
BOOLEAN_TO_NPVARIANT(value->toBoolean(exec), *result);
} else if (type == UnspecifiedType) {
VOID_TO_NPVARIANT(*result);
} else if (type == NullType) {
NULL_TO_NPVARIANT(*result);
} else if (type == ObjectType) {
JSObject *objectImp = static_cast<JSObject*>(value);
if (objectImp->classInfo() == &RuntimeObjectImp::info) {
RuntimeObjectImp* imp = static_cast<RuntimeObjectImp *>(value);
CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance());
NPObject* obj = instance->getObject();
_NPN_RetainObject(obj);
OBJECT_TO_NPVARIANT(obj, *result);
} else {
Interpreter *originInterpreter = exec->dynamicInterpreter();
const Bindings::RootObject *originExecutionContext = rootForInterpreter(originInterpreter);
Interpreter *interpreter = 0;
if (originInterpreter->isGlobalObject(value)) {
interpreter = originInterpreter->interpreterForGlobalObject(value);
}
if (!interpreter)
interpreter = originInterpreter;
const Bindings::RootObject *executionContext = rootForInterpreter(interpreter);
if (!executionContext) {
Bindings::RootObject *newExecutionContext = new Bindings::RootObject(0);
newExecutionContext->setInterpreter(interpreter);
executionContext = newExecutionContext;
}
NPObject* obj = (NPObject *)exec->dynamicInterpreter()->createLanguageInstanceForValue(exec, Instance::CLanguage, value->toObject(exec), originExecutionContext, executionContext);
OBJECT_TO_NPVARIANT(obj, *result);
}
}
else
VOID_TO_NPVARIANT(*result);
}
JSValue *convertNPVariantToValue(ExecState*, const NPVariant* variant)
{
NPVariantType type = variant->type;
if (type == NPVariantType_Bool)
return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant));
if (type == NPVariantType_Null)
return jsNull();
if (type == NPVariantType_Void)
return jsUndefined();
if (type == NPVariantType_Int32)
return jsNumber(NPVARIANT_TO_INT32(*variant));
if (type == NPVariantType_Double)
return jsNumber(NPVARIANT_TO_DOUBLE(*variant));
if (type == NPVariantType_String) {
NPUTF16 *stringValue;
unsigned int UTF16Length;
convertNPStringToUTF16(&variant->value.stringValue, &stringValue, &UTF16Length); UString resultString((const UChar *)stringValue,UTF16Length);
free(stringValue);
return jsString(resultString);
}
if (type == NPVariantType_Object) {
NPObject *obj = variant->value.objectValue;
if (obj->_class == NPScriptObjectClass)
return ((JavaScriptObject *)obj)->imp;
return Instance::createRuntimeObject(Instance::CLanguage, obj);
}
return jsUndefined();
}
} }