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-03 21:02:55 -08:00
|
|
|
#include "jscntxt.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
#include "Function.h"
|
2009-10-06 12:16:38 -07:00
|
|
|
#include "Library.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ctypes {
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
** Static helpers
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2009-09-25 09:48:13 -07:00
|
|
|
static bool
|
2009-10-06 12:16:38 -07:00
|
|
|
GetABI(JSContext* cx, jsval aCallType, ffi_abi& aResult)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JSVAL_IS_OBJECT(aCallType) || JSVAL_IS_NULL(aCallType))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ABICode abi = GetABICode(cx, JSVAL_TO_OBJECT(aCallType));
|
2009-10-06 12:16:38 -07:00
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
// determine the ABI from the subset of those available on the
|
2010-03-11 17:17:36 -08:00
|
|
|
// given platform. ABI_DEFAULT specifies the default
|
2009-09-23 10:57:22 -07:00
|
|
|
// C calling convention (cdecl) on each platform.
|
2009-10-06 12:16:38 -07:00
|
|
|
switch (abi) {
|
2010-03-11 17:17:36 -08:00
|
|
|
case ABI_DEFAULT:
|
2009-09-23 10:57:22 -07:00
|
|
|
aResult = FFI_DEFAULT_ABI;
|
2009-09-25 09:48:13 -07:00
|
|
|
return true;
|
2010-03-11 17:17:36 -08:00
|
|
|
case ABI_STDCALL:
|
2010-01-05 13:39:56 -08:00
|
|
|
#if (defined(_WIN32) && !defined(_WIN64)) || defined(_OS2)
|
2009-09-23 10:57:22 -07:00
|
|
|
aResult = FFI_STDCALL;
|
2009-09-25 09:48:13 -07:00
|
|
|
return true;
|
2009-09-23 10:57:22 -07:00
|
|
|
#endif
|
2010-03-11 17:17:36 -08:00
|
|
|
case INVALID_ABI:
|
|
|
|
break;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
2010-03-11 17:17:36 -08:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-09-25 09:48:13 -07:00
|
|
|
static bool
|
2009-09-23 10:57:22 -07:00
|
|
|
PrepareType(JSContext* aContext, jsval aType, Type& aResult)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JSVAL_IS_OBJECT(aType) ||
|
|
|
|
JSVAL_IS_NULL(aType) ||
|
|
|
|
!CType::IsCType(aContext, JSVAL_TO_OBJECT(aType))) {
|
|
|
|
JS_ReportError(aContext, "not a ctypes type");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
JSObject* typeObj = JSVAL_TO_OBJECT(aType);
|
|
|
|
TypeCode typeCode = CType::GetTypeCode(aContext, typeObj);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
if (typeCode == TYPE_array) {
|
|
|
|
// convert array argument types to pointers, just like C.
|
|
|
|
// ImplicitConvert will do the same, when passing an array as data.
|
|
|
|
JSObject* baseType = ArrayType::GetBaseType(aContext, typeObj);
|
|
|
|
typeObj = PointerType::CreateInternal(aContext, NULL, baseType, NULL);
|
|
|
|
if (!typeObj) {
|
|
|
|
JS_ReportError(aContext, "couldn't create pointer type from array");
|
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
2010-03-11 17:17:36 -08:00
|
|
|
} else if (typeCode == TYPE_void_t) {
|
|
|
|
// disallow void argument types
|
|
|
|
JS_ReportError(aContext, "Cannot have void argument type");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
// libffi cannot pass types of zero size by value.
|
|
|
|
JS_ASSERT(CType::GetSize(aContext, typeObj) != 0);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
aResult.mType = typeObj;
|
|
|
|
aResult.mFFIType = *CType::GetFFIType(aContext, typeObj);
|
|
|
|
return true;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-09-25 09:48:13 -07:00
|
|
|
static bool
|
2010-03-11 17:17:36 -08:00
|
|
|
PrepareResultType(JSContext* aContext, jsval aType, Type& aResult)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JSVAL_IS_OBJECT(aType) ||
|
|
|
|
JSVAL_IS_NULL(aType) ||
|
|
|
|
!CType::IsCType(aContext, JSVAL_TO_OBJECT(aType))) {
|
|
|
|
JS_ReportError(aContext, "not a ctypes type");
|
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
2010-03-11 17:17:36 -08:00
|
|
|
|
|
|
|
JSObject* typeObj = JSVAL_TO_OBJECT(aType);
|
|
|
|
TypeCode typeCode = CType::GetTypeCode(aContext, typeObj);
|
|
|
|
|
|
|
|
// Arrays can never be return types.
|
|
|
|
if (typeCode == TYPE_array) {
|
|
|
|
JS_ReportError(aContext, "Result type cannot be an array");
|
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
2010-03-11 17:17:36 -08:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Our libffi_msvc fork doesn't support returning structs by value yet.
|
|
|
|
if (typeCode == TYPE_struct) {
|
|
|
|
JS_ReportError(aContext,
|
|
|
|
"Returning structs by value is unsupported on Windows");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
2010-03-11 17:17:36 -08:00
|
|
|
#endif
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
// libffi cannot pass types of zero size by value.
|
|
|
|
JS_ASSERT(typeCode == TYPE_void_t || CType::GetSize(aContext, typeObj) != 0);
|
|
|
|
|
|
|
|
aResult.mType = typeObj;
|
|
|
|
aResult.mFFIType = *CType::GetFFIType(aContext, typeObj);
|
2009-09-25 09:48:13 -07:00
|
|
|
return true;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
2009-10-06 12:16:38 -07:00
|
|
|
** Function implementation
|
2009-09-23 10:57:22 -07:00
|
|
|
*******************************************************************************/
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
Function::Function()
|
2009-10-06 12:16:38 -07:00
|
|
|
: mNext(NULL)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
Function::~Function()
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-09-25 09:48:13 -07:00
|
|
|
bool
|
2009-09-23 10:57:22 -07:00
|
|
|
Function::Init(JSContext* aContext,
|
|
|
|
PRFuncPtr aFunc,
|
2009-10-06 12:16:38 -07:00
|
|
|
jsval aCallType,
|
2009-09-23 10:57:22 -07:00
|
|
|
jsval aResultType,
|
2009-10-06 12:16:38 -07:00
|
|
|
jsval* aArgTypes,
|
|
|
|
uintN aArgLength)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
|
|
|
mFunc = aFunc;
|
|
|
|
|
|
|
|
// determine the ABI
|
2009-10-06 12:16:38 -07:00
|
|
|
if (!GetABI(aContext, aCallType, mCallType)) {
|
2009-09-23 10:57:22 -07:00
|
|
|
JS_ReportError(aContext, "Invalid ABI specification");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare the result type
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!PrepareResultType(aContext, aResultType, mResultType))
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
|
|
|
|
// prepare the argument types
|
2009-10-06 12:16:38 -07:00
|
|
|
mArgTypes.SetCapacity(aArgLength);
|
|
|
|
for (PRUint32 i = 0; i < aArgLength; ++i) {
|
2009-09-25 09:48:13 -07:00
|
|
|
if (!PrepareType(aContext, aArgTypes[i], *mArgTypes.AppendElement()))
|
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
|
|
|
|
// ffi_prep_cif requires an array of ffi_types; prepare it separately.
|
2009-09-23 10:57:22 -07:00
|
|
|
mFFITypes.AppendElement(&mArgTypes[i].mFFIType);
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ffi_status status = ffi_prep_cif(&mCIF, mCallType, mFFITypes.Length(),
|
2009-09-23 10:57:22 -07:00
|
|
|
&mResultType.mFFIType, mFFITypes.Elements());
|
2009-09-23 10:57:22 -07:00
|
|
|
switch (status) {
|
|
|
|
case FFI_OK:
|
2009-09-25 09:48:13 -07:00
|
|
|
return true;
|
2009-09-23 10:57:22 -07:00
|
|
|
case FFI_BAD_ABI:
|
|
|
|
JS_ReportError(aContext, "Invalid ABI specification");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
case FFI_BAD_TYPEDEF:
|
|
|
|
JS_ReportError(aContext, "Invalid type specification");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
default:
|
|
|
|
JS_ReportError(aContext, "Unknown libffi error");
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-25 09:48:13 -07:00
|
|
|
bool
|
2009-10-06 12:16:38 -07:00
|
|
|
Function::Execute(JSContext* cx, PRUint32 argc, jsval* vp)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
if (argc != mArgTypes.Length()) {
|
|
|
|
JS_ReportError(cx, "Number of arguments does not match declaration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
// prepare the values for each argument
|
2010-03-11 17:17:36 -08:00
|
|
|
nsAutoTArray<AutoValue, 16> values;
|
|
|
|
nsAutoTArray<AutoValue, 16> strings;
|
2009-09-23 10:57:22 -07:00
|
|
|
for (PRUint32 i = 0; i < mArgTypes.Length(); ++i) {
|
2010-03-11 17:17:36 -08:00
|
|
|
AutoValue& value = *values.AppendElement();
|
|
|
|
jsval arg = JS_ARGV(cx, vp)[i];
|
|
|
|
bool freePointer = false;
|
|
|
|
if (!value.SizeToType(cx, mArgTypes[i].mType)) {
|
|
|
|
JS_ReportAllocationOverflow(cx);
|
2009-09-25 09:48:13 -07:00
|
|
|
return false;
|
2010-03-11 17:17:36 -08:00
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!ImplicitConvert(cx, arg, mArgTypes[i].mType, value.mData, true, &freePointer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (freePointer) {
|
|
|
|
// ImplicitConvert converted a string for us, which we have to free.
|
|
|
|
// Keep track of it.
|
|
|
|
strings.AppendElement()->mData = *static_cast<char**>(value.mData);
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize a pointer to an appropriate location, for storing the result
|
2010-03-11 17:17:36 -08:00
|
|
|
AutoValue resultValue;
|
|
|
|
if (CType::GetTypeCode(cx, mResultType.mType) != TYPE_void_t &&
|
|
|
|
!resultValue.SizeToType(cx, mResultType.mType)) {
|
|
|
|
JS_ReportAllocationOverflow(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
|
|
|
// suspend the request before we call into the function, since the call
|
|
|
|
// may block or otherwise take a long time to return.
|
2009-10-06 12:16:38 -07:00
|
|
|
jsrefcount rc = JS_SuspendRequest(cx);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
ffi_call(&mCIF, FFI_FN(mFunc), resultValue.mData, reinterpret_cast<void**>(values.Elements()));
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JS_ResumeRequest(cx, rc);
|
2009-09-23 10:57:22 -07:00
|
|
|
|
|
|
|
// prepare a JS object from the result
|
2009-10-06 12:16:38 -07:00
|
|
|
jsval rval;
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!ConvertToJS(cx, mResultType.mType, NULL, resultValue.mData, false, &rval))
|
2009-10-06 12:16:38 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
JS_SET_RVAL(cx, vp, rval);
|
|
|
|
return true;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
void
|
|
|
|
Function::Trace(JSTracer *trc)
|
|
|
|
{
|
|
|
|
// Identify the result CType to the tracer.
|
|
|
|
JS_CALL_TRACER(trc, mResultType.mType, JSTRACE_OBJECT, "CType");
|
|
|
|
|
|
|
|
// Identify each argument CType to the tracer.
|
|
|
|
for (PRUint32 i = 0; i < mArgTypes.Length(); ++i)
|
|
|
|
JS_CALL_TRACER(trc, mArgTypes[i].mType, JSTRACE_OBJECT, "CType");
|
|
|
|
}
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
/*******************************************************************************
|
2009-10-06 12:16:38 -07:00
|
|
|
** JSObject implementation
|
2009-09-23 10:57:22 -07:00
|
|
|
*******************************************************************************/
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
JSObject*
|
|
|
|
Function::Create(JSContext* aContext,
|
|
|
|
JSObject* aLibrary,
|
|
|
|
PRFuncPtr aFunc,
|
|
|
|
const char* aName,
|
|
|
|
jsval aCallType,
|
|
|
|
jsval aResultType,
|
|
|
|
jsval* aArgTypes,
|
|
|
|
uintN aArgLength)
|
2009-09-23 10:57:22 -07:00
|
|
|
{
|
2009-10-06 12:16:38 -07:00
|
|
|
// create new Function instance
|
|
|
|
nsAutoPtr<Function> self(new Function());
|
|
|
|
if (!self)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// deduce and check the ABI and parameter types
|
|
|
|
if (!self->Init(aContext, aFunc, aCallType, aResultType, aArgTypes, aArgLength))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// create and root the new JS function object
|
|
|
|
JSFunction* fn = JS_NewFunction(aContext, JSNative(Function::Call),
|
|
|
|
aArgLength, JSFUN_FAST_NATIVE, NULL, aName);
|
|
|
|
if (!fn)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
JSObject* fnObj = JS_GetFunctionObject(fn);
|
|
|
|
JSAutoTempValueRooter fnRoot(aContext, fnObj);
|
|
|
|
|
|
|
|
// stash a pointer to self, which Function::Call will need at call time
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JS_SetReservedSlot(aContext, fnObj, SLOT_FUNCTION, PRIVATE_TO_JSVAL(self.get())))
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// make a strong reference to the library for GC-safety
|
2010-03-11 17:17:36 -08:00
|
|
|
if (!JS_SetReservedSlot(aContext, fnObj, SLOT_LIBRARYOBJ, OBJECT_TO_JSVAL(aLibrary)))
|
2009-10-06 12:16:38 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-03-11 17:17:36 -08:00
|
|
|
// Tell the library we exist, so it can (a) identify our CTypes to the tracer
|
|
|
|
// and (b) delete our Function instance when it comes time to finalize.
|
|
|
|
// (JS functions don't have finalizers.)
|
2009-10-06 12:16:38 -07:00
|
|
|
if (!Library::AddFunction(aContext, aLibrary, self))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
self.forget();
|
|
|
|
return fnObj;
|
|
|
|
}
|
2009-09-23 10:57:22 -07:00
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
static Function*
|
|
|
|
GetFunction(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
|
|
|
jsval slot;
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_GetReservedSlot(cx, obj, SLOT_FUNCTION, &slot);
|
2009-10-06 12:16:38 -07:00
|
|
|
return static_cast<Function*>(JSVAL_TO_PRIVATE(slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
Function::Call(JSContext* cx, uintN argc, jsval* vp)
|
|
|
|
{
|
|
|
|
JSObject* callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp));
|
|
|
|
|
|
|
|
jsval slot;
|
2010-03-11 17:17:36 -08:00
|
|
|
JS_GetReservedSlot(cx, callee, SLOT_LIBRARYOBJ, &slot);
|
2009-10-06 12:16:38 -07:00
|
|
|
|
|
|
|
PRLibrary* library = Library::GetLibrary(cx, JSVAL_TO_OBJECT(slot));
|
|
|
|
if (!library) {
|
|
|
|
JS_ReportError(cx, "library is not open");
|
|
|
|
return JS_FALSE;
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:16:38 -07:00
|
|
|
return GetFunction(cx, callee)->Execute(cx, argc, vp);
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
|
2009-09-23 10:57:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|