Bug 1835710 - Cancel off-thread JIT compilation before changing nursery allocation...
[gecko.git] / dom / workers / ChromeWorkerScope.cpp
blobe2ec3340160db319cd250f23a11990c91da4c683
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"
9 #include "jsapi.h"
10 #include "js/PropertyAndElement.h" // JS_GetProperty
11 #include "js/experimental/CTypes.h" // JS::InitCTypesClass, JS::CTypesCallbacks, JS::SetCTypesCallbacks
12 #include "js/MemoryFunctions.h"
14 #include "nsNativeCharsetUtils.h"
15 #include "nsString.h"
17 namespace mozilla::dom {
19 namespace {
21 #ifdef BUILD_CTYPES
23 char* UnicodeToNative(JSContext* aCx, const char16_t* aSource,
24 size_t aSourceLen) {
25 nsDependentSubstring unicode(aSource, aSourceLen);
27 nsAutoCString native;
28 if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
29 JS_ReportErrorASCII(aCx, "Could not convert string to native charset!");
30 return nullptr;
33 char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
34 if (!result) {
35 return nullptr;
38 memcpy(result, native.get(), native.Length());
39 result[native.Length()] = 0;
40 return result;
43 #endif // BUILD_CTYPES
45 } // namespace
47 bool DefineChromeWorkerFunctions(JSContext* aCx,
48 JS::Handle<JSObject*> aGlobal) {
49 // Currently ctypes is the only special property given to ChromeWorkers.
50 #ifdef BUILD_CTYPES
52 JS::Rooted<JS::Value> ctypes(aCx);
53 if (!JS::InitCTypesClass(aCx, aGlobal) ||
54 !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
55 return false;
58 static const JS::CTypesCallbacks callbacks = {UnicodeToNative};
60 JS::SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
62 #endif // BUILD_CTYPES
64 return true;
67 } // namespace mozilla::dom