]>
git.saurik.com Git - apple/javascriptcore.git/blob - bindings/c/c_utility.cpp
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if ENABLE(NETSCAPE_API)
31 #include "c_utility.h"
33 #include "NP_jsobject.h"
34 #include "c_instance.h"
35 #include "JSGlobalObject.h"
36 #include "npruntime_impl.h"
37 #include "npruntime_priv.h"
38 #include "runtime_object.h"
39 #include "runtime_root.h"
41 #include <wtf/Assertions.h>
42 #include <wtf/unicode/UTF8.h>
44 using namespace WTF::Unicode
;
46 namespace KJS
{ namespace Bindings
{
48 // Requires free() of returned UTF16Chars.
49 static void convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8
* UTF8Chars
, int UTF8Length
, NPUTF16
** UTF16Chars
, unsigned int* UTF16Length
)
51 ASSERT(UTF8Chars
|| UTF8Length
== 0);
55 UTF8Length
= static_cast<int>(strlen(UTF8Chars
));
57 *UTF16Length
= UTF8Length
;
58 *UTF16Chars
= static_cast<NPUTF16
*>(malloc(sizeof(NPUTF16
) * (*UTF16Length
)));
60 const char* sourcestart
= UTF8Chars
;
61 const char* sourceend
= sourcestart
+ UTF8Length
;
63 ::UChar
* targetstart
= reinterpret_cast< ::UChar
*>(*UTF16Chars
);
64 ::UChar
* targetend
= targetstart
+ UTF8Length
;
66 ConversionResult result
= convertUTF8ToUTF16(&sourcestart
, sourceend
, &targetstart
, targetend
);
68 *UTF16Length
= targetstart
- reinterpret_cast< ::UChar
*>(*UTF16Chars
);
70 // Check to see if the conversion was successful
71 // Some plugins return invalid UTF-8 in NPVariantType_String, see <http://bugs.webkit.org/show_bug.cgi?id=5163>
72 // There is no "bad data" for latin1. It is unlikely that the plugin was really sending text in this encoding,
73 // but it should have used UTF-8, and now we are simply avoiding a crash.
74 if (result
!= conversionOK
) {
75 *UTF16Length
= UTF8Length
;
77 if (!*UTF16Chars
) // If the memory wasn't allocated, allocate it.
78 *UTF16Chars
= (NPUTF16
*)malloc(sizeof(NPUTF16
) * (*UTF16Length
));
80 for (unsigned i
= 0; i
< *UTF16Length
; i
++)
81 (*UTF16Chars
)[i
] = UTF8Chars
[i
] & 0xFF;
85 // Variant value must be released with NPReleaseVariantValue()
86 void convertValueToNPVariant(ExecState
*exec
, JSValue
*value
, NPVariant
*result
)
90 JSType type
= value
->type();
92 VOID_TO_NPVARIANT(*result
);
94 if (type
== StringType
) {
95 UString ustring
= value
->toString(exec
);
96 CString cstring
= ustring
.UTF8String();
97 NPString string
= { (const NPUTF8
*)cstring
.c_str(), static_cast<uint32_t>(cstring
.size()) };
98 NPN_InitializeVariantWithStringCopy(result
, &string
);
99 } else if (type
== NumberType
) {
100 DOUBLE_TO_NPVARIANT(value
->toNumber(exec
), *result
);
101 } else if (type
== BooleanType
) {
102 BOOLEAN_TO_NPVARIANT(value
->toBoolean(exec
), *result
);
103 } else if (type
== UnspecifiedType
) {
104 VOID_TO_NPVARIANT(*result
);
105 } else if (type
== NullType
) {
106 NULL_TO_NPVARIANT(*result
);
107 } else if (type
== ObjectType
) {
108 JSObject
* object
= static_cast<JSObject
*>(value
);
109 if (object
->classInfo() == &RuntimeObjectImp::info
) {
110 RuntimeObjectImp
* imp
= static_cast<RuntimeObjectImp
*>(value
);
111 CInstance
* instance
= static_cast<CInstance
*>(imp
->getInternalInstance());
113 NPObject
* obj
= instance
->getObject();
114 _NPN_RetainObject(obj
);
115 OBJECT_TO_NPVARIANT(obj
, *result
);
118 JSGlobalObject
* globalObject
= exec
->dynamicGlobalObject();
120 RootObject
* rootObject
= findRootObject(globalObject
);
122 NPObject
* npObject
= _NPN_CreateScriptObject(0, object
, rootObject
);
123 OBJECT_TO_NPVARIANT(npObject
, *result
);
129 JSValue
*convertNPVariantToValue(ExecState
*, const NPVariant
* variant
, RootObject
* rootObject
)
133 NPVariantType type
= variant
->type
;
135 if (type
== NPVariantType_Bool
)
136 return jsBoolean(NPVARIANT_TO_BOOLEAN(*variant
));
137 if (type
== NPVariantType_Null
)
139 if (type
== NPVariantType_Void
)
140 return jsUndefined();
141 if (type
== NPVariantType_Int32
)
142 return jsNumber(NPVARIANT_TO_INT32(*variant
));
143 if (type
== NPVariantType_Double
)
144 return jsNumber(NPVARIANT_TO_DOUBLE(*variant
));
145 if (type
== NPVariantType_String
) {
146 NPUTF16
*stringValue
;
147 unsigned int UTF16Length
;
148 convertNPStringToUTF16(&variant
->value
.stringValue
, &stringValue
, &UTF16Length
); // requires free() of returned memory
149 UString
resultString((const UChar
*)stringValue
,UTF16Length
);
151 return jsString(resultString
);
153 if (type
== NPVariantType_Object
) {
154 NPObject
*obj
= variant
->value
.objectValue
;
156 if (obj
->_class
== NPScriptObjectClass
)
157 // Get JSObject from NP_JavaScriptObject.
158 return ((JavaScriptObject
*)obj
)->imp
;
160 // Wrap NPObject in a CInstance.
161 return Instance::createRuntimeObject(Instance::CLanguage
, obj
, rootObject
);
164 return jsUndefined();
167 // Requires free() of returned UTF16Chars.
168 void convertNPStringToUTF16(const NPString
*string
, NPUTF16
**UTF16Chars
, unsigned int *UTF16Length
)
170 convertUTF8ToUTF16WithLatin1Fallback(string
->UTF8Characters
, string
->UTF8Length
, UTF16Chars
, UTF16Length
);
173 Identifier
identifierFromNPIdentifier(const NPUTF8
* name
)
176 unsigned UTF16Length
;
177 convertUTF8ToUTF16WithLatin1Fallback(name
, -1, &methodName
, &UTF16Length
); // requires free() of returned memory.
178 Identifier
identifier((const KJS::UChar
*)methodName
, UTF16Length
);
185 #endif // ENABLE(NETSCAPE_API)