This source file includes following definitions.
- convertV8ObjectToNPVariant
- convertNPVariantToV8Object
- getStringIdentifier
- pushExceptionHandler
- popExceptionHandler
#include "config.h"
#include "bindings/v8/V8NPUtils.h"
#include "bindings/v8/NPV8Object.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8NPObject.h"
#include "bindings/v8/npruntime_impl.h"
#include "bindings/v8/npruntime_priv.h"
#include "core/frame/DOMWindow.h"
#include "wtf/text/WTFString.h"
#include <stdlib.h>
namespace WebCore {
void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NPVariant* result, v8::Isolate* isolate)
{
VOID_TO_NPVARIANT(*result);
ASSERT(!object.IsEmpty());
if (object.IsEmpty())
return;
if (object->IsNumber())
DOUBLE_TO_NPVARIANT(object->NumberValue(), *result);
else if (object->IsBoolean())
BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result);
else if (object->IsNull())
NULL_TO_NPVARIANT(*result);
else if (object->IsUndefined())
VOID_TO_NPVARIANT(*result);
else if (object->IsString()) {
v8::Handle<v8::String> str = object.As<v8::String>();
int length = str->Utf8Length() + 1;
char* utf8Chars = reinterpret_cast<char*>(malloc(length));
str->WriteUtf8(utf8Chars, length, 0, v8::String::HINT_MANY_WRITES_EXPECTED);
STRINGN_TO_NPVARIANT(utf8Chars, length-1, *result);
} else if (object->IsObject()) {
DOMWindow* window = currentDOMWindow(isolate);
NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(object), window, isolate);
if (npobject)
_NPN_RegisterObject(npobject, owner);
OBJECT_TO_NPVARIANT(npobject, *result);
}
}
v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObject* owner, v8::Isolate* isolate)
{
NPVariantType type = variant->type;
switch (type) {
case NPVariantType_Int32:
return v8::Integer::New(isolate, NPVARIANT_TO_INT32(*variant));
case NPVariantType_Double:
return v8::Number::New(isolate, NPVARIANT_TO_DOUBLE(*variant));
case NPVariantType_Bool:
return v8Boolean(NPVARIANT_TO_BOOLEAN(*variant), isolate);
case NPVariantType_Null:
return v8::Null(isolate);
case NPVariantType_Void:
return v8::Undefined(isolate);
case NPVariantType_String: {
NPString src = NPVARIANT_TO_STRING(*variant);
return v8::String::NewFromUtf8(isolate, src.UTF8Characters, v8::String::kNormalString, src.UTF8Length);
}
case NPVariantType_Object: {
NPObject* object = NPVARIANT_TO_OBJECT(*variant);
if (V8NPObject* v8Object = npObjectToV8NPObject(object))
return v8::Local<v8::Object>::New(isolate, v8Object->v8Object);
return createV8ObjectForNPObject(object, owner, isolate);
}
default:
return v8::Undefined(isolate);
}
}
NPIdentifier getStringIdentifier(v8::Handle<v8::String> str)
{
const int kStackBufferSize = 100;
int bufferLength = str->Utf8Length() + 1;
if (bufferLength <= kStackBufferSize) {
char stackBuffer[kStackBufferSize];
str->WriteUtf8(stackBuffer, bufferLength);
return _NPN_GetStringIdentifier(stackBuffer);
}
v8::String::Utf8Value utf8(str);
return _NPN_GetStringIdentifier(*utf8);
}
struct ExceptionHandlerInfo {
ExceptionHandlerInfo* previous;
ExceptionHandler handler;
void* data;
};
static ExceptionHandlerInfo* topHandler;
void pushExceptionHandler(ExceptionHandler handler, void* data)
{
ExceptionHandlerInfo* info = new ExceptionHandlerInfo;
info->previous = topHandler;
info->handler = handler;
info->data = data;
topHandler = info;
}
void popExceptionHandler()
{
ASSERT(topHandler);
ExceptionHandlerInfo* doomed = topHandler;
topHandler = topHandler->previous;
delete doomed;
}
ExceptionCatcher::ExceptionCatcher()
{
if (!topHandler)
m_tryCatch.SetVerbose(true);
}
ExceptionCatcher::~ExceptionCatcher()
{
if (!m_tryCatch.HasCaught())
return;
if (topHandler)
topHandler->handler(topHandler->data, *v8::String::Utf8Value(m_tryCatch.Exception()));
}
}