toolkit/components/ctypes/ctypes.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ctypes.h"
michael@0 7 #include "jsapi.h"
michael@0 8 #include "mozilla/ModuleUtils.h"
michael@0 9 #include "nsMemory.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsNativeCharsetUtils.h"
michael@0 12 #include "mozilla/Preferences.h"
michael@0 13 #include "mozJSComponentLoader.h"
michael@0 14 #include "nsZipArchive.h"
michael@0 15
michael@0 16 #define JSCTYPES_CONTRACTID \
michael@0 17 "@mozilla.org/jsctypes;1"
michael@0 18
michael@0 19
michael@0 20 #define JSCTYPES_CID \
michael@0 21 { 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } }
michael@0 22
michael@0 23 namespace mozilla {
michael@0 24 namespace ctypes {
michael@0 25
michael@0 26 static char*
michael@0 27 UnicodeToNative(JSContext *cx, const jschar *source, size_t slen)
michael@0 28 {
michael@0 29 nsAutoCString native;
michael@0 30 nsDependentString unicode(reinterpret_cast<const char16_t*>(source), slen);
michael@0 31 nsresult rv = NS_CopyUnicodeToNative(unicode, native);
michael@0 32 if (NS_FAILED(rv)) {
michael@0 33 JS_ReportError(cx, "could not convert string to native charset");
michael@0 34 return nullptr;
michael@0 35 }
michael@0 36
michael@0 37 char* result = static_cast<char*>(JS_malloc(cx, native.Length() + 1));
michael@0 38 if (!result)
michael@0 39 return nullptr;
michael@0 40
michael@0 41 memcpy(result, native.get(), native.Length() + 1);
michael@0 42 return result;
michael@0 43 }
michael@0 44
michael@0 45 static JSCTypesCallbacks sCallbacks = {
michael@0 46 UnicodeToNative
michael@0 47 };
michael@0 48
michael@0 49 NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
michael@0 50
michael@0 51 NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
michael@0 52
michael@0 53 Module::Module()
michael@0 54 {
michael@0 55 }
michael@0 56
michael@0 57 Module::~Module()
michael@0 58 {
michael@0 59 }
michael@0 60
michael@0 61 #define XPC_MAP_CLASSNAME Module
michael@0 62 #define XPC_MAP_QUOTED_CLASSNAME "Module"
michael@0 63 #define XPC_MAP_WANT_CALL
michael@0 64 #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
michael@0 65 #include "xpc_map_end.h"
michael@0 66
michael@0 67 static bool
michael@0 68 SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name)
michael@0 69 {
michael@0 70 JS::Rooted<JS::Value> prop(cx);
michael@0 71 if (!JS_GetProperty(cx, parent, name, &prop))
michael@0 72 return false;
michael@0 73
michael@0 74 if (prop.isUndefined()) {
michael@0 75 // Pretend we sealed the object.
michael@0 76 return true;
michael@0 77 }
michael@0 78
michael@0 79 JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull());
michael@0 80 if (!JS_GetProperty(cx, obj, "prototype", &prop))
michael@0 81 return false;
michael@0 82
michael@0 83 JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull());
michael@0 84 return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
michael@0 85 }
michael@0 86
michael@0 87 static bool
michael@0 88 InitAndSealCTypesClass(JSContext* cx, JS::Handle<JSObject*> global)
michael@0 89 {
michael@0 90 // Init the ctypes object.
michael@0 91 if (!JS_InitCTypesClass(cx, global))
michael@0 92 return false;
michael@0 93
michael@0 94 // Set callbacks for charset conversion and such.
michael@0 95 JS::Rooted<JS::Value> ctypes(cx);
michael@0 96 if (!JS_GetProperty(cx, global, "ctypes", &ctypes))
michael@0 97 return false;
michael@0 98
michael@0 99 JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &sCallbacks);
michael@0 100
michael@0 101 // Seal up Object, Function, Array and Error and their prototypes. (This
michael@0 102 // single object instance is shared amongst everyone who imports the ctypes
michael@0 103 // module.)
michael@0 104 if (!SealObjectAndPrototype(cx, global, "Object") ||
michael@0 105 !SealObjectAndPrototype(cx, global, "Function") ||
michael@0 106 !SealObjectAndPrototype(cx, global, "Array") ||
michael@0 107 !SealObjectAndPrototype(cx, global, "Error"))
michael@0 108 return false;
michael@0 109
michael@0 110 // Finally, seal the global object, for good measure. (But not recursively;
michael@0 111 // this breaks things.)
michael@0 112 return JS_FreezeObject(cx, global);
michael@0 113 }
michael@0 114
michael@0 115 NS_IMETHODIMP
michael@0 116 Module::Call(nsIXPConnectWrappedNative* wrapper,
michael@0 117 JSContext* cx,
michael@0 118 JSObject* obj,
michael@0 119 const JS::CallArgs& args,
michael@0 120 bool* _retval)
michael@0 121 {
michael@0 122 mozJSComponentLoader* loader = mozJSComponentLoader::Get();
michael@0 123 JS::Rooted<JSObject*> targetObj(cx);
michael@0 124 nsresult rv = loader->FindTargetObject(cx, &targetObj);
michael@0 125 NS_ENSURE_SUCCESS(rv, rv);
michael@0 126
michael@0 127 *_retval = InitAndSealCTypesClass(cx, targetObj);
michael@0 128 return NS_OK;
michael@0 129 }
michael@0 130
michael@0 131 }
michael@0 132 }
michael@0 133
michael@0 134 NS_DEFINE_NAMED_CID(JSCTYPES_CID);
michael@0 135
michael@0 136 static const mozilla::Module::CIDEntry kCTypesCIDs[] = {
michael@0 137 { &kJSCTYPES_CID, false, nullptr, mozilla::ctypes::ModuleConstructor },
michael@0 138 { nullptr }
michael@0 139 };
michael@0 140
michael@0 141 static const mozilla::Module::ContractIDEntry kCTypesContracts[] = {
michael@0 142 { JSCTYPES_CONTRACTID, &kJSCTYPES_CID },
michael@0 143 { nullptr }
michael@0 144 };
michael@0 145
michael@0 146 static const mozilla::Module kCTypesModule = {
michael@0 147 mozilla::Module::kVersion,
michael@0 148 kCTypesCIDs,
michael@0 149 kCTypesContracts
michael@0 150 };
michael@0 151
michael@0 152 NSMODULE_DEFN(jsctypes) = &kCTypesModule;

mercurial