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 "bindings/core/v8/V8NPUtils.h" | |
32 | |
33 #include "bindings/core/v8/NPV8Object.h" | |
34 #include "bindings/core/v8/V8Binding.h" | |
35 #include "bindings/core/v8/V8NPObject.h" | |
36 #include "bindings/core/v8/npruntime_impl.h" | |
37 #include "bindings/core/v8/npruntime_priv.h" | |
38 #include "core/frame/LocalDOMWindow.h" | |
39 #include "wtf/text/WTFString.h" | |
40 | |
41 #include <stdlib.h> | |
42 | |
43 namespace blink { | |
44 | |
45 void convertV8ObjectToNPVariant(v8::Isolate* isolate, v8::Local<v8::Value> objec
t, 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 ca
se 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.As<v8::Number>()->Value(), *result); | |
58 } else if (object->IsBoolean()) { | |
59 BOOLEAN_TO_NPVARIANT(object.As<v8::Boolean>()->Value(), *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::Local<v8::String> str = object.As<v8::String>(); | |
66 int length = str->Utf8Length() + 1; | |
67 char* utf8Chars = reinterpret_cast<char*>(malloc(length)); | |
68 str->WriteUtf8(utf8Chars, length, 0, v8::String::HINT_MANY_WRITES_EXPECT
ED); | |
69 STRINGN_TO_NPVARIANT(utf8Chars, length-1, *result); | |
70 } else if (object->IsObject()) { | |
71 LocalDOMWindow* window = currentDOMWindow(isolate); | |
72 NPObject* npobject = npCreateV8ScriptObject(isolate, 0, v8::Local<v8::Ob
ject>::Cast(object), window); | |
73 if (npobject) | |
74 _NPN_RegisterObject(npobject, owner); | |
75 OBJECT_TO_NPVARIANT(npobject, *result); | |
76 } | |
77 } | |
78 | |
79 v8::Local<v8::Value> convertNPVariantToV8Object(v8::Isolate* isolate, const NPVa
riant* variant, NPObject* owner) | |
80 { | |
81 NPVariantType type = variant->type; | |
82 | |
83 switch (type) { | |
84 case NPVariantType_Int32: | |
85 return v8::Integer::New(isolate, NPVARIANT_TO_INT32(*variant)); | |
86 case NPVariantType_Double: | |
87 return v8::Number::New(isolate, NPVARIANT_TO_DOUBLE(*variant)); | |
88 case NPVariantType_Bool: | |
89 return v8Boolean(NPVARIANT_TO_BOOLEAN(*variant), isolate); | |
90 case NPVariantType_Null: | |
91 return v8::Null(isolate); | |
92 case NPVariantType_Void: | |
93 return v8::Undefined(isolate); | |
94 case NPVariantType_String: { | |
95 NPString src = NPVARIANT_TO_STRING(*variant); | |
96 return v8AtomicString(isolate, src.UTF8Characters, src.UTF8Length); | |
97 } | |
98 case NPVariantType_Object: { | |
99 NPObject* object = NPVARIANT_TO_OBJECT(*variant); | |
100 if (V8NPObject* v8Object = npObjectToV8NPObject(object)) | |
101 return v8::Local<v8::Object>::New(isolate, v8Object->v8Object); | |
102 return createV8ObjectForNPObject(isolate, object, owner); | |
103 } | |
104 default: | |
105 return v8::Undefined(isolate); | |
106 } | |
107 } | |
108 | |
109 // Helper function to create an NPN String Identifier from a v8 string. | |
110 NPIdentifier getStringIdentifier(v8::Local<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. H
ere 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 | |
130 } // namespace blink | |
OLD | NEW |