gecko/js/ctypes/Library.cpp
2010-03-31 08:21:07 -07:00

287 lines
8.5 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is js-ctypes.
*
* The Initial Developer of the Original Code is
* The Mozilla Foundation <http://www.mozilla.org/>.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mark Finkle <mark.finkle@gmail.com>, <mfinkle@mozilla.com>
* Fredrik Larsson <nossralf@gmail.com>
* Dan Witte <dwitte@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "jscntxt.h"
#include "Library.h"
#include "CTypes.h"
#include "nsServiceManagerUtils.h"
#include "nsIXPConnect.h"
#include "nsILocalFile.h"
#include "nsNativeCharsetUtils.h"
namespace mozilla {
namespace ctypes {
/*******************************************************************************
** JSAPI function prototypes
*******************************************************************************/
namespace Library
{
static void Finalize(JSContext* cx, JSObject* obj);
static JSBool Close(JSContext* cx, uintN argc, jsval* vp);
static JSBool Declare(JSContext* cx, uintN argc, jsval* vp);
}
/*******************************************************************************
** JSObject implementation
*******************************************************************************/
static JSClass sLibraryClass = {
"Library",
JSCLASS_HAS_RESERVED_SLOTS(LIBRARY_SLOTS) | JSCLASS_MARK_IS_TRACE,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub,JS_ResolveStub, JS_ConvertStub, Library::Finalize,
JSCLASS_NO_OPTIONAL_MEMBERS
};
#define CTYPESFN_FLAGS \
(JSFUN_FAST_NATIVE | JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
static JSFunctionSpec sLibraryFunctions[] = {
JS_FN("close", Library::Close, 0, CTYPESFN_FLAGS),
JS_FN("declare", Library::Declare, 0, CTYPESFN_FLAGS),
JS_FS_END
};
JSObject*
Library::Create(JSContext* cx, jsval aPath)
{
JSObject* libraryObj = JS_NewObject(cx, &sLibraryClass, NULL, NULL);
if (!libraryObj)
return NULL;
js::AutoValueRooter root(cx, libraryObj);
// initialize the library
if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY, PRIVATE_TO_JSVAL(NULL)))
return NULL;
// attach API functions
if (!JS_DefineFunctions(cx, libraryObj, sLibraryFunctions))
return NULL;
nsresult rv;
PRLibrary* library;
// get the path argument. we accept either an nsILocalFile or a string path.
// determine which we have...
if (JSVAL_IS_STRING(aPath)) {
const PRUnichar* path = reinterpret_cast<const PRUnichar*>(
JS_GetStringCharsZ(cx, JSVAL_TO_STRING(aPath)));
if (!path)
return NULL;
// We don't use nsILocalFile, because it doesn't use the system search
// rules when resolving library path.
PRLibSpec libSpec;
#ifdef XP_WIN
// On Windows, converting to native charset may corrupt path string.
// So, we have to use Unicode path directly.
libSpec.value.pathname_u = path;
libSpec.type = PR_LibSpec_PathnameU;
#else
nsCAutoString nativePath;
NS_CopyUnicodeToNative(nsDependentString(path), nativePath);
libSpec.value.pathname = nativePath.get();
libSpec.type = PR_LibSpec_Pathname;
#endif
library = PR_LoadLibraryWithFlags(libSpec, 0);
if (!library) {
JS_ReportError(cx, "couldn't open library");
return NULL;
}
} else if (!JSVAL_IS_PRIMITIVE(aPath)) {
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
nsISupports* file = xpc->GetNativeOfWrapper(cx, JSVAL_TO_OBJECT(aPath));
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file);
if (!localFile) {
JS_ReportError(cx, "open takes a string or nsILocalFile argument");
return NULL;
}
rv = localFile->Load(&library);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "couldn't open library");
return NULL;
}
} else {
// don't convert the argument
JS_ReportError(cx, "open takes a string or nsIFile argument");
return NULL;
}
// stash the library
if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY,
PRIVATE_TO_JSVAL(library)))
return NULL;
return libraryObj;
}
bool
Library::IsLibrary(JSContext* cx, JSObject* obj)
{
return JS_GET_CLASS(cx, obj) == &sLibraryClass;
}
PRLibrary*
Library::GetLibrary(JSContext* cx, JSObject* obj)
{
JS_ASSERT(IsLibrary(cx, obj));
jsval slot;
JS_GetReservedSlot(cx, obj, SLOT_LIBRARY, &slot);
return static_cast<PRLibrary*>(JSVAL_TO_PRIVATE(slot));
}
void
Library::Finalize(JSContext* cx, JSObject* obj)
{
// unload the library
PRLibrary* library = GetLibrary(cx, obj);
if (library)
PR_UnloadLibrary(library);
}
JSBool
Library::Open(JSContext* cx, uintN argc, jsval *vp)
{
if (argc != 1 || JSVAL_IS_VOID(JS_ARGV(cx, vp)[0])) {
JS_ReportError(cx, "open requires a single argument");
return JS_FALSE;
}
JSObject* library = Create(cx, JS_ARGV(cx, vp)[0]);
if (!library)
return JS_FALSE;
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(library));
return JS_TRUE;
}
JSBool
Library::Close(JSContext* cx, uintN argc, jsval* vp)
{
JSObject* obj = JS_THIS_OBJECT(cx, vp);
if (!IsLibrary(cx, obj)) {
JS_ReportError(cx, "not a library");
return JS_FALSE;
}
if (argc != 0) {
JS_ReportError(cx, "close doesn't take any arguments");
return JS_FALSE;
}
// delete our internal objects
Finalize(cx, obj);
JS_SetReservedSlot(cx, obj, SLOT_LIBRARY, PRIVATE_TO_JSVAL(NULL));
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JSBool
Library::Declare(JSContext* cx, uintN argc, jsval* vp)
{
JSObject* obj = JS_THIS_OBJECT(cx, vp);
if (!IsLibrary(cx, obj)) {
JS_ReportError(cx, "not a library");
return JS_FALSE;
}
PRLibrary* library = GetLibrary(cx, obj);
if (!library) {
JS_ReportError(cx, "library not open");
return JS_FALSE;
}
// we always need at least a method name, a call type and a return type
if (argc < 3) {
JS_ReportError(cx, "declare requires at least three arguments");
return JS_FALSE;
}
jsval* argv = JS_ARGV(cx, vp);
if (!JSVAL_IS_STRING(argv[0])) {
JS_ReportError(cx, "first argument must be a string");
return JS_FALSE;
}
const char* name = JS_GetStringBytesZ(cx, JSVAL_TO_STRING(argv[0]));
if (!name)
return JS_FALSE;
PRFuncPtr func = PR_FindFunctionSymbol(library, name);
if (!func) {
JS_ReportError(cx, "couldn't find function symbol in library");
return JS_FALSE;
}
// Create a FunctionType representing the function.
JSObject* typeObj = FunctionType::CreateInternal(cx,
argv[1], argv[2], &argv[3], argc - 3);
if (!typeObj)
return JS_FALSE;
js::AutoValueRooter root(cx, typeObj);
JSObject* fn = CData::Create(cx, typeObj, obj, &func, true);
if (!fn)
return JS_FALSE;
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(fn));
// Seal the CData object, to prevent modification of the function pointer.
// This permanently associates this object with the library, and avoids
// having to do things like reset SLOT_REFERENT when someone tries to
// change the pointer value.
// XXX This will need to change when bug 541212 is fixed -- CData::ValueSetter
// could be called on a sealed object.
return JS_SealObject(cx, fn, JS_FALSE);
}
}
}