| OLD | NEW |
| (Empty) |
| 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 "bindings/v8/V8NPUtils.h" | |
| 33 | |
| 34 #include "bindings/v8/NPV8Object.h" | |
| 35 #include "bindings/v8/V8Binding.h" | |
| 36 #include "bindings/v8/V8NPObject.h" | |
| 37 #include "bindings/v8/npruntime_impl.h" | |
| 38 #include "bindings/v8/npruntime_priv.h" | |
| 39 #include "core/frame/LocalDOMWindow.h" | |
| 40 #include "wtf/text/WTFString.h" | |
| 41 | |
| 42 #include <stdlib.h> | |
| 43 | |
| 44 namespace WebCore { | |
| 45 | |
| 46 void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NP
Variant* result, v8::Isolate* isolate) | |
| 47 { | |
| 48 VOID_TO_NPVARIANT(*result); | |
| 49 | |
| 50 // It is really the caller's responsibility to deal with the empty handle ca
se because there could be different actions to | |
| 51 // take in different contexts. | |
| 52 ASSERT(!object.IsEmpty()); | |
| 53 | |
| 54 if (object.IsEmpty()) | |
| 55 return; | |
| 56 | |
| 57 if (object->IsNumber()) | |
| 58 DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); | |
| 59 else if (object->IsBoolean()) | |
| 60 BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); | |
| 61 else if (object->IsNull()) | |
| 62 NULL_TO_NPVARIANT(*result); | |
| 63 else if (object->IsUndefined()) | |
| 64 VOID_TO_NPVARIANT(*result); | |
| 65 else if (object->IsString()) { | |
| 66 v8::Handle<v8::String> str = object.As<v8::String>(); | |
| 67 int length = str->Utf8Length() + 1; | |
| 68 char* utf8Chars = reinterpret_cast<char*>(malloc(length)); | |
| 69 str->WriteUtf8(utf8Chars, length, 0, v8::String::HINT_MANY_WRITES_EXPECT
ED); | |
| 70 STRINGN_TO_NPVARIANT(utf8Chars, length-1, *result); | |
| 71 } else if (object->IsObject()) { | |
| 72 LocalDOMWindow* window = currentDOMWindow(isolate); | |
| 73 NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::C
ast(object), window, isolate); | |
| 74 if (npobject) | |
| 75 _NPN_RegisterObject(npobject, owner); | |
| 76 OBJECT_TO_NPVARIANT(npobject, *result); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObj
ect* owner, v8::Isolate* isolate) | |
| 81 { | |
| 82 NPVariantType type = variant->type; | |
| 83 | |
| 84 switch (type) { | |
| 85 case NPVariantType_Int32: | |
| 86 return v8::Integer::New(isolate, NPVARIANT_TO_INT32(*variant)); | |
| 87 case NPVariantType_Double: | |
| 88 return v8::Number::New(isolate, NPVARIANT_TO_DOUBLE(*variant)); | |
| 89 case NPVariantType_Bool: | |
| 90 return v8Boolean(NPVARIANT_TO_BOOLEAN(*variant), isolate); | |
| 91 case NPVariantType_Null: | |
| 92 return v8::Null(isolate); | |
| 93 case NPVariantType_Void: | |
| 94 return v8::Undefined(isolate); | |
| 95 case NPVariantType_String: { | |
| 96 NPString src = NPVARIANT_TO_STRING(*variant); | |
| 97 return v8::String::NewFromUtf8(isolate, src.UTF8Characters, v8::String::
kNormalString, src.UTF8Length); | |
| 98 } | |
| 99 case NPVariantType_Object: { | |
| 100 NPObject* object = NPVARIANT_TO_OBJECT(*variant); | |
| 101 if (V8NPObject* v8Object = npObjectToV8NPObject(object)) | |
| 102 return v8::Local<v8::Object>::New(isolate, v8Object->v8Object); | |
| 103 return createV8ObjectForNPObject(object, owner, isolate); | |
| 104 } | |
| 105 default: | |
| 106 return v8::Undefined(isolate); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 // Helper function to create an NPN String Identifier from a v8 string. | |
| 111 NPIdentifier getStringIdentifier(v8::Handle<v8::String> str) | |
| 112 { | |
| 113 const int kStackBufferSize = 100; | |
| 114 | |
| 115 int bufferLength = str->Utf8Length() + 1; | |
| 116 if (bufferLength <= kStackBufferSize) { | |
| 117 // Use local stack buffer to avoid heap allocations for small strings. H
ere we should only use the stack space for | |
| 118 // stackBuffer when it's used, not when we use the heap. | |
| 119 // | |
| 120 // WriteUtf8 is guaranteed to generate a null-terminated string because
bufferLength is constructed to be one greater | |
| 121 // than the string length. | |
| 122 char stackBuffer[kStackBufferSize]; | |
| 123 str->WriteUtf8(stackBuffer, bufferLength); | |
| 124 return _NPN_GetStringIdentifier(stackBuffer); | |
| 125 } | |
| 126 | |
| 127 v8::String::Utf8Value utf8(str); | |
| 128 return _NPN_GetStringIdentifier(*utf8); | |
| 129 } | |
| 130 | |
| 131 struct ExceptionHandlerInfo { | |
| 132 ExceptionHandlerInfo* previous; | |
| 133 ExceptionHandler handler; | |
| 134 void* data; | |
| 135 }; | |
| 136 | |
| 137 static ExceptionHandlerInfo* topHandler; | |
| 138 | |
| 139 void pushExceptionHandler(ExceptionHandler handler, void* data) | |
| 140 { | |
| 141 ExceptionHandlerInfo* info = new ExceptionHandlerInfo; | |
| 142 info->previous = topHandler; | |
| 143 info->handler = handler; | |
| 144 info->data = data; | |
| 145 topHandler = info; | |
| 146 } | |
| 147 | |
| 148 void popExceptionHandler() | |
| 149 { | |
| 150 ASSERT(topHandler); | |
| 151 ExceptionHandlerInfo* doomed = topHandler; | |
| 152 topHandler = topHandler->previous; | |
| 153 delete doomed; | |
| 154 } | |
| 155 | |
| 156 ExceptionCatcher::ExceptionCatcher() | |
| 157 { | |
| 158 if (!topHandler) | |
| 159 m_tryCatch.SetVerbose(true); | |
| 160 } | |
| 161 | |
| 162 ExceptionCatcher::~ExceptionCatcher() | |
| 163 { | |
| 164 if (!m_tryCatch.HasCaught()) | |
| 165 return; | |
| 166 | |
| 167 if (topHandler) | |
| 168 topHandler->handler(topHandler->data, *v8::String::Utf8Value(m_tryCatch.
Exception())); | |
| 169 } | |
| 170 | |
| 171 } // namespace WebCore | |
| OLD | NEW |