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/MemoryFunctions.h"
13 #include "nsNativeCharsetUtils.h"
16 #include "WorkerPrivate.h"
25 char* UnicodeToNative(JSContext
* aCx
, const char16_t
* aSource
,
27 nsDependentString
unicode(aSource
, aSourceLen
);
30 if (NS_FAILED(NS_CopyUnicodeToNative(unicode
, native
))) {
31 JS_ReportErrorASCII(aCx
, "Could not convert string to native charset!");
35 char* result
= static_cast<char*>(JS_malloc(aCx
, native
.Length() + 1));
40 memcpy(result
, native
.get(), native
.Length());
41 result
[native
.Length()] = 0;
45 #endif // BUILD_CTYPES
49 bool DefineChromeWorkerFunctions(JSContext
* aCx
,
50 JS::Handle
<JSObject
*> aGlobal
) {
51 // Currently ctypes is the only special property given to ChromeWorkers.
54 JS::Rooted
<JS::Value
> ctypes(aCx
);
55 if (!JS_InitCTypesClass(aCx
, aGlobal
) ||
56 !JS_GetProperty(aCx
, aGlobal
, "ctypes", &ctypes
)) {
60 static const JSCTypesCallbacks callbacks
= {UnicodeToNative
};
62 JS_SetCTypesCallbacks(ctypes
.toObjectOrNull(), &callbacks
);
64 #endif // BUILD_CTYPES
70 } // namespace mozilla