|
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/. */ |
|
5 |
|
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" |
|
15 |
|
16 #define JSCTYPES_CONTRACTID \ |
|
17 "@mozilla.org/jsctypes;1" |
|
18 |
|
19 |
|
20 #define JSCTYPES_CID \ |
|
21 { 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } } |
|
22 |
|
23 namespace mozilla { |
|
24 namespace ctypes { |
|
25 |
|
26 static char* |
|
27 UnicodeToNative(JSContext *cx, const jschar *source, size_t slen) |
|
28 { |
|
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; |
|
35 } |
|
36 |
|
37 char* result = static_cast<char*>(JS_malloc(cx, native.Length() + 1)); |
|
38 if (!result) |
|
39 return nullptr; |
|
40 |
|
41 memcpy(result, native.get(), native.Length() + 1); |
|
42 return result; |
|
43 } |
|
44 |
|
45 static JSCTypesCallbacks sCallbacks = { |
|
46 UnicodeToNative |
|
47 }; |
|
48 |
|
49 NS_GENERIC_FACTORY_CONSTRUCTOR(Module) |
|
50 |
|
51 NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) |
|
52 |
|
53 Module::Module() |
|
54 { |
|
55 } |
|
56 |
|
57 Module::~Module() |
|
58 { |
|
59 } |
|
60 |
|
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" |
|
66 |
|
67 static bool |
|
68 SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name) |
|
69 { |
|
70 JS::Rooted<JS::Value> prop(cx); |
|
71 if (!JS_GetProperty(cx, parent, name, &prop)) |
|
72 return false; |
|
73 |
|
74 if (prop.isUndefined()) { |
|
75 // Pretend we sealed the object. |
|
76 return true; |
|
77 } |
|
78 |
|
79 JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull()); |
|
80 if (!JS_GetProperty(cx, obj, "prototype", &prop)) |
|
81 return false; |
|
82 |
|
83 JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull()); |
|
84 return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); |
|
85 } |
|
86 |
|
87 static bool |
|
88 InitAndSealCTypesClass(JSContext* cx, JS::Handle<JSObject*> global) |
|
89 { |
|
90 // Init the ctypes object. |
|
91 if (!JS_InitCTypesClass(cx, global)) |
|
92 return false; |
|
93 |
|
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; |
|
98 |
|
99 JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &sCallbacks); |
|
100 |
|
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; |
|
109 |
|
110 // Finally, seal the global object, for good measure. (But not recursively; |
|
111 // this breaks things.) |
|
112 return JS_FreezeObject(cx, global); |
|
113 } |
|
114 |
|
115 NS_IMETHODIMP |
|
116 Module::Call(nsIXPConnectWrappedNative* wrapper, |
|
117 JSContext* cx, |
|
118 JSObject* obj, |
|
119 const JS::CallArgs& args, |
|
120 bool* _retval) |
|
121 { |
|
122 mozJSComponentLoader* loader = mozJSComponentLoader::Get(); |
|
123 JS::Rooted<JSObject*> targetObj(cx); |
|
124 nsresult rv = loader->FindTargetObject(cx, &targetObj); |
|
125 NS_ENSURE_SUCCESS(rv, rv); |
|
126 |
|
127 *_retval = InitAndSealCTypesClass(cx, targetObj); |
|
128 return NS_OK; |
|
129 } |
|
130 |
|
131 } |
|
132 } |
|
133 |
|
134 NS_DEFINE_NAMED_CID(JSCTYPES_CID); |
|
135 |
|
136 static const mozilla::Module::CIDEntry kCTypesCIDs[] = { |
|
137 { &kJSCTYPES_CID, false, nullptr, mozilla::ctypes::ModuleConstructor }, |
|
138 { nullptr } |
|
139 }; |
|
140 |
|
141 static const mozilla::Module::ContractIDEntry kCTypesContracts[] = { |
|
142 { JSCTYPES_CONTRACTID, &kJSCTYPES_CID }, |
|
143 { nullptr } |
|
144 }; |
|
145 |
|
146 static const mozilla::Module kCTypesModule = { |
|
147 mozilla::Module::kVersion, |
|
148 kCTypesCIDs, |
|
149 kCTypesContracts |
|
150 }; |
|
151 |
|
152 NSMODULE_DEFN(jsctypes) = &kCTypesModule; |