1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/ModuleUtils.h"
11 #include "nsNativeCharsetUtils.h"
12 #include "mozilla/Preferences.h"
13 #include "mozJSComponentLoader.h"
14 #include "nsZipArchive.h"
16 #define JSCTYPES_CONTRACTID \
17 "@mozilla.org/jsctypes;1"
20 #define JSCTYPES_CID \
21 { 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } }
27 UnicodeToNative(JSContext
*cx
, const jschar
*source
, size_t slen
)
30 nsDependentString
unicode(reinterpret_cast<const char16_t
*>(source
), slen
);
31 nsresult rv
= NS_CopyUnicodeToNative(unicode
, native
);
33 JS_ReportError(cx
, "could not convert string to native charset");
37 char* result
= static_cast<char*>(JS_malloc(cx
, native
.Length() + 1));
41 memcpy(result
, native
.get(), native
.Length() + 1);
45 static JSCTypesCallbacks sCallbacks
= {
49 NS_GENERIC_FACTORY_CONSTRUCTOR(Module
)
51 NS_IMPL_ISUPPORTS(Module
, nsIXPCScriptable
)
61 #define XPC_MAP_CLASSNAME Module
62 #define XPC_MAP_QUOTED_CLASSNAME "Module"
63 #define XPC_MAP_WANT_CALL
64 #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
65 #include "xpc_map_end.h"
68 SealObjectAndPrototype(JSContext
* cx
, JS::Handle
<JSObject
*> parent
, const char* name
)
70 JS::Rooted
<JS::Value
> prop(cx
);
71 if (!JS_GetProperty(cx
, parent
, name
, &prop
))
74 if (prop
.isUndefined()) {
75 // Pretend we sealed the object.
79 JS::Rooted
<JSObject
*> obj(cx
, prop
.toObjectOrNull());
80 if (!JS_GetProperty(cx
, obj
, "prototype", &prop
))
83 JS::Rooted
<JSObject
*> prototype(cx
, prop
.toObjectOrNull());
84 return JS_FreezeObject(cx
, obj
) && JS_FreezeObject(cx
, prototype
);
88 InitAndSealCTypesClass(JSContext
* cx
, JS::Handle
<JSObject
*> global
)
90 // Init the ctypes object.
91 if (!JS_InitCTypesClass(cx
, global
))
94 // Set callbacks for charset conversion and such.
95 JS::Rooted
<JS::Value
> ctypes(cx
);
96 if (!JS_GetProperty(cx
, global
, "ctypes", &ctypes
))
99 JS_SetCTypesCallbacks(ctypes
.toObjectOrNull(), &sCallbacks
);
101 // Seal up Object, Function, Array and Error and their prototypes. (This
102 // single object instance is shared amongst everyone who imports the ctypes
104 if (!SealObjectAndPrototype(cx
, global
, "Object") ||
105 !SealObjectAndPrototype(cx
, global
, "Function") ||
106 !SealObjectAndPrototype(cx
, global
, "Array") ||
107 !SealObjectAndPrototype(cx
, global
, "Error"))
110 // Finally, seal the global object, for good measure. (But not recursively;
111 // this breaks things.)
112 return JS_FreezeObject(cx
, global
);
116 Module::Call(nsIXPConnectWrappedNative
* wrapper
,
119 const JS::CallArgs
& args
,
122 mozJSComponentLoader
* loader
= mozJSComponentLoader::Get();
123 JS::Rooted
<JSObject
*> targetObj(cx
);
124 nsresult rv
= loader
->FindTargetObject(cx
, &targetObj
);
125 NS_ENSURE_SUCCESS(rv
, rv
);
127 *_retval
= InitAndSealCTypesClass(cx
, targetObj
);
134 NS_DEFINE_NAMED_CID(JSCTYPES_CID
);
136 static const mozilla::Module::CIDEntry kCTypesCIDs
[] = {
137 { &kJSCTYPES_CID
, false, nullptr, mozilla::ctypes::ModuleConstructor
},
141 static const mozilla::Module::ContractIDEntry kCTypesContracts
[] = {
142 { JSCTYPES_CONTRACTID
, &kJSCTYPES_CID
},
146 static const mozilla::Module kCTypesModule
= {
147 mozilla::Module::kVersion
,
152 NSMODULE_DEFN(jsctypes
) = &kCTypesModule
;