1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ChromeWorkerScope.h"
10 #include "js/experimental/CTypes.h" // JS::InitCTypesClass, JS::CTypesCallbacks, JS::SetCTypesCallbacks
11 #include "js/MemoryFunctions.h"
13 #include "nsNativeCharsetUtils.h"
23 char* UnicodeToNative(JSContext
* aCx
, const char16_t
* aSource
,
25 nsDependentSubstring
unicode(aSource
, aSourceLen
);
28 if (NS_FAILED(NS_CopyUnicodeToNative(unicode
, native
))) {
29 JS_ReportErrorASCII(aCx
, "Could not convert string to native charset!");
33 char* result
= static_cast<char*>(JS_malloc(aCx
, native
.Length() + 1));
38 memcpy(result
, native
.get(), native
.Length());
39 result
[native
.Length()] = 0;
43 #endif // BUILD_CTYPES
47 bool DefineChromeWorkerFunctions(JSContext
* aCx
,
48 JS::Handle
<JSObject
*> aGlobal
) {
49 // Currently ctypes is the only special property given to ChromeWorkers.
52 JS::Rooted
<JS::Value
> ctypes(aCx
);
53 if (!JS::InitCTypesClass(aCx
, aGlobal
) ||
54 !JS_GetProperty(aCx
, aGlobal
, "ctypes", &ctypes
)) {
58 static const JS::CTypesCallbacks callbacks
= {UnicodeToNative
};
60 JS::SetCTypesCallbacks(ctypes
.toObjectOrNull(), &callbacks
);
62 #endif // BUILD_CTYPES
68 } // namespace mozilla