| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2008, Google Inc. | |
| 2 // 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 #include "config.h" | |
| 31 | |
| 32 #include "V8NPUtils.h" | |
| 33 | |
| 34 #include "DOMWindow.h" | |
| 35 #include "Frame.h" | |
| 36 #include "PlatformString.h" | |
| 37 #undef LOG | |
| 38 | |
| 39 #include "npruntime_priv.h" | |
| 40 #include "NPV8Object.h" | |
| 41 #include "V8NPObject.h" | |
| 42 #include "v8_proxy.h" | |
| 43 | |
| 44 void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, NP
Variant* result) | |
| 45 { | |
| 46 VOID_TO_NPVARIANT(*result); | |
| 47 | |
| 48 // It is really the caller's responsibility to deal with the empty handle | |
| 49 // case because there could be different actions to take in different | |
| 50 // contexts. | |
| 51 ASSERT(!object.IsEmpty()); | |
| 52 | |
| 53 if (object.IsEmpty()) | |
| 54 return; | |
| 55 | |
| 56 if (object->IsInt32()) | |
| 57 INT32_TO_NPVARIANT(object->NumberValue(), *result); | |
| 58 else if (object->IsNumber()) | |
| 59 DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); | |
| 60 else if (object->IsBoolean()) | |
| 61 BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); | |
| 62 else if (object->IsNull()) | |
| 63 NULL_TO_NPVARIANT(*result); | |
| 64 else if (object->IsUndefined()) | |
| 65 VOID_TO_NPVARIANT(*result); | |
| 66 else if (object->IsString()) { | |
| 67 v8::String::Utf8Value utf8(object); | |
| 68 char* utf8_chars = strdup(*utf8); | |
| 69 STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result); | |
| 70 } else if (object->IsObject()) { | |
| 71 WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(); | |
| 72 NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::C
ast(object), window); | |
| 73 if (npobject) | |
| 74 _NPN_RegisterObject(npobject, owner); | |
| 75 OBJECT_TO_NPVARIANT(npobject, *result); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 | |
| 80 v8::Handle<v8::Value> convertNPVariantToV8Object(const NPVariant* variant, NPObj
ect* npobject) | |
| 81 { | |
| 82 NPVariantType type = variant->type; | |
| 83 | |
| 84 if (type == NPVariantType_Int32) | |
| 85 return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); | |
| 86 if (type == NPVariantType_Double) | |
| 87 return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); | |
| 88 if (type == NPVariantType_Bool) | |
| 89 return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); | |
| 90 if (type == NPVariantType_Null) | |
| 91 return v8::Null(); | |
| 92 if (type == NPVariantType_Void) | |
| 93 return v8::Undefined(); | |
| 94 if (type == NPVariantType_String) { | |
| 95 NPString src = NPVARIANT_TO_STRING(*variant); | |
| 96 return v8::String::New(src.UTF8Characters, src.UTF8Length); | |
| 97 } | |
| 98 if (type == 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 return v8::Undefined(); | |
| 105 } | |
| 106 | |
| 107 // Helper function to create an NPN String Identifier from a v8 string. | |
| 108 NPIdentifier getStringIdentifier(v8::Handle<v8::String> str) | |
| 109 { | |
| 110 const int kStackBufSize = 100; | |
| 111 | |
| 112 int bufLen = str->Length() + 1; | |
| 113 if (bufLen <= kStackBufSize) { | |
| 114 // Use local stack buffer to avoid heap allocations for small strings. | |
| 115 // Here we should only use the stack space for stack_buf when it's used, | |
| 116 // not when we use the heap. | |
| 117 char stackBuf[kStackBufSize]; | |
| 118 str->WriteAscii(stackBuf); | |
| 119 return NPN_GetStringIdentifier(stackBuf); | |
| 120 } | |
| 121 | |
| 122 v8::String::AsciiValue ascii(str); | |
| 123 return NPN_GetStringIdentifier(*ascii); | |
| 124 } | |
| OLD | NEW |