michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ctypes.h" michael@0: #include "jsapi.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "nsMemory.h" michael@0: #include "nsString.h" michael@0: #include "nsNativeCharsetUtils.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozJSComponentLoader.h" michael@0: #include "nsZipArchive.h" michael@0: michael@0: #define JSCTYPES_CONTRACTID \ michael@0: "@mozilla.org/jsctypes;1" michael@0: michael@0: michael@0: #define JSCTYPES_CID \ michael@0: { 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } } michael@0: michael@0: namespace mozilla { michael@0: namespace ctypes { michael@0: michael@0: static char* michael@0: UnicodeToNative(JSContext *cx, const jschar *source, size_t slen) michael@0: { michael@0: nsAutoCString native; michael@0: nsDependentString unicode(reinterpret_cast(source), slen); michael@0: nsresult rv = NS_CopyUnicodeToNative(unicode, native); michael@0: if (NS_FAILED(rv)) { michael@0: JS_ReportError(cx, "could not convert string to native charset"); michael@0: return nullptr; michael@0: } michael@0: michael@0: char* result = static_cast(JS_malloc(cx, native.Length() + 1)); michael@0: if (!result) michael@0: return nullptr; michael@0: michael@0: memcpy(result, native.get(), native.Length() + 1); michael@0: return result; michael@0: } michael@0: michael@0: static JSCTypesCallbacks sCallbacks = { michael@0: UnicodeToNative michael@0: }; michael@0: michael@0: NS_GENERIC_FACTORY_CONSTRUCTOR(Module) michael@0: michael@0: NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) michael@0: michael@0: Module::Module() michael@0: { michael@0: } michael@0: michael@0: Module::~Module() michael@0: { michael@0: } michael@0: michael@0: #define XPC_MAP_CLASSNAME Module michael@0: #define XPC_MAP_QUOTED_CLASSNAME "Module" michael@0: #define XPC_MAP_WANT_CALL michael@0: #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL michael@0: #include "xpc_map_end.h" michael@0: michael@0: static bool michael@0: SealObjectAndPrototype(JSContext* cx, JS::Handle parent, const char* name) michael@0: { michael@0: JS::Rooted prop(cx); michael@0: if (!JS_GetProperty(cx, parent, name, &prop)) michael@0: return false; michael@0: michael@0: if (prop.isUndefined()) { michael@0: // Pretend we sealed the object. michael@0: return true; michael@0: } michael@0: michael@0: JS::Rooted obj(cx, prop.toObjectOrNull()); michael@0: if (!JS_GetProperty(cx, obj, "prototype", &prop)) michael@0: return false; michael@0: michael@0: JS::Rooted prototype(cx, prop.toObjectOrNull()); michael@0: return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); michael@0: } michael@0: michael@0: static bool michael@0: InitAndSealCTypesClass(JSContext* cx, JS::Handle global) michael@0: { michael@0: // Init the ctypes object. michael@0: if (!JS_InitCTypesClass(cx, global)) michael@0: return false; michael@0: michael@0: // Set callbacks for charset conversion and such. michael@0: JS::Rooted ctypes(cx); michael@0: if (!JS_GetProperty(cx, global, "ctypes", &ctypes)) michael@0: return false; michael@0: michael@0: JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &sCallbacks); michael@0: michael@0: // Seal up Object, Function, Array and Error and their prototypes. (This michael@0: // single object instance is shared amongst everyone who imports the ctypes michael@0: // module.) michael@0: if (!SealObjectAndPrototype(cx, global, "Object") || michael@0: !SealObjectAndPrototype(cx, global, "Function") || michael@0: !SealObjectAndPrototype(cx, global, "Array") || michael@0: !SealObjectAndPrototype(cx, global, "Error")) michael@0: return false; michael@0: michael@0: // Finally, seal the global object, for good measure. (But not recursively; michael@0: // this breaks things.) michael@0: return JS_FreezeObject(cx, global); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: Module::Call(nsIXPConnectWrappedNative* wrapper, michael@0: JSContext* cx, michael@0: JSObject* obj, michael@0: const JS::CallArgs& args, michael@0: bool* _retval) michael@0: { michael@0: mozJSComponentLoader* loader = mozJSComponentLoader::Get(); michael@0: JS::Rooted targetObj(cx); michael@0: nsresult rv = loader->FindTargetObject(cx, &targetObj); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *_retval = InitAndSealCTypesClass(cx, targetObj); michael@0: return NS_OK; michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: NS_DEFINE_NAMED_CID(JSCTYPES_CID); michael@0: michael@0: static const mozilla::Module::CIDEntry kCTypesCIDs[] = { michael@0: { &kJSCTYPES_CID, false, nullptr, mozilla::ctypes::ModuleConstructor }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module::ContractIDEntry kCTypesContracts[] = { michael@0: { JSCTYPES_CONTRACTID, &kJSCTYPES_CID }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module kCTypesModule = { michael@0: mozilla::Module::kVersion, michael@0: kCTypesCIDs, michael@0: kCTypesContracts michael@0: }; michael@0: michael@0: NSMODULE_DEFN(jsctypes) = &kCTypesModule;