2009-09-23 10:57:22 -07:00
|
|
|
/* -*- 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>
|
2009-09-23 10:57:22 -07:00
|
|
|
* Fredrik Larsson <nossralf@gmail.com>
|
2009-09-23 10:57:22 -07:00
|
|
|
* 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 ***** */
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
#include "jscntxt.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
#include "Library.h"
|
2010-03-25 15:00:42 -07:00
|
|
|
#include "CTypes.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2009-10-06 12:16:38 -07:00
|
|
|
#include "nsIXPConnect.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
#include "nsILocalFile.h"
|
2009-10-18 04:11:52 -07:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ctypes {
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
** JSObject implementation
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
static JSClass sLibraryClass = {
|
|
|
|
"Library",
|
2010-03-11 17:17:36 -08:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(LIBRARY_SLOTS) | JSCLASS_MARK_IS_TRACE,
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub,JS_ResolveStub, JS_ConvertStub, Library::Finalize,
|
2010-03-11 17:17:36 -08:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, JS_CLASS_TRACE(Library::Trace), NULL
|
2009-10-06 12:16:38 -07:00
|
|
|
};
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
#define CTYPESFN_FLAGS \
|
|
|
|
(JSFUN_FAST_NATIVE | JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
static JSFunctionSpec sLibraryFunctions[] = {
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_FN("close", Library::Close, 0, CTYPESFN_FLAGS),
|
|
|
|
JS_FN("declare", Library::Declare, 0, CTYPESFN_FLAGS),
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
Library::Create(JSContext* cx, jsval aPath)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
JSObject* libraryObj = JS_NewObject(cx, &sLibraryClass, NULL, NULL);
|
|
|
|
if (!libraryObj)
|
|
|
|
return NULL;
|
2010-03-11 17:17:36 -08:00
|
|
|
JSAutoTempValueRooter root(cx, libraryObj);
|
2009-10-06 12:16:38 -07:00
|
|
|
|
2009-10-13 20:57:42 -07:00
|
|
|
// initialize the library
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY, PRIVATE_TO_JSVAL(NULL)))
|
2009-10-13 20:57:42 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// initialize our Function list to empty
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JS_SetReservedSlot(cx, libraryObj, SLOT_FUNCTIONLIST,
|
|
|
|
PRIVATE_TO_JSVAL(NULL)))
|
2009-10-13 20:57:42 -07:00
|
|
|
return NULL;
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
// attach API functions
|
|
|
|
if (!JS_DefineFunctions(cx, libraryObj, sLibraryFunctions))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
nsresult rv;
|
2009-10-18 04:11:52 -07:00
|
|
|
PRLibrary* library;
|
2009-10-06 12:16:38 -07:00
|
|
|
|
|
|
|
// get the path argument. we accept either an nsILocalFile or a string path.
|
|
|
|
// determine which we have...
|
|
|
|
if (JSVAL_IS_STRING(aPath)) {
|
2010-03-11 17:17:36 -08:00
|
|
|
const PRUnichar* path = reinterpret_cast<const PRUnichar*>(
|
|
|
|
JS_GetStringCharsZ(cx, JSVAL_TO_STRING(aPath)));
|
2009-10-06 12:16:38 -07:00
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
// We don't use nsILocalFile, because it doesn't use the system search
|
|
|
|
// rules when resolving library path.
|
2009-10-18 04:11:52 -07:00
|
|
|
PRLibSpec libSpec;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// On Windows, converting to native charset may corrupt path string.
|
|
|
|
// So, we have to use Unicode path directly.
|
2010-03-11 17:17:36 -08:00
|
|
|
libSpec.value.pathname_u = path;
|
2009-10-18 04:11:52 -07:00
|
|
|
libSpec.type = PR_LibSpec_PathnameU;
|
|
|
|
#else
|
|
|
|
nsCAutoString nativePath;
|
2010-03-11 17:17:36 -08:00
|
|
|
NS_CopyUnicodeToNative(nsDependentString(path), nativePath);
|
2009-10-18 04:11:52 -07:00
|
|
|
libSpec.value.pathname = nativePath.get();
|
|
|
|
libSpec.type = PR_LibSpec_Pathname;
|
|
|
|
#endif
|
|
|
|
library = PR_LoadLibraryWithFlags(libSpec, 0);
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!library) {
|
|
|
|
JS_ReportError(cx, "couldn't open library");
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
2010-03-11 17:17:36 -08:00
|
|
|
}
|
2009-10-06 12:16:38 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
} else if (!JSVAL_IS_PRIMITIVE(aPath)) {
|
2009-10-06 12:16:38 -07:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
|
|
|
|
|
|
|
|
nsISupports* file = xpc->GetNativeOfWrapper(cx, JSVAL_TO_OBJECT(aPath));
|
2009-10-18 04:11:52 -07:00
|
|
|
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file);
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!localFile) {
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_ReportError(cx, "open takes a string or nsILocalFile argument");
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
2010-03-11 17:17:36 -08:00
|
|
|
}
|
2009-10-06 12:16:38 -07:00
|
|
|
|
2009-10-18 04:11:52 -07:00
|
|
|
rv = localFile->Load(&library);
|
2010-03-11 17:17:36 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_ReportError(cx, "couldn't open library");
|
2009-10-18 04:11:52 -07:00
|
|
|
return NULL;
|
2010-03-11 17:17:36 -08:00
|
|
|
}
|
2009-10-18 04:11:52 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
} else {
|
|
|
|
// don't convert the argument
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_ReportError(cx, "open takes a string or nsIFile argument");
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
// stash the library
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY,
|
|
|
|
PRIVATE_TO_JSVAL(library)))
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return libraryObj;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
PRLibrary*
|
|
|
|
Library::GetLibrary(JSContext* cx, JSObject* obj)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ASSERT(JS_GET_CLASS(cx, obj) == &sLibraryClass);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
jsval slot;
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_GetReservedSlot(cx, obj, SLOT_LIBRARY, &slot);
|
2009-10-06 12:16:38 -07:00
|
|
|
return static_cast<PRLibrary*>(JSVAL_TO_PRIVATE(slot));
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
static Function*
|
|
|
|
GetFunctionList(JSContext* cx, JSObject* obj)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ASSERT(JS_GET_CLASS(cx, obj) == &sLibraryClass);
|
|
|
|
|
|
|
|
jsval slot;
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_GetReservedSlot(cx, obj, SLOT_FUNCTIONLIST, &slot);
|
2009-10-06 12:16:38 -07:00
|
|
|
return static_cast<Function*>(JSVAL_TO_PRIVATE(slot));
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
JSBool
|
2009-10-06 12:16:38 -07:00
|
|
|
Library::AddFunction(JSContext* cx, JSObject* aLibrary, Function* aFunction)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
// add the new Function instance to the head of the list
|
|
|
|
aFunction->Next() = GetFunctionList(cx, aLibrary);
|
2010-03-11 17:17:36 -08:00
|
|
|
return JS_SetReservedSlot(cx, aLibrary, SLOT_FUNCTIONLIST,
|
2010-03-11 17:17:36 -08:00
|
|
|
PRIVATE_TO_JSVAL(aFunction));
|
2010-03-11 17:17:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Library::Trace(JSTracer *trc, JSObject* obj)
|
|
|
|
{
|
|
|
|
// Walk the Function list and for each Function, identify each CType
|
|
|
|
// associated with it to the tracer.
|
|
|
|
Function* current = GetFunctionList(trc->context, obj);
|
|
|
|
while (current) {
|
|
|
|
current->Trace(trc);
|
|
|
|
current = current->Next();
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
void
|
|
|
|
Library::Finalize(JSContext* cx, JSObject* obj)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
// unload the library
|
|
|
|
PRLibrary* library = GetLibrary(cx, obj);
|
|
|
|
if (library)
|
|
|
|
PR_UnloadLibrary(library);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
// delete each Function instance
|
|
|
|
Function* current = GetFunctionList(cx, obj);
|
|
|
|
while (current) {
|
|
|
|
Function* next = current->Next();
|
|
|
|
delete current;
|
|
|
|
current = next;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JSBool
|
|
|
|
Library::Open(JSContext* cx, uintN argc, jsval *vp)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2010-03-11 17:17:36 -08:00
|
|
|
if (argc != 1 || JSVAL_IS_VOID(JS_ARGV(cx, vp)[0])) {
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ReportError(cx, "open requires a single argument");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* library = Create(cx, JS_ARGV(cx, vp)[0]);
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!library)
|
2009-10-06 12:16:38 -07:00
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(library));
|
|
|
|
return JS_TRUE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JSBool
|
|
|
|
Library::Close(JSContext* cx, uintN argc, jsval* vp)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
JSObject* obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (JS_GET_CLASS(cx, obj) != &sLibraryClass) {
|
|
|
|
JS_ReportError(cx, "not a library");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
if (argc != 0) {
|
|
|
|
JS_ReportError(cx, "close doesn't take any arguments");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
// delete our internal objects
|
|
|
|
Finalize(cx, obj);
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_SetReservedSlot(cx, obj, SLOT_LIBRARY, PRIVATE_TO_JSVAL(NULL));
|
|
|
|
JS_SetReservedSlot(cx, obj, SLOT_FUNCTIONLIST, PRIVATE_TO_JSVAL(NULL));
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JSBool
|
|
|
|
Library::Declare(JSContext* cx, uintN argc, jsval* vp)
|
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (JS_GET_CLASS(cx, obj) != &sLibraryClass) {
|
|
|
|
JS_ReportError(cx, "not a library");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
PRLibrary* library = GetLibrary(cx, obj);
|
|
|
|
if (!library) {
|
|
|
|
JS_ReportError(cx, "library not open");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
|
|
|
// we always need at least a method name, a call type and a return type
|
|
|
|
if (argc < 3) {
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ReportError(cx, "declare requires at least three arguments");
|
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
if (!JSVAL_IS_STRING(argv[0])) {
|
|
|
|
JS_ReportError(cx, "first argument must be a string");
|
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
const char* name = JS_GetStringBytesZ(cx, JSVAL_TO_STRING(argv[0]));
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!name)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
PRFuncPtr func = PR_FindFunctionSymbol(library, name);
|
2009-09-23 10:57:22 -07:00
|
|
|
if (!func) {
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ReportError(cx, "couldn't find function symbol in library");
|
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
JSObject* fn = Function::Create(cx, obj, func, name, argv[1], argv[2],
|
|
|
|
&argv[3], argc - 3);
|
2009-10-06 12:16:38 -07:00
|
|
|
if (!fn)
|
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(fn));
|
|
|
|
return JS_TRUE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|