toolkit/components/ctypes/ctypes.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/ctypes/ctypes.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,152 @@
     1.4 +/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "ctypes.h"
    1.10 +#include "jsapi.h"
    1.11 +#include "mozilla/ModuleUtils.h"
    1.12 +#include "nsMemory.h"
    1.13 +#include "nsString.h"
    1.14 +#include "nsNativeCharsetUtils.h"
    1.15 +#include "mozilla/Preferences.h"
    1.16 +#include "mozJSComponentLoader.h"
    1.17 +#include "nsZipArchive.h"
    1.18 +
    1.19 +#define JSCTYPES_CONTRACTID \
    1.20 +  "@mozilla.org/jsctypes;1"
    1.21 +
    1.22 +
    1.23 +#define JSCTYPES_CID \
    1.24 +{ 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } }
    1.25 +
    1.26 +namespace mozilla {
    1.27 +namespace ctypes {
    1.28 +
    1.29 +static char*
    1.30 +UnicodeToNative(JSContext *cx, const jschar *source, size_t slen)
    1.31 +{
    1.32 +  nsAutoCString native;
    1.33 +  nsDependentString unicode(reinterpret_cast<const char16_t*>(source), slen);
    1.34 +  nsresult rv = NS_CopyUnicodeToNative(unicode, native);
    1.35 +  if (NS_FAILED(rv)) {
    1.36 +    JS_ReportError(cx, "could not convert string to native charset");
    1.37 +    return nullptr;
    1.38 +  }
    1.39 +
    1.40 +  char* result = static_cast<char*>(JS_malloc(cx, native.Length() + 1));
    1.41 +  if (!result)
    1.42 +    return nullptr;
    1.43 +
    1.44 +  memcpy(result, native.get(), native.Length() + 1);
    1.45 +  return result;
    1.46 +}
    1.47 +
    1.48 +static JSCTypesCallbacks sCallbacks = {
    1.49 +  UnicodeToNative
    1.50 +};
    1.51 +
    1.52 +NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
    1.53 +
    1.54 +NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
    1.55 +
    1.56 +Module::Module()
    1.57 +{
    1.58 +}
    1.59 +
    1.60 +Module::~Module()
    1.61 +{
    1.62 +}
    1.63 +
    1.64 +#define XPC_MAP_CLASSNAME Module
    1.65 +#define XPC_MAP_QUOTED_CLASSNAME "Module"
    1.66 +#define XPC_MAP_WANT_CALL
    1.67 +#define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
    1.68 +#include "xpc_map_end.h"
    1.69 +
    1.70 +static bool
    1.71 +SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name)
    1.72 +{
    1.73 +  JS::Rooted<JS::Value> prop(cx);
    1.74 +  if (!JS_GetProperty(cx, parent, name, &prop))
    1.75 +    return false;
    1.76 +
    1.77 +  if (prop.isUndefined()) {
    1.78 +    // Pretend we sealed the object.
    1.79 +    return true;
    1.80 +  }
    1.81 +
    1.82 +  JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull());
    1.83 +  if (!JS_GetProperty(cx, obj, "prototype", &prop))
    1.84 +    return false;
    1.85 +
    1.86 +  JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull());
    1.87 +  return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
    1.88 +}
    1.89 +
    1.90 +static bool
    1.91 +InitAndSealCTypesClass(JSContext* cx, JS::Handle<JSObject*> global)
    1.92 +{
    1.93 +  // Init the ctypes object.
    1.94 +  if (!JS_InitCTypesClass(cx, global))
    1.95 +    return false;
    1.96 +
    1.97 +  // Set callbacks for charset conversion and such.
    1.98 +  JS::Rooted<JS::Value> ctypes(cx);
    1.99 +  if (!JS_GetProperty(cx, global, "ctypes", &ctypes))
   1.100 +    return false;
   1.101 +
   1.102 +  JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &sCallbacks);
   1.103 +
   1.104 +  // Seal up Object, Function, Array and Error and their prototypes.  (This
   1.105 +  // single object instance is shared amongst everyone who imports the ctypes
   1.106 +  // module.)
   1.107 +  if (!SealObjectAndPrototype(cx, global, "Object") ||
   1.108 +      !SealObjectAndPrototype(cx, global, "Function") ||
   1.109 +      !SealObjectAndPrototype(cx, global, "Array") ||
   1.110 +      !SealObjectAndPrototype(cx, global, "Error"))
   1.111 +    return false;
   1.112 +
   1.113 +  // Finally, seal the global object, for good measure. (But not recursively;
   1.114 +  // this breaks things.)
   1.115 +  return JS_FreezeObject(cx, global);
   1.116 +}
   1.117 +
   1.118 +NS_IMETHODIMP
   1.119 +Module::Call(nsIXPConnectWrappedNative* wrapper,
   1.120 +             JSContext* cx,
   1.121 +             JSObject* obj,
   1.122 +             const JS::CallArgs& args,
   1.123 +             bool* _retval)
   1.124 +{
   1.125 +  mozJSComponentLoader* loader = mozJSComponentLoader::Get();
   1.126 +  JS::Rooted<JSObject*> targetObj(cx);
   1.127 +  nsresult rv = loader->FindTargetObject(cx, &targetObj);
   1.128 +  NS_ENSURE_SUCCESS(rv, rv);
   1.129 +
   1.130 +  *_retval = InitAndSealCTypesClass(cx, targetObj);
   1.131 +  return NS_OK;
   1.132 +}
   1.133 +
   1.134 +}
   1.135 +}
   1.136 +
   1.137 +NS_DEFINE_NAMED_CID(JSCTYPES_CID);
   1.138 +
   1.139 +static const mozilla::Module::CIDEntry kCTypesCIDs[] = {
   1.140 +  { &kJSCTYPES_CID, false, nullptr, mozilla::ctypes::ModuleConstructor },
   1.141 +  { nullptr }
   1.142 +};
   1.143 +
   1.144 +static const mozilla::Module::ContractIDEntry kCTypesContracts[] = {
   1.145 +  { JSCTYPES_CONTRACTID, &kJSCTYPES_CID },
   1.146 +  { nullptr }
   1.147 +};
   1.148 +
   1.149 +static const mozilla::Module kCTypesModule = {
   1.150 +  mozilla::Module::kVersion,
   1.151 +  kCTypesCIDs,
   1.152 +  kCTypesContracts
   1.153 +};
   1.154 +
   1.155 +NSMODULE_DEFN(jsctypes) = &kCTypesModule;

mercurial