Merge mozilla-b2g34 to 2.1s. a=merge
[gecko.git] / toolkit / components / ctypes / ctypes.cpp
blobba050d61c48d2dbcd401408a89a5a7680aa8022f
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/. */
6 #include "ctypes.h"
7 #include "jsapi.h"
8 #include "mozilla/ModuleUtils.h"
9 #include "nsMemory.h"
10 #include "nsString.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 } }
23 namespace mozilla {
24 namespace ctypes {
26 static char*
27 UnicodeToNative(JSContext *cx, const jschar *source, size_t slen)
29 nsAutoCString native;
30 nsDependentString unicode(reinterpret_cast<const char16_t*>(source), slen);
31 nsresult rv = NS_CopyUnicodeToNative(unicode, native);
32 if (NS_FAILED(rv)) {
33 JS_ReportError(cx, "could not convert string to native charset");
34 return nullptr;
37 char* result = static_cast<char*>(JS_malloc(cx, native.Length() + 1));
38 if (!result)
39 return nullptr;
41 memcpy(result, native.get(), native.Length() + 1);
42 return result;
45 static JSCTypesCallbacks sCallbacks = {
46 UnicodeToNative
49 NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
51 NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
53 Module::Module()
57 Module::~Module()
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"
67 static bool
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))
72 return false;
74 if (prop.isUndefined()) {
75 // Pretend we sealed the object.
76 return true;
79 JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull());
80 if (!JS_GetProperty(cx, obj, "prototype", &prop))
81 return false;
83 JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull());
84 return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
87 static bool
88 InitAndSealCTypesClass(JSContext* cx, JS::Handle<JSObject*> global)
90 // Init the ctypes object.
91 if (!JS_InitCTypesClass(cx, global))
92 return false;
94 // Set callbacks for charset conversion and such.
95 JS::Rooted<JS::Value> ctypes(cx);
96 if (!JS_GetProperty(cx, global, "ctypes", &ctypes))
97 return false;
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
103 // module.)
104 if (!SealObjectAndPrototype(cx, global, "Object") ||
105 !SealObjectAndPrototype(cx, global, "Function") ||
106 !SealObjectAndPrototype(cx, global, "Array") ||
107 !SealObjectAndPrototype(cx, global, "Error"))
108 return false;
110 // Finally, seal the global object, for good measure. (But not recursively;
111 // this breaks things.)
112 return JS_FreezeObject(cx, global);
115 NS_IMETHODIMP
116 Module::Call(nsIXPConnectWrappedNative* wrapper,
117 JSContext* cx,
118 JSObject* obj,
119 const JS::CallArgs& args,
120 bool* _retval)
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);
128 return NS_OK;
134 NS_DEFINE_NAMED_CID(JSCTYPES_CID);
136 static const mozilla::Module::CIDEntry kCTypesCIDs[] = {
137 { &kJSCTYPES_CID, false, nullptr, mozilla::ctypes::ModuleConstructor },
138 { nullptr }
141 static const mozilla::Module::ContractIDEntry kCTypesContracts[] = {
142 { JSCTYPES_CONTRACTID, &kJSCTYPES_CID },
143 { nullptr }
146 static const mozilla::Module kCTypesModule = {
147 mozilla::Module::kVersion,
148 kCTypesCIDs,
149 kCTypesContracts
152 NSMODULE_DEFN(jsctypes) = &kCTypesModule;