1/* 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "V8NPUtils.h" 33 34#include "DOMWindow.h" 35#include "Frame.h" 36#include "PlatformString.h" 37#include "npruntime_impl.h" 38#include "npruntime_priv.h" 39#include "NPV8Object.h" 40#include "V8NPObject.h" 41#include "V8Proxy.h" 42 43namespace WebCore { 44 45void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NPVariant* result) 46{ 47 VOID_TO_NPVARIANT(*result); 48 49 // It is really the caller's responsibility to deal with the empty handle case because there could be different actions to 50 // take in different contexts. 51 ASSERT(!object.IsEmpty()); 52 53 if (object.IsEmpty()) 54 return; 55 56 if (object->IsNumber()) 57 DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); 58 else if (object->IsBoolean()) 59 BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); 60 else if (object->IsNull()) 61 NULL_TO_NPVARIANT(*result); 62 else if (object->IsUndefined()) 63 VOID_TO_NPVARIANT(*result); 64 else if (object->IsString()) { 65 v8::String::Utf8Value utf8(object); 66 int length = utf8.length() + 1; 67 char* utf8Chars = reinterpret_cast<char*>(malloc(length)); 68 memcpy(utf8Chars, *utf8, length); 69 STRINGN_TO_NPVARIANT(utf8Chars, utf8.length(), *result); 70 } else if (object->IsObject()) { 71 DOMWindow* window = V8Proxy::retrieveWindow(V8Proxy::currentContext()); 72 NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(object), window); 73 if (npobject) 74 _NPN_RegisterObject(npobject, owner); 75 OBJECT_TO_NPVARIANT(npobject, *result); 76 } 77} 78 79v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObject* npobject) 80{ 81 NPVariantType type = variant->type; 82 83 switch (type) { 84 case NPVariantType_Int32: 85 return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); 86 case NPVariantType_Double: 87 return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); 88 case NPVariantType_Bool: 89 return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); 90 case NPVariantType_Null: 91 return v8::Null(); 92 case NPVariantType_Void: 93 return v8::Undefined(); 94 case NPVariantType_String: { 95 NPString src = NPVARIANT_TO_STRING(*variant); 96 return v8::String::New(src.UTF8Characters, src.UTF8Length); 97 } 98 case NPVariantType_Object: { 99 NPObject* obj = NPVARIANT_TO_OBJECT(*variant); 100 if (obj->_class == npScriptObjectClass) 101 return reinterpret_cast<V8NPObject*>(obj)->v8Object; 102 return createV8ObjectForNPObject(obj, npobject); 103 } 104 default: 105 return v8::Undefined(); 106 } 107} 108 109// Helper function to create an NPN String Identifier from a v8 string. 110NPIdentifier getStringIdentifier(v8::Handle<v8::String> str) 111{ 112 const int kStackBufferSize = 100; 113 114 int bufferLength = str->Utf8Length() + 1; 115 if (bufferLength <= kStackBufferSize) { 116 // Use local stack buffer to avoid heap allocations for small strings. Here we should only use the stack space for 117 // stackBuffer when it's used, not when we use the heap. 118 // 119 // WriteUtf8 is guaranteed to generate a null-terminated string because bufferLength is constructed to be one greater 120 // than the string length. 121 char stackBuffer[kStackBufferSize]; 122 str->WriteUtf8(stackBuffer, bufferLength); 123 return _NPN_GetStringIdentifier(stackBuffer); 124 } 125 126 v8::String::Utf8Value utf8(str); 127 return _NPN_GetStringIdentifier(*utf8); 128} 129 130struct ExceptionHandlerInfo { 131 ExceptionHandlerInfo* previous; 132 ExceptionHandler handler; 133 void* data; 134}; 135 136static ExceptionHandlerInfo* topHandler; 137 138void pushExceptionHandler(ExceptionHandler handler, void* data) 139{ 140 ExceptionHandlerInfo* info = new ExceptionHandlerInfo; 141 info->previous = topHandler; 142 info->handler = handler; 143 info->data = data; 144 topHandler = info; 145} 146 147void popExceptionHandler() 148{ 149 ASSERT(topHandler); 150 ExceptionHandlerInfo* doomed = topHandler; 151 topHandler = topHandler->previous; 152 delete doomed; 153} 154 155ExceptionCatcher::ExceptionCatcher() 156{ 157 if (!topHandler) 158 m_tryCatch.SetVerbose(true); 159} 160 161ExceptionCatcher::~ExceptionCatcher() 162{ 163 if (!m_tryCatch.HasCaught()) 164 return; 165 166 if (topHandler) 167 topHandler->handler(topHandler->data, *v8::String::Utf8Value(m_tryCatch.Exception())); 168} 169 170} // namespace WebCore 171