gecko/js/ctypes/CTypes.h

278 lines
11 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):
* 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 ***** */
#ifndef CTYPES_H
#define CTYPES_H
#include "jsapi.h"
#include "nsString.h"
#include "ffi.h"
namespace mozilla {
namespace ctypes {
// for JS error reporting
enum ErrorNum {
#define MSG_DEF(name, number, count, exception, format) \
name = number,
#include "ctypes.msg"
#undef MSG_DEF
CTYPESERR_LIMIT
};
const JSErrorFormatString*
GetErrorMessage(void* userRef, const char* locale, const uintN errorNumber);
bool TypeError(JSContext* cx, const char* expected, jsval actual);
/**
* ABI constants that specify the calling convention to use.
* ctypes.default_abi corresponds to the cdecl convention, and in almost all
* cases is the correct choice. ctypes.stdcall_abi is provided for calling
* functions in the Microsoft Win32 API.
*/
enum ABICode {
ABI_DEFAULT,
ABI_STDCALL,
INVALID_ABI
};
enum TypeCode {
TYPE_void_t,
#define DEFINE_TYPE(name, type, ffiType) TYPE_##name,
#include "typedefs.h"
TYPE_pointer,
TYPE_array,
TYPE_struct
};
ABICode GetABICode(JSContext* cx, JSObject* obj);
struct FieldInfo
{
nsCString mName;
JSObject* mType;
size_t mOffset;
};
bool InitTypeClasses(JSContext* cx, JSObject* parent);
bool ConvertToJS(JSContext* cx, JSObject* typeObj, JSObject* dataObj, void* data, bool wantPrimitive, jsval* result);
bool ImplicitConvert(JSContext* cx, jsval val, JSObject* targetType, void* buffer, bool isArgument, bool* freePointer);
bool ExplicitConvert(JSContext* cx, jsval val, JSObject* targetType, void* buffer);
// Contents of the various slots on each JSClass. The slot indexes are given
// enumerated names for readability.
enum CABISlot {
SLOT_ABICODE = 0, // ABICode of the CABI object
CABI_SLOTS
};
enum CTypeProtoSlot {
SLOT_POINTERPROTO = 0, // ctypes.PointerType.prototype object
SLOT_ARRAYPROTO = 1, // ctypes.ArrayType.prototype object
SLOT_STRUCTPROTO = 2, // ctypes.StructType.prototype object
SLOT_INT64PROTO = 3, // ctypes.Int64.prototype object
SLOT_UINT64PROTO = 4, // ctypes.UInt64.prototype object
CTYPEPROTO_SLOTS
};
enum CTypeSlot {
SLOT_TYPECODE = 0, // TypeCode of the CType object
SLOT_FFITYPE = 1, // ffi_type representing the type
SLOT_ALIGN = 2, // alignment of the data type, in bytes
SLOT_PTR = 3, // cached PointerType object for type.ptr
SLOT_FIELDINFO = 4, // (StructTypes only) FieldInfo array
CTYPE_SLOTS
};
enum CDataSlot {
SLOT_CTYPE = 0, // CType object representing the underlying type
SLOT_REFERENT = 1, // CData object this object refers to, if any
SLOT_DATA = 2, // pointer to a buffer containing the binary data
CDATA_SLOTS
};
enum Int64Slot {
SLOT_INT64 = 0, // pointer to a 64-bit buffer containing the integer
INT64_SLOTS
};
enum Int64FunctionSlot {
SLOT_FN_INT64PROTO = 0 // ctypes.{Int64,UInt64}.prototype object
// JSFunction objects always get exactly two slots.
};
class CType {
public:
static JSObject* Create(JSContext* cx, JSObject* proto, JSString* name, TypeCode type, jsval size, jsval align, ffi_type* ffiType);
static JSObject* DefineBuiltin(JSContext* cx, JSObject* parent, const char* propName, JSObject* proto, const char* name, TypeCode type, jsval size, jsval align, ffi_type* ffiType);
static void Finalize(JSContext* cx, JSObject* obj);
static JSBool ConstructAbstract(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static JSBool ConstructData(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static JSBool ConstructBasic(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static bool IsCType(JSContext* cx, JSObject* obj);
static TypeCode GetTypeCode(JSContext* cx, JSObject* typeObj);
static bool TypesEqual(JSContext* cx, JSObject* t1, JSObject* t2);
static size_t GetSize(JSContext* cx, JSObject* obj);
static bool GetSafeSize(JSContext* cx, JSObject* obj, size_t* result);
static bool IsSizeDefined(JSContext* cx, JSObject* obj);
static size_t GetAlignment(JSContext* cx, JSObject* obj);
static ffi_type* GetFFIType(JSContext* cx, JSObject* obj);
static JSString* GetName(JSContext* cx, JSObject* obj);
static JSObject* GetProtoFromCtor(JSContext* cx, JSObject* obj, CTypeProtoSlot slot);
static JSObject* GetProtoFromType(JSContext* cx, JSObject* obj, CTypeProtoSlot slot);
static JSBool PtrGetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool Array(JSContext* cx, uintN argc, jsval* vp);
static JSBool ToString(JSContext* cx, uintN argc, jsval* vp);
static JSBool ToSource(JSContext* cx, uintN argc, jsval* vp);
};
class PointerType {
public:
static JSBool Create(JSContext* cx, uintN argc, jsval* vp);
static JSObject* CreateInternal(JSContext* cx, JSObject* ctor, JSObject* baseType, JSString* name);
static JSBool ConstructData(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static JSObject* ConstructInternal(JSContext* cx, JSObject* typeObj, JSObject* parentObj, void* data);
static JSObject* GetBaseType(JSContext* cx, JSObject* obj);
static JSBool ContentsGetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool ContentsSetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
};
class ArrayType {
public:
static JSBool Create(JSContext* cx, uintN argc, jsval* vp);
static JSObject* CreateInternal(JSContext* cx, JSObject* baseType, size_t length, bool lengthDefined);
static JSBool ConstructData(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static JSObject* ConstructInternal(JSContext* cx, JSObject* typeObj, JSObject* parentObj, void* data);
static JSObject* GetBaseType(JSContext* cx, JSObject* obj);
static size_t GetLength(JSContext* cx, JSObject* obj);
static bool GetSafeLength(JSContext* cx, JSObject* obj, size_t* result);
static JSBool Getter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool Setter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool AddressOfElement(JSContext* cx, uintN argc, jsval* vp);
};
class StructType {
public:
static JSBool Create(JSContext* cx, uintN argc, jsval* vp);
static JSBool ConstructData(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static JSObject* ConstructInternal(JSContext* cx, JSObject* typeObj, JSObject* parentObj, void* data);
static nsTArray<FieldInfo>* GetFieldInfo(JSContext* cx, JSObject* obj);
static FieldInfo* LookupField(JSContext* cx, JSObject* obj, jsval idval);
static JSBool FieldGetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool FieldSetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool AddressOfField(JSContext* cx, uintN argc, jsval* vp);
};
class CData {
public:
static JSObject* Create(JSContext* cx, JSObject* type, JSObject* base, void* data);
static void Finalize(JSContext* cx, JSObject* obj);
static JSObject* GetCType(JSContext* cx, JSObject* dataObj);
static void* GetData(JSContext* cx, JSObject* dataObj);
static bool IsCData(JSContext* cx, JSObject* obj);
static JSBool ValueGetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool ValueSetter(JSContext* cx, JSObject* obj, jsval idval, jsval* vp);
static JSBool Address(JSContext* cx, uintN argc, jsval* vp);
static JSBool Cast(JSContext* cx, uintN argc, jsval* vp);
static JSBool ReadString(JSContext* cx, uintN argc, jsval* vp);
static JSBool ToSource(JSContext* cx, uintN argc, jsval* vp);
};
class Int64Base {
public:
static JSObject* Construct(JSContext* cx, JSObject* proto, PRUint64 data, bool isUnsigned);
static void Finalize(JSContext* cx, JSObject* obj);
static PRUint64 GetInt(JSContext* cx, JSObject* obj);
static JSBool ToString(JSContext* cx, JSObject* obj, uintN argc, jsval* vp, bool isUnsigned);
static JSBool ToSource(JSContext* cx, JSObject* obj, uintN argc, jsval* vp, bool isUnsigned);
};
class Int64 : public Int64Base {
public:
static JSBool Construct(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static bool IsInt64(JSContext* cx, JSObject* obj);
static JSBool ToString(JSContext* cx, uintN argc, jsval* vp);
static JSBool ToSource(JSContext* cx, uintN argc, jsval* vp);
// ctypes.Int64 static functions
static JSBool Compare(JSContext* cx, uintN argc, jsval* vp);
static JSBool Lo(JSContext* cx, uintN argc, jsval* vp);
static JSBool Hi(JSContext* cx, uintN argc, jsval* vp);
static JSBool Join(JSContext* cx, uintN argc, jsval* vp);
};
class UInt64 : public Int64Base {
public:
static JSBool Construct(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
static bool IsUInt64(JSContext* cx, JSObject* obj);
static JSBool ToString(JSContext* cx, uintN argc, jsval* vp);
static JSBool ToSource(JSContext* cx, uintN argc, jsval* vp);
// ctypes.UInt64 static functions
static JSBool Compare(JSContext* cx, uintN argc, jsval* vp);
static JSBool Lo(JSContext* cx, uintN argc, jsval* vp);
static JSBool Hi(JSContext* cx, uintN argc, jsval* vp);
static JSBool Join(JSContext* cx, uintN argc, jsval* vp);
};
}
}
#endif